Deploy Laravel App on Ubuntu Nginx Production Server

Summary:
Prepare your Laravel project for live production deployment.


Deploying a Laravel application on an Ubuntu server with Nginx is a reliable way to achieve speed, security, and scalability for your app. This guide provides a step-by-step walkthrough to go from local development to a production-ready server.


Table of Contents

  1. Prerequisites
  2. Update Your Server
  3. Install Nginx
  4. Install PHP and Extensions
  5. Install Composer
  6. Configure MySQL Database
  7. Clone Your Laravel Project
  8. Set File Permissions
  9. Configure Environment Variables
  10. Install Laravel Dependencies
  11. Generate Application Key
  12. Configure Nginx for Laravel
  13. Set Up SSL with Let's Encrypt (Optional)
  14. Optimize Laravel for Production
  15. Conclusion

Prerequisites

  • Ubuntu 20.04 or newer server (with root/sudo access)
  • Domain name pointed to your server's IP
  • Laravel app ready in a Git repository (GitHub, GitLab, Bitbucket, etc.)

Update Your Server

First, ensure your system is up to date:

sudo apt update
sudo apt upgrade -y

Install Nginx

Install Nginx using:

sudo apt install nginx -y

To enable and start Nginx:

sudo systemctl start nginx
sudo systemctl enable nginx

Allow HTTP and HTTPS through the firewall:

sudo ufw allow 'Nginx Full'
sudo ufw enable

Install PHP and Extensions

Laravel requires PHP 8.x+ (check your app's requirements):

sudo apt install php-fpm php-mysql php-xml php-mbstring php-curl php-zip php-bcmath php-gd php-cli unzip git -y

Check PHP version:

php -v

Install Composer

Composer is required to manage Laravel dependencies:

cd ~
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php --install-dir=/usr/local/bin --filename=composer
composer --version

Configure MySQL Database

Install MySQL if not already installed:

sudo apt install mysql-server -y

Secure the installation:

sudo mysql_secure_installation

Create a database and user:

sudo mysql -u root -p

CREATE DATABASE laravel_db;
CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'strong_password';
GRANT ALL PRIVILEGES ON laravel_db.* TO 'laravel_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Clone Your Laravel Project

Navigate to your web root and clone your repository:

cd /var/www
sudo git clone https://github.com/yourusername/your-laravel-app.git laravelapp
cd laravelapp

Set File Permissions

Set appropriate permissions and ownership (replace www-data with your web server user if different):

sudo chown -R www-data:www-data /var/www/laravelapp
sudo find /var/www/laravelapp -type f -exec chmod 664 {} \;
sudo find /var/www/laravelapp -type d -exec chmod 775 {} \;
sudo chgrp -R www-data storage bootstrap/cache
sudo chmod -R ug+rwx storage bootstrap/cache

Configure Environment Variables

Copy the example environment file and update your credentials:

cp .env.example .env
nano .env

Update the following:

APP_NAME="YourAppName"
APP_ENV=production
APP_KEY=
APP_DEBUG=false
APP_URL=https://yourdomain.com

DB_DATABASE=laravel_db
DB_USERNAME=laravel_user
DB_PASSWORD=strong_password

Install Laravel Dependencies

Install the PHP packages with Composer:

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

Generate Application Key

php artisan key:generate

Configure Nginx for Laravel

Remove the default config and create a new one:

sudo rm /etc/nginx/sites-enabled/default
sudo nano /etc/nginx/sites-available/laravelapp

Add the following (update server_name and paths):

server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;

    root /var/www/laravelapp/public;
    index index.php index.html;

    access_log /var/log/nginx/laravelapp_access.log;
    error_log /var/log/nginx/laravelapp_error.log;

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock; # match your PHP version
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

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

Replace php8.1-fpm.sock with your installed PHP version (use php -v to check).

Enable the site and reload Nginx:

sudo ln -s /etc/nginx/sites-available/laravelapp /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

Set Up SSL with Let's Encrypt (Optional)

Use Certbot for free SSL:

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

Certbot will automatically configure SSL and reload Nginx. Test SSL at SSL Labs.


Optimize Laravel for Production

Run artisan optimization commands:

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

(Optional) Set up a cron job for Laravel scheduler:

sudo crontab -e

Add:

* * * * * cd /var/www/laravelapp && php artisan schedule:run >> /dev/null 2>&1

Conclusion

Your Laravel application is now securely deployed on an Ubuntu Nginx production server. By following these steps, you ensure your app is fast, secure, and ready for real-world traffic. Always keep your server and dependencies updated, monitor logs, and use additional tools (like queue/workers, Redis, supervisor, etc.) as needed for scaling!

Happy deploying! 🚀