How to Deploy Laravel Application with Nginx on Ubuntu

Posted on September 7th, 2019

Laravel is one of the best PHP frameworks right now. Same is with Nginx, It is one of the most popular web servers in the market. Apache ranks #1 in the market but Nginx is gaining more and more popularity because of its performance and because it is lightweight. In this guide, we will see how to deploy a Laravel application on Nginx web server.

Laravel is a PHP framework. It means that your server must already have Nginx, PHP and the database management system installed. We have a guide that you can follow to configure a complete Nginx and PHP-FPM stack on your server. If you already have Nginx and PHP configured on your server, you can continue with this guide.

The only requirement to follow this guide is to have a root or sudo access on the server. It is because we have to edit Nginx virtual host files to host our Laravel Application on the server.

So, If you have root access or sudo privileges on the server and your server has Nginx and PHP configured, we can start with the first step.

Update Nginx Virtual Host

Virtual host files are the configuration files that contain information about websites hosted on the server. Whenever a request is made, the Web server will refer to Virtual host files to identify a website by domain name and to redirect a request to a specific directory called DocumentRoot.

The virtual host files of Nginx are stored in /etc/nginx/sites-available a directory. In this case, however, I will update the default Nginx virtual host file for demonstration. However, if you have multiple virtual host files on your server, you can edit a virtual host file as per your requirements.

The reason why we have to update a virtual host file is, Laravel’s main index.php file is stored in the public directory of the project. It means that we have to add /public to the document root in Nginx virtual host to make our Laravel site work without adding /public at the end of the domain name or IP address.

So, execute the following command to open a virtual host file in edit mode.

$ sudo nano /etc/nginx/sites-available/default

In this case, I am updating the default Nginx virtual host file. But you can update any virtual host file you want as per your requirements. Once the file is open in edit mode, add /public at the end of the root directive, just like the following example.

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /var/www/html/public;
        index index.html index.php index.htm index.nginx-debian.html;
        server_name _;

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        location / {
                try_files $uri $uri/ =404;

    location ~ \.php$ {
            include fastcgi.conf;
            fastcgi_intercept_errors on;
            fastcgi_pass unix:/run/php/php7.2-fpm.sock;

If you followed our guide to set up Nginx with PHP-FPM, this configuration should work perfectly fine without any manual configuration. However, if you are not using PHP-FPM on Unix socket, you might have to edit the content inside location block to make your PHP files work.

Once done, restart the Nginx server by executing the following command.

$ sudo service nginx restart

We have to restart the Nginx server to apply the changes we made in the virtual host file. Once done, we can move on to the next part.

Install Required Dependencies

Laravel uses composer to manage all the PHP dependencies and other PHP packages. So, we have to install composer on our server. And, If your version control system is git, we also have to install git on the server.

So, we will install Git and composer on the server. It is a very easy task, we just have to run a few commands to install these packages on our server. Execute the following commands to install these packages.

$ sudo apt-get update
$ sudo apt-get install composer git -y

It might take a few minutes to install these packages. Once done, we can finally move on to deploy our Laravel project on our server.

Option 1: Clone a git repository

Git is the most popular version control system in the world. Most probably, you are using git as a version control system for your Laravel project. As we now have git installed on our server, we can clone a git repository.

In this demonstration, I will clone the official Laravel git repository. But you can clone your project repository from Github, Bitbucket or any other service provider. First of all, we have to clear our project directory, otherwise, git will not clone the application code.

Execute the following command to clear the project directory.

$ cd /var/www/html
$ rm -rf * .*

In my case, the project directory is /var/www/html because the document root is /var/www/html/public. So, I have to clear everything inside my project directory. Yours can be different. You can find the project directory from your Nginx virtual host.

Once the project directory is clean, execute the git clone command to clone your repository. In this demonstration, I am cloning the official Laravel git repository from

$ cd /var/www/html
$ git clone .

The . at the end of the command will make git clone command clone all the files into the current directory. Once your git repository is cloned successfully, execute the following command to install all the dependencies.

$ composer install

And then, execute the following commands to copy all the contents of .env.example file to .env file and to create an encryption key.

$ sudo mv .env.example .env
$ php artisan key:generate

Now, we have to give ownership to www-data so that Nginx can create cache and update Laravel log files. Execute the following command to change file permissions.

$ sudo chown -R www-data:www-data /var/www/html

Now, you will be able to access your website by accessing your domain name or IP address of the server. Now, Let’s see how to create a brand new Laravel project on the server.

Option 2: Deploy Brand new Laravel App

Similar to option 1, we first have to clear our project directory. By clear, I mean we have to remove all the files inside our project directory. You can follow that step from Option 1.

Once done, execute the following command in your project directory to create a brand new Laravel Project.

$ cd /var/www/html
$ composer create-project --prefer-dist laravel/laravel .

Now, execute the following command to give ownership of the project files to www-data.

$ sudo chown -R www-data:www-data /var/www/html

Finally, you will be able to access your the brand new Laravel application by accessing your domain name or IP address in the browser.


Conclusion: Nginx can work perfectly with Laravel. And it is not so hard to set up your Laravel project on Nginx web server. If you followed our guide to set up Nginx web server with PHP-FPM, this guide should work perfectly fine for you without any kind of manual work.

Let us know if you have any questions in the comment section given below. Or, you can also contact the InterServer support team for help! We are happy to help!

One Response to “How to Deploy Laravel Application with Nginx on Ubuntu”

  1. Sebastian says:

    It is VERY hard to set up laravel on nginx server because routing not work outside the box

Leave a Reply