Bobcares

How we deploy Laravel using Nginx on Ubuntu 16.04

by | Jan 10, 2021

Are you looking for steps to deploy Laravel using Nginx on Ubuntu? Take a peek at this blog.

Here at Bobcares, we have seen several such Ubuntu related queries as part of our Server Management Services for web hosts and online service providers.

Today we’ll take a look at how to set up Laravel with Nginx on Ubuntu 16.04

 

How To Deploy a Laravel Application with Nginx on Ubuntu 16.04

Now let’s take a look at how our Support Engineers set up the Laravel application.

 

1. Installing Package Dependencies

First, we need some PHP extensions and a PHP dependency manager in addition to the basic LEMP stack to run Laravel applications.

For that, we first update the package manager cache.

$ sudo apt-get update

Next, we install the PHP extensions, Composer, and unzip at the same time.

$ sudo apt-get install php7.0-mbstring php7.0-xml composer unzip

 

2. Configuring MySQL

Laravel supports various database servers. But here we are using LEMP stack, MySQL will store data for the application.

So, we will log into the MySQL root administrative account.

$ mysql -u root -p

We enter the password of MySQL root when asked for it to log in.

Then we create a new database called laravel.

mysqL> CREATE DATABASE laravel DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;

After that, we create a new user named laraveluser.

mysqL> GRANT ALL ON laravel.* TO 'laraveluser'@'localhost' IDENTIFIED BY 'password';

Then, we flush the privileges to notify the MySQL server of the changes.

mysqL> FLUSH PRIVILEGES;

Finally, we exit MySQL.

mysqL> exit

 

3. Setting Up the Demo Application

GitHub provides a demo quickstart application that is distributed by Laravel and is a simple task list. Also, it allows to add and remove to-do items and stores its tasks in the MySQL database.

First, we create a directory within the Nginx webroot. Since the demo application is named quickstart, let’s use /var/www/html/quickstart.

$ sudo mkdir -p /var/www/html/quickstart

Next, we change the ownership of the directory.

$ sudo chown sammy:sammy /var/www/html/quickstart

After that, we move to the new directory and clone the demo application using Git.

$ cd /var/www/html/quickstart
$ git clone https://github.com/laravel/quickstart-basic .

Then we install the project dependencies. Here we are installing Composer as Laravel utilizes it to handle dependency management.

$ composer install

Now, the app itself is setup.

 

4. Configuring the Application Environment

Now we will modify some security-related application settings. For that, we will open the Laravel environment configuration file with a nano text editor.

$ sudo nano /var/www/html/quickstart/.env

We make the below changes to the file.

APP_ENV=production
APP_DEBUG=false
APP_KEY=b809vCwvtawRbsG0BmP1tWgnlXQypSKf
APP_URL=http://example.com

DB_HOST=127.0.0.1
DB_DATABASE=laravel
DB_USERNAME=laraveluser
DB_PASSWORD=password

. . .

We save the file and exit.

Next, we will run the database migrations. This will populate the newly created database with the necessary tables for the demo application to run properly.

$ php artisan migrate

Artisan will ask to confirm if we intend to run it in production mode. We answer y to the question. Afterward, it will run the necessary database tasks.

Now we have fully installed and configured Laravel. When opening Laravel in the web browser, the following page must appear.

laravel nginx ubuntu

 

5. Configuring Nginx

Now, we shall change the group ownership of the storage and bootstrap/cache directories to www-data.

$ sudo chgrp -R www-data storage bootstrap/cache

Then we recursively grant all permissions, including write and execute, to the group.

$ sudo chmod -R ug+rwx storage bootstrap/cache

Now, we will alter the Nginx configuration to make it correctly work with the Laravel installation. For that, first, we will create a new server block config file for our application by copying over the default file.

$ sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/example.com

Then we will open the newly created configuration file.

$ sudo nano /etc/nginx/sites-available/example.com

We remove the default_server designation from listen to directives. Also. we update the server_name directive and update the request URI.

The modified Nginx configuration file will look as below.

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

. . .

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

server_name example.com www.example.com;

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

. . .
}

We then save and close the file.

Now, we need to enable the new configuration file by creating a symbolic link from this file to the sites-enabled directory.

$ sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

Finally, we reload Nginx to take the changes into account.

$ sudo systemctl reload nginx

[Need any further assistance with Ubuntu related queries? – We’re available 24*7]

 

Conclusion

Today, we saw how our Support Engineers set up Laravel with Nginx on Ubuntu 16.04.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.