How to Deploy Laravel Application with Nginx on Ubuntu
Tags: nginx, ubuntu, laravel, webdev
Published: 2022-01-24 14:27:47

Laravel is one of the most popular open-source web application frameworks written in PHP. It aims to help developers build complex and straightforward applications by making frequently used application tasks (like caching and authentication) easier.

This blog will deploy a simple Laravel application with a production environment in mind, which requires a few common steps. For example, applications should use a dedicated database user with access limited to necessary databases. File permissions should guarantee that only required directories and files are writable. Application settings should be taken into consideration to make sure no debugging information is being displayed to the end-user, which could expose application configuration details.

This blog will show you how to install Laravel with Nginx on Ubuntu.

## 1. Prerequisites

- The operating system running Ubuntu Linux
- A root or non-root user with Sudo privileges
- Has stable internet connection
- Terminal window / Command line

## 2. Install Nginx On Ubuntu
If you have installed Nginx, you can skip this step. If you have not installed Nginx, then you click on this link: [Install Nginx on Ubuntu 20.04 LTS](https://techvblogs.com/blog/install-nginx-on-ubuntu-20-04)

## 3. Install PHP On Ubuntu
Laravel is based on PHP, so you’ll need to install PHP and related modules. PHP 8 is the default in Ubuntu repositories at the time of this writing.

To install PHP and related modules, run the following commands:
```
sudo apt-get update
sudo apt-get install software-properties-common -y
sudo add-apt-repository ppa:ondrej/php -y
sudo apt-get install php8.0 php8.0-mbstring php8.0-gettext php8.0-zip php8.0-fpm php8.0-curl php8.0-mysql php8.0-gd php8.0-cgi php8.0-soap php8.0-sqlite3 php8.0-xml php8.0-redis php8.0-bcmath php8.0-imagick php8.0-intl -y
```
The -y flag in the command will automatically allow the server to install PHP 8. If you don’t add the -y flag, it will simply ask you for confirmation.

PHP 8 should be installed and ready to use.

## 3. Install Composer On Ubuntu
There are a few steps that we can follow to deploy Laravel on Nginx. The first one is to install all the required dependencies on the server. The second is to clone the git repository or create a new Laravel project inside our project directory.
So, Let's get started with the first step.

If we are going to clone a git repo, we have to install git on our server. It is straightforward. And, we also have to install composer on the server because we have to install Laravel's dependencies using composer update or composer install command. So, Let's, first of all, install the git and composer on our server.

Execute the following commands to install git and composer on the Ubuntu server.
```
sudo apt-get install git composer -y
```

## 4. Install Laravel

### Option 1: Clone a Git Repository
[Git](https://techvblogs.com/blog/how-to-install-git-on-ubuntu-20-04) is a free and open-source distributed version control system designed to handle small and extensive projects with speed and efficiency. You are probably using git as a version control system for your Laravel project. 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, we have to clear our project directory. Otherwise, git will not clone the application code.

Run the following command to clear the project directory.
```
cd /var/www/html
sudo 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. I am cloning the official Laravel git repository from Github.com in this demonstration.
```
cd /var/www/html
sudo git clone https://github.com/laravel/laravel.git .
sudo composer install
```
For example, I am cloning the Laravel project from the official git repository of Laravel. You can clone your project from the repository you desire. Just replace the URL, and you are good to go. We added the "." at the end of the git clone command to clone the project files in the same directory.

### Option 2: Deploy a new Laravel Project
Similar to option 1, we first have to clear our project directory. We have to remove all the files inside our project directory by clear. You can follow that step from Option 1.

Once done, run the following command in your project directory to create a brand new Laravel Project.
```
cd /var/www/html
sudo composer create-project --prefer-dist laravel/laravel .
```

## 5. Update Permission
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.

## 6. Update ENV File and Generate An Encryption Key
To copy the file from .env.example to .env and generate an encryption key, run the following commands.
```
cd /var/www/html
sudo cp .env.example .env
php artisan key:generate
```
Next, edit the .env file and define your database:
```
cd /var/www/html
sudo nano .env
```
Change the following lines:
```
APP_URL=https://techvblogs.com

DB_CONNECTION=mysql
DB_HOST=YOUR_DB_HOST
DB_PORT=3306
DB_DATABASE=techvblogs
DB_USERNAME=admin
DB_PASSWORD=YOUR_PASSWORD
```
After updating your .env file, press CTRL+X, Y, and Enter key to save the .env file.

## 7. Configure Nginx for Laravel
Laravel is tricky. It is because the main index.php file of the project lives in the project's public directory. It means that we have to update our virtual host so that it should route the traffic into the public directory inside our project directory.

Next, We will need to create an Nginx virtual host configuration file for Laravel. Run the following command:
```
sudo nano /etc/nginx/sites-available/techvblogs.conf
```
Add the following lines:
```
server {
    listen 80;
    server_name techvblogs.com;
    root /var/www/html/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";

    index index.php;

    charset utf-8;

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

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}
```
After updating your virtual host file, press CTRL+X, Y, and Enter key to save the updated virtual host file.

Next, configure the Laravel virtual host above, enable it. Run the following command:
```
sudo ln -s /etc/nginx/sites-available/techvblogs.conf /etc/nginx/sites-enabled/techvblogs.conf
```
Finally, restart the Nginx server to apply the changes. Run the following command:
```
sudo systemctl restart nginx
```
If your Nginx server successfully restarts, you will be able to access your Laravel project in the browser.

Thank you for reading this blog.