Skip to main content

Install Nginx, MariaDB and PHP7 (LEMP Stack) on Ubuntu 16.04 LTS

install lemp on ubuntu 16.04

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 (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-get update

sudo apt-get 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-get install nginx

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

sudo systemctl enable nginx

Then start Nginx with this command:

sudo systemctl start nginx

Now check out its status.

systemctl status nginx


● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2016-06-04 08:31:23 EDT; 1 day 2h ago
 Main PID: 298 (nginx)
   CGroup: /system.slice/nginx.service
     ├─298 nginx: master process /usr/sbin/nginx -g daemon on; master_process on
     └─299 nginx: worker process

We can see that Nginx is enabled and it’s running.

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.

install nginx on ubuntu

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

sudo apt-get install curl


If you are installing LEMP on your local Ubuntu 16.04 box, just type 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-get install mariadb-server mariadb-client

After it’s installed, MariaDB server should be automatically stared. Use systemctl to check its status.

systemctl status mysql


● mysql.service - LSB: Start and stop the mysql database server daemon
 Loaded: loaded (/etc/init.d/mysql; bad; vendor preset: enabled)
 Active: active (running) since Wed 2016-04-20 18:52:01 EDT; 1min 30s ago
 Docs: man:systemd-sysv-generator(8)

If it’s not running, start it with this command:

sudo systemctl start mysql

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

Enter the following command to install PHP7 and PHP7 extensions.

sudo apt-get install php7.0-fpm php7.0-mbstring php7.0-xml php7.0-mysql php7.0-common php7.0-gd php7.0-json php7.0-cli php7.0-curl

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:

PHP message: PHP Fatal error:  Uncaught Error: Call to undefined function xml_parser_create()

Install these PHP extensions ensures you CMS runs smoothly.

Now start php7.0-fpm.

sudo systemctl start php7.0-fpm

Check status:

[email protected]:~$ systemctl status php7.0-fpm
● php7.0-fpm.service - The PHP 7.0 FastCGI Process Manager
 Loaded: loaded (/lib/systemd/system/php7.0-fpm.service; enabled; vendor pre
set: enabled)
 Active: active (running) since Wed 2016-04-20 19:21:05 EDT; 2s ago

Step 5: Create Nginx Server Block File

Create a default server block file under /etc/nginx/conf.d/ directory.

sudo nano /etc/nginx/conf.d/default.conf

Paste the following text into the file. Replace with your actual server IP.

server {
  listen 80;
  root /usr/share/nginx/html/;
  index index.php index.html index.htm index.nginx-debian.html;

  location / {
    try_files $uri $uri/ =404;

  error_page 404 /404.html;
  error_page 500 502 503 504 /50x.html;

  location = /50x.html {
    root /usr/share/nginx/html;

  location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;

Save and close the file. Then test nginx configuration and reload it.

sudo nginx -t

sudo systemctl reload nginx

Step 6: Test PHP

To test the cli version of PHP7, we just need to enter this command:

[email protected]:~$ php --version
PHP 7.0.4-7ubuntu2 (cli) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
  with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies

To test PHP-FPM, first create a test.php file in the Web root directory.

sudo nano /usr/share/nginx/html/test.php

Paste the following PHP code into the file.

<?php phpinfo(); ?>

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.

ubuntu php7

For your server’s security, you should delete test.php file now.

Congrats! You have successfully installed Nginx, MariaDB and PHP7 (LEMP stack) on Ubuntu 16.04 LTS Xenial Xerus.

Comments, questions or suggestions are always welcome. If you found this post useful, ? please share it with your friends on social media! Stay tuned for more Linux tutorials.

Rate this tutorial
[Total: 1 Average: 5]
  • John Mackay

    Thanks so much for this. It saved me a heap of googling 🙂

  • Tom

    Good job but I am wondering why you choose /usr/share/nginx/html/ instead of /var/www/html?

    • /var/www/html is an Apache tradition. Nginx officially recommends /usr/share/nginx/html.

  • Hello. Thank you for this great guide…it seems super helpful. I aim to install WordPress multisite with subdirectories and domain mapping. What additional steps will I need to perform?
    Also, I’ll need SSL certificates by Let’s Encrypt on some of the sites I’ll be serving through this multisite setup. Any help on additional steps required for that will also be most appreciated.