Replace Apache2 server with Nginx on Ubuntu for Drupal sites

  • Posted on: 10 September 2017
  • By: Zhijun Chen

Here are the steps to move your Drupal site to Nginx from Apache2.

1. Stop Apache2

sudo service apache2 stop

2. Install Nginx

sudo apt-get install nginx

3. Edit /etc/nginx/site-enabled/default to the following config, make sure root is pointing to your site address

server {
    server_name example.com;
    root /var/www/drupal8; ## <-- Your only path reference.

    location = /favicon.ico {
        log_not_found off;
        access_log off;

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;

    # Very rarely should these ever be accessed outside of your lan
    location ~* \.(txt|log)$ {
        deny all;

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

    location ~ ^/sites/.*/private/ {
        return 403;

    # Allow "Well-Known URIs" as per RFC 5785
    location ~* ^/.well-known/ {
        allow all;

    # Block access to "hidden" files and directories whose names begin with a
    # period. This includes directories used by version control systems such
    # as Subversion or Git to store control files.
    location ~ (^|/)\. {
        return 403;

    location / {
        # try_files $uri @rewrite; # For Drupal <= 6
        try_files $uri /index.php?$query_string; # For Drupal >= 7

    location @rewrite {
        rewrite ^/(.*)$ /index.php?q=$1;

    # Don't allow direct access to PHP files in the vendor directory.
    location ~ /vendor/.*\.php$ {
        deny all;
        return 404;

    # In Drupal 8, we must also match new paths where the '.php' appears in
    # the middle, such as update.php/selection. The rule we use is strict,
    # and only allows this pattern with the update.php front controller.
    # This allows legacy path aliases in the form of
    # blog/index.php/legacy-path to continue to route to Drupal nodes. If
    # you do not have any paths like that, then you might prefer to use a
    # laxer rule, such as:
    #   location ~ \.php(/|$) {
    # The laxer rule will continue to work if Drupal uses this new URL
    # pattern with front controllers other than update.php in a future
    # release.
    location ~ '\.php$|^/update.php' {
        fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
        # Security note: If you're running a version of PHP older than the
        # latest 5.3, you should have "cgi.fix_pathinfo = 0;" in php.ini.
        # See http://serverfault.com/q/627903/94922 for details.
        include fastcgi_params;
        # Block httpoxy attacks. See https://httpoxy.org/.
        fastcgi_param HTTP_PROXY "";
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_intercept_errors on;
        # PHP 5 socket location.
        #fastcgi_pass unix:/var/run/php5-fpm.sock;
        # PHP 7 socket location.
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;

    # Fighting with Styles? This little gem is amazing.
    # location ~ ^/sites/.*/files/imagecache/ { # For Drupal <= 6
    location ~ ^/sites/.*/files/styles/ { # For Drupal >= 7
        try_files $uri @rewrite;

    # Handle private files through Drupal. Private file's path can come
    # with a language prefix.
    location ~ ^(/[a-z\-]+)?/system/files/ { # For Drupal >= 7
        try_files $uri /index.php?$query_string;

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires max;
        log_not_found off;

4. Restart Nginx and you should see your site up and running.

sudo service nginx start

5. Remove Apache2 dependencies

sudo apt-get purge apache2 apache2-utils apache2.2-bin apache2-common
sudo apt-get autoremove

Create Drupal 7 Slide Show Using Views Slideshow Modules

  • Posted on: 12 November 2014
  • By: Zhijun Chen

I've been building Drupal websites during my spare time recently. One of the common features in modern sites is a home page slideshow.
There are a few ways to achieve this in Drupal. In this tutorial I will be using Views Slideshow module together with Views module.

  • Install modules and libraries

Download the following modules, install and enable the related modules via admin modules interface.

  2. jQuery Update
  3. Views slideshow
  4. Ctools
  5. Libraries API

Download jQuery Cycle plugin and install it by placing the js file inside jquery.cycle directory under sites/all/libraries.

  • Optional: Apply patch for customized navigation texts

By default the Views slideshow module does not offer customized navigation texts, only "previous" and "next" available for paging.
In my case I would like to use "<" and ">" as navigation buttons and style them afterwards simply because I prefer CSS3 to images. :)
In order to achieve this you need to apply the patch from this thread.
Once the patch is applied, add the following code to theme's template.php file (replace THEME with your theme name):

 function THEME_preprocess_views_slideshow_controls_text_previous(&$vars) {
  $vars['previous_text'] = '<';

 * Implements template_preprocess_views_slideshow_controls_text_next().
function THEME_preprocess_views_slideshow_controls_text_next(&$vars) {
  $vars['next_text'] = '>';

 * Implements template_preprocess_views_slideshow_controls_text_next().
function THEME_preprocess_views_slideshow_controls_text_pause(&$vars) {
  $vars['start_text'] = '';


  • Create content type

Next step we need to add a content type for slide pages. Add a content type by navigating to Structure -> Content types -> Add content types.
Here is an example content type I created:

  • Create contents

Once the content type is created, we can create some contents for slideshow by navigating to Content -> Add Content -> [Your content type]

  • Create slideview

After contents are created, we finally reach the last step: create the view.
Navigate to Structure -> Views -> Add new view. Select the display format to be Slideview as shown in the following:

Edit the view to include all the fields in the slideview, for example:

You can configure the Slideview by clicking on the settings link on the right, here you can choose the effect, and add pager: