Skip to main content

Server Setup

Prerequisites

  • Ubuntu 22.04 LTS (or similar)
  • Root or sudo access
  • Domain name configured

Installation Steps

1. Update System

sudo apt update
sudo apt upgrade -y

2. Install PHP

sudo apt install php8.2 php8.2-fpm php8.2-mysql php8.2-xml php8.2-mbstring php8.2-curl -y

3. Install MySQL

sudo apt install mysql-server -y
sudo mysql_secure_installation

4. Install Nginx

sudo apt install nginx -y

5. Install Composer

curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

6. Install Node.js

curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install nodejs -y

Application Deployment

Backend

cd /var/www/backend
composer install --optimize-autoloader --no-dev
php artisan migrate --force
php artisan config:cache
php artisan route:cache
php artisan view:cache

Frontend

cd /var/www/frontend
npm install
npm run build

Web Server Configuration

Nginx Configuration

server {
listen 80;
server_name api.yourdomain.com;
root /var/www/backend/public;

add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";

index index.php;

location / {
try_files $uri $uri/ /index.php?$query_string;
}

location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
}

SSL Certificate

sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d api.yourdomain.com

Environment Configuration

cp .env.example .env
nano .env

Update values:

  • Database credentials
  • APP_URL
  • APP_ENV=production
  • APP_DEBUG=false

Permissions

sudo chown -R www-data:www-data /var/www/backend
sudo chmod -R 755 /var/www/backend/storage

Verify Installation

Test backend:

curl https://api.yourdomain.com/api/v1/health