Drupal 8 on Centos 7 with Nginx and PostgreSQL

This article details the steps needed to install Drupal 8 on CentOS with Nginx as a web server and PostgreSQL as a database. At the time of writing, CentOS 8 is still in the works, so we will use the current CentOS 7 release instead, 7.6.1810. Since Drupal 8 requires a more modern version of PHP than the one supplied by CentOS 7 (PHP 5.4), we will use Remi Collet’s RHEL repository to install PHP 7.3. Follow the instructions on Remi’s site to install and enable the Enterprise Linux 7 repository, remi-release-7.rpm. Alternatively, you can obtain the same result using the command line:

$ su -
# yum install epel-release yum-utils policycoreutils policycoreutils-python
# yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
# yum-config-manager --enable remi-php73

The first command switches the current user to the root user; enter root’s password when prompted. The second command installs EPEL -if it’s not already available, yum-utils to enable additional repositories and policycoreutils for SELinux management. The last command enables Remi’s PHP 7.3 repository. You’ll be asked to confirm importing keys and package installation; just press `y’ when prompted to do so.
Then, we need to install the packages required by Drupal 8 (release 8.7.4 at the time of writing):

# yum install php-pdo php-pgsql php-mbstring php-opcache php-xml php-gd php-fpm nginx postgresql postgresql-server postgresql-contrib certbot-nginx

The above command will install the required PHP modules, Nginx 1.12.2 and PostgreSQL 9.2.24. Certbot is Let’s Encrypt’s utility to automate HTTPS configuration on Nginx and on other web servers.
If Apache (httpd) is installed on the system, it is probably wise to uninstall it:

# yum erase httpd
# userdel -r apache

To complete the installation of the required software, go to Drupal’s web site, download the current version and extract the tarball in a subdirectory of Nginx’s document root, say drupal8:

# mkdir -p /var/www/drupal8
# cd /var/www/drupal8
# tar zxf $HOME/drupal-8.7.4.tar.gz

Then adjust ownership, permissions and SELinux context for the newly created directory:

# chown -R root.nginx .
# semanage fcontext -a -t httpd_sys_rw_content_t \
# semanage fcontext -a -t httpd_sys_rw_content_t \
# restorecon /var/www/drupal8/sites/default/files

The following bash script can be used to automatically set permissions to the whole document root’s hierarchy:

if [ "$(pwd)" != "/var/www/drupal8" ]
    echo 'Must be run from /var/www/drupal8/'
chown -R root.nginx .
find . -type d -exec chmod u=rwx,g=rx,o= '{}' \;
find . -type f -exec chmod u=rw,g=r,o= '{}' \;
cd ./sites
for d in ./*/files
    find $d -type d -exec chmod ug=rwx,o= '{}' \;
    find $d -type f -exec chmod ug=rw,o= '{}' \;

Save the file, change its permissions and run it:

# chmod 700 /var/www/drupal8/setdrupalperms.sh
# ./setdrupalperms.sh

At this point, it is time to start installation of PostgreSQL and Nginx/php-fpm:

• PostgreSQL

Create a user to own the database, say drupaldb, assign a password to it and switch to the PostgreSQL admin user to modify the authentication METHOD in /var/lib/pgsql/data/pg_hba.conf:

# useradd drupaldb
# passwd drupaldb
# su – postgres

Edit the file with your favorite text editor and modify the following lines:

local all all ident
host all all ident
host all all ::1/128 ident


local all all md5
host all all md5
host all all ::1/128 md5

Then enable and start the postgresql service:

$ su - -c "systemctl enable postgresql && systemctl start postgresql"

Follow the instructions to initialize the database and then proceed to create the database user with createdb privilege:

$ createuser --interactive --createdb drupaldb

Follow the instructions to create the internal drupaldb user, then switch to the system user and create the database for Drupal:

$ su – drupaldb
$ createdb drupaldb

Then type

$ exit

twice to exit from the drupaldb and postgres sessions. You should now have created a new database drupaldb with its system owner. Take note of the password you assigned to the drupaldb user, as you will need it for the Drupal installation process.

• Nginx

Follow these instructions to apply the necessary configuration to /etc/nginx/nginx.conf, then follow these steps to have certbot enable HTTPS support for Nginx.

• Php-fpm

Apply the following modifications to the php-fpm configuration file, /etc/php-fpm.d/www.conf:

listen = /run/php-fpm/www.sock
listen.owner = nginx
listen.group = nginx

Also, remember to allow HTTP and HTTPS traffic through your firewall (here's a handy guide). We are now ready to enable and start the two processes:

# systemctl enable php-fpm && systemctl start php-fpm
# systemctl enable nginx && systemctl start nginx

Now open a browser and point it to your web site; the Drupal installation process should automatically begin. As an additional precaution, you can now disable the postgres and drupaldb users, if you want:

# usermod -s /sbin/nologin drupaldb
# usermod -s /sbin/nologin postgres

This concludes our guide on how to install Drupal 8 on Centos 7 with Nginx and PostgreSQL. Thank you for following so far and good luck!