Right Nginx Configuration for Drupal on Ubuntu
But one day I tried lighttpd and it changed my views a lot. It is easier to configure than Apache, it’s faster and lighter. By trying something new I understood that there are other ways which can fit my needs better. I’m still using lighttpd for some of my projects but my new passion is nginx.
Nginx is lightweight server with extensible features. It serves perfectly static files and it can be used as proxy cache server. So I decided to find the best configuration of this server for Drupal and I’ve done it.
- Drupal 6 and 7/8 support
- Clean URL support
- Multisite support
- IPv6 and IPv4
- TCP and UNIX sockets
- Enhanced security features
- Boost and Advanced Aggregation support
- Imagecache hotlinking protection
I’ve decided to write these notes for people, especially from my current project team, who might want to configure Nginx for Drupal and avoid typical issues that I met during the configuration process. This article is not about performance tuning, although I will advise you of anything that can enhance performance.
So, here we go!
I have virtual system with Ubuntu 12.04. Apache is installed by default there.
Let’s make sure we don’t have Apache enabled. I prefer to uninstall it but you may at least turn it off. In this case please remember it’s necessary to remove it from autoload.
To uninstall it use the following command:
sudo apt-get purge apache2 apache2-utils apache2.2-bin apache2-common
Install the latest Nginx
Now it is 1.4.1.
sudo apt-get install python-software-properties sudo add-apt-repository ppa:nginx/stable sudo apt-get update sudo apt-get install nginx-extras
Note: For Ubuntu 12.10 you should install software-properties-common package instead of python-software-properties.
Why nginx-extras? This package includes necessary modules, not all but enough for normal work :)
Oh, you’d like to install Nginx from sources? This article is not about that, sorry.
Install MySQL server and PHP
The default versions for Ubuntu 12.04 are MySQL 5.5 and PHP 5.3. So it’s good enough.
sudo apt-get install mysql-server libmysqlclient15-dev sudo apt-get install php5-cgi php5-mysql php5-curl php5-mcrypt php5-gd php-pear php5-dev php5-fpm
As you can see, I’m going to use PHP-FPM to manage PHP process (workers).
In addition, I recommend that you install an op-code cache if you need to improve your site performance at least by 2 times. I’m installing APC.
sudo apt-get install php-apc
Also, It'd be better if you added a couple of lines to APC config /etc/php5/fpm/conf.d/apc.ini:
apc.write_lock = 1 # APC timebomb bug apc.slam_defense = 0 # APC timebomb bug
Some versions of APC have the timebomb bug and you can get 502 Bad Gateway error on high load.
Before we start with Nging configuration, please take a look at perusio’s documentation for his configs The documentation is useful, indeed.
The repository contains 3 branches. I’m using master branch because it contains configuration files for both Drupal versions Drupal 6 and Drupal 7.
Oh, yes, I forgot to mention, you will need git to clone perusio’s repositories. It’s easy to install.
sudo apt-get install git
Let’s follow the perusio installation guide for master branch.
Make sure you chose PHP-FPM method. Open /etc/nginx/nginx.conf and comment out the line:
Uncomment another one:
That’s because we will use UNIX sockets instead of TCP approach.
Read documentation of php5-fpm perusio config to learn how to configure PHP-FPM and what it is.
I can only mention that I used one pool for php5-fpm, you may use 3 or more depending on available resources.
One important thing to remember is now to reload PHP configuration you should restart php5-fpm service. It’s not enough to reload web-server configuration only, as we did for mod_php of Apache.
sudo service php5-fpm restart
Read section below with information about error troubleshooting if you have any problems.
Troubleshoot errors in configuration
If you try to run nginx as a service you will probably get nothing the first time. Let’s check it.
sudo service nginx restart
The normal response of this command should be something like:
* Restarting nginx nginx [ OK ]
If you got nothing try to run nginx as command:
Nging error logs are located in /var/log/nginx/
PHP5-FPM log: /var/log/php5-fpm.log
The errors I met are collected below.
Errors which I caught during configuration
nginx: [emerg] invalid parameter "ipv6_only=on" in /etc/nginx/sites-enabled/default:22
I don’t need IP 6 protocol support so I’m commenting this line out.
2. Error with microcache. Nginx could not write to the cache folder /var/cache/nginx
Obviously we should create the folder and give permissions for www-data to write into it. Use chown and chmod commands to change folder owner and set permissions.
3. When I enabled new virtual host for my Drupal site and reloaded nginx configuration I got new error.
nginx: [emerg] unknown directive "aio" in /etc/nginx/apps/drupal/drupal6.conf:14
To be honest, I just commented out the particular lines in the configuration, because it seems I have to install one more module for nginx to fix it.
4. Funny error, but I should mention it too. When I requested any path from my website I got index.php as text file.
That’s because I didn’t set right permissions for /var/www/mysiteroot/ folder.
Time for coffee
If you followed my and perusio’s recommendations your site is up and running.
Hurray! We’ve configured Nginx + PHP5-FPM for Drupal and it’s time for coffee. Just a minute, though.
And remember, the ability to adapt other people’s experience and share your expertise is professionalism. @KKomelin