How to setup your LEMP server on Ubuntu 17.04
How to setup Nginx, MariaDB and PHP7 for Laravel5 on Ubuntu 17.04
Introduction
The goal of this tutorial is to provide the steps needed for the right environment configuration to host a Laravel5 application. To achieve this, you need to install and configure:
- a web server: Nginx (a virtual host);
- a database server: MariaDB;
- a PHP7 interpreter.
In this tutorial I suppose to configure your web server to answer to http request to a hostname: http://www.example.com.
Choose your hosting provider
My last project was built with Laravel 5.4, so I needed a stack with a Web Server, PHP7 and MySQL (MariaDB). Today we can choose from different Cloud Hosting Providers for example:
The following steps are based on an Ubuntu 17.04 installation so the instructions are the same whether you choose Vultr or Digital Ocean.
If you need a Droplet on DigitalOcean, you can register on Digital Ocean with this link and get 10$ of credit to use for your Droplet.
Step One: login via root and user creation
During the Droplet creation step on Digital Ocean Control panel, if you didn’t select “SSH Key” option, you should have received an email with the credentials and an IP address like this:
Droplet Name: tutorial-ubuntu-lemp
IP Address: your_server_ip
Username: root
Password: your_password
Access to your new Virtual Server (or droplet):
ssh root@your_server_ip
During the first login, accept the fingerprint, and the system will ask to change password. Type the password received by email and then type twice your new private password (for root user).
For the installation of the Web Server, Database Server and PHP you can use root privileges, but for deploying the Laravel5 application you will need “normal” user privileges.
To add a user named example you need:
adduser example
and then you need to add the user to sudo group to allow it to execute commands with special privileges. To enable the new user example for using sudo command:
usermod -aG sudo example
Step two: Update the operating system and activate the firewall
First of all you need to keep your system updated:
apt-get update
apt-get upgrade
Then, install Nginx:
apt-get install nginx
Now you need to close some ports and to activate the firewall.
Open only HTTP and SSH ports:
ufw allow 'Nginx HTTP'
ufw allow 'OpenSSH'
Activate the firewall:
ufw enable
And finally check the status:
ufw status
The output should be something like this:
Status: active
To Action From
— — — — — —
Nginx HTTP ALLOW Anywhere
OpenSSH ALLOW Anywhere
Nginx HTTP (v6) ALLOW Anywhere (v6)
OpenSSH (v6) ALLOW Anywhere (v6)
Step Three: setup a Virtual Host in Nginx
Create the directory where to place your PHP files:
mkdir -p /var/www/example.com/htdocs/public
Install the PHP interpreter:
apt-get install php-fpm php-mysql
Create a PHP test file under the new directory:
vim /var/www/example.com/htdocs/public/test.php
the content of the new file is:
<?php
phpinfo();
Give the right ownership to the directory (example is the user owner, www-data is the group owner) and permission (750) in recursive way:
chown -R example:www-data /var/www/example.com/
chmod -R 750 /var/www/example.com/
Now you need to create the Nginx configuration file for the virtual host:
cp /etc/nginx/sites-available/default /etc/nginx/sites-available/example.com
The content of the Nginx configuration file is:
Enable the configuration and create a symbolic link for the new configuration file from sites-enabled directory to sites-available:
ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
Add the row:
server_names_hash_bucket_size 64;
to the /etc/nginx/nginx.conf file in the http section.
If you want to deny the request via http with numeric IP address, remove the symbolic link for the deactivation of the the main/default virtual host:
rm /etc/nginx/sites-enabled/default
Test your Nginx configuration:
nginx -t
Restart the Nginx server:
service nginx restart
Step Four: install the Database (MariaDB)
Install the database server:
apt-get install mariadb-server
execute the script to complete the configuration:
/usr/bin/mysql_secure_installation
during the execution of this script, it will ask you to type the root database password.
When the execution is completed, try to access to your database server:
mysql -u root -p
and input the new password to access to the database administration console.
In the database administration console:
- create the database db_example;
- create the user to access the database (dbuserexample) with the password db_password
- grant the right privileges to the new user;
create database db_example;
CREATE USER 'dbuserexample'@'localhost' IDENTIFIED BY 'db_password';
GRANT ALL PRIVILEGES ON * . * TO 'dbuserexample'@'localhost';
FLUSH PRIVILEGES;
Step Five: test your installation and configuration
Open your Web Browser and go to your URL (http://www.example.com/test.php)
If everything is OK, you should see the PHP info page ;-)
In the next tutorial we will deploy a Laravel application in this new environment via Makefile.
Suggestion
If you want to enable HTTPS protocol (for free) on your Virtual Host, I suggest to read my new post: https://medium.com/@robertodev/how-to-enable-https-with-lets-encrypt-on-your-web-server-cc1ce465ad2c
If you need a Droplet on DigitalOcean, you can register on Digital Ocean with this link and get 10$ of credit to use for your Droplet.