December 29, 2016 · Setup Guides

Install a LEMP Stack on Ubuntu 16.04 (Nginx, MariaDB and PHP 7)

In this quick post, I would like to document how I deployed an Nginx, MariaDB and PHP 7 stack on Ubuntu 16.04 for my personal MediaWiki site.

This is actually pretty straightforward:

Step 1 -- Install Nginx

sudo apt-get install nginx -y

Step 2 -- Install MariaDB

sudo apt-get install mariadb-server
sudo mysql_secure_installation

Step 3 -- Install PHP 7

sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get install php-fpm php-mysql

Edit fpm/php.ini

sudo vim /etc/php/7.0/fpm/php.ini

...and apply the following changes:

; cgi.fix_pathinfo=1

Step 4 -- Test your setup

Since I deployed this in its own machine, I updated my default Nginx vhost file to use PHP FPM

Here's my /etc/nginx/sites-available/default:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;
    index index.php index.html index.htm index.nginx-debian.html;

    server_name server_domain_or_IP;

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;

    location ~ /\.ht {
        deny all;

Restart Nginx:

sudo service nginx restart

Under /var/www/html, I created a new file named info.php:

sudo vim /var/www/html/info.php

...with the following content:


Going to http://your-server-ip/info.php should now display your PHP installation details.

That's it! You now have a running LEMP Stack on Ubuntu 16.04 with MariaDB and PHP 7.