Deploy CraftCMS via ploi.io on Hetzner cloud (the fast way)
Tags: craftcms, php, devops, ddev
Published: 2023-05-12 19:13:27

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](https://ploi.io/), a [Hetzner CX11 virtual cloud server](https://www.hetzner.com/cloud) (max. € 4.51 / month) and [DDEV](https://ddev.com/) for local development.

Beware: I'm a CraftCMS newbie! 🤓

**Prerequisites**

1. Install [DDEV](https://ddev.com/) for local development
1. Sign up for a [Hetzner Cloud account](https://accounts.hetzner.com/signUp)
1. Sign up for a [ploi.io account](https://ploi.io/)
1. Connect the Hetzner account via API key on ploi.io (as service provider)
1. Create your first server via ploi dashboard

![Screenshot ploi service provider](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l7zj6jydt7w6o8zbc6ah.png)

![Screenshot ploi create server](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0gkg9ocf9i8gh9r3xw9i.png)

![Screenshot create server](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y6vabkw4p6id7sd6lapm.png)

**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](https://ddev.readthedocs.io/en/latest/users/quickstart/)

```bash
# 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
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v2me8y2i2ihbzrm6voz9.png)

**Install phpMyAdmin, connect via DB user and password**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xyfa6uc2rv2kc0m309rz.png)

**Import the database dump file**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9qyaikcdnzpaz3yl7io8.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/636r4ldi946uznhu3aev.png)

**Install from git repository**

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/byw3g2wtjfyjmda7jvqu.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/prno97tfgmauw0sqyc4g.png)

**Adjust the deploy script**

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q1j0mdbqssm6v2zqfss7.png)

```bash
# 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!"
```

**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](https://bitwarden.com/password-generator/).

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3zypf9tvcquds8lr8lfv.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gvqznojq143h3vr9yln9.png)

**Add a free "Let's encrypt" SSL certificate**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p6daup0ognu657j40pa5.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5wouiiinvm0rkvk0ondq.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bpzaj7xvpyqid7wy7hkv.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/np7jq1uo7lv64o2le8j6.png)

If you want to trigger auto deployment when pushing to GitHub, check out this guide: [ploi.io - How to trigger deployments via GitHub actions](https://ploi.io/documentation/deployment/how-to-trigger-deployments-via-github-actions)

If you're interested in integrating Vite, there is an awesome plugin [nystudio107/vite](https://plugins.craftcms.com/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](https://discord.gg/hCZFfAMc5k). 💚