How to Install Nginx, MariaDB and PHP7 (LEMP Stack) 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.

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

enabled” indicates that auto start at boot time is enabled and we can see that Nginx is 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 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 install mariadb-server mariadb-client

After it’s installed, MariaDB server should be automatically started. 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 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()

Installing these PHP extensions ensures that your CMS runs smoothly.

Now start php7.0-fpm.

sudo systemctl start php7.0-fpm

Check status:

systemctl status php7.0-fpm

Sample output:

● 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 a Default Nginx Server Block File

Remove the default symlink in sites-enabled directory.

sudo rm /etc/nginx/sites-enabled/default

Then create a new 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;
  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$ {
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    include snippets/fastcgi-php.conf;

  location ~ /\.ht {
    deny all;

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:

php --version

Sample output:

PHP 7.0.15-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 16.04 nginx php

How to Install PHP7.2

PHP7.2 is the latest stable version of PHP, released on November 30, 2017 and it has minor performance boost over PHP7.0. We can add the PPA from Ondrej Sury to install PHP7.2 on Ubuntu 17.10. That guy is also the maintainer of Certbot PPA.

sudo apt install software-properties-common

sudo add-apt-repository ppa:ondrej/php -y 

sudo apt update

Then we can install PHP7.2 and common extensions by using the following command.

sudo apt install php7.2 php7.2-fpm php7.2-mysql php-common php7.2-cli php7.2-common php7.2-json php7.2-opcache php7.2-readline php7.2-mbstring php7.2-xml php7.2-gd php7.2-curl

Now start PHP7.2-FPM.

sudo systemctl start php7.2-fpm

Enable auto-start at system boot time.

sudo systemctl enable php7.2-fpm

Check its status:

systemctl status php7.2-fpm

Using PHP7.2-FPM with Nginx

To make Nginx use PHP7.2-FPM instead of PHP7.0-FPM, we need to edit the Nginx server block file.

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

Find the following line.

fastcgi_pass unix:/run/php/php7.0-fpm.sock;

Change php7.0-fpm to php7.2-fpm.

fastcgi_pass unix:/run/php/php7.2-fpm.sock;

Save and close the file. Then reload Nginx for the change to take effect.

sudo systemctl reload nginx

If you refresh the server-ip-address/info.php page, you will see Nginx is now using PHP7.2-FPM.

Ubuntu 16.04 php7.2

PHP7.0 can coexist with PHP7.2. If you don’t want to use PHP7.0, then you can remove it. Please beware that some web application may not be compatible with PHP7.2 like NextCloud 12. In my test, WordPress runs well with PHP7.2.

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: 68 Average: 4.7]

25 Responses to “How to Install Nginx, MariaDB and PHP7 (LEMP Stack) on Ubuntu 16.04 LTS

  • John Mackay
    5 years ago

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

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

    • Xiao Guoan
      5 years ago

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

  • Haroon Q. Raja
    5 years ago

    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.

  • Pete Long
    3 years ago

    Great walkthrough, saved me a ton of time ThanQ

  • Zend HetMij
    3 years ago

    p.s. the auto anable steps for the stack aren’t necessary, they are set by default.

  • Thank you very much. Everything is clear and consice explained.

  • JayBeRayBearGun
    3 years ago

    On my Debian 9 system I also needed php7.0-bz2

  • JayBeRayBearGun
    3 years ago

    also changed a line in nginx default.conf to read like this:

    listen [::]:80 ipv6only=on;

    this gets rid of a trivial error message in the log

  • joao vitor casarin
    3 years ago

    Hello, if I would wanna change the main page of my domain, currently it’s showing “Welcome to Nginx” and a little text, i should change the files in /usr/share/nginx/html OR /var/www/html ??? I really need your help, please answer asap!!

    • If your Nginx configuration file set the web root to /usr/share/nginx/html/, then you should change the files in /usr/share/nginx/html/.

      If you copy the configurations from this article, then your web root is set to /usr/share/nginx/html/.

      • João Vitor Casarin
        3 years ago

        I don’t need to change anything in /var/www/html ???

    • If you set web root to /usr/share/nginx/html/, you should put your html and php files to this directory, and there’s no need to change anything in /var/www/html/.

      If you set web root to /var/www/html/, then you should put html and php files to this directory, and there’s no need to change anything in /usr/share/nginx/html/.

      • João Vitor Casarin
        3 years ago

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

        Is this the part I set my web root to /usr/share/nginx/html/ ?

    • The web root is set in Nginx configuration file, in this case the /etc/nginx/conf.d/default.conf file. The following line indicates that web root is set to /usr/share/nginx/html/.

       root /usr/share/nginx/html/;

      This command:

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

      is just to make sure Nginx can write to this directory.

      • João Vitor Casarin
        3 years ago

        I’ve just changed the files in /usr/share/nginx/html/

        Deletede index.html, left only index.php on default.conf, reloaded nginx and it still shows me that “welcome to nginx” page… Idk what to do… Also, mysql(mariadb) isn’t running properly, when it should be outputed “enabled”, it says “bad”, and I can’t reload/restart its service…

    • Maybe you forgot to delete the index.html file in /usr/share/nginx/html/ directory.

      The MariaDB package on Ubuntu 16.04 doesn’t ship a native SystemD service, so the status shows “bad”, but it will automatically start at boot time.

      To restart MariaDB server, run

      sudo systemctl restart mysql

      You can’t reload MariaDB server, there’s no reload command in MariaDB.

  • Good shortcut but why don’t use something like SlickStack

  • Hi There,
    Firstly thank you for the guide. I have one small problem, every time I try to open the test.php , it downloads rather than opens. I have googled a bit and can’t seem to fix it. I think my problem may be that I have a existing nginx conf file that is acting as a reverse proxy. When I do a nginx -t it points to my reverse proxy conf rather than the path above. I ultimately want o install roundcube and still have my reverse proxy.

  • Hi,
    Great tutorial.
    I followe all the steps and is working.
    I have one porblem, after this tutorial I was trying to setup Nginx chache and also Rednis. I do not like wordpress chaching plugins, I better prefer a back end chaching system. But I am having trouble because every time I change something to my page is not reflecting after I restart.
    Can you make a full tutorial on setting LEMP and also WordPress and hardening Nginx and how to setup chache.
    I find tutorials around only for beggineres but nothing that can be a full setup.
    Thank you.

  • robinJiao
    2 years ago

    tried so many but only this one works!!! great job!
    thank you Lao Xiao

  • Thank you!

  • thanks for sharing

Leave a Comment

  • Comments with links are moderated by admin before published.
  • Your email address will not be published.
  • Use <pre> ... </pre> HTML tag to quote the output from your terminal/console.
  • Please use the community ( for questions unrelated to this article.
  • I don't have time to answer every question. Making a donation would incentivize me to spend more time answering questions.

The maximum upload file size: 2 MB. You can upload: image. Links to YouTube, Facebook, Twitter and other services inserted in the comment text will be automatically embedded. Drop file here