Need help?

Our experts have had an average response time of 11.7 minutes in August 2021 to fix urgent issues.

We will keep your servers stable, secure, and fast at all times for one fixed price.

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 *

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF