Caddy, MySQL and PHP-FPM on Ubuntu 20.04

Set up the repository for Caddy and install

$ sudo echo "deb [trusted=yes] https://apt.fury.io/caddy/ /" | sudo tee -a /etc/apt/sources.list.d/caddy-fury.list
$ sudo apt update
$ sudo apt install caddy

Install PHP

$ sudo apt install php7.4-fpm php7.4-cli php7.4-gd php7.4-xml php7.4-zip php7.4-mbstring php7.4-curl

Optionally install MySQL (MariaDB)

$ sudo apt install mariadb-server mariadb-client php7.4-mysql

Configure and secure MariaDB

$ sudo mysql_secure_installation

You will be asked the following:

Enter current password for root (enter for none): 
Set root password? [Y/n] y  
Remove anonymous users? [Y/n] y  
Disallow root login remotely? [Y/n] y  
Remove test database and access to it? [Y/n] y  
Reload privilege tables now? [Y/n] y  

Make sure Caddy, PHP-FPM and MariaDB start automatically

$ sudo systemctl enable caddy
$ sudo systemctl enable php7.4-fpm
$ sudo systemctl enable mariadb

Set up your system users and create a directory to hold their websites. We’ll start with two but you can add more later

$ sudo useradd user1
$ sudo mkdir -p /home/user1/www; chown user1:user1 /home/user1/www; chmod +x /home/user1
$ sudo useradd user2
$ sudo mkdir -p /home/user2/www; chown user2:user2 /home/user2/www; chmod +x /home/user2

Modify your /etc/caddy/Caddyfile to contain the following:

http://user1.example.com {
root * /home/user1/www
encode gzip
file_server

php_fastcgi unix//var/run/php/user1.socket {
split .php
index index.php
}
}

http://user2.example.com {
root * /home/user2/www
file_server
encode gzip

php_fastcgi unix//var/run/php/user2.socket {
split .php
index index.php
}
}

*Be careful of the syntax in the Caddyfile. The file must start with a “virtual host” block. If there is more than one virtual host, make sure they are all defined within their own section i.e. domain.com { definitions } After modifying your Caddyfile, you can check the syntax of the file with $ caddy validate /etc/caddy/Caddyfile

Now we need to set up our individual PHP-FPM pools.

/etc/php/7.4/fpm/pool.d/user1.conf

[user1]

 user = user1
 group = user1
 listen.owner = caddy
 listen.group = caddy
 listen.mode = 0660

 listen = /var/run/php/user1.socket
 pm = dynamic
 pm.max_children = 5
 pm.start_servers = 2
 pm.min_spare_servers = 1
 pm.max_spare_servers = 3

/etc/php/7.4/fpm/pool.d/user2.conf

[user2]

 user = user2
 group = user2
 listen.owner = caddy
 listen.group = caddy
 listen.mode = 0660

 listen = /var/run/php/user2.socket
 pm = dynamic
 pm.max_children = 5
 pm.start_servers = 2
 pm.min_spare_servers = 1
 pm.max_spare_servers = 3

Reload php-fpm and Caddy to load our changes

$ sudo systemctl reload php7.4-fpm
$ sudo systemctl restart caddy

Allow http and https through the firewall

$ sudo ufw allow http
$ sudo ufw allow https