Are you trying to deploy Laravel using Nginx on Ubuntu?
This guide is for you.
Laravel is one of the most popular open-source web application frameworks written in PHP. It aims to help developers build both simple and complex applications by making frequently-used application tasks (like caching and authentication) easier.
Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform Ubuntu related Installation tasks.
In this context, we shall look into the steps to set up Laravel with Nginx on Ubuntu 16.04.
To set up a Laravel Application, follow the steps below:
1. Installing Package Dependencies on Ubuntu?
First, we need some PHP extensions and a PHP dependency manager in addition to the basic LEMP stack to run Laravel applications.
i. For that, we first update the package manager cache.
$ sudo apt-get update
ii. 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 on Ubuntu ?
Laravel supports various database servers. But here we are using LEMP stack, MySQL will store data for the application.
i. So, we will log into the MySQL root administrative account.
$ mysql -u root -p
ii. Next, 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;
iii. After that, we create a new user named laraveluser.
mysqL> GRANT ALL ON laravel.* TO 'laraveluser'@'localhost' IDENTIFIED BY 'password';
iv. Then, we flush the privileges to notify the MySQL server of the changes.
mysqL> FLUSH PRIVILEGES;
v. 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.
i. 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
ii. Next, we change the ownership of the directory.
$ sudo chown ibmimedia:ibmimedia/var/www/html/quickstart
iii. 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 .
iv. 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.
5. Configuring Nginx on Ubuntu
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
6. How to Install PHP-FPM 7.2 and Related Modules ?
The last component of the LEMP stack is PHP-FPM… It’s the P in the LEMP stack.
To install PHP-FPM and related PHP-FPM modules, run the commands below;
sudo apt install php-fpm php-common php-mbstring php-xmlrpc php-soap php-gd php-xml php-intl php-mysql php-cli php-zip php-curl
After installing PHP, run the commands below to find the version installed on the server;
php -v
You should see an output like the one below:
PHP 7.2.10-0ubuntu0.18.04.1 (cli) (built: Sep 13 2018 13:45:02) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.10-0ubuntu0.18.04.1, Copyright (c) 1999-2018, by Zend Technologies
The version number determines the location of PHP default configuration file… For PHP 7.2, the location is as shown below:
sudo nano /etc/php/7.2/fpm/php.ini
Replace the version number above with the version of PHP installed.
When the file opens, make the changes on the following lines below in the file and save. The value below are great settings to apply in your environments.
file_uploads = On
allow_url_fopen = On
memory_limit = 256M
upload_max_filesize = 100M
max_execution_time = 360
date.timezone = America/Chicago
After making the change above, save the file and close out.
7. Restart Nginx
When you’re done making PHP changes above, run the commands below to restart Nginx HTTP server for PHP settings to apply:
sudo systemctl restart nginx.service
To test PHP settings with Nginx, create a phpinfo.php file in Nginx root directory by running the commands below:
sudo nano /var/www/html/phpinfo.php
Then type the content below and save the file:
<?php phpinfo( ); ?>
Save the file.. then browse to your server hostname followed by phpinfo.php
Next, run the commands below to open Nginx default site configuration file:
sudo nano /etc/nginx/sites-available/default
The uncomment the PHP block as shown in the code below:
# pass PHP scripts to FastCGI server
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}
Save the file and exit..
This guide will help to set up Laravel with #Nginx on #Ubuntu 16.04. Laravel is the most popular, free, and open-source #PHP #framework in the world, known for its expressive and elegant syntax.
To move laravel project from localhost to production server, do:
1. Zip your #laravel app files.
2. Export your app's #database then set it up on your server.
3. After you have uploaded your laravel app files, open the . env file found on the root.
4. That's it!, now check your live #site.