Caddy Configuration for Typecho — Revisited
I decided to stop using pre-built Docker images and instead manually configure PHP + Caddy + Typecho.
It seems the very first post on this blog showed how to set up Caddy, but at that time I used someone else’s Docker image which bundled nginx, PHP, and Typecho, and I simply reverse-proxied Caddy to that port.
Now I rented a server on Alibaba Cloud with only 512MB RAM, which is a bit tight. To avoid the extra memory overhead of nginx and Docker, I decided to hand-build the Typecho environment.
Install the world’s best programming language
Add the Sury PPA repository
First, add the PPA that contains the latest PHP packages. You need to install some prerequisite packages.
sudo apt update
sudo apt install lsb-release apt-transport-https ca-certificates software-properties-common -ybashAfter installing the tools, import the Sury GPG key. Sury provides almost every PHP version. Typecho requires PHP > 7.4, so we’ll install 8.2.
sudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpgbashThen add the repository to your sources list.
sudo sh -c 'echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list'bashUpdate the package list to verify it’s working.
sudo apt updatebashInstall PHP 8.2 packages
Install PHP 8.2 and common extensions.
sudo apt install php8.2 php8.2-cli php8.2-fpm php8.2-mysql php8.2-curl php8.2-gd php8.2-mbstring php8.2-xml php8.2-zip php8.2-opcache php8.2-sqlite3 -ybashInstall Caddy v2
I found many guides using Caddy v1 plus special rewrite rules, but one important upgrade in Caddy v2 is that you don’t need extra rewrite rules for typical setups — v2 is simply more convenient. Don’t try to force old patterns.
Caddy provides an official script; this is what I recommend:
sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
sudo apt update
sudo apt install caddybashIf you need extra plugins (for example, DNS providers), you can use xcaddy to build your own binary, but that’s out of scope for this post.
Configure the Caddyfile
Again: I prefer you learn to use the Caddyfile rather than the JSON config.
The following Caddyfile has been tested — paste and use it as-is:
YOUR WEBSITE {
encode gzip
log
tls YOUR EMAIL
header Strict-Transport-Security max-age=31536000
root * /var/www/YOUR WEBSITE
php_fastcgi unix//run/php/php8.2-fpm.sock
file_server
}bashDo you now appreciate Caddy v2’s convenience? You don’t need to configure php-fpm details or rewrite rules — it’s basically ready out of the box.
Everything is self-explanatory, but remember to replace YOUR WEBSITE and YOUR EMAIL with your actual domain and email address. If you want to read more about Caddyfile options, check the official documentation ↗, “it is amazingly easy to read” (my peer who studies Medical Sciences has said so).
Final step: add your Typecho site files
Download the latest Typecho release with wget:
wget https://github.com/typecho/typecho/releases/latest/download/typecho.zipbashUnzip Typecho into /var/www:
-
Make sure /var/www exists:
bashsudo mkdir -p /var/wwwThen create your site directory; for example, if your site is 20051110.xyz, create:
bashsudo mkdir /var/www/20051110.xyz -
Unzip typecho.zip into /var/www/your-site-directory:
bashsudo cd /var/www/your-site-directory sudo unzip /root/typecho.zip # remember to replace with the actual download location of Typecho
Final step: change file ownership of /var/www and its subdirectories to the www-data user and group (the user typically used by web servers):
sudo chown -R www-data:www-data /var/wwwbashYou can check that your directory structure looks like this:
/var/www/your-site
├── admin/
├── install/
├── usr/
├── var/
├── index.php
├── install.php
└── ...bashIf everything is correct, start Caddy:
caddy run --config=Caddyfile # I ran this because my Caddyfile is in the same directorybashCaddy will automatically obtain certificates for you. Once that’s done, visit the site and proceed with the Typecho installation (don’t worry — it’s a GUI, just click through).
Thanks for reading! Hope this post helps.