Laravel Project Deploy on Ubuntu Server


A comprehensive walkthrough for setting up a production-ready Laravel environment with Nginx, PHP 8.3, MySQL, Ubuntu server, and VPS hosting.
In today's digital landscape, efficiently deploying robust web applications is essential for developers and businesses. Laravel has become one of the most elegant PHP frameworks, offering a streamlined development experience. However, successful deployment requires careful configuration of various components to ensure optimal performance, security, and reliability.
This guide provides a comprehensive walkthrough for deploying a Laravel application on an Ubuntu server, covering everything from basic server setup to advanced configurations such as reverse proxies and automated deployments.
Prerequisites
An Ubuntu server (preferably Ubuntu 22.04 LTS or newer)
SSH access with sudo privileges
A domain name pointing to your server (optional but recommended)
Basic knowledge of Linux commands
Step 1: Install and Configure Nginx
Nginx is a high-performance web server that will serve your Laravel application to users.
sudo apt update
sudo apt install -y nginx
sudo systemctl start nginx
sudo systemctl enable nginx
Step 2: Install PHP 8.3 and Required Extensions
Laravel requires PHP and several PHP extensions to function properly. PHP 8.3 offers significant performance improvements over older versions.
sudo apt install -y php8.3 \
php8.3-common \
php8.3-opcache \
php8.3-cli \
php8.3-gd \
php8.3-curl \
php8.3-mysql \
php8.3-mbstring \
php8.3-zip \
php8.3-xml \
php8.3-intl \
php8.3-bcmath \
php8.3-soap \
php8.3-fpm \
php8.3-imagick \
php8.3-ldap
Verify your PHP installation with:
php -v
Step 3: Set Up MySQL Database
sudo apt install -y mysql-server
sudo systemctl start mysql
sudo systemctl enable mysql
Step 4: Create Database and User
sudo mysql -u root
CREATE USER 'admin'@'localhost' IDENTIFIED BY 'yourpassword';
GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
CREATE DATABASE `tech_adda`;
EXIT;
Note: Remember to replace 'yourpassword' with a strong, unique password in a production environment.
Step 5: Install Supervisor for Queue Management
Step 5: Install Supervisor for Queue Management
sudo apt-get install -y supervisor
sudo systemctl start supervisor
sudo systemctl enable supervisor
Step 6: Install Git for Version Control
sudo apt-get install -y git
sudo mkdir -p /var/www/tech_adda
sudo chown -R www-data:www-data /var/www/tech_adda
Step 8: Clone Your Repository
For private repositories, you'll need to set up SSH keys:
# Generate SSH key
ssh-keygen -t rsa -b 4096 -C "your-email@example.com"
# Display the public key to add to GitHub/GitLab
cat ~/.ssh/id_rsa.pub
Then clone your repository:
cd /var/www
sudo git clone https://github.com/yourusername/your-repo.git tech_adda
sudo chown -R www-data:www-data /var/www/tech_adda
Step 9: Configure Nginx for Your Laravel Application
Create a new server block configuration:
sudo nano /etc/nginx/sites-available/tech_adda
Add the following configuration:
server {
listen 80;
listen [::]:80;
server_name your-domain.com; # Replace with your domain
root /var/www/tech_adda/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_hide_header X-Powered-By;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
Enable the site and test your configuration:
sudo ln -s /etc/nginx/sites-available/tech_adda /etc/nginx/sites-enabled/
sudo rm -f /etc/nginx/sites-enabled/default # Remove default site if needed
sudo nginx -t
sudo systemctl restart nginx
Step 10: Set Up Supervisor for Laravel Queue Workers
Create a Supervisor configuration file:
sudo nano /etc/supervisor/conf.d/tech_adda.conf
Add this configuration:
[program:tech_adda-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/tech_adda/artisan queue:work --sleep=3 --tries=3
autostart=true
autorestart=true
user=www-data
numprocs=2
redirect_stderr=true
stdout_logfile=/var/www/tech_adda/storage/logs/worker.log
stopwaitsecs=3600
Update Supervisor:
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start tech_adda-worker:*
Step 11: Configure Laravel Environment
cd /var/www/tech_adda
sudo cp .env.example .env
sudo php artisan key:generate
sudo php artisan config:cache
sudo php artisan route:cache
Install dependencies:
# For Composer dependencies
sudo composer install --no-dev --optimize-autoloader
# For frontend assets
sudo npm install
sudo npm run production
Set Proper File Permissions
Laravel requires specific permissions to function correctly. The web server needs to be able to write to certain directories:
cd /var/www/tech_adda
# Set the web server as the owner
sudo chown -R www-data:www-data .
# Add your user to group
sudo usermod -a -G www-data user
# Set file(s) permission
sudo find . -type f -exec chmod 644 {} \;
# Set folder(s) permission
sudo find . -type d -exec chmod 755 {} \;
# Set cache directory permission
sudo chgrp -R www-data storage bootstrap/cache
sudo chmod -R ug+rwx storage bootstrap/cache
These commands ensure that:
All files have 644 permissions (owner can read/write, group and others can read)
All directories have 755 permissions (owner can read/write/execute, group and others can read/execute)
Storage and cache directories have 775 permissions (owner and group can read/write/execute, others can read/execute)
Note: Incorrect permissions are a common source of issues in Laravel applications, particularly for caching, session handling, and file uploads.
Step 12: Automated Deployments (Optional)
Create a deployment script for easier updates:
sudo nano /var/www/deploy.sh
Add this script:
#!/bin/bash
cd /var/www/tech_adda
git pull origin main
composer install --no-dev --optimize-autoloader
php artisan migrate --force
php artisan config:cache
php artisan route:cache
php artisan view:cache
sudo supervisorctl restart tech_adda-worker:*
sudo systemctl restart php8.3-fpm
Make the script executable:
sudo chmod +x /var/www/deploy.sh
Step 14: Secure Your Installation
Install Certbot for SSL certificates:
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d your-domain.com
sudo certbot --nginx -d proxy.your-domain.com # For your reverse proxy
Set up a basic firewall:
sudo apt install -y ufw
sudo ufw allow 'Nginx Full'
sudo ufw allow ssh
sudo ufw enable
Conclusion
You now have a fully configured Laravel application running on Ubuntu with Nginx, PHP 8.3, MySQL, and all the necessary components for a production environment. This setup provides:
High-performance web serving with Nginx
Latest PHP version with optimized extensions
Reliable database with MySQL
Background job processing with Supervisor
Version control with Git
Optional reverse proxy capabilities
HTTPS security with Let's Encrypt
Basic firewall protection
Remember to regularly update your server's packages and your Laravel application to maintain security and performance. With this foundation in place, your Laravel application is ready to serve users efficiently and securely.
Subscribe to my newsletter
Read articles from Jalis Mahamud directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Jalis Mahamud
Jalis Mahamud
๐จโ๐ป Laravel Developer | PHP | MySQL | REST APIs Hi, I'm a Laravel developer with experience in building web applications using PHP and modern development practices. I work on backend systems, RESTful APIs, and database structures. I enjoy writing clean code, integrating third-party services, and improving workflows for better performance. ๐ง Tools I often use: Laravel, MySQL, Git, Composer, REST APIs ๐ Focused on: Simplicity, performance, and maintainability