LEMP Server Ubuntu 12.04 for serving Drupal on Nginx

| Servers | 1 seen

In this tutorial you will learn how to setup a LEMP stack on Ubuntu 12.04 for serving a Drupal site (s).

Update: I originally started this post to document my setup for actually configuring Nginx server on Ubuntu for Drupal site at the start of 2015. Many things have changed ,ever since, thus most of the written bellow will work.

LEMP is a variation of the ubiquitous LAMP stack used for developing and deploying web applications. Traditionally, LAMP consists of Linux, Apache, MySQL, and PHP. Due to its modular nature, the components can easily be swapped out. With LEMP, Apache is replaced with the lightweight yet powerful Nginx.

I have been serving Drupal websites running Nginx for years already, my favorite combination is a Linode VPS, Ubuntu and Nginx. For the past 3 years I have been running Ubuntu 10.04 Nginx 0.67 and PHP 5.2. It worked and still works great, but many things have advanced in those last 3 years, like for running a Drupal 8 I'm required to have a php 5.3 version at least. The support for Ubuntu 10.04 has ended on April 30, 2015, not to speak of my existing version of Nginx.

In fact the reason that made me to do those upgrades are that recently some bot-hacker cracked my website(s) and changed content of index.php files. Well I'm not sure for 100% right now, but I believe there were some server vulnerabilities involved as well.

To perform this brand new installation I bought a new 1GB VPS server for 10$/month from Linode

LEMP Server on Ubuntu 12.04 (Precise Pangolin)

Pretty much of written here I have taken from this guide on Linode

Follow this tutorial on Linode for getting started (Deploying a server), then add some basic security to your VPS

Installing Nginx web server compiling from source

install the prerequisites:

apt-get install libpcre3-dev build-essential libssl-dev sudo

Check the Nginx download page and ensure you are using the most recent “stable” version. Latest stable version of nginx by writting this article was 1.8.0. Issue the following commands to download and install the Nginx web server:

cd /opt/
wget http://nginx.org/download/nginx-1.8.0.tar.gz
tar -zxvf nginx-1.6.2.tar.gz
cd /opt/nginx-1.6.2/

The following ./configure command will prepare nginx for compilation:

./configure --prefix=/opt/nginx --user=nginx --group=nginx --with-http_ssl_module

Issue the following commands to compile and then install the software

make
make install

Create a dedicated system user to run the Nginx process under by issuing the following command:

adduser --system --no-create-home --disabled-login --disabled-password --group nginx 

Now install and configure the init script to make it possible to start and stop the web server more easily. Issue the following command sequence:

wget -O init-deb.sh http://www.linode.com/docs/assets/1131-init-deb.sh
mv init-deb.sh /etc/init.d/nginx
chmod +x /etc/init.d/nginx
/usr/sbin/update-rc.d -f nginx defaults 

Now, issue the following command to start the web-server:

/etc/init.d/nginx start

Configure Nginx Virtual Server Blocks

A lot of I have taken from Insready article here

sudo mkdir -p /srv/www/reinisfischer.com/{public_html,logs}
sudo usermod -a -G www-data admin
sudo chown -R www-data:www-data /srv/www
sudo chmod -R 775 /srv/www/reinisfischer.com
sudo mkdir -p /opt/etc/nginx/{sites-available,sites-enabled}

Make sure you change admin in second row to your username

Telling Nginx you are using virtual servers

Insert the server directives directly into the http section of the /opt/nginx/conf/nginx.conf

sudo nano /opt/nginx/conf/nginx.conf

add a line into http section

http {
# [...] 
include /opt/etc/nginx/sites-enabled/*;

# [...] }

Once you’ve configured and loaded the Nginx configuration, restart the web server to implement the new configuration by issuing the following command:

sudo
/etc/init.d/nginx restart

Make sure that the directories referenced in your configuration exist on your file system before restarting.

Configure Nginx for Drupal 7

Add a new server_name

sudo nano /opt/etc/nginx/sites-available/reinisfischer.com

copy/paste code bellow (make sure you change reinisfischer.com to your actual domain name)

server {
server_name reinisfischer.com;
root /srv/www/reinisfischer.com/public_html; ## <-- Your only path $
access_log /srv/www/reinisfischer.com/logs/access.log;
error_log /srv/www/reinisfischer.com/logs/error.log;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
# This matters if you use drush
location = /backup {
deny all;
}
# Very rarely should these ever be accessed outside of your lan
location ~* \.(txt|log)$ {
allow 192.168.0.0/16;
deny all;
}
location ~ \..*/.*\.php$ {
return 403;
}
location / {
# This is cool because no php is touched for static content
try_files $uri @rewrite;
}
location @rewrite {
# Some modules enforce no slash (/) at the end of the URL
# Else this rewrite block wouldn't be needed (GlobalRedirect)
rewrite ^/(.*)$ /index.php?q=$1;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
}
# Fighting with ImageCache? This little gem is amazing.
location ~ ^/sites/.*/files/styles/ {
try_files $uri @rewrite;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
}

CTRL+O and Enter CTRL+X to exit

Make a symlink

sudo ln -s /opt/etc/nginx/sites-available/reinisfischer.com /opt/etc/nginx/sites-enabled

Restart Nginx

/etc/init.d/nginx restart

To remove a symlink

sudo rm /opt/etc/nginx/sites-enabled/reinisfischer.com
/etc/init.d/nginx restart

Deploy PHP with FastCGI

In order to deploy PHP applications, you will need to implement the following “PHP-FastCGI” solution to allow nginx to properly handle and serve pages that contain PHP code.

apt-get install php5-cli php5-cgi php5-fpm

When you’ve completed the modifications to the configuration, make sure that the virtual host is enabled and issue the following command to restart the web server:

service php5-fpm restart
/etc/init.d/nginx restart

Congratulations! You can now deploy PHP scripts with with your LEMP stack.

Install the MySQL Database Server

apt-get install mysql-server php5-mysql

During the installation process you will be prompted to set a password for the MySQL root user. Choose a strong password and keep it in a safe place for future reference.

Issue the following command to secure the MySQL instance:

mysql_secure_installation

Answer all questions prompted during this process.

Install phpMyAdmin

sudo aptitude install phpmyadmin

Hit ESC when the installation prompts you for auto-configuration, because there is no option for Nginx.

sudo ln -s /usr/share/phpmyadmin/ /srv/www/reinisfischer.com/public_html/phpmyadmin

now access phpmyadmin on your browser. 

Fix for missing mysqli

I found a message warning that no mysqli extension can be found, to fix this I followed advice here:http://www.maketecheasier.com/install-lemp-server-in-ubuntu

uncommented “mysqli.allow_local_infile = On” in php.ini 

possible locations for php.ini

/etc/php5/fpm/php.ini
/etc/php5/cli/php.ini

Restart FPM

service php5-fpm restart

Installing Drupal

cd /srv/www/reinisfischer.com
sudo wget http://ftp.drupal.org/files/projects/drupal-7.38.tar.gz
sudo tar -xvzf drupal-7.38.tar.gz
sudo cp drupal-7.38/* public_html/ -R
sudo chown www-data:www-data public_html -R

Drupal Backup and Restore using Wget

I was deploying a new server with just one aim - to run a Drupal sites. Since I already had a server serving Drupal sites I found the most fastest way to transfer those site is by creating archives on one server and transfer them to other using wget and then extracting and editing just some settings.php file

On server 1

sudo mkdir /srv/www/reinisfischer.com/public_html/backups
sudo tar czf /srv/www/reinisfischer.com/public_html/backups/archive.tar.gz /srv/www/reinisfischer.com/public_html

With first line we created a new folder where to store our backup files, while with second file we archived all files from public_html directory and placed in backups directory

On Server 2

cd /srv/www/reinisfischer.com/public_html
sudo wget http://www.reinisficher.com/backups/archive.tar.gz
sudo tar -zxvf archive.tar.gz

Install Postfix for sending Mail

In many cases your website will use a webform and it's neat to have those emails been sent to your inbox.Read: https://www.digitalocean.com/community/questions/email-not-working-with-fresh-drupal-install-on-ubuntu-14-04

sudo apt-get install postfix

Then, edit /etc/postfix/main.cf and set inet_interfaces to loopback-only

sudo nano /etc/postfix/main.cf

Restart postfix

sudo service postfix restart