DEV Community

Paul Ayuk
Paul Ayuk

Posted on • Updated on

How to deploy a laravel application using nginx on ubuntu

Deploying PHP applications is often a headache for PHP developers. These days, you no longer throw all your PHP files into a folder on a shared host and have it manage everything for you, Instead, you have to create and configure a server, perform multiple installations, and write the appropriate configuration etc.

In this tutorial, I will walk you through deploying a laravel application using nginx on an ubuntu server.

N/B: This tutorial assumes you have a linux server with root access already setup.

Getting Started

When deploying laravel, we often use a web-service solution stack like LAMP (Linux, Apache, Mysql, PHP) or LEMP (Linux, Nginx, Mysql, PHP). This tutorial is focused on the LEMP stack.

Linux: Open-source open operating system that has become the most accepted by developers over the years. It will be the OS powering our web application.

Nginx: Open source HTTP proxy application with reputably much smaller footprints compared to Apache, allowing it to handle higher load of HTTP requests. It’d help us handle our routing (requests / response).

PHP & Mysql: The programming language and default storage program behind laravel.

Installing Nginx

First, from your terminal log into your server and perform an update:

// log into the server
ssh root@your_server_ip

// perform update
apt-get update 
Enter fullscreen mode Exit fullscreen mode

Next, install nginx with the following commands:

// install nginx
apt-get install nginx
Enter fullscreen mode Exit fullscreen mode

Once nginx is installed, we need to configure our firewall to allow access to the service.

The default firewall configuration tool for Ubuntu is ufw (Uncomplicated firewall). Developed to ease iptables firewall configuration, ufw provides a user-friendly way to create an IPv4 or IPv6 host-based firewall. By default UFW is disabled.

To see a list of all the application configurations that ufw knows how to work with, type:

ufw app list
Enter fullscreen mode Exit fullscreen mode

You should see a list similar to this:

Enable Nginx HTTP by running the following in your terminal:

ufw allow 'Nginx HTTP'
Enter fullscreen mode Exit fullscreen mode

You should see the rules for HTTP now allowed from the output.

Ubuntu 18.04 automatically starts Nginx once it is installed. To see your installation in action visit the public IP of your server from a browser and you should see the following screen:

Default nginx screen

Installing PHP

From your terminal, run the following command to install Php and all the necessary packages needed to work with laravel:

apt-get install php curl unzip php-pear php-fpm php-dev php-zip php-curl php-xmlrpc php-gd php-mysql php-mbstring php-xml
Enter fullscreen mode Exit fullscreen mode

Once successful, verify that PHP is installed by typing the following:

php -v
Enter fullscreen mode Exit fullscreen mode

You should see the following:

Restart nginx to allow Php run by typing the following:

systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

Installing Mysql

To be able to store data, we need a storage engine. Laravel supports mysql out of the box but you can also use other storage engines. To install Mysql, run the following from your terminal:

apt-get install mysql-server
Enter fullscreen mode Exit fullscreen mode

During the process, you may be prompted with a screen to input your password, just follow the prompt. Once that is done, configure mysql by running the following on your terminal:

mysql_secure_installation 
Enter fullscreen mode Exit fullscreen mode

Follow the prompts to set your root user password and other presets and you are good to go.

Next, log in to MySQL and create a new database with the following commands:

mysql -u username -p

create database YOUR_DATABASE_NAME
Enter fullscreen mode Exit fullscreen mode

Installing Laravel

At this point, everything is in place to get our app running. We need to get our laravel application on the server before we can use it.

To install laravel and all its associated packages, we need to install composer.

Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.

Run the following commands to install composer:

// download the installer
sudo curl -s https://getcomposer.org/installer | php

// move the composer.phar
sudo mv composer.phar /usr/local/bin/composer
Enter fullscreen mode Exit fullscreen mode

From the command line, type composer to verify the installation. You should see the following screen.

Next, let’s install laravel via composer. From your server root path, navigate to var/www/html path and run the following command:

composer create-project --prefer-dist laravel/laravel blog
Enter fullscreen mode Exit fullscreen mode

Once successful, you should see a newly created blog directory. Navigate to it and set the appropriate permission for the storage folder and your application key by typing the following:

chmod -R 777 storage/

php artisan key:generate
Enter fullscreen mode Exit fullscreen mode

Now update your database details in your .env file to the database you created earlier:

DB_DATABASE=your_database_name
DB_USERNAME=your_database_username
DB_PASSWORD=your_database_password 
Enter fullscreen mode Exit fullscreen mode

Finally, before we try out our application, we need to configure nginx to serve our directory by default.

Open up the config file from your terminal with this command nano /etc/nginx/sites-available/default and update the code to the following:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

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

    server_name localhost;

    location / {
    try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
      try_files $uri /index.php =404;
      fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}
Enter fullscreen mode Exit fullscreen mode

Save the file and restart nginx with the following command:

service nginx configtest
service nginx restart
Enter fullscreen mode Exit fullscreen mode

Now, visit your public IP in the browser and you will see the following:

To test that our database is working run a database migration with the following code:

php artisan migrate
Enter fullscreen mode Exit fullscreen mode

You should see the following response that verifies everything works correctly:

Conclusion

In this tutorial, we walked through the process of deploying a laravel application. We talked about lamp/lemp and the different packages required to successfully run a laravel application. Note that this is just the basics of deploying an application they are so many other aspects we didn’t cover like security, scalability, access control etc. To learn more about laravel deployments click here.

Top comments (4)

Collapse
 
arerex profile image
Arif Zuhairi

I have configure nginx server block without fastcgi_index and fastcgi_param before.. what are the options for?

Collapse
 
arerex profile image
Arif Zuhairi

from all the php packages there, what are the necessary ones?

Collapse
 
josephmhlanga6 profile image
Joseph@CST

Thanks man. Worked for me! Had spent many minutes configuring stuff with no luck. Cheers

Collapse
 
paulayuk profile image
Paul Ayuk

Thanks! I'm glad you found it useful.