Skip to content

Enviroment Preparation

Darkk-kami edited this page Jul 21, 2024 · 1 revision

Overview

This section covers setting up the Laravel application on an Ubuntu server. These processes were taken to ensure that the application functions properly and securely. The instructions below will walk you through the process, including installing required software, configuring environment files, and preparing for various deployment environments (development, staging, and production).

Configurations

PHP Installation and Depedencies

Install PHP-FPM PHP-FPM (FastCGI Process Manager) is PHP's FastCGI implementation. It significantly outperforms typical CGI-based approaches.

sudo apt install php8.2-fpm

Install Necessary PHP Extensions Laravel requires various PHP extensions to work properly. These extensions offer additional features including database connections, data manipulation, and internationalization.

sudo apt install php8.2-pgsql php8.2-curl php8.2-gd php8.2-xml php8.2-mbstring php8.2-intl
  • php8.2-pgsql: Provides PostgreSQL database support.
  • php8.2-curl: Allows Laravel to send HTTP requests and interact with RESTful APIs.
  • php8.2-gd: Used for image processing and manipulation.
  • php8.2-xml: Enables XML parsing, which is essential for various data exchange formats.
  • php8.2-mbstring: Provides support for multibyte string functions, essential for handling non-ASCII text.
  • php8.2-intl: Supports internationalization, helping with locale-aware formatting and string operations.

Install Composer A dependency manager for PHP, which simplifies the management of package dependencies, ensuring that the Laravel application has all the necessary libraries to run correctly.

sudo apt install composer

Enviroment Directory Setup

Clone the Repository The project repository is cloned into three separate directories for each environment: production, staging, and development. This configuration ensures that each environment is isolated and can be maintained separately.

# Production
git clone <repository-url> /var/www/langlearnai-be/api
cd /var/www/langlearnai-be/api
git checkout <production-branch>

# Staging
git clone <repository-url> /var/www/langlearnai-be/staging
cd /var/www/langlearnai-be/staging
git checkout <staging-branch>

# Development
git clone <repository-url> /var/www/langlearnai-be/deployment
cd /var/www/langlearnai-be/deployment
git checkout <development-branch>

Setting up Enviroment files Edit the ```.env file to include the relevant database credentials, Redis configurations, and other settings specific to each environment. i.e These are created seperately in each enviroment

# Copy and edit the .env file in each directory
cp .env.example .env
nano .env

Install Composer Dependencies Each environment needs its own set of dependencies. For production and staging environments, skip the development dependencies to optimize performance and reduce potential security risks.

# Production
cd /var/www/langlearnai-be/api
composer install --no-dev

# Staging
cd /var/www/langlearnai-be/staging
composer install --no-dev

# Development
cd /var/www/langlearnai-be/deployment
composer install

Set Directory Permissions Proper directory permissions are set to ensure that the web server can read and write the necessary files. The following commands set the correct group ownership and permissions for the web server

sudo chgrp -R www-data /var/www
sudo chmod -R 775 /var/www

Starting the application

starting the PHP-FPM service and ensures the PHP-FPM starts automatically on server boot

sudo systemctl start php8.2-fpm
sudo systemctl enable php8.2-fpm