How to install LEMP and run Drupal on Ubuntu 16.04

On the 21th of April, Canonical (the company behind Ubuntu) released Ubuntu 16.04 LTS (Xenial Xerus). So, what’s new in Ubuntu 16.04? The bulletin points below show the main new features that were implemented in Xenial:


But let’s not dwell into the specifics further because we have a job to do. If you want to know more about Xenial’s new features, you can check the official release notes.

Today’s article is all about installing Drupal with LEMP (Linux, Nginx, MySQL and PHP-FPM) on an Ubuntu 16.04 VPS. A LEMP stack is a synonym of LEMP server or LEMP web server. It refers to a set-up which includes Linux, Nginx, MySQL (MariaDB) and PHP.


We will be using our SSD 1 Linux VPS hosting plan for this tutorial.


# ssh root@server_ip

You can check whether you have the proper Ubuntu version installed on your server with the following command:

# lsb_release -a

You should get this output:

Distributor ID: Ubuntu
Description:    Ubuntu 16.04 LTS
Release:        16.04
Codename:       xenial


Make sure your server is fully up to date using:

# apt-get update && apt-get upgrade

With the below command you will install the LEMP stack along with some needed PHP extensions.

# apt-get install nginx mysql-server php-fpm php-mysql php-gd php-xml

During the installation you will be prompted with an output from MySQL where you need to set the new MySQL root password just like the image below:








Once the installation is finished, edit the server php.ini file and change the cgi.fix_pathinfo parameter value to 0. By default it will be commented out with a semi-colon and the value set to 1 which practically ensures that PHP will attempt to execute the closest file available when a requested PHP file can’t be found. This is a bad security practice, so let’s change it. Execute the below command:

# nano /etc/php/7.0/fpm/php.ini

Now find the cgi.fix_pathinfo line, uncomment it and set the value to 0. Save and close the file.

Restart PHP-FPM so the changes can take effect.

# systemctl restart php7.0-fpm

You should now configure Nginx to use PHP which can be done by editing the default server block. Issue the below command:

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

Add index.php in the index directive so the index.php files are served.

Then uncomment a part of the section that handles PHP requests. This part consists of the ‘location ~ \.php$ {‘ block which includes the fastcgi-php.conf snippet and socket connected with php-fpm. After editing, the file should look like this:

server {
        listen 80 default_server;
       root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.php index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;

        # pass the PHP scripts to FastCGI server listening on
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        location ~ /\.ht {
                deny all;

Save and close the file. Then, check the configuration file for syntax errors:

# nginx -t

If no errors are reported, restart Nginx for the changes to take effect:

# systemctl restart nginx

The LEMP installation is now complete. If you want to check whether Nginx handles PHP files correctly, create a test phpinfo.php file in the Nginx document root. Open a file with the nano text editor:

# nano /var/www/html/phpinfo.php

Paste the following:


// Show all information, defaults to INFO_ALL

// Show just the module information.
// phpinfo(8) yields identical results.


Now open your favorite web browser and navigate to http://your_server_IP/phpinfo.php . You should be presented with a page like the one in the image below:






















OK. Now that everything is in order with the LEMP installation, install Drupal for your website. We will install Drupal in the Nginx document root (/var/www/html). Enter the directory:

# cd /var/www/html/

Install Drush:

# apt-get install drush php-console-table

Using Drush download the latest stable Drupal version:

# drush dl drupal

You will see something like this. The version of Drupal might be different at the time of your installation.

Project drupal (7.43) downloaded to /var/www/html/drupal-7.43.

Rename the downloaded directory to something more easy to access:

# mv /var/www/html/drupal-7.43/ /var/www/html/drupal

Set the correct files/directories ownership:

# chown www-data: -R drupal

You now need to create a database for the Drupal installation. Enter MySQL as root and execute the below queries:

# mysql -u root -p

mysql> create database drupal;

mysql> grant all privileges on drupal.* to drupaluser@localhost identified by 'your_password';

mysql> flush privileges;

mysql> exit

Now create a virtual block in Nginx so you can access Drupal with your domain. Enter the underneath command:

# nano /etc/nginx/sites-available/drupal

Paste the following:

server {
    server_name domainname.com;

    access_log /var/log/nginx/domainname.com-access.log;
    error_log /var/log/nginx/domainname.com-error.log;
    root /var/www/html/drupal;

    location / {
        index index.php;
        try_files $uri $uri/ /index.php$is_args$args;

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

Of course, don’t forget to replace domainname.com with your actual domain. Save and close the file. Then, enable it by creating a symlink:

# ln -s /etc/nginx/sites-available/drupal /etc/nginx/sites-enabled/

Test the Nginx configuration:

# nginx -t

If everything is successful, restart Nginx so the changes can take effect:

# systemctl restart nginx

Now open your web browser and navigate to http://your_domain.com to finish the Drupal installation.

Congratulations, you have successfully installed Drupal with LEMP on your Ubuntu 16.04 server.

Of course you don’t have to do any of this if you use one of our Linux VPS Hosting services, in which case you can simply ask our expert Linux admins to install Drupal with the LEMP stack for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

Categories: Tutorials

View Comments

Leave a Comment