YouTube Icon

Code Playground.

How to Install Drupal on CentOS 7


How to Install Drupal on CentOS 7

Drupal is one of the main open-source CMS structures international. It is flexible, scalable and can be used to build unique styles of web sites ranging from small non-public blogs to massive company, political, and authorities sites.

In this academic, we’ll provide an explanation for a way to set up Drupal 8.6 on CentOS 7.

There are a couple of approaches to put in Drupal. This guide covers the steps important for installing Drupal the use of a composer template for Drupal projects known as drupal-mission.

We’ll be the usage of Nginx as an internet server, the modern-day PHP 7.2 and MySQL/MariaDB as a database server.

Before you start the installation, make certain you've got met the following stipulations:
Have a domain call pointing to your public server IP. We’ll use example.Com.
Have Nginx mounted .
Have an SSL certificates installed for your domain. You can set up a loose Let’s Encrypt SSL certificate with the aid of following those commands .
Logged in as a person with sudo privileges .
Create a MySQL database
The first step is to create a brand new database and user account and to grant suitable permissions to the user.

If MySQL or MariaDB is already installed on your server you may bypass this step, if now not you can installation the MariaDB five.Five server bundle from the CentOS’s default repositories by way of typing:

sudo yum install mariadb-server

For clean MariaDB/MySQL installations, it's far recommended to run the mysql_secure_installation command to enhance the security of your database server.
Login to the MySQL shell with the aid of typing the subsequent command and enter the password whilst brought about:

mysql -u root -p

To create a database named drupal, user named drupaluser and to grant the important permissions to the consumer run the subsequent commands:

CREATE DATABASE drupal CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES ON drupal.* TO 'drupaluser'@'localhost' IDENTIFIED BY 'change-with-strong-password';

Install PHP
CentOS 7 ships with PHP version 5.4, which is old and not supported. The endorsed PHP version for Drupal is PHP 7.2.

To install PHP 7.2 on CentOS 7 first we need to allow the EPEL and Remi repositories:

sudo yum install epel-release yum-utils
sudo yum install
sudo yum-config-manager --enable remi-php72

Once the repositories are enabled to put in PHP 7.2 and all required PHP extensions run the following commands:
sudo yum install personal home page-cli personal home page-fpm Hypertext Preprocessor-mysql personal home page-json php-opcache php-mbstring Hypertext Preprocessor-xml personal home page-gd php-curl git
We mounted PHP FPM due to the fact we will be the usage of Nginx as an internet server.

By default PHP FPM will run as user apache on port 9000. We’ll exchange the consumer to nginx and transfer from TCP socket to Unix socket. To achieve this open the /etc/php-fpm.D/www.Conf document and edit the strains highlighted in yellow:


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

Make positive the /var/lib/personal home page directory has the ideal ownership the usage of the subsequent chown command :

sudo chown -R root:nginx /var/lib/php

Finally, permit and begin the PHP FPM carrier:

sudo systemctl enable php-fpm
sudo systemctl start php-fpm

Install Composer
Composer is a dependency supervisor for PHP. We will download the Drupal template and set up all essential Drupal additives with composer.

The following command will install composer globally by downloading the Composer installer with curl and transferring the file to the

curl -sS | sudo php -- --install-dir=/usr/local/bin --filename=composer

Verify the set up through going for walks the subsequent command so that you can print the composer model:

composer --version

The output need to appearance some thing like this:

Composer version 1.8.4 2019-02-11 10:52:10

Install Drupal
Now that composer has been hooked up, create a new Drupal assignment using the Drupal template inside /var/www/my_drupal listing:

sudo /usr/local/bin/composer create-project drupal-composer/drupal-project:8.x-dev /var/www/my_drupal --stability dev --no-interaction

The command above will download the template, fetch all required php packages and run the scripts vital to prepare the challenge for set up. The system can also take a few minutes and if it is a success the quit of the output will look like this:

Create a sites/default/settings.php file with chmod 0666
Create a sites/default/files directory with chmod 0777

Next step is to install Drupal the use of Drush. In the command below we're passing the MySQL database and person information we created in the first section:

cd /var/www/my_drupal
sudo vendor/bin/drush site-install --db-url=mysql://drupaluser:change-with-strong-password@localhost/drupal

The installer will activate you with the following message, just press enter to maintain.

You are about to DROP all tables in your 'drupal' database. Do you want to continue? (yes/no) [yes]:

Once the set up is finished the script will print the administrative username and password. The output have to look some thing like the following:

[notice] Starting Drupal installation. This takes a while.
[success] Installation complete.  User name: admin  User password: frxka2Db5v

Finally, set the suitable permissions so that the web server could have complete access to the site’s documents and directories:

sudo chown -R nginx: /var/www/my_drupal

Configure Nginx
By now, you should have already got Nginx with SSL certificate established on your machine, if no longer test the conditions for this academic.

To create a brand new server block for our new Drupal venture we are able to use the Nginx recipe from the legitimate Nginx web page.

Open your textual content editor and create the subsequent record:

sudo nano /etc/nginx/conf.d/


# Redirect HTTP -> HTTPS
server {
    listen 80;

    include snippets/letsencrypt.conf;
    return 301$request_uri;

# Redirect WWW -> NON WWW
server {
    listen 443 ssl http2;

    ssl_certificate /etc/letsencrypt/live/;
    ssl_certificate_key /etc/letsencrypt/live/;
    ssl_trusted_certificate /etc/letsencrypt/live/;
    include snippets/ssl.conf;

    return 301$request_uri;

server {
    listen 443 ssl http2;

    root /var/www/my_drupal/web;

    # SSL parameters
    ssl_certificate /etc/letsencrypt/live/;
    ssl_certificate_key /etc/letsencrypt/live/;
    ssl_trusted_certificate /etc/letsencrypt/live/;
    include snippets/ssl.conf;

    # log files
    access_log /var/log/nginx/;
    error_log /var/log/nginx/;

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

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

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

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

    # Block access to scripts in site files directory
    location ~ ^/sites/[^/]+/files/.*\.php$ {
        deny 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 /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;

    location ~ '\.php$|^/update.php' {
        fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
        include fastcgi_params;
        # Block httpoxy attacks. See
        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;
        fastcgi_pass unix:/run/php-fpm/www.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|svg)$ {
        try_files $uri @rewrite;
        expires max;
        log_not_found off;


Don’t neglect to update instance.Com together with your Drupal domain and set the ideal path to the SSL certificate documents. All the HTTP requests could be redirected to HTTPS . The snippets used on this configuration are created in this guide .
Before restarting the Nginx provider make a take a look at to make sure that there are not any syntax mistakes:

sudo nginx -t

Restart the Nginx provider for adjustments to take impact via typing:

sudo systemctl restart nginx

Test the Installation
Open your browser, kind your area and assuming the set up is a hit, a display much like the subsequent will appear:

Drupal Installation
You can log in as an admin and begin customizing your new Drupal installation.

Install Drupal Modules and Themes
Now that you have your Drupal assignment mounted, you’ll want to install a few modules and issues. Drupal modules and issues are hosted on a custom composer repository, which drupal-mission configures for us out of the field.

To set up a module or a topic, all you want to do is to cd to the undertaking directory and type composer require drupal/module_or_theme_name. For example, if we want to put in the Pathauto module, we need to run the following command:

cd /var/www/my_drupal
sudo -u nginx /usr/local/bin/composer require drupal/pathauto

By prepending sudo -u nginx we're running the command as user nginx

Using version ^1.3 for drupal/pathauto
./composer.json has been updated
> DrupalProject\composer\ScriptHandler::checkComposerVersion
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 3 installs, 0 updates, 0 removals
  - Installing drupal/token (1.5.0): Downloading (100%) 
  - Installing drupal/ctools (3.2.0): Downloading (100%)
  - Installing drupal/pathauto (1.3.0): Downloading (100%)
Package phpunit/phpunit-mock-objects is abandoned, you should avoid using it. No replacement was suggested.
Writing lock file
Generating autoload files
> DrupalProject\composer\ScriptHandler::createRequiredFiles

As you could see from the output above composer also installs all of the package dependencies for us.

Update Drupal Core
Before upgrading it is always a terrific idea to take a backup of your documents and database. You can either use the Backup and Migrate module or manually backup your database and documents.

To back up the installation documents you can use the subsequent rsync command , of course, you’ll want to use the appropriate direction to the installation listing:

sudo rsync -a /var/www/my_drupal/  /var/www/my_drupal_$(date +%F)

To lower back up the database we can either use the same old mysqldump command :

mysqldump -u root -p > /var/www/my_drupal_database_$(date +%F).sql

Or drush square-unload:

cd /var/www/my_drupal
vendor/bin/drush sql-dump > /var/www/my_drupal_database_$(date +%F).sql

Now that we created a backup we will hold and replace all Drupal core files via going for walks the following command:

sudo -u nginx /usr/local/bin/composer update drupal/core webflo/drupal-core-require-dev symfony/* --with-dependencies

Congratulations, you have correctly hooked up Drupal eight using composer and learned a way to deploy modules and subject matters. You can now begin customizing your website. The Drupal 8 User Guide is a great beginning location to research extra about a way to control your Drupal set up. Also, keep in mind to go to the Drupal Composer template venture on Github.

If you have questions, experience free to leave a remark beneath.