Nginx, MariaDB and PHP7 on Ubuntu 16.04 LTS

PHP7 is available from Ubuntu 16.04 LTS Xenial Xerus so you no longer have to rely on third-party PPA to get PHP7 installed on Ubuntu 16.04. In this tutorial, we are going to look at how to install Nginx, MariaDB and PHP7-FPM (LEMP stack) on Ubuntu 16.04 LTS Xenial Xerus.

Step 1: Update Ubuntu 16.04 LTS

Before we install any software, it’s always a good idea to update repository and software packages. SSH into your Ubuntu 16.04 server and enter the below commands.

sudo apt update

sudo apt upgrade

Step 2: Install Nginx Web Server

Nginx is a high performance web server. It also can be used as a reverse proxy. Enter this command to install Nginx Web server.

sudo apt install nginx

After it’s installed, we can enable Nginx to auto start when Ubuntu is booted by running the following command.

Now in your browser’s address bar, type the public IP address of Ubuntu 16.04 LTS server. You should see the “Welcome to nginx!” Web page which means Nginx Web server is running correctly.

You can use the following command to fetch the public IP address of Ubuntu 16.04 server.

sudo apt install curl

curl http://yourname.com

If you are installing LEMP on your local Ubuntu 16.04 box, just type 127.0.0.1 or localhost in the browser address bar.

Finally, we need to make www-data (Nginx user) as the owner of web root directory.

sudo chown www-data /usr/share/nginx/html -R

Step 3: Install MariaDB

MariaDB is a drop-in replacement for MySQL. It is developed by former members of MySQL team who concerned that Oracle might turn MySQL into a closed-source product. Many Linux distributions (Arch Linux, Fedora etc), companies and organizations (Google, Wikipedia) have migrated to MariaDB. So we’re going to install MariaDB instead of MySQL.

sudo apt install mariadb-server mariadb-client
After it’s installed, MariaDB server should be automatically started. Use systemctl to check its status.

To enable MariaDB to automatically start when Ubuntu 16.04 is rebooted:

sudo systemctl enable mysql

Now run the post installation security script.

sudo mysql_secure_installation

When it asks you to enter MariaDB root password, press enter because you have not set the root password yet. Then enter y to set the root password for MariaDB server.

install mariadb on Ubuntu

Next you can just press Enter to answer all the remaining questions. This will remove anonymous user, disable remote root login and remove test database. This step is a basic requirement for MariaDB database security.

mariadb secure installation

Step 4: Install PHP7

PHP extensions are commonly needed for content management systems (CMS) like WordPress. For example, if your installation lacks php7.0-xml, then some of your WordPress site page may be blank and you can find an error in Nginx error log like:

Paste the following PHP code into the file.
Save and close the file. Now in the browser address bar, enter server-ip-address/test.php. Replace sever-ip-address with your actual IP. You should see your server’s PHP information. This means PHP processing is fine.