Sep 10, 2023 10 min read

How to Install Drupal on CentOS 7

Install Drupal on CentOS 7 with our step-by-step tutorial. It is an open-source CMS for creating and managing websites and applications.

Install Drupal on CentOS 7
Table of Contents

Choose a different version or distribution

Introduction

Before we begin talking about how to install Drupal on CentOS 7, let’s briefly understand – What is Drupal?

Drupal is an open-source content management system (CMS) for creating and managing websites and applications. From small personal websites to large enterprise solutions, Drupal provides a flexible and scalable platform for building and customizing digital experiences.

With its extensive module library and strong community support, Drupal offers endless possibilities for designing visually appealing, user-friendly websites that rank high in search engine results.

Advantages of Drupal

  1. Flexible and Scalable: Drupal provides a highly adaptable platform, allowing users to build websites that can easily grow and evolve with their needs.
  2. Extensive Community Support: With a strong and active community, Drupal users can access a wealth of resources, modules, and themes to enhance their websites.
  3. Search Engine Optimization (SEO) Friendly: Drupal's built-in SEO features and modules help improve search engine rankings, making it easier for users to find your website.
  4. Robust Security: Drupal has a strong security track record, with regular updates and a dedicated security team, ensuring your website is protected against vulnerabilities.
  5. Customization: Drupal's flexible architecture enables users to create unique and custom experiences, leveraging its powerful theming system and module library.

Prerequisites

  • You'll be required to have a domain name pointing to your public server IP. We'll be using example.com.
  • You'll need to install Nginx.
  • An SSL certificate is installed for your domain. You can also install a free Let’s Encrypt SSL certificate.
  • Log in as a user with sudo privileges.

Step 1 - Creating a MySQL Database

We have to begin with creating a database and granting permissions to the user.

1) This step can be skipped if MySQL or MariaDB is already installed in your system. In case you don't have it, use the following command to install MariaDB 5.5 server package from the CentOS 7 default repositories and start the service.

sudo yum install mariadb-server

sudo systemctl enable --now mariadb
Note: It is recommended to run the mysql_secure_installation command to improve the security of the database server for fresh MariaDB/MySQL installations.

2) Using the command given below, log in to the MySQL shell and put in the password when prompted.

mysql -u root -p

3) Next, use the following command to create a database drupal, with username drupaluser and to grant necessary permissions to the user.

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';
exit;

Step 2 - Installing PHP

CentOS 7 ships with an old PHP version 5.7 which is outdated and need to be replaced with version 7.2.

1) For installing the latest version, we need to enable EPEL and Remi repositories

sudo yum install epel-release yum-utils
sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
sudo yum-config-manager --enable remi-php72

2) Next, run the commands given below in order to install PHP 7.2 and all required PHP extensions.

sudo yum install php-cli php-fpm php-mysql php-json php-opcache php-mbstring php-xml php-gd php-curl git zip unzip nginx nano

Here, we have installed PHP FPM, since we're using Nginx as the web-server.

3) PHP FPM will run as a user apache on port 9000 by default. You need to change the user to nginx and switch from the TCP socket to Unix socket. Open the /etc/php-fpm.d/www.conf file and edit the content after = sign:

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

4) Use the command below to make sure the /var/lib/php the directory has the correct ownership.

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

5) Now, enable the PHP FPM service

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

Step 3 - Installing Composer

The composer happens to be a dependency manager for PHP. You need to download the Drupal template and install the necessary Drupal components with the help of Composer.

1) Now, use the command given below to install Composer with the help of the Composer installer along with curl command and moving the file to the /usr/local/bin directory:

curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer

2) Next, verify the installation with the help of the following command. This will print the composer version.

composer --version

3) You should receive the following output:

Output

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

Step 4 - Installing Drupal

1) Once the Composer is installed, create a new Drupal project using the Drupal template in the /var/www/my_drupal directory:

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

2. If you got output something like this, then we have to allow one plugin.  

Output

In PluginManager.php line 740:

  cweagans/composer-patches contains a Composer plugin which is blocked by your allow-plugins config. You may add it to the list if you consider it safe. You can run "composer config --no-plugins allow plugins.cweagans/composer-patches [true|false]" to enable it (true) or disable it explicitly and suppress this exception (false)
  See https://getcomposer.org/allow-plugins

3. Fix the above problem with these commands:

cd /var/www/my_drupal/
sudo chmod 666 composer.json
composer config --no-plugins allow-plugins.cweagans/composer-patches true
sudo /usr/local/bin/composer install

Press y and Enter for every prompt while this installation.

This will make the program download the template, fetch the required PHP packages, and run the scripts necessary to prepare the project for installation. The process may take a few minutes. Once done, the output should be similar to the one below.

Output

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

2) Now, download Drupal with the help of Drush. You need to pass the MySQL database and user information created initially with the help of the command given below:  

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

3) You will receive the following prompt, press enter, and continue

Output

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

4) Once the process is complete, the administrative username and password will be printed by the script. It should look similar to the output below:

Output

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

5) Now, set the permissions so that all files and directories can be accessed by the web-server.

sudo restorecon -Rv /var/www/my_drupal
sudo chown -R nginx:nginx /var/www/my_drupal
sudo chmod -R g+w /var/www/my_drupal
sudo chmod g+s /var/www/my_drupal
sudo semanage port -a -t mysqld_port_t -p tcp 3306

Step 5 - Configuring Nginx

1) Now, you need to use the Nginx recipe from its official site to create a new server block for the Drupal project.

Open the text editor, then create the following file:

sudo nano /etc/nginx/conf.d/example.conf

If you have generated SSL certificates, then paste this configuration:

# Redirect HTTP -> HTTPS
server {
    listen 80;
    server_name www.example.com example.com;

    include snippets/letsencrypt.conf;
    return 301 https://example.com$request_uri;
}

# Redirect WWW -> NON WWW
server {
    listen 443 ssl http2;
    server_name www.example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
    include snippets/ssl.conf;

    return 301 https://example.com$request_uri;
}

server {
    listen 443 ssl http2;
    server_name example.com;

    root /var/www/my_drupal/web;

    # SSL parameters
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
    include snippets/ssl.conf;

    # log files
    access_log /var/log/nginx/example.com.access.log;
    error_log /var/log/nginx/example.com.error.log;

    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 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;
        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;
    }

}
Note: Replace example.com with your Drupal domain and set the correct path to the SSL certificate files. All HTTP requests will be redirected to HTTPS. The snippets used in the configuration are created in this guide.

Else use this configuration without SSL, You can enter any port you want we have set to 8080:  

server {

    listen 8080;
    server_name _;

    root /var/www/my_drupal/web;
    index index.php index.html index.htm index.nginx-debian.html;


    # log files
    access_log /var/log/nginx/example.com.access.log;
    error_log /var/log/nginx/example.com.error.log;

    location / {
                #try_files $uri $uri/ =404;
          try_files $uri $uri/ /index.php$is_args$args;
     }


    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 @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 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;
        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;
    }
}

2) Check for any syntax errors before restarting with the following command:

sudo nginx -t

3) In order to apply the changes, restart the Nginx service using the following command:

sudo systemctl restart nginx

Step 6 - Testing the Installation

1) When the installation is complete, you'll see something like the image below when you type your domain in the browser, or visit http://_ip_:8080/user/login if not using domain and SSL.

Drush Welcome Tab

If you can't see any css is loaded, then you have to again run the below command:

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

Now just reload the page and you will see the css.

2) You can now log in as an admin and customize the Drupal installation.

Step 7 - Installing Drupal Modules and Themes

Once Drupal has been installed, you can install additional modules and themes. These are hosted on a custom composer repository, configured by the drupal-project.

For installing a module or a theme, cd to the project directory and type composer require drupal/module_or_theme_name. For instance, if you have to install the Pathauto module, you need to run the following command:

cd /var/www/my_drupal
sudo -u nginx /usr/local/bin/composer require drupal/pathauto
Note: By prepending sudo -u nginx you'll be running the command as user nginx.
Output

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

The output shows that even Composer installs all package dependencies for us.

Step 8 - Back up and Updating Drupal Core

It would be a good idea to back up the files and database. You can do so with the help of the Backup & Migrate module, or simply do it manually.

1) Use the following command to back up the installation files. Make sure to use the correct path to the installation directory.

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

2) We can use the standard mysqldump command to backup:

sudo mysqldump --all-databases -u root -p | sudo tee /var/www/my_drupal_database_$(date +%F).sql >/dev/null

3) Alternatively, we can use the drush sql-dump:

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

4) Next, we can update all the Drupal core files using the following command:

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

FAQ's to Install Drupal on CentOS 7

What are the system requirements for installing Drupal on CentOS 7? 

Drupal requires Apache/Nginx, PHP, and a database (MySQL/MariaDB) on a CentOS 7 server.

Is there a specific version of Drupal for CentOS 7? 

No, you can download the latest stable version of Drupal from the official website for installation on CentOS 7.

Can I use Apache as the web server for Drupal on CentOS 7? 

Yes, Apache is a commonly used web server for hosting Drupal on CentOS 7. You can install and configure Apache to serve as the web server for your Drupal installation.

Can I use a different web server like Nginx instead of Apache for Drupal on CentOS 7? 

Yes, you can use Nginx as the web server for Drupal on CentOS 7. Adjust the necessary configurations and ensure it supports PHP-FPM.

Can I use CentOS 7's default PHP package for Drupal? 

The default PHP package provided by CentOS 7 may not include all the required extensions and features. It is recommended to use the latest versions of PHP available from the Remi repository or another trusted source.

Can I migrate an existing Drupal site to CentOS 7? 

Yes, you can migrate an existing Drupal site to CentOS 7. Ensure you have a backup of your files and the database, then install Drupal on the new CentOS 7 server, copy the files, and import the database.

How can I troubleshoot common issues during Drupal installation on CentOS 7? 

Some common troubleshooting steps include checking file permissions, verifying the PHP extensions are installed, reviewing the Apache error logs, and ensuring the database credentials are correct. The Drupal community forums and documentation can also provide helpful tips for troubleshooting specific issues.

Conclusion

We hope this detailed guide helped you understand how to install, configure and modify Drupal on CentOS 7 server. To learn more about Drupal installation on CentOS 7, check out the official Drupal installation Documentation.

If you have any queries, please leave a comment below, and we’ll be happy to respond to them for sure.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Blog - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.