Setup nginx drupal and phpmyadmin on ubuntu & RHEL

Setup an nginx webserver with a Drupal installation and phpMyAdmin. I am assuming that you have already installed the Nginx web server on Ubuntu 16.04/RHEL.

Nginx is a web server for serving static content, but we are going to use it for Drupal 7.x and 8.x websites. In order to configure it, we need an additional service for PHP support, i.e. php-fpm.

  • Open terminal by alt+ctrl T and cd /etc/nginx/conf.d
  • Create default.conf as mentioned below:
# Default server configuration

  server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {
      # First attempt to serve request as file, then
      # as directory, then fall back to displaying a 404.
      try_files $uri $uri/ =404;
    }
    # Allow phpmyadmin to access from nginx
    location /phpmyadmin {
      root /usr/share;
      index index.php;
      try_files $uri $uri/ /phpmyadmin/index.php$args$is_args;
      location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
        root /usr/share;
      }
    }
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {
      include snippets/fastcgi-php.conf;
      fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }
  }
  • Install php7.0-fpm with php-gd extension that is required by drupal:
sudo apt-get install php7.0-fpm php7.0-cli php7.0-gd php7.0-mysql php7.0-xml -y
  • Configure php-fpm to serve HTTP requests for PHP pages by modifying the /etc/php/7.0/fpm/php.ini file and replacing cgi.fix_pathinfo = 1 with cgi.fix_pathinfo = 0.
sudo vi /etc/php/7.0/fpm/php.ini

In this tutorial, I am taking drupal-dev.com as the domain for Drupal installation:

  • Now we will create a folder named drupal-dev.com in/var/www
    /var/www/drupal-dev.com/public
  • Let's create a server-block in nginx for the drupal-dev.com domain. For that, we need to navigate to /etc/nginx/conf.d and create a file named drupal-dev.conf and add the following snippet.
server {
  listen 80;
  server_name www.drupal-dev.com;
  # rewrite ^/(.*) http://drupal-main.com/$1 permanent;
}
server {
  listen 80;
  server_name drupal-dev.com;

  root /var/www/drupal-dev.com/public/;
  index index.php;
  # For Drupal >= 7 clean url

  location / {
    try_files $uri /index.php?$query_string; 
  }
  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
  }
}
  • Add the domain name entry to the hosts file by editing/etc/hosts and adding the below line:
  127.0.0.1 localhost
  127.0.1.1 drupal-dev.com www.drupal-dev.com

We are done, restart nginx by sudo service nginx restart and visit your site: http://drupal-dev.com

0/5