DEV Community

Matthias Andrasch
Matthias Andrasch

Posted on • Updated on

Deploy CraftCMS via ploi.io on Hetzner cloud (the fast way)

What is an easy way to get a CraftCMS site up and running? I found this to be a rather simple method with help of ploi.io, a Hetzner CX11 virtual cloud server (max. € 4.51 / month) and DDEV for local development.

Beware: I'm a CraftCMS newbie! 🤓

Prerequisites

  1. Install DDEV for local development
  2. Sign up for a Hetzner Cloud account
  3. Sign up for a ploi.io account
  4. Connect the Hetzner account via API key on ploi.io (as service provider)
  5. Create your first server via ploi dashboard

Screenshot ploi service provider

Screenshot ploi create server

Screenshot create server

Create GitHub repository, clone it locally

Create a new GitHub project (with README), clone it locally and run these commands for the CraftCMS installation:

DDEV quickstart for CraftCMS

# Set up the DDEV environment:
ddev config --project-type=craftcms --docroot=web --create-docroot

# Boot the project and install the starter project:
ddev start
ddev composer create -y --no-scripts craftcms/craft

# Run the Craft installer:
ddev craft install
ddev launch
Enter fullscreen mode Exit fullscreen mode

Push the new files to your GitHub repository.

Dump the local database

For the initial installation we need a dump of the database. This can be achieved via ddev export-db -f dump.sql.gz.

This database dump will be later imported into the live sites database.

But do not worry: After that initial DB import, changes to the data structure will be applied via files in git and by CLI migrate commands (for example php craft migrate/all --no-content --interactive=0).

Create a new database on ploi.io (server)

Databases are managed via Server

Image description

Install phpMyAdmin, connect via DB user and password

Image description

Import the database dump file

Image description

Create a new site via ploi

You don't have to connect the real domain immediately, you can also generate a test domain later. Therefore you can also use "mysite.example.com" e.g.

Image description

Install from git repository

We use a git repository, not the 1-click installer for CraftCMS.

Image description

If you use a public repository, you can just paste the URL:

Image description

Adjust the deploy script

These are commands which are executed when you hit the "deploy now" button.

Image description

# ploi standard commands
cd /home/ploi/craft-playground.mandrasch.dev
git pull origin main

# NodeJS support, e.g. for vite
# npm install
# npm run build

composer install --no-dev --no-interaction --prefer-dist --optimize-autoloader

# deployment best practices by craftcms docs:
# https://craftcms.com/knowledge-base/deployment-best-practices
php craft update/composer-install --interactive=0
php craft migrate/all --no-content --interactive=0
php craft project-config/apply
php craft migrate --track=content --interactive=0

echo "" | sudo -S service php8.2-fpm reload
echo "🚀 Application deployed!"
Enter fullscreen mode Exit fullscreen mode

Add .env values via "Edit environment"

Copy the .env values from .env.example.production, add a security key and adjust the database connection settings. For the security key you can use a generator like Bitwarden Generator.

Image description

Image description

Add a free "Let's encrypt" SSL certificate

Image description

Optional: Add a test domain

If you haven't connected a real domain yet, you can also use a test domain freely provided by ploi.io (for test usage / development).

Image description

gitignore the license.key

One last thing I would suggest is adding config/license.key to your .gitignore, otherwise you will end up with "Your local changes to the following files would be overwritten by merge: config/license.key" after the first deployment.

Ready for deployment! 🎉

Hit the "Deploy now" button and pull the craftcms installation from your GitHub repository:

Image description

That's it. Open your site and enjoy developing with Craft CMS! 🎉

Image description

If you want to trigger auto deployment when pushing to GitHub, check out this guide: ploi.io - How to trigger deployments via GitHub actions

If you're interested in integrating Vite, there is an awesome plugin nystudio107/vite for CraftCMS. Check out example code and tutorial here: https://github.com/mandrasch/ddev-craftcms-vite.

In future it would be cool to have a good and robust ddev pull script to download media assets and live content into local development.

Also make sure to join the DDEV discord. 💚

Top comments (0)