Setting up server for Drupal with Nginx and Php-fpm under Debian

Submitted by admin on Mon, 07/10/2017 - 13:24

In the previous article Setting up Nginx on a Debian server as front-end for Apache of the series of articles for Drupal sysadmins we explained Nginx configs that allow it working through static queries while Apache serves dynamic content. This article offers a look at an alternative setup, where Php-fpm takes the place of Apache. The operating principle for our web server will be as follows:

  1. Nginx receives queries from clients;
  2. Nginx serves static content itself;
  3. Nginx passes dynamic content queries to php-fpm, which operates locally at port 9000.

Components installation

First off, we need to update the local packages cache:

apt-get update

Next, we install everything we need:

apt-get install nginx php5-fpm mysql-server php5-gd php5-mysql

Nginx configuration

Nginx configuration in file /etc/nginx/sites-enabled/ we need is as follows:

server {
 rewrite (.*)$1;

server {
  listen 80;
  root /home/webmaster/domains/;

  index index.html index.htm;
  access_log /home/webmaster/domains/;
  error_log /home/webmaster/domains/;

  # serve imagecache files directly or redirect to drupal if they do not exist
  location ^~ /sites/default/files/styles/ {
    access_log off;
    expires 30d;
    try_files $uri @rewrite;

  # serve static files directly
  location ~* ^.+\.(gz|jpg|jpeg|gif|css|png|js|ico|html|swf|flv|pdf)$ {
    access_log        off;
    expires           30d;

  location / {
    # Trying to upload the file, switch to Apache in case of failure
    try_files $uri @drupal;

  location ~ \.php$ {
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;

  location @drupal {
    fastcgi_index  index.php;
    fastcgi_read_timeout 600;
    fastcgi_param  SCRIPT_FILENAME      $document_root/index.php;
    fastcgi_param  QUERY_STRING         q=$uri&$args;
    fastcgi_param  REQUEST_METHOD       $request_method;
    fastcgi_param  CONTENT_TYPE         $content_type;
    fastcgi_param  CONTENT_LENGTH       $content_length;
    fastcgi_param  REDIRECT_STATUS      200;
    fastcgi_param  SCRIPT_NAME          /index.php;
    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;  

We leave /etc/nginx/fastcgi_params unchanged.

Php-fpm configuration

We need to introduce the following to /etc/php5/fpm/php-fpm.conf:

syslog.facility = daemon
syslog.ident = php-fpm
log_level = error
emergency_restart_interval = 12h
events.mechanism = epoll

Next, we need to set up the pool /etc/php5/fpm/pool.d/www.conf

user = www-data
group = www-data
pm = dynamic
pm.max_requests = 1500
security.limit_extensions = .php
php_admin_value[error_log] = /home/webmaster/domains/
php_admin_flag[log_errors] = on
php_admin_value[memory_limit] = 256M
php_admin_value[upload_tmp_dir] = "/home/webmaster/domains/tmp"
php_admin_value[session.save_path] = "/home/webmaster/domains/tmp"

Config file /etc/php5/fpm/php-fpm.conf contains sufficiently detailed descriptions of parameters. You can learn more in the official manuals.

That’s it! You now have an environment for a Drupal-powered website that will work without Аpache.


Add new comment

Filtered HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.