Deploying Drupal 10 to an Ubuntu Server with Git Integration and Automated Deployment

When it comes to deploying a Drupal 10 website, ensuring a smooth and efficient setup is essential for keeping your server organized and running effectively. This guide will walk you through the entire process of deploying a Drupal 10 website to an Ubuntu server, from setting up your environment to automating deployments using Git and webhooks. In this tutorial, we will cover everything you need to get started, including configuring your server, initializing your deployment directory, and setting up Git for version control. Additionally, we'll explore how to create a deployment script to streamline updates and implement webhooks to enable automatic deployment with every new commit. By the end of this guide, you’ll have a fully operational and automated deployment workflow, ensuring that your Drupal site stays updated and secure with minimal manual effort. Follow along, and let’s get started with preparing your Ubuntu server, installing necessary software, and configuring your deployment pipeline for a hassle-free Drupal 10 experience.
Step 1: Prepare Your Environment
- Ensure your Ubuntu server has all necessary software installed:
- Apache/Nginx (web server)
- MySQL/MariaDB (database)
- PHP 8.1+ and necessary PHP extensions for Drupal 10
- Install Git:
sudo apt update
sudo apt install git -y
- Install Drush:
Install Drush globally:
composer global require drush/drush
Make sure the Composer vendor bin is added to your PATH:
echo 'export PATH="$PATH:$HOME/.composer/vendor/bin"' >> ~/.bashrc
source ~/.bashrc
Step 2: Set Up the Deployment Directory
- SSH into your server using the
bitnami
user:
ssh -i /Users/ericswanson/.ssh/lightsail.pem bitnami@<your-server-ip>
- Navigate to the default Bitnami Drupal directory:
cd /opt/bitnami/drupal
This is the default directory where the Drupal files are located in a Bitnami Drupal Lightsail instance.
Step 3: Set Up Git on the Server
- Initialize a Git Repository:
Navigate to the Drupal directory and initialize a Git repository:
git init
- Add Remote Origin:
Add the GitHub repository as the remote origin:
git remote add origin git@github.com:username/your-drupal10-repo.git
- Configure Git User:
Set the Git user name and email to avoid commit errors:
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
- Add and Commit Existing Files:
Add all existing files to the repository and make the initial commit:
git add .
git commit -m "Initial commit of existing Drupal files"
- Push to GitHub:
Push the local repository to GitHub:
git push -u origin main
- Set Up SSH Access for GitHub:
- Generate an SSH key (if not already generated):
```bash
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
```
- Add the public key to your GitHub repository as a Deploy Key. You can do this under Settings > Deploy Keys in GitHub.
Step 4: Create a Deployment Script
- Create a script to deploy the latest code. Place it in
/opt/bitnami/scripts/deploy_drupal.sh
:
#!/bin/bash
REPO_DIR="/opt/bitnami/drupal"
cd $REPO_DIR
# Ensure you are on the correct branch
git checkout main
# Pull the latest code from GitHub
git pull origin main
# Run database updates and clear cache
drush updatedb -y
drush cache-rebuild
- Make this script executable:
chmod +x /opt/bitnami/scripts/deploy_drupal.sh
Step 5: Set Up GitHub Webhook for Automatic Deployment
- Set Up a Webhook Listener:
You can use a simple webhook listener to trigger the deployment script when changes are pushed to the GitHub repository. Here's an example using Flask:
- Create a Python script
/home/bitnami/webhook_listener.py
:
```python
from flask import Flask, request
import subprocess
app = Flask(__name__)
@app.route('/webhook', methods=['POST'])
def webhook():
if request.method == 'POST':
subprocess.call(['/opt/bitnami/scripts/deploy_drupal.sh'])
return '', 200
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
```
- Install Flask:
```bash
sudo apt install python3-pip
pip3 install Flask
```
- Run the webhook listener:
```bash
screen -S webhook_listener
source /opt/bitnami/flask_env/bin/activate
python3 /home/bitnami/webhook_listener.py
```
To keep the listener running, detach from the screen session by pressing CTRL + A
, then D
. You can reattach it later using:
screen -r webhook_listener
Step 6: Automate and Secure the Process
- Create a Systemd Service for the Webhook Listener:
To ensure the Flask webhook listener runs automatically after a reboot, create a systemd service file:
sudo nano /etc/systemd/system/webhook_listener.service
Add the following content:
[Unit]
Description=Flask Webhook Listener
After=network.target
[Service]
User=bitnami
WorkingDirectory=/home/bitnami
Environment="PATH=/opt/bitnami/flask_env/bin"
ExecStart=/opt/bitnami/flask_env/bin/python3 /home/bitnami/webhook_listener.py
Restart=always
[Install]
WantedBy=multi-user.target
Save the file, then reload systemd and start the service:
sudo systemctl daemon-reload
sudo systemctl start webhook_listener.service
sudo systemctl enable webhook_listener.service
This will ensure that the webhook listener starts at boot and restarts automatically if it fails.
- Automate Deployment with Cron (Optional):
If webhooks are not ideal, you can create a cron job that runs periodically to pull changes:
crontab -e
Add the following line to run the deployment script every 5 minutes:
*/5 * * * * /opt/bitnami/scripts/deploy_drupal.sh
- Lock Down SSH Access:
Ensure SSH is secured:
- Disable password login.
- Use strong keys for authentication.
Step 7: Troubleshooting and Testing
- Test the Webhook:
Make changes to the Drupal repository and push to the main
branch. Verify that the deployment script runs successfully.
- Debugging:
- Check server logs if any issues arise.
- View Flask logs for any errors during webhook handling.
- Use
screen
to monitor the webhook listener's console output.
- Read more about Deploying Drupal 10 to an Ubuntu Server with Git Integration and Automated Deployment
- Log in or register to post comments