You are here:Home-Tutorials-Setting Up Nginx, MySQL and PHP on an Ubuntu 16.04 Web Server

Setting Up Nginx, MySQL and PHP on an Ubuntu 16.04 Web Server

In order to install a PHP-based CMS on our Ubuntu 16.04 web server, we need to complete our LEMP webstack. LEMP stands for Linux, Nginx (pronounced “Engine X“), MySQL, PHP. This is a popular alternative to the LAMP web stack (where Apache is used instead of Nginx). Nginx is faster and lighter than Apache, but note that it’s also a bit trickier to configure.

That being said, let’s build on our LEMP web stack!

Note: This tutorial follows the one dedicated to Installing a Ubuntu 16.04 Web Server.

1. Install Nginx

Install NGINX and the PHP dependencies

apt-get install nginx php7.0-cli php7.0-cgi php7.0-fpm

2. Configure Nginx Virtual Hosting and the PHP Processor

We’re using in this guide, but it needs to be replaced with your own domain name. NGINX uses server directives to specify name-based virtual hosts. NGINX calls these server blocks. All server blocks are contained within server directives in site files, located in /etc/nginx/sites-available.

Create a sample configuration based on the default file by running this command:

tail /etc/nginx/sites-available/default -n 13 | cut -c 2- | sudo tee /etc/nginx/sites-available/ 1> /dev/null

Open the file in Nano or Vi:

nano /etc/nginx/sites-available/

Replace all instances of with your domain, modify the root path as shown below, and add the location ~ \.php$ block:

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


    root   /var/www/;
    index  index.html index.php;

    location / {
        try_files $uri $uri/ =404;
    location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            include fastcgi_params;
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
            fastcgi_param SCRIPT_FILENAME /var/www/$fastcgi_script_name;

Create the root directory referenced in this configuration, replacing with your domain name:

sudo mkdir -p /var/www/

Enable the site, disable the default host, and restart the web server:

sudo ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled
sudo rm /etc/nginx/sites-enabled/default
sudo systemctl restart php7.0-fpm nginx

NB: To deactivate a site, simply delete the symbolic link:

sudo rm /etc/nginx/sites-enabled/
sudo systemctl restart nginx

You may also want to edit the http block in /etc/nginx/nginx.conf, which applies across all sites and allows the following options, among others:

  • Hide HTTP header information using server_tokens
  • Configure SSL/TLS settings
  • Customize log file paths

3. Test PHP with FastCGI

Create a file called test.php in your site’s public_html directory:

nano /var/www/

With the following contents:

When you visit in your browser, the standard “PHP info” output is shown.

4. Install Additional PHP Extensions

To complete the install in the perspective of using it with CMS and themes:

sudo apt-get install php-curl php-gd php-mbstring php-mcrypt php-xml php-xmlrpc php7.0-zip

Restart NGINX:

sudo systemctl restart nginx

5. Allow Permalinks (WordPress)

Modify the virtual host to allow permalinks in WordPress, by adding these lines in the location / { block of the file.

location / {
    index index.php index.html index.htm;
    try_files $uri $uri/ /index.php?$args;

Restart PHP and NGINX:

sudo systemctl restart php7.0-fpm nginx

6. Install MySQL

Install the MySQL Database Server:

sudo apt-get install mysql-server php7.0-mysql

7. Create a Database and User

Open MySQL:

mysql -u root -p

Create a database and user with permissions for it.

GRANT ALL PRIVILEGES ON web.* TO 'webuser' IDENTIFIED BY 'password';

Restart PHP:

sudo systemctl restart php7.0-fpm

8. Test The LEMP Stack

Create a test file phptest.php, in the public_html directory.

nano /var/www/

Copy and paste the code available here.

If everything is working fine, it’s now possible to install a CMS like WordPress or Osclass. You have a web server running and ready to go!

By | 2017-06-16T16:52:13+00:00 June 16th, 2017|Categories: Tutorials|Tags: , , , , |2 Comments

About the Author:


  1. […] Installing Nginx, MySQL And PHP 7.0 […]

  2. […] To continue with a LEMP web stack (Ubuntu 16.04, Nginx, MySQL, PHP), check this guide: Installing nginx, MySQL and PHP on an Ubuntu 16.04 Web Server. […]

Leave A Comment