Deploying a Laravel App on Ubuntu with Nginx

DevOps ~ 8 minutes

# Welcome to Laravel deployment

Hello and welcome! You have built a nice Laravel app on your computer. Now it is time to show it to the world. In this tutorial, we will put your app on an Ubuntu server and serve it with Nginx. Nginx is a fast and popular web server.

Deployment can feel scary at first. Do not worry. We will go slowly, and each command will have its own explanation.

What you need before we start

  • An Ubuntu 22.04 or 24.04 server with a public IP address
  • A user with sudo rights (do not use the root user for daily work)
  • A domain name that points to your server (optional, but needed for HTTPS)
  • Your Laravel code in a Git repository

You can also read the official guide at laravel.com/docs/deployment. Please connect to your server now.

Connect to the server
Code Copied to Clipboard

# Step 1: Update the server

Please change the user name and the IP address to your own. When you are inside the server, the first job is always to refresh the software list. This makes sure you install the newest and safest versions.

Refresh the package list
Code Copied to Clipboard

# Step 2: Install Nginx

Now, let us install the web server. Nginx will receive requests from visitors and send them to your PHP code. The flag -y means that we answer "yes" to the questions automatically.

Install Nginx
Code Copied to Clipboard

Test that Nginx works

After the install, please type your server IP address in a browser. You should see a page with the text "Welcome to nginx!". If the page does not open, a firewall may be blocking port 80. You can read more about Nginx at nginx.org.

# Step 3: Install PHP

Laravel is written in PHP, so the server needs PHP too. We will use PHP-FPM. FPM means FastCGI Process Manager. Nginx does not run PHP itself. It passes the work to PHP-FPM, and PHP-FPM sends back the result.

The next command installs PHP and the extensions that Laravel needs. Please check the PHP version that your Laravel version requires. We use 8.3 in this example.

Install PHP and extensions
Code Copied to Clipboard

# Step 4: Install Composer

Composer is the package manager for PHP. Laravel uses it to download all its libraries. We also need git and unzip, but Ubuntu usually has them already. Let us install Composer now.

Install Composer
Code Copied to Clipboard
Check your PHP version

On some Ubuntu versions, the default PHP is older than what your Laravel needs. Please run php -v and compare it with the Laravel requirements.

If the version is too old, add a trusted PHP repository, for example the Ondrej PPA, and install the correct version. Using the wrong PHP version is the most common cause of strange errors during deployment.

Install MySQL
Code Copied to Clipboard

# Step 5: Prepare the database

Most Laravel apps need a database. We just installed MySQL. Now, let us create an empty database for our app. The command below uses mysql -e, which runs one SQL statement and then exits.

For a real project, you should also create a separate database user with a strong password, and not use the root account in your app.

Create the database
Code Copied to Clipboard

# Step 6: Create the app folder

Web apps on Ubuntu usually live inside /var/www. Let us create a folder named myapp there. We need sudo, because normal users cannot write inside /var/www.

Create the app folder
Code Copied to Clipboard

# Take ownership

The folder now belongs to the root user. It is easier if your deploy user owns it, so you can use Git without sudo. The command below gives the ownership to you. The variable $USER means your current user name.

Change the folder owner
Code Copied to Clipboard

# Step 7: Get your code

Now, we download the code from Git. Please replace the address with your own repository. If the repository is private, you need to add a deploy key to GitHub first. The folder path at the end of the command tells Git where to put the files.

Clone your repository
Code Copied to Clipboard

# Enter the project

Let us go inside the project folder. All the next Laravel commands must be run from here. If you run them in another folder, you will see a message that the file artisan is not found.

Go to the project folder
Code Copied to Clipboard

# Step 8: Install PHP packages

Your Git repository does not contain the vendor folder, because it is big. So we install the packages with Composer. The flag --no-dev skips the testing tools, and --optimize-autoloader makes the app faster.

Install dependencies
Code Copied to Clipboard

Frontend assets

If your app uses Vite for CSS and JavaScript, you also need Node.js. You can build the files on your own computer and upload the public/build folder, or install Node.js on the server and run npm run build. Both ways are fine.

# Step 9: Set up the environment file

Laravel keeps its secrets in a file named .env. This file is not in Git. We create it from the example file that comes with the project.

Create the .env file
Code Copied to Clipboard

# Edit the .env values

Now, please open the file with nano .env and change the important values. Look at the example below. The most important lines are APP_ENV, APP_DEBUG, APP_URL and the database settings. Save the file with Ctrl + O, then press Enter, then leave with Ctrl + X.

.env (production values)
Code Copied to Clipboard

# Why APP_DEBUG must be false

When APP_DEBUG is true, Laravel shows detailed errors with file paths and even some settings. On a live site, strangers can see this. So please set it to false on every production server.

Next, we generate the application key. Laravel uses it to encrypt your data, such as sessions and cookies.

Generate the app key
Code Copied to Clipboard

# Step 10: Run the migrations

Migrations create your database tables. On production, Laravel asks for a confirmation before it runs them. The --force flag answers that question for us.

Run migrations
Code Copied to Clipboard
Fix folder permissions

Laravel must write files into the storage and bootstrap/cache folders. Nginx and PHP-FPM run as the user www-data. If this user cannot write there, you will see a blank page or a "permission denied" error.

We will fix this in two small steps. First, we give the folders to www-data. Then we give write access to the owner and the group. Please do not use 777.

Give storage to www-data
Code Copied to Clipboard

# Set the write access

Now, the web server user owns these two folders. The next command gives read and write power to the owner and the group, and read power to others.

Set the folder permissions
Code Copied to Clipboard

# Step 11: Configure Nginx

Now comes the most important part. We tell Nginx where your app is and how to run PHP. Please create a new file with sudo nano /etc/nginx/sites-available/myapp. We write the config in two small parts.

The first part sets the domain, the root folder and the main rule. Please note that the root must point to the public folder, not to the project folder.

Nginx config (part 1)
Code Copied to Clipboard

# Understanding part 1

The try_files line is the heart of Laravel routing. It says: "If a real file exists, serve it. If not, send the request to index.php." This lets Laravel handle all your pretty URLs.

Now, we add the second part. It sends the PHP files to PHP-FPM and hides private files. Please paste it below the first part, before the last closing bracket.

Nginx config (part 2)
Code Copied to Clipboard

# Understanding part 2

The fastcgi_pass line must match your PHP version. If you installed PHP 8.2, change the socket name to php8.2-fpm.sock. The last block denies access to hidden files, such as .env. This is very important for your security.

Nginx only reads sites from the sites-enabled folder. So we make a symbolic link, which is like a shortcut, from sites-available to sites-enabled.

Enable the site
Code Copied to Clipboard

# Test before you reload

Before we restart anything, let us ask Nginx to check our file. If there is a small typing mistake, Nginx will tell us the file name and the line number. This is much better than breaking a live server.

Test the Nginx config
Code Copied to Clipboard

If the test fails

Read the error message slowly. The most common problems are a missing semicolon at the end of a line, a missing closing bracket, or a wrong socket path. Fix the file, and run the test again until you see the words "syntax is ok".

Reload Nginx
Code Copied to Clipboard

# Your app is live

Wonderful! Now, please open your domain in the browser. You should see your Laravel app. If you see "502 Bad Gateway", check that PHP-FPM is running and the socket path is correct. If you see "403" or "404", check the root path.

The logs are your best friends. You can find them in /var/log/nginx/error.log and in storage/logs/laravel.log.

Next, we add HTTPS, so that your visitors have a safe connection. We will use a free certificate from Let's Encrypt.

Install Certbot
Code Copied to Clipboard

# Step 12: Enable HTTPS

Certbot is a tool that gets the certificate and edits the Nginx file for you. Please make sure that your domain already points to your server IP. Then run the next command with your own domain names.

Get a free SSL certificate
Code Copied to Clipboard
Certificates renew by themselves

Let's Encrypt certificates last for 90 days. The good news is that Certbot installs an automatic renewal job. You do not need to do anything. If you like, you can test it with sudo certbot renew --dry-run.

Also remember to change APP_URL in your .env file to use https://.

Cache the configuration
Code Copied to Clipboard

# Summary and next steps

Congratulations! Your Laravel app is now live on Ubuntu with Nginx and HTTPS. Let us remember the steps.

What we did

  • Installed Nginx, PHP-FPM, Composer and MySQL
  • Cloned the code and installed packages with Composer
  • Set up .env, the app key and the migrations
  • Fixed the permissions of storage and bootstrap/cache
  • Wrote an Nginx config that points to the public folder
  • Added a free HTTPS certificate with Certbot
  • Cached the configuration for better speed

Where to go next

  • Set up a firewall with ufw and allow only SSH, HTTP and HTTPS
  • Run queue workers with Supervisor
  • Add a Git based deploy script for easy updates
  • Take regular database backups

Please practice on a small test server first. You are doing great, and see you in the next tutorial!