Header

Deploying a LeafPHP Application to Digital Ocean with DeployHQ

Version Support

This tutorial assumes use of LeafPHP >= 3.0 and PHP >=7.0.

What Are We Building?

This experiment will guide you deploying your first LeafMVC application to Digital Ocean using DeployHQ for automated deployments. A majority of the same steps apply to Leaf v3 core as well.

Prerequisites

Before continuing, ensure you have:

  • A Digital Ocean account
  • A GitHub (or similar) repository for your Leaf application
  • A DeployHQ account

1. Setup DeployHQ

  1. Log into DeployHQ
  2. Create a new project
  3. Connect your GitHub repository
  4. Configure deployment settings:
    • Source branch (e.g., main)
    • Deployment path: /var/www/$DOMAIN
    • Add deployment commands (SSH):
     # This command can be done on the server itself or in the Build Pipeline
     composer install
     # Post deployment SSH Commands
     php leaf db:migrate
     php leaf db:seed

More info about SSH Commands, here.

2. Create a New Droplet

From the control panel, click the green "Create" button and select droplet. We will create a VPS with the following options selected:

  • Ubuntu: 20.04 (LTS)
  • Plan: Basic
  • CPU Options: Premium AMD or Regular Intel
  • $6/mo package

Authentication

It is highly recommended that you utilize SSH-based authentication. Select an existing key, or generate a new key, then add it.

3. Server Configuration for DeployHQ

Create Deployment User

adduser deployhq
usermod -aG sudo deployhq

Setup SSH Access

  1. Generate SSH key for DeployHQ
  2. Add public key to ~deployhq/.ssh/authorized_keys
  3. Set correct permissions:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

4. Initial Droplet Setup

After your droplet has been created, you will need to login, secure it, and install required software.

Create Admin User

ssh root@$DOMAIN
adduser username
usermod -aG sudo username
rsync --archive --chown=username:username ~/.ssh /home/username

Test the admin account: su - username

Setup Firewall

Next, we will setup UFW - Ubuntu Firewall:

sudo ufw allow 22
sudo ufw allow 80
sudo ufw allow 443
sudo ufw enable

View firewall status: sudo ufw status

Install Required Software

Update system software:

sudo apt update
sudo apt upgrade

Install NGINX, PHP, MySQL, and curl:

sudo apt install nginx php-fpm php-mysql php-curl

NGINX Configuration

Ensure your NGINX configuration includes:

server {
    server_name $DOMAIN www.$DOMAIN;
    root /var/www/$DOMAIN/public;
    index index.html index.htm index.php;
    location / {
        try_files $uri /index.php?$query_string;
    }
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    }
}

Install MySQL

sudo apt install mysql-server

Please note that the db will be within the droplet, so you will need to have backups as well if it contains productive data.

5. DeployHQ Deployment Configuration

In DeployHQ project settings:

  • Add server connection details
  • Specify deployment path
  • Configure automatic deployments (optional)

Deployment Hooks

Add custom hooks in DeployHQ:

# Post-deployment script
cd /var/www/$DOMAIN
composer install
php leaf db:migrate
sudo systemctl restart nginx

6. Install Leaf Application

Clone your repository and install dependencies:

composer install
php leaf db:install
php leaf db:migrate
php leaf db:seed
sudo apt install certbot python3-certbot-nginx
sudo systemctl reload nginx
sudo certbot --nginx -d $DOMAIN -d www.$DOMAIN

When prompted, select Option 2 to force HTTPS traffic.

Trigger First Deployment

  1. Push changes to your repository
  2. DeployHQ will automatically detect and deploy

Additional Tips

  • Use config files in DeployHQ for sensitive configurations
  • Set up deployment notifications
  • Configure rollback strategies

Troubleshooting

  • Ensure DeployHQ user has correct permissions
  • Verify SSH key authentication
  • Check deployment logs in DeployHQ dashboard

Conclusion

You now have an automated deployment workflow for your Leaf application using Digital Ocean and DeployHQ!

Scaling ⚡️

Should your application grow in requirements or traffic, you can always come back and increase your package selection.

Happy deploying!

Tree

Proudly powered by Katapult. Running on 100% renewable energy.