How to Deploy Laravel Project to Live Nginx Server

In this tutorial, we will learn how to deploy a Laravel application to a live Nginx server. It assumes you already have Ngnix installed and configured.


Step 1 Create Directory for App & Upload Files

The first step is to create a directory for your Laravel app inside the /var/www directory. Replace with your domain.


sudo mkdir /var/www/


Now (as none root) take ownership and group permissions for this file.


sudo chown your_username -R /var/www/
sudo chgrp your_username -R /var/www/


Now upload your files to the /var/www/ directory using scp (ADD_LINK).


The SCP command will be something like:


scp -r * your_username@server_ip:/var/www/


Set File Permissions

cd into the app root and set the storage and bootstrap/cache directories to have these permissions for www-data:


sudo chgrp -R www-data storage bootstrap/cache
sudo chmod -R ug+rwx storage bootstrap/cache


Create an Nginx Config File

The next step is to create an Nginx config file for the app in the /etc/nginx/sites-available directory.


sudo nano /etc/nginx/sites-available/


Add the following contents, replacing example:


Note – you might need to comment out the ssl lines if you have not created an SSL Certificate yet.


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

       return 301 https://$server_name$request_uri;

server {
       listen 443 ssl http2;
       listen [::]:443 ssl http2;

       include snippets/;
       include snippets/ssl-params.conf;

       root /var/www/html/quickstart/public;

       index index.php index.html index.htm index.nginx-debian.html;


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

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

       location ~ /\.ht {
               deny all;

       location ~ /.well-known {
               allow all;


Now, create a symbolic link for the new config file in /etc/nginx/sites/enabled:


sudo ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/


Test the nginx configuration


sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful


Reload Nginx:


sudo systemctl reload nginx


Create MySQL Database, User and Grant Permissions

Begin by logging into the mysql server, suppling the mysql root password when required:


mysql -u root -p


Now create a database with the following command, replacing exampledb with the name of the database to create:





Now create a new MySQL user with the following command, replacing newuser and user_password:


CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'user_password';


Next, grant all privileges for the new user to the new database:


GRANT ALL PRIVILEGES ON database_name.* TO 'database_user'@'localhost';


Add DB Credientials to the .env File

Now, open the .env file for your app and add the credentials for the database you just created like this:


nano /var/www/



You will also need to change the Laravel public path if you are using a directory other than public_html to serve public files.