Setup Drupal 7 with nginx and PostgreSQL on Ubuntu 14.04

2014-08-09
#ubuntu #howto #server #nginx #postgresql

This note is similar to the previous drupal and joomla installations. Install the requirements (web server, php5, DB server, DB adapter for PHP) as root:

1apt-get install -y nginx php5-fpm nginx postgresql php5-pgsql php5-gd

Create a user and database (replace USER with the desired username and HOSTNAME_db with the database name; we will need these values in the Drupal setup later) for the future site:

1sudo -u postgres -i
2createuser USER --pwprompt --encrypted
3createdb HOSTNAME_db
4exit

Now set up nginx. First create the fastcgi config /etc/nginx/fastcgi.conf:

 1# vim /etc/nginx/fastcgi.conf
 2fastcgi_param   QUERY_STRING            $query_string;
 3fastcgi_param   REQUEST_METHOD          $request_method;
 4fastcgi_param   CONTENT_TYPE            $content_type;
 5fastcgi_param   CONTENT_LENGTH          $content_length;
 6fastcgi_param   SCRIPT_FILENAME         $document_root$fastcgi_script_name;
 7fastcgi_param   SCRIPT_NAME             $fastcgi_script_name;
 8fastcgi_param   PATH_INFO               $fastcgi_path_info;
 9fastcgi_param   REQUEST_URI             $request_uri;
10fastcgi_param   DOCUMENT_URI            $document_uri;
11fastcgi_param   DOCUMENT_ROOT           $document_root;
12fastcgi_param   SERVER_PROTOCOL         $server_protocol;
13fastcgi_param   GATEWAY_INTERFACE       CGI/1.1;
14fastcgi_param   SERVER_SOFTWARE         nginx/$nginx_version;
15fastcgi_param   REMOTE_ADDR             $remote_addr;
16fastcgi_param   REMOTE_PORT             $remote_port;
17fastcgi_param   SERVER_ADDR             $server_addr;
18fastcgi_param   SERVER_PORT             $server_port;
19fastcgi_param   SERVER_NAME             $server_name;
20fastcgi_param   HTTPS                   $https;
21fastcgi_param   REDIRECT_STATUS         200;

Now create the config for the site (change HOSTNAME to your site hostname):

 1#vim /etc/nginx/sites-available/HOSTNAME
 2server {
 3  listen 80;
 4  server_name HOSTNAME;
 5  access_log /var/log/nginx/HOSTNAME.access.log;
 6  error_log /var/log/nginx/HOSTNAME.error.log;
 7  root /srv/HOSTNAME;
 8  index index.php index.html index.htm default.html default.htm;
 9  # Support Clean (aka Search Engine Friendly) URLs
10  location / {
11    try_files $uri $uri/ /index.php?$args;
12  }
13  # deny running scripts inside writable directories
14  location ~* /(images|cache|media|logs|tmp)/.*\.(php|pl|py|jsp|asp|sh|cgi)$ {
15    return 403;
16    error_page 403 /403_error.html;
17  }
18  location ~ \.php$ {
19    fastcgi_pass  unix:/var/run/php5-fpm.sock;
20    fastcgi_index index.php;
21    include fastcgi_params;
22    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
23    include /etc/nginx/fastcgi.conf;
24  }
25  # caching of files
26  location ~* \.(ico|pdf|flv)$ {
27    expires 1y;
28  }
29  location ~* \.(js|css|png|jpg|jpeg|gif|swf|xml|txt)$ {
30    expires 14d;
31  }
32}

and enable it:

1ln -s /etc/nginx/sites-available/HOSTNAME /etc/nginx/sites-enabled/

Now you need to restart services as root:

1service php5-fpm restart
2service nginx restart

Download the latest Drupal package to /srv/ and unpack it. Rename the folder to HOSTNAME (see the nginx config for the host). Create settings from the sample ones and change the owner of /srv/HOSTNAME to www-data:www-data. Remove the unnecessary Drupal package archive. All the commands above must look like this (as root):

1wget https://ftp.drupal.org/files/projects/drupal-7.31.tar.gz
2tar xvf drupal-7.31.tar.gz
3mv drupal-7.31 bikulov.org
4rm drupal-7.31.tar.gz
5cp bikulov.org/sites/default/default.settings.php bikulov.org/sites/default/settings.php
6chown -R www-data:www-data bikulov.org

Now complete the web installation of Drupal using your database name and user.