generalnginx,drupal

Install Drupal On Top Of The Bitnami NGINX Stack

Introduction

If you’re interested in creating a personal or a business website, you can use Drupal. Drupal is an open source content management framework used by thousand of enterprises, governments, higher-education institutions and NGOs for creating their websites with reliably and flexibly.

NGINX is an open source web server that helps developers to improve the security, performance and reliability of their applications. And not only that, it can also function as a proxy server for email (IMAP, POP3, and SMTP) and a reverse proxy and load balancer for HTTP, TCP, and UDP servers.

This guide walks you through the process of installing Drupal on top of a running NGINX server.

Assumptions and prerequisites

This guide makes the following assumptions:

  • You have a basic understanding of NGINX.
  • You have a Bitnami stack installed with an NGINX server - for example, the Bitnami NGINX Stack.

Step 1: Prepare your NGINX server

This step consists of preparing your NGINX server for Drupal. To do so, follow the instructions below:

  • Download the latest version of Drupal and extract the files to the /opt/bitnami/drupal/ directory. Replace the X.Y.Z placeholder with the Drupal version number.

      $ cd /tmp
      $ wget https://www.drupal.org/download-latest/tar.gz
      $ sudo tar xfvz tar.gz -C /opt/bitnami/
      $ sudo mv /opt/bitnami/drupal-X.Y.Z /opt/bitnami/drupal
      $ sudo cp /opt/bitnami/drupal/sites/default/default.settings.php /opt/bitnami/drupal/sites/default/settings.php
    
  • Run the following commands to assign the necessary directory permissions:

      $ sudo chown -R bitnami:daemon /opt/bitnami/drupal
      $ sudo chmod -R g+w /opt/bitnami/drupal
      $ sudo find /opt/bitnami/drupal/ -type d -exec chmod 755 {} \;
      $ sudo find /opt/bitnami/drupal/ -type f -exec chmod 644 {} \;
      $ sudo chown -R daemon /opt/bitnami/drupal/sites/default /opt/bitnami/drupal/modules /opt/bitnami/drupal/themes
    
  • Create and edit the /opt/bitnami/nginx/conf/server_blocks/drupal-server-block.conf file and add the configuration block shown below:

      server {
        # Port to listen on, can also be set in IP:PORT format
        listen 80 default_server;
        root /opt/bitnami/drupal;
        # Catch-all server block
        # See: https://nginx.org/en/docs/http/server_names.html#miscellaneous_names
        server_name _;
        location = /favicon.ico {
          log_not_found off;
          access_log off;
        }
        location = /robots.txt {
          allow all;
          log_not_found off;
          access_log off;
        }
        location ~ ^/sites/.*/private/ {
          return 403;
        }
        # Block access to scripts in site files directory
        location ~ ^/sites/[^/]+/files/.*\.php$ {
          deny all;
        }
        # Allow "Well-Known URIs" as per RFC 5785
        location ~* ^/.well-known/ {
          allow all;
        }
        location / {
          try_files $uri /index.php?$query_string;
        }
        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;
        }
        # Fighting with Styles? This little gem is amazing.
        location ~ ^/sites/.*/files/styles/ {
          try_files $uri @rewrite;
        }
        # Handle private files through Drupal. Private file's path can come
        # with a language prefix.
        location ~ ^(/[a-z\-]+)?/system/files/ {
          try_files $uri /index.php?$query_string;
        }
        location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
          try_files $uri @rewrite;
          expires max;
          log_not_found off;
        }
        location ~ \.php$|^/update.php {
          fastcgi_read_timeout 300;
          fastcgi_pass   unix:/opt/bitnami/php/var/run/www.sock;
          fastcgi_index  index.php;
          fastcgi_param  SCRIPT_FILENAME $request_filename;
          fastcgi_split_path_info ^(.+?.php)(|/.*)$;
          include fastcgi_params;
        }
        include  "/opt/bitnami/nginx/conf/bitnami/*.conf";
      }
    
  • Create and edit the /opt/bitnami/nginx/conf/server_blocks/drupal-https-server-block.conf file and add the configuration block shown below:

      server {
        # Port to listen on, can also be set in IP:PORT format
        listen 443 ssl default_server;
        root /opt/bitnami/drupal;
        # Catch-all server block
        # See: https://nginx.org/en/docs/http/server_names.html#miscellaneous_names
        server_name _;
        ssl_certificate      bitnami/certs/server.crt;
        ssl_certificate_key  bitnami/certs/server.key;
        include  "/opt/bitnami/nginx/conf/bitnami/*.conf";
      }
    
  • Restart NGINX:

      $ sudo /opt/bitnami/ctlscript.sh restart nginx
    

Step 2: Create and configure the Drupal database

Follow the steps below to create the Drupal database:

  • Create a database for Drupal (you need to indicate the MySQL credentials).

      $ mysql -u root -e "create database bitnami_drupal;" -p
    
  • Create a database user for Drupal (you need to indicate the MySQL credentials). Replace the DRUPAL-PASSWORD placeholder with a user-defined password that you will use when installing Drupal.

      $ mysql -u root -e "create user 'bn_drupal'@'localhost' identified by 'DRUPAL-PASSWORD'; grant all privileges on bitnami_drupal.* to 'bn_drupal'@'localhost'" -p
    

Step 3: Install Drupal

Follow the steps below to install Drupal:

  • Open a browser and go to http://YOUR-SERVER-IP/core/install.php. You should see a page similar to the one shown below:

    Select Drupal language

  • Choose the language.

  • Choose the “Standard” profile.

  • When setting the database, use the DRUPAL-PASSWORD you previously chose. Once you have finished, click “Save and continue”:

    Drupal database configuration

  • Enter the required information in the “Configure site” screen and click “Save and continue”:

    Drupal site configuration

Drupal will now be installed. Once installed, you will be able to access it by browsing to the IP address of your NGINX Web server.

Step 4: Configure security settings

As a final step, it is also recommended to set a trusted host pattern in your Drupal configuration file at /opt/bitnami/apps/drupal/htdocs/sites/default/settings.php. Edit this file and add the following line to it, replacing the YOUR-SERVER-IP placeholder with the IP address or domain name for your Drupal website.

    $settings['trusted_host_patterns'] = [
      '^YOUR\.SERVER\.IP$',
    ];
Last modification February 9, 2023