How to Deploy a Laravel Website on a VPS Using Nginx

Deploying a Laravel website on a VPS using Nginx ensures better performance and flexibility. This guide will walk you through setting up your Laravel project on a VPS with a LEMP stack (Linux, Nginx, MySQL, PHP).

Step 1: Prepare Your VPS

Connect to Your VPS via SSH
ssh root@your_server_ip
Update System Packages
sudo apt update && sudo apt upgrade -y
Install Required Dependencies
sudo apt install unzip curl git -y

Step 2: Install LEMP Stack

Install Nginx
sudo apt install nginx -y

Verify if Nginx is running:

systemctl status nginx
Install MySQL (Optional)
sudo apt install mysql-server -y

Secure MySQL:

sudo mysql_secure_installation

Create a database:

CREATE DATABASE laravel_db;
CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'secure_password';
GRANT ALL PRIVILEGES ON laravel_db.* TO 'laravel_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Install PHP & Extensions
sudo apt install php-fpm php-mysql php-cli php-mbstring php-xml php-bcmath php-tokenizer php-curl php-zip -y

Verify PHP installation:

php -v

Step 3: Deploy Laravel Project

Navigate to Web Root
cd /var/www/
Clone Laravel Project
git clone https://github.com/your-repo/your-laravel-project.git
Set Correct 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
Install Composer & Dependencies
cd /var/www/your-laravel-project
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
composer install --no-dev --optimize-autoloader
Set Up Environment File
cp .env.example .env
nano .env

Update the database credentials in .env:

DB_DATABASE=laravel_db
DB_USERNAME=laravel_user
DB_PASSWORD=secure_password
Run Migrations & Generate Key
php artisan key:generate
php artisan migrate --force
php artisan config:cache
php artisan route:cache

Step 4: Configure Nginx

Create a New Nginx Configuration File
sudo nano /etc/nginx/sites-available/laravel

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.1-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}
Enable the Site
sudo ln -s /etc/nginx/sites-available/laravel /etc/nginx/sites-enabled/
Test and Restart Nginx
sudo nginx -t
sudo systemctl restart nginx

Step 5: Set Up Supervisor for Queues (Optional)

sudo apt install supervisor -y
sudo nano /etc/supervisor/conf.d/laravel-worker.conf

Add the following configuration:

[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/log/supervisor/laravel-worker.log

Reload Supervisor:

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

Step 6: Enable HTTPS with Let’s Encrypt (Optional)

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

Step 7: Set Up Laravel Scheduler

Edit the crontab:

crontab -e

Add:

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

Step 8: Restart Everything

sudo systemctl restart nginx php8.1-fpm mysql

Conclusion

Your Laravel website is now successfully deployed on a VPS using Nginx! πŸŽ‰ Your site should be accessible at http://yourdomain.com. Happy coding! πŸš€

Leave a Reply

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