Run Drupal 7 in Nginx

After so many days we have talked about setting up Nginx, PHP-FPM, MariaDB and some PHP caching. We can now try to run a Drupal instance on them. Before we starts, let me listed out all the previous posts.


It’s time to start the Drupal installation.

1. Go to the /srv folder and download the latest Drupal core.


2. Extract the archive.

  • tar zxf drupal-7.19.tar.gz


3. Rename the folder to a proper name. In this post, we rename it to drupal.

  • mv drupal-7.19 drupal


4. Create a new database through phpMyAdmin.

5. Create a new Nginx VirtualHost setting as follow.

server {
  listen   80; ## listen for ipv4; this line is default and implied
  #listen   [::]:80 default ipv6only=on; ## listen for ipv6

  server_name <your domain>;
  root /srv/drupal;
  index index.php;
  # Log
  access_log /var/log/nginx/drupal.access.log;
  error_log  /var/log/nginx/drupal.error.log info;
  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)$ {
    deny all;

  location ~ \..*/.*\.php$ {
    return 403;

  location / {
    # This is cool because no php is touched for static content
    try_files $uri $uri/ @rewrite;
    expires max;

  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$ {
    # Setup var defaults
    set $no_cache "";
    # If non GET/HEAD, don't cache and mark user as uncacheable for 1 second via cookie
    if ($request_method !~ ^(GET|HEAD)$) {
      set $no_cache "1";
    # Drop no cache cookie if need be
    # (for some reason, add_header fails if included in prior if-block)
    if ($no_cache = "1") {
      add_header Set-Cookie "_mcnc=1; Max-Age=2; Path=/";
      add_header X-Microcachable "0";
    # Bypass cache if no-cache cookie is set
    if ($http_cookie ~* "_mcnc") {
      set $no_cache "1";
    # Bypass cache if flag is set
    fastcgi_no_cache $no_cache;
    fastcgi_cache_bypass $no_cache;
    # Settings
    fastcgi_cache microcache;
    fastcgi_cache_key $server_name|$request_uri;
    fastcgi_cache_valid 404 30m;
    fastcgi_cache_valid 200 10s;
    fastcgi_max_temp_file_size 1M;
    fastcgi_cache_use_stale updating;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_pass_header Set-Cookie;
    fastcgi_pass_header Cookie;
    fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    #fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
    #fastcgi_intercept_errors on;
    include fastcgi_params;


6. Restart Nginx.

  • /etc/init.d/nginx restart


7. Continue the installation by accessing the URL with browser.

Done =)


Update @ 20130223: I found that a few days after the above setup, the following error occurred.

I am not sure why the mysqld.sock was gone. I restarted the server but the same problem occurred again after a while.

Finally, i solved the problem by the solution suggested in the post below.
StackOverflow – Getting “Can’t connect…through socket ‘/tmp/mysql’” when installing MySQL on Mac OS X 10.6 with Homebrew


11 thoughts on “Run Drupal 7 in Nginx”

  1. Great Job dude. It Helped me a lot ! Thank you. -> I have followed all your installation step

    Nginx – Installation on Ubuntu Precise
    Nginx + PHP-FPM on Ubuntu Precise
    Nginx – Enable Microcaching
    MariaDB – Installation on Ubuntu Precise
    Run phpMyAdmin on Nginx in Ubuntu Precise
    Ubuntu – Install APC with PHP-FPM and Nginx
    Ubuntu – Install Memcached with PHP-FPM and Nginx

    i am getting 5000+ request per sec .


  2. Hello ykyuen,

    Really great tutorial. I am planning to test this on aws ec2.

    I am working on secured drupal commerce website wherein whole website will be using https.
    If possible, suggest what changes do i need to address.


  3. hi, thanks for this great tutorial 🙂
    I am a beginner, I have managed to install NGINX on ubuntu and I am able to get the “welcome to NGINX” only via IP address and not via hostname. I have set my hostname CGDB and installed Mysql. I have followed your drupal installation procedures, but I am not able to go beyond the NGINX home screen and access thru hostname. Could you please suggest what can I do to resolve this please…Many thanks


    1. If you want your server to be accessed on the internet publicly, you need to purchase a hostname online, you could go to and search if your hostname is available.

      if it is just for private use, you could update your machine host file and map your hostname with your server ip address?


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.