Apache is like Microsoft Word, it has a million options but you only need six. Nginx does those six things, and it does five of them 50 times faster than Apache.
The internet appears to have fallen out of love with Apache, the faithful workhorse that has dominated the landscape for the past decade plus. While Apache still powers the majority of websites out there, it has steadily lost ground to nginx (engine-x) over the past few years.
In this guide, we’ll discuss how to get Drupal 8 installed on your Ubuntu 14.04 server with Nginex (I’ll do it with Kubuntu 14.04, but anyway it is going to run on Ubuntu and derived).
Install LEMP (Linux, Nginx, MySQL and PHP)
#1 – Download and Install Nginx
Nginx package is available by default on Ubuntu; however, if you want to get the last version of nginx, you need to follow the first two steps. Otherwise, you’ll get Nginx 1.4
For Ubuntu, in order to authenticate the nginx repository signature and to eliminate warnings about missing PGP key during installation of the nginx package, it is necessary to add the key used to sign the nginx packages and repository to the
apt program keyring. Please download this key from nginx web site, and add it to the
apt program keyring with the following command:
$ sudo apt-key add nginx_signing.key
Then, append the following to the end of the /etc/apt/sources.list file. Remember to replace codename with your Ubuntu distribution codename:
Now, you can download nginx using:
$ sudo apt-get update
$ sudo apt-get install nginx
In Ubuntu 14.04, by default, Nginx automatically starts when it is installed. Otherwise you can start it with the following command:
$ sudo service nginx start
#2 – Test Nginx
MySQL Installation On Ubuntu
#1 – Install MySQL and PHP support
Type the following command:
$ sudo apt-get install mysql-server php5-mysql
During the installation, MySQL will ask you to enter MySQL root password, other wise the installation will not continue. Type the password you want (you can even leave it in black but it is not recommended), then press OK to continue the installation.
#2 – Test MySQL
Once mysql installation finished, we can test it. Open your console and type the following command:
$ mysql -u root -p
If you type your password correctly, then you will see the mysql prompt.
PHP Installation For Server Side Scripting
#1 – Download and install PHP
Most administrators know that PHP, the widely used scripting language, can be embedded in HTML and works with all major web servers. What’s less widely known, however is that you can run PHP in different ways on your server. The most common option is the mod_php module that’s runs by default in the Apache HTTP Server. If your primarily goal is performance, however, you should consider other options like PHP-FPM (FastCGI Process Manager) and PHP FastCGI .
FastCGI is a generic protocol for interfacing interactive programs with a web server. It’s a variation on the earlier Common Gateway Interface (CGI) that reduces the overhead associated with interfacing the web server and CGI programs, allowing a server to handle more page requests at once.
PHP-FPM, the newest way to use PHP with a webserver, is an alternative PHP FastCGI implementation with some additional features that can be useful for websites that run on both small VPSes and multiple servers. It can be used with any web server that is compatible with FastCGI.
PHP-FPM gives administrators the ability to gracefully stop and start PHP worker processes without losing any queries. This allows you to gradually update the configuration and binary without losing any queries. You can start workers with different uid, gid, and chroot environments and different php.ini options. You can also run an emergency restart on all the processes in the event of any accidental destruction of the shared memory opcodes cache, if you’re using an accelerator.
We will choose the newest, so we are going to install PHP-FPM 🙂
As usual, we can download and install it using apt-get command. Just type the following command on your console or over the ssh based session:
$ sudo apt-get install php5-fpm
Learn more about php-fpm here
Now we have all components installed. You could add a one-liner phpinfo.php file to the root dir /var/www/html to show PHP info:
<?php phpinfo(); ?>
Drop in a vhost
Create a new file in /etc/nginx/conf.d/ with any name, but it must have the extension .conf. Copy the config from the Nginx Drupal guide into that file and edit the server_name and root variables.
Edit /etc/hosts adding:
Note: In my case, my server_name variable in my nginx configuration was drupal8, so the new localhost must be named as drupal8
Then restart nginx
sudo service nginx restart
Installing Drupal 8
#1 – Install git and drush
These two, git and drush, are optional but I highly recommend them.
Note: At the date that this article was made the way to get a version of drush that works with Drupal 8 is not the following, so you can try this too
sudo apt-get install git drush
#2 – Download Drupal 8 and create directories and files
Go to your site root, download latest stable Drupal.
git clone --branch 8.0.x http://git.drupal.org/project/drupal.git
You can also install it with drush.
drush dl drupal
Or you can even do it from drupal page
Note: Remember to copy drupal files inside the directory you specified in your root variable in the vhost configuration
In sites/default change the name of default.services.yml and default.settings.php by services.yml and settings.php respectively, create a new folder called files and give the appropriate permissions to these three. Here you have more info
#3 – Start installing Drupal
#4 – Solving PHP extensions disabled
Just install php-gd with the following command:
sudo apt-get install php5-gd
#5 – Database configuration
Drupal 8 installation can create a database itself if the user specified has the correct permissions, therefore I only passed the root user for this example, but you can create users and databases yourself. Read this guide
#6 – Complete the installation
Now, get fun seeing how fast Drupal is installed on Nginx and next configure your site.
Bonus: Why use Nginx with PHP-FPM rather than Apache with PHP-FPM?
Time is money. This somewhat shallow and overused saying fits perfectly well into any on-line business. Users are impatient and every millisecond brings them closer to leaving your website.
How Apache works and why it has limitations
Apache creates processes and threads to handle additional connections. The administrator can configure the server to control the maximum number of allowable processes. This configuration varies depending on the available memory on the machine. Too many processes exhaust memory and can cause the machine to swap memory to disk, severely degrading performance. Plus, when the limit of processes is reached, Apache refuses additional connections.
The limiting factor in tuning Apache is memory and the potential to dead-locked threads that are contending for the same CPU and memory. If a thread is stopped, the user waits for the web page to appear, until the process makes it free, so it can send back the page. If a thread is deadlocked, it does not know how to restart, thus remaining stuck.
How Nginx works?
Nginx works differently than Apache, mainly with regard to how it handles threads.
Nginx does not create new processes for each web request, instead the administrator configures how many worker processes to create for the main Nginx process. (One rule of thumb is to have one worker process for each CPU.) Each of these processes is single-threaded. Each worker can handle thousands of concurrent connections. It does this asynchronously with one thread, rather than using multi-threaded programming.
The Nginx also spins off cache loader and cache manager processes to read data from disk and load it into the cache and expire it from the cache when directed.
Nginx is composed of modules that are included at compile time. That means the user downloads the source code and selects which modules to compile. There are modules for connection to back end application servers, load balancing, proxy server, and others. There is no module for PHP, as Nginx can compile PHP code itself.
#1 – Speed
Serving PHP scripting with PHP-FPM on Apache or Nginx doesn’t have too much difference in the speed, but obviously a web application doesn’t consist of PHP files only. There are plenty of static assets which have to be delivered, and that is why the best option is PHP-FPM on Nginx.
#2 – Security
Another issue is the vulnerability of apache to HTTP DoS attacks. A Slow HTTP Denial of Service (DoS) attack, otherwise referred to as Slowloris HTTP DoS attack, makes use of HTTP GET requests to occupy all available HTTP connections permitted on a web server.
A Slow HTTP DoS Attack takes advantage of a vulnerability in thread-based web servers which wait for entire HTTP headers to be received before releasing the connection. While some thread-based servers such as Apache make use of a timeout to wait for incomplete HTTP requests, the timeout, which is set to 300 seconds by default, is re-set as soon as the client sends additional data.
This creates a situation where a malicious user could open several connections on a server by initiating an HTTP request but does not close it. By keeping the HTTP request open and feeding the server bogus data before the timeout is reached, the HTTP connection will remain open until the attacker closes it. Naturally, if an attacker had to occupy all available HTTP connections on a web server, legitimate users would not be able to have their HTTP requests processed by the server, thus experiencing a denial of service.
Since there is a limit on the number of threads Apache can spawn, it can be attacked by HTTP DoS creating a lot of half open connections with it.