Setup Drupal 7 with nginx and PostgreSQL on Ubuntu 14.04

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

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

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

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

sudo -u postgres -i
createuser USER --pwprompt --encrypted
createdb HOSTNAME_db
exit

Now setup nginx. First create fastcgi config /etc/nginx/fastcgi.conf:

# vim /etc/nginx/fastcgi.conf
fastcgi_param   QUERY_STRING            $query_string;
fastcgi_param   REQUEST_METHOD          $request_method;
fastcgi_param   CONTENT_TYPE            $content_type;
fastcgi_param   CONTENT_LENGTH          $content_length;
fastcgi_param   SCRIPT_FILENAME         $document_root$fastcgi_script_name;
fastcgi_param   SCRIPT_NAME             $fastcgi_script_name;
fastcgi_param   PATH_INFO               $fastcgi_path_info;
fastcgi_param   REQUEST_URI             $request_uri;
fastcgi_param   DOCUMENT_URI            $document_uri;
fastcgi_param   DOCUMENT_ROOT           $document_root;
fastcgi_param   SERVER_PROTOCOL         $server_protocol;
fastcgi_param   GATEWAY_INTERFACE       CGI/1.1;
fastcgi_param   SERVER_SOFTWARE         nginx/$nginx_version;
fastcgi_param   REMOTE_ADDR             $remote_addr;
fastcgi_param   REMOTE_PORT             $remote_port;
fastcgi_param   SERVER_ADDR             $server_addr;
fastcgi_param   SERVER_PORT             $server_port;
fastcgi_param   SERVER_NAME             $server_name;
fastcgi_param   HTTPS                   $https;
fastcgi_param   REDIRECT_STATUS         200;

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

#vim /etc/nginx/sites-available/HOSTNAME
server {
  listen 80;
  server_name HOSTNAME;
  access_log /var/log/nginx/HOSTNAME.access.log;
  error_log /var/log/nginx/HOSTNAME.error.log;
  root /srv/HOSTNAME;
  index index.php index.html index.htm default.html default.htm;
  # Support Clean (aka Search Engine Friendly) URLs
  location / {
    try_files $uri $uri/ /index.php?$args;
  }
  # deny running scripts inside writable directories
  location ~* /(images|cache|media|logs|tmp)/.*\.(php|pl|py|jsp|asp|sh|cgi)$ {
    return 403;
    error_page 403 /403_error.html;
  }
  location ~ \.php$ {
    fastcgi_pass  unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include /etc/nginx/fastcgi.conf;
  }
  # caching of files
  location ~* \.(ico|pdf|flv)$ {
    expires 1y;
  }
  location ~* \.(js|css|png|jpg|jpeg|gif|swf|xml|txt)$ {
    expires 14d;
  }
}

and enable it:

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

Now you need to restart services as root:

service php5-fpm restart
service nginx restart

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

wget http://ftp.drupal.org/files/projects/drupal-7.31.tar.gz
tar xvf drupal-7.31.tar.gz
mv drupal-7.31 bikulov.org
rm drupal-7.31.tar.gz
cp bikulov.org/sites/default/default.settings.php bikulov.org/sites/default/settings.php
chown -R www-data:www-data bikulov.org

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