Deploy Laravel Project On Real Server

Deploying your Laravel project on a server involves several steps. Here’s a complete guide for deploying on a Linux server with Nginx and PHP 8.2.1 (since you’re using Nginx and PHP 8.2.1):


Step 1: Server Setup

Make sure your server has the required software installed:

  • Nginx
  • PHP 8.2.1 (with necessary extensions)
  • MySQL / MariaDB
  • Composer
  • Git
  • Supervisor (for queue management, if needed)
  • Certbot (for SSL, optional)

Install Required Packages

sudo apt update && sudo apt upgrade -y
sudo apt install nginx php8.2 php8.2-cli php8.2-fpm php8.2-mbstring php8.2-xml php8.2-bcmath php8.2-curl php8.2-zip unzip git composer -y

Step 2: Upload Laravel Project

Option 1: Clone from Git

cd /var/www
git clone https://github.com/your-repo/your-laravel-project.git
cd your-laravel-project

Option 2: Upload via SFTP

If you are using VS Code SFTP, upload your Laravel project to /var/www/your-laravel-project.


Step 3: Set Up Environment File

cp .env.example .env
nano .env

Update the database configuration:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database
DB_USERNAME=your_username
DB_PASSWORD=your_password

Save and exit (CTRL+X, then Y, then Enter).


Step 4: Install Dependencies

composer install --no-dev --optimize-autoloader

Step 5: Generate Key

php artisan key:generate

Step 6: Set File Permissions

sudo chown -R www-data:www-data /var/www/your-laravel-project
sudo chmod -R 775 /var/www/your-laravel-project/storage /var/www/your-laravel-project/bootstrap/cache

Step 7: Migrate Database

php artisan migrate --force

Step 8: Configure Nginx

Create a new Nginx configuration file for your Laravel project:

sudo nano /etc/nginx/sites-available/your-laravel-project

Add the following configuration:

server {
    listen 80;
    server_name yourdomain.com;
    root /var/www/your-laravel-project/public;

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

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

    location ~ /\.ht {
        deny all;
    }
}

//

Save and exit.


Step 9: Enable the Site

sudo ln -s /etc/nginx/sites-available/your-laravel-project /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx

Step 10: Set Up Supervisor (For Queues)

If your Laravel project uses queues, create a Supervisor config:

sudo nano /etc/supervisor/conf.d/laravel-worker.conf

Add the following:

[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/your-laravel-project/artisan queue:work --tries=3
autostart=true
autorestart=true
numprocs=1
user=www-data
redirect_stderr=true
stdout_logfile=/var/www/your-laravel-project/storage/logs/worker.log

Save and reload Supervisor:

sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start laravel-worker:*

Step 11: Set Up SSL (Optional)

If you are using a domain and want HTTPS:

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

Step 12: Set Up Cron Jobs

Laravel requires a cron job for scheduled tasks. Open crontab:

sudo crontab -e

Add this line at the bottom:

* * * * * php /var/www/your-laravel-project/artisan schedule:run >> /dev/null 2>&1

Save and exit.


Step 13: Cache Configurations

php artisan config:cache
php artisan route:cache
php artisan view:cache

Step 14: Restart Services

sudo systemctl restart nginx php8.2-fpm

Your Laravel Project is Now Live! 🎉

Visit http://yourdomain.com or http://your-server-ip to check if your Laravel app is running.


Let me know if you need help with any step! 🚀

Leave a Reply

Your email address will not be published. Required fields are marked *