How to Set Up a Drupal Workflow in cPanel with Git

Unlike other CMSs, the update of Drupal core is not difficult, but somehow complicated because of all the details involved. In this tutorial, we are going to implement a local Drupal 9 installation, and then we will upload (push) this codebase to a central repository on Bitbucket. We will then download (pull) the codebase from this repository to the live server, where the website is going to be stored.  

Using this (terminal-oriented) process, you will be able to speed up Drupal core and module updates, and you will follow best practices. 

It is assumed that you already have a LAMP stack installed on your system. Furthermore, you require terminal access on your web server.

Keep reading to learn how!

Step 1: Install Drupal 9 Locally

Open the Terminal application of your system, and place the cursor on your local server directory, typically /var/www/html on a Linux system.

  • Type:  composer create-project drupal/recommended-project my_drupal

This will download the latest stable Drupal version to your machine. 

  • Type:  
cd my_drupal/
mkdir config
cd web/sites/default/
cp default.settings.php settings.php
cd ..
sudo chmod -R 777 default/

These terminal commands perform the following actions in order: 

  • Access the my_drupal directory
  • Create a directory called config
  • Access the default directory
  • Copy the file default.settings.php and change its name to settings.php
  • Go back to the sites directory
  • Assign global permissions to the default directory, so that the system can complete the installation process

Once this is completed, we need to create a database. 

  • Type:  mysql -u root -p 

Enter your MySQL root password.

create database my_drupal;
exit;

This creates the database you will link to your codebase. 

  • Open your web browser
  • Type:  localhost/my_drupal/web

Finish the local installation process.


Step 2: Creating and Adding a Key to the Remote Repository

For the purpose of this tutorial, I am going to use Bitbucket. The steps are pretty similar to a Github profile. 

Once you have logged in to your Bitbucket account, click your profile icon, located (at the time of this writing) on the bottom left corner. 

  • Select Personal settings
  • Click SSH Keys
  • Click Add key

You will see a popup window prompting you for a label and a key. 

  • Type MyLaptop, BobsPC, or whatever you like to identify your own machine at Bitbucket
  • Open your terminal application
  • Type:  ls -al ~/.ssh

The period before the name is telling us, this is a hidden directory.  There are two possibilities:

  1. You get a list of files, one of these files has the extension .pub.
  1. You get the message “File or directory not found”.

The file with the .pub extension is the public key of your system. If you do not see this file, you have to create one. If you do see the file, keep reading anyway. 

  • Type ssh-keygen -t rsa -C "my_email@my_domain.com"
  • Press Enter to select the default directory where the key is going to be stored (.ssh).
  • Enter the same passphrase twice (You can leave this empty by pressing Enter twice.)

The system will notify you about the location of your public key and its fingerprint. If you already had the .pub key on your system, step back in here.

  • Open the file .ssh/id_rsa.pub with your preferred code editor
  • Copy the whole text, including your email address
  • Open your browser and paste it into the text area
  • Click Add key

Your local machine can now connect to Bitbucket.


Step 3: Creating the Remote Repository

On your Bitbucket profile:

  • Click Create repository
  • Select a Workspace and a project name (create them if this is the first time)
  • Give your repo a proper name
  • Do not include a README file
  • Do not include a .gitignore file
  • Click Create repository

Step 4: Add the Local Repository to Bitbucket

  • Type:
cd /var/www/html/my_drupal/web
mv example.gitignore .gitignore
git init
git add .
git commit -m 'Fresh Drupal installation'

You are accessing the root directory of your Drupal installation, changing the name of the file example.gitignore to .gitignore, initializing your local repository, adding the files to the git staging area, and finally committing these “changes” (the repo was initially empty) to the repository.

  • Type:
git remote add origin git@bitbucket.org:[MY_PROJECT]/my-drupal.git
git push -u origin master 

You can copy and paste these two commands from the repository’s page at Bitbucket.

  • Type yes, if prompted
  • Enter your passphrase

Check the repository on Bitbucket. Almost all files inside the web directory (your local repo) have been copied over to your remote repository. Take a look at the sites/default directory.  

Notice that the settings.php file is missing. This is because it is included inside the .gitignore file.

  • Open your terminal and
  • Type:  nano .gitignore
  • Type Ctrl+x to close the nano editor

If you take a look at the cards in the first image of this tutorial, you will notice that we have already finished the tasks of card 1, and there is only one task left in card 2. Let’s continue with the last part of this process.


Step 5: Create a Live Database

Open your dashboard at your hosting provider and create a new Drupal account with your domain name.

  • Click the Drupal “one button installer”.
  • Click Create account

This may vary from provider to provider, so you have to find your way out here. 

You will receive an email with a Drupal user (admin) and a Drupal password. Furthermore, you will also receive the username and password for your cPanel account. These are the ones we need. They are also available on your dashboard at your hosting provider.

  • Click Enable SSH, so we can have access to the terminal on the live server.
  • Click the cPanel link
  • Type databases in the search box
  • Select MySQL® Databases

The one-clck installer created a database. Delete this database.

  • Enter a proper name for your database.

Notice the database prefix automatically added by cPanel (in most of the cases your user name). 


Step 6: Import the Local Database

  • Open your terminal application
  • Type:  
mysqldump -u root p my_drupal >
~/[path/to/your/desktop]/[cpanel_sql_db_name].sql
  • Enter your SQL root password

This will make a copy of your database and place it on your desktop. This database on the desktop must have the same name as the one you created on cPanel. 

If you have MySQL version 8 on your machine, you will most probably have to adapt the database to work with MySQL 5 (available at your hosting provider).

  • Type:
sed -i -e 's/utf8mb4_0900_ai_ci/utf8mb4_unicode_ci/g'
[cpanel_sql_db_name].sql 

This is the Linux command to perform this operation.

  • Open your cPanel dashboard
  • Type phpMyAdmin in the search box and select it
  • Click on the name of your database to select it. It should be empty (No tables found in database)
  • Click Import
  • Click Choose file
  • Select the database from your desktop
  • Scroll down and click Go

This will take a couple minutes, so please be patient. 


Step 7: Redirect Your Domain

Drupal is stored inside the web directory, so to access your site, you have to enter the URL:  https://yoursite.xxx/web

If you use a cloud service, this is no problem, you can edit your hosts file. With cPanel, this is not possible (as far as I know). You have to create a permanent URL redirect.

  • Open your cPanel dashboard
  • Type Redirects on the search box  
  • Press Enter
  • Select Permanent redirect (301)
  • Choose the name of your domain from the dropdown
  • Add the redirect link
  • Click Add
  • Type Domains in the search box
  • Press Enter
  • Click the Force HTTPS Redirect toggler

Step 8: Upload the Codebase

  • Open your cPanel dashboard
  • Type File Manager in the search box
  • Press Enter
  • Double click the public_html directory
  • Click the Settings button on the top right part of the screen
  • Check Show Hidden Files (dotfiles)
  • Click Save
  • Click Select all
  • Click Delete
  • Open the File Manager of your system
  • Compress the whole my_drupal folder
How to Set Up a Drupal Workflow in cPanel with Git
  • Open the cPanel File Manager
  • Click Upload Inside the public_html directory
  • Click Select File
  • Upload the zip file
  • Click Go back once the file has been uploaded to the server
  • Right-click the zip file and select Extract > Extract Files > Close
  • Double click the my_drupal folder
  • Make sure you have the Show hidden files option activated
  • Click Select all
  • Click Move
  • Select the public_html directory
  • Click Move files
  • Click Up one level
  • Right-click the zip file and click Delete
  • Remove the zip file from your local server too
  • Open your terminal or file manager
  • Copy the settings.php file of the local installation to your desktop
  • Open the file with your preferred code editor
  • Replace the username with the username supplied by your hosting provider (Do the same with the name of the database and the password – see Step # 5)
  • Save and close the file
  • Open the File Manager in the cPanel dashboard
  • Click public_html/web/sites/default
  • Click Upload
  • Select the settings.php file from your desktop
  • Click Yes to overwrite the file
  • Click Go back
  • Change the file and directory permissions according to the image below
  • Click Up one level
  • Change the permissions of the default folder to 0755

Step 9: Set the PHP Version on Your Server

Drupal requires at least PHP 7.3. 

  • Open your cPanel dashboard
  • Type Select PHP in the search box
  • Press Enter
  • Select 7.4 
  • Click Set as current
  • Check the opcache extension
  • Type [yoursite.xxx/web] in the address bar of your browser
  • Click Login with the username and password of your local Drupal installation
  • Click the Edit tab and change your password if necessary

Step 10: Add the Production Server Key to Bitbucket

  • Open your cPanel dashboard
  • Type Terminal in the search box
  • Click Enter
  • Click I understand and want to proceed
  • Repeat the process detailed in Step # 2
  • Type:  less ~/.ssh/id_rsa.pub

less is a terminal editor

  • Copy and paste the key like shown in Step # 2, this time for the server machine
  • Type q

That will let you exit less.

You will have one key for each machine (or virtual machine) connected to Bitbucket.


Step 11: Clone the Remote Repository to the Production Server

  • Open the terminal application of your server
  • Type
cd public_html
rm -rf web
git clone git@bitbucket.org:[YOUR_PROJECT]/my-drupal.git web

You are deleting the web folder and replacing it with the remote repository. You obtain the last terminal command if you open the Bitbucket repository and click the Clone button (don’t forget to add web at the end of the command!).


 Step 12: Install a Module or Theme

  • Open the terminal application of your local system
  • Type:
cd /var/www/html/my_drupal
composer require drupal/bootstrap
  • Type cd web to get into the web directory
  • Type:
git add .
git commit -m 'Bootstrap theme installation'
git status
  • Type git push 
  • Enter the passphrase if required
  • Open the terminal on your remote server.
  • Type (enter passphrase if required):
git fetch
git status

If you see:  Your branch is behind ‘origin/master’ by 1 commit, and can be fast-forwarded,

  • Type:  git merge

 This will merge the code in your server with the code being pulled from the remote repository.  

  • Go to the themes section of your Drupal backend, the Bootstrap theme should be there

NOTE:  You only have to repeat this last step, each time you download a module, update Drupal core, or edit the CSS code of your theme.


Troubleshooting

If you get 404 Error pages after installing Drupal on the production server,

  • Delete the .htaccess file
  • Upload it again to the server

 I hope you liked this tutorial. Thanks for reading!

Author

  • Jorge Montoya

    Jorge lived in Ecuador and Germany. Now he is back to his homeland Colombia. He spends his time translating from English and German to Spanish. He enjoys playing with Drupal and other Open Source Content Management Systems and technologies.

    View all posts
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x