Install Nginx, MariaDB and PHP 7.1 (LEMP) with Ubuntu 16.04 LTS Server

Looking for an easy way to install and configure LEMP on Ubuntu 16.04 LTS? The steps below will get you on your way!

LEMP is an acronym for Linux (Ubuntu), Engine-x (Nginx), MySQL (MariaDB) and PHP… LEMP is an alternative to LAMP… It’s a stack of open source applications typically installed together to create dynamic and powerful websites… WordPress, Drupal and few other content management systems use LEMP…

With PHP 7.1 now readily available and supported, you can now use the LEMP stack to improve your websites and blogs… For those who are new and want to learn how to install LAMP with Ubuntu 16.04 LTS server, the steps below should be a great starting point.

This brief tutorial is going to show students and new users how to install Ubuntu 16.04 LT with Nginx, MariaDB and PHP 7.1 support, or the LEMP Stack.

To get started with installing LEMP, follow the steps below:

Step 1: Install Nginx HTTP Server

Nginx HTTP Server is probably the second most popular web server in use today and member of the LEMP stack.. So, go and install Nginx on Ubuntu by running the commands below:

sudo apt install nginx

Next, run the commands below to stop, start and enable Nginx service to always start up with the server boots.

sudo systemctl stop nginx.service
sudo systemctl start nginx.service
sudo systemctl enable nginx.service

After installing Nginx, open your browser and browse to the server name or IP address…

ex.. http://localhost

You should see Nginx default test page.

Nginx ssl ubuntu

Step 2: Install MariaDB Database Server

MariaDB database server is a great place to start when looking at open source database server… Although MySQL was originally the default database server, MariaDB has taken over.. To install it run the commands below.

sudo apt-get install mariadb-server mariadb-client

After installing, the commands below can be used to stop, start and enable MariaDB service to always start up when the server boots.

sudo systemctl stop mysql.service
sudo systemctl start mysql.service
sudo systemctl enable mysql.service

After that, run the commands below to secure MariaDB server by creating a root password and disallowing remote root access.

sudo mysql_secure_installation

When prompted, answer the questions below by following the guide.

  • Enter current password for root (enter for none): Just press the Enter
  • Set root password? [Y/n]: Y
  • New password: Enter password
  • Re-enter new password: Repeat password
  • Remove anonymous users? [Y/n]: Y
  • Disallow root login remotely? [Y/n]: Y
  • Remove test database and access to it? [Y/n]:  Y
  • Reload privilege tables now? [Y/n]:  Y

Restart MariaDB server

sudo systemctl restart mysql.service

To test if MariaDB is installed, type the commands below to logon to MariaDB server

sudo mysql -u root -p

Then type the password you created above to sign on… if successful, you should see MariaDB welcome message

mariadb welcome

Step 3: Install PHP 7.1 FPM and Related Modules

PHP 7.1 isn’t available on Ubuntu default repositories… in order to install it, you will have to get it from third-party repositories.

Run the commands below to add the below third party repository to upgrade to PHP 7.1

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/php

Then update and upgrade to PHP 7.1

sudo apt update

Run the commands below to install PHP 7.1 FPM and related modules.

sudo apt install php7.1-fpm php7.1-common php7.1-mbstring php7.1-xmlrpc php7.1-soap php7.1-gd php7.1-xml php7.1-intl php7.1-mysql php7.1-cli php7.1-mcrypt php7.1-zip php7.1-curl

After install PHP 7.1, run the commands below to open PHP-FPM default file.

sudo nano /etc/php/7.1/fpm/php.ini

Then 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.

Step 4: Configure Nginx PHP-FPM Settings

Next, open the Nginx site configuration file… by default it’s stored at /etc/nginx/sites-available/default

If you have a custom file, then edit it to enable Nginx PHP support. Run the commands below to open Nginx default site configuration file

sudo nano /etc/nginx/sites-available/default

Then uncomment or remove the # symbol on the highlighted lines below to enable Nginx PHP support.

server {
    listen 80;
    listen [::]:80;
    root /var/www/html;
    index  index.php index.html index.htm;

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

     # 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.1-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass;

Restart Nginx and PHP-FPM services

sudo systemctl restart nginx.service
systemctl restart php7.1-fpm.service

To test PHP-FPM 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

ex. http://localhost/phpinfo.php

You should see PHP default test page…

ubuntu php apache2

That’s it!

This is how to install LEMP with Ubuntu 16.04 LTS Server

You may also like the post below:


  1. Hi !Robot,

    Thanks for comprehensive tutorial. I followed it and configured my server. Everything was fine but after I edited the nginx server block and try to open phpinfo.php page in browser, it returns me 502 Bad Gateway. I checked everything but could not solve it. Could you please help me.

    Server Configuration:
    server {
    listen 80;
    listen [::]:80;
    root /var/www/html;
    index index.php index.html index.htm;
    server_name localhost;
    location / {
    try_files $uri $uri/ =404;
    location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;

  2. Suresh Khanal

    change fastcgi_pass unix:/run/php/php7.0-fpm.sock; to fastcgi_pass unix:/run/php/php7.1-fpm.sock; (or path to yours php version)

    restart php daemon

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.