Hi
Deploy Laravel to Ubuntu and nginx
Apr 2025
How to host a Laravel app with nginx on a fresh Ubuntu server
Let's deploy a Laravel app to a fresh Ubuntu server!
I'm using Laravel 11 and PHP 8.3.
This can be broken down into three stages:
- Set up dependencies
- Set up your project
- Set up Nginx
Set up dependencies
Install required packages:
# Nginx and PHP
sudo apt install nginx php unzip
# Database (sqlite for now)
sudo apt install sqlite3
# Laravel dependencies
sudo apt install php-sqlite3 php-curl php-xml php-zip php-mbstring php-gd
# PHP-fpm -- NOTE: use your php version
sudo apt install php8.3-fpm
sudo systemctl start php8.3-fpm
# Composer
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
sudo chmod +x /usr/local/bin/composer
Setup project
Clone your repository:
cd /var/www/html
git clone https://repo.sitory/name.git
cd repo
Set up the Laravel project:
cp .env.example .env
composer install
# Generate the application key
php artisan key:generate
# Setup the database
php artisan migrate
Open the .env file in vim and set these values:
APP_ENV=production
APP_URL=https://your.url
Install NPM:
sudo apt install npm
npm install
# NOTE: If `npm install` hangs, you may have to:
# 1. Ctrl+c to kill the job
# 2. Clear the cache with `npm cache clean --force`
# 3. Run `npm install` again
npm run build
Optimise the Laravel project:
php artisan optimize
Set up your user and file permissions:
# Add your user to the www-data group
sudo usermod -a -G www-data ubuntu
# Change owner to www-data (web server)
sudo chown -R $USER:www-data .
# Update file permissions
sudo find . -type f -exec chmod 664 {} \;
sudo find . -type d -exec chmod 775 {} \;
# Change permissions for Laravel to write to cache
sudo chgrp -R www-data storage bootstrap/cache
sudo chmod -R ug+rwx storage bootstrap/cache
Setup Nginx
If apache is running, disable it and prevent starting on boot:
sudo systemctl stop apache2
# Verify it is stopped
sudo systemctl status apache2
# Prevent starting automatically
sudo systemctl disable apache2
Set up the nginx config:
# Remove default nginx site
sudo rm /etc/nginx/sites-enabled/default
# Create your nginx config -- see https://laravel.com/docs/12.x/deployment#nginx
# for a good starting point
sudo vi /etc/nginx/sites-available/mysite
# Link the config to 'enable' the site
sudo ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled/
# Test and start the nginx service
sudo nginx -t
sudo systemctl start nginx
Your site should now be available!
Back to top