Students Tutorial – Install Drupal with Nginx Support on Ubuntu 16.10

Drupal is a content management system (CMS) that allows users to run and manage professional websites and blogs with ease. Just like WordPress and Joomla, Drupal requires the LAMP stack to operate. LAMP is an acronym for Linux, Apache2, MySQL and PHP.

The stack default configuration has always been Linux, Apache2, MySQL and PHP. However, for this post, we’re going to be using LEMP stack instead. LEMP is an acronym for Linux, Engine-X (Nginx), MySQL and PHP.

We’re going to replace the A for Apache2 in LAMP to E for Nginx webserver.

When you’re ready, follow the guide below from start to finish to get this working on Ubuntu 16.10.

Step 1: Install LEMP on Ubuntu

As mentioned above, to get Drupal installed and functioning, you must install LAMP or LEMP. Fortunately, we’ve written how to easily install LEMP in Ubuntu. To read that post, please click the link below:

Lesson 36: Installing the LEMP Stack on Ubuntu

Step 2: Creating Drupal Database and User

Now that LEMP is installed, continue below to create Drupal database and database user. Drupal stores its content in databases and the database user is used to connect to the databases to retrieve and manage the stored content.

To create the database and user in MySQL, follow the guide below:

Run the commands below to logon to MySQL Database server.

mysql -u root -p

Then the commands below to create a database called drupaldb.

CREATE DATABASE drupaldb;

Next, run the commands below to create a database user named drupaluser and set a new password. Then grant the user full rights to manage the database.

GRANT ALL ON drupaldb.* TO 'drupaluser'@'localhost' IDENTIFIED BY 'new_password_here';

Next, run the commands below to save your changes and exit.

FLUSH PRIVILEGES;
exit;

Step 3: Download and Configure Drupal

After creating the database and the user account, go and download the latest Drupal content. At the time of this post, the latest version number was at 8.2.4. So if you need to install the most current, you may have to adjust the version number accordingly.

Use the commands below to download Drupal

cd /tmp && wget http://ftp.drupal.org/files/projects/drupal-8.2.4.tar.gz

Next, run the commands below to extract the downloaded file

tar xzvf drupal*

When the content is extracted, copy Drupal content to Nginx root default root directory at /var/www/html/

sudo mv drupal-8.2.4/* /var/www/html

Next, create Drupal settings.php file by making copy of the default.settings.php file. To do that, run the commands below

sudo cp /var/www/html/sites/default/default.settings.php /var/www/html/sites/default/settings.php

Don’t forget to delete Nginx and Apache2 default index files

sudo rm /var/www/html/index.html
sudo rm /var/www/html/index.nginx-debian.html

Next, run the commands below to adjust the root directory so Drupal can function properly.

sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html

Step 4: Configure Nginx Server

Since we want Nginx as our default webserver, let’s stop and disable Apache2 webserver so it doesn’t conflict with Nginx.

sudo systemctl stop apache2.service
sudo systemctl disable apache2.service

When Apache2 is disabled, open Nginx default site configuration file and add the below highlighted changes.

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

# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
	listen 80 default_server;
	listen [::]:80 default_server;

	# SSL configuration
	#
	# listen 443 ssl default_server;
	# listen [::]:443 ssl default_server;
	#
	# Note: You should disable gzip for SSL traffic.
	# See: https://bugs.debian.org/773332
	#
	# Read up on ssl_ciphers to ensure a secure configuration.
	# See: https://bugs.debian.org/765782
	#
	# Self signed certs generated by the ssl-cert package
	# Don't use them in a production server!
	#
	# include snippets/snakeoil.conf;

	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;
                try_files $uri /index.php?$query_string;
	}
       
        location @rewrite {
               rewrite ^/(.*)$ /index.php?q=$1;
        }

	# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
	#
	location ~ \.php$ {
                fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
		include snippets/fastcgi-php.conf;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
	}

        location ~ ^/sites/.*/files/styles/ { # For Drupal >= 7
               try_files $uri @rewrite;
        }


        location ~ ^(/[a-z\-]+)?/system/files/ { # For Drupal >= 7
        try_files $uri /index.php?$query_string;
        }

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


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#	listen 80;
#	listen [::]:80;
#
#	server_name example.com;
#
#	root /var/www/example.com;
#	index index.html;
#
#	location / {
#		try_files $uri $uri/ =404;
#	}
#}

Save your changes and restart Nginx webserver.

sudo nginx -t
sudo systemctl restart nginx

Step 5: Configuring Drupal

At this stage, everything should be ready. Now open your web browser and browse to the server IP or hostname and you should see the default Drupal setup page. Choose your language, save and continue.

drupal setup page

Next, select the installation profile. Standard installs with commonly used features and pre-configured, where as Minimal installs without pre-configured functionality. The default is standard.

drupal setup page ubuntu

Next, setup the database name, username and password.. then save and continue.

After that, Drupal should install.. wait until the installation is done. On the final page, type your site name, your site admin username and password, select your location and save.

drupal site configuration

After everything.. your new Drupal site should be ready.

drupal default site

Enjoy!