Install Caddy with PHP & HTTPS using Let’s Encrypt on Ubuntu. In this guide you are going to learn how to install Caddy with PHP 8.3 and also configure HTTPs on Ubuntu 22.04.
Caddy is an open-source web server with automatic HTTPS written in Go language. It takes care of TLS certificate renewals, OCSP stapling, static file serving, reverse proxying, and more.
This tutorial is tested on Google Cloud and AWS, so it works fine on other cloud services like Azure, DigitalOcean or any VPS or any Dedicated servers running Ubuntu.
If you are on Google Cloud you should follow the below listed prerequisites.
Prerequisites for Google Cloud
- Your Compute Engine Instance running.
- For setting up Compute Engine, see the Setting up Compute Engine Instance.
- Set up Cloud DNS, see the Setting up Google Cloud DNS for your domain.
If you are on AWS you should follow these below listed prerequisites.
Prerequisites for AWS
- A running EC2 Instance. Learn how to create an AWS EC2 instance.
- Assigned a Elastic IP to your EC2 Instance.
- Setup and configure Route 53 and point your domain to AWS.
- Successful SSH connection to your EC2 Instance.
SSH to your EC2 Instance and perform the steps listed below.
Initial Server Setup
Let’s start by updating the local package index with the following command to the latest available version.
sudo apt update
sudo apt upgrade
Install all necessary packages to install Caddy.
sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https curl
Once the update and the necessary packages are installed you can start the installation of Caddy.
Install Caddy
Make sure you completed the above step and domain name pointed to your server for SSL installation.
Add the Caddy repository to the sources list
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
Update the packages using the apt update command.
sudo apt update
Now you can install Caddy.
sudo apt install caddy
Once the installation is completed you can check the version of Caddy using this command.
caddy -v
You will get an output similar to the one below.
v2.7.6 h1:w0NymbG2m9PcvKWsrXO6EEkY9Ru4FJK8uQbYcev1p3A=
You can also check the status of Caddy using the following command.
sudo service caddy status
You should see an output similar to the one below.
Output
● caddy.service - Caddy
Loaded: loaded (/lib/systemd/system/caddy.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2024-03-15 05:32:12 UTC; 3s ago
Docs: https://caddyserver.com/docs/
Main PID: 17580 (caddy)
Tasks: 8 (limit: 1129)
Memory: 8.9M
CPU: 69ms
CGroup: /system.slice/caddy.service
└─17580 /usr/bin/caddy run --environ --config /etc/caddy/Caddyfile
Install and configure PHP for Caddy
Add the ondrej/php
which has PHP 8.3 FPM package and other required PHP extensions.
sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update
Install PHP 8.3 and some common extensions.
sudo apt install php8.3-fpm php8.3-common php8.3-mysql php8.3-xml php8.3-xmlrpc php8.3-curl php8.3-gd php8.3-imagick php8.3-cli php8.3-dev php8.3-imap php8.3-mbstring php8.3-opcache php8.3-soap php8.3-zip php8.3-intl php8.3-bcmath unzip -y
Once PHP 8.3 FPM is installed you can configure your domain name with Caddy. Learn how to configure PHP options and advanced PHP-FPM configurations.
After installing PHP, edit the PHP-FPM configuration file and change the default user and group with caddy
sudo nano /etc/php/8.3/fpm/pool.d/www.conf
Find and replace user and group name from www-data to caddy:
user = caddy
group = caddy
listen.owner = caddy
listen.group = caddy
Save and close the file then restart the PHP-FPM service to apply the changes:
sudo service php8.3-fpm restart
Configure Domain in Caddy
Create a new directory for your website files and configure correct permissions.
sudo mkdir /var/www
sudo chown -R caddy:caddy /var/www
Edit the Caddy file Caddyfile
located inside /etc/caddy/
and configure your domain name with HTTPS
.
sudo nano /etc/caddy/Caddyfile
Comment out any previous configuration and copy the below configuration and paste it inside this file.
https://domain.com {
root * /var/www/
encode gzip zstd
php_fastcgi unix//run/php/php8.3-fpm.sock {
split .php
index index.php
}
}
Hit CTRL + X
followed by Y
and ENTER
to save and exit the file.
Restart/Start Caddy to have the changes available and Let’s Encrypt configured automatically.
sudo service caddy restart
If you get any error about Certificate Maintenance
or JSON parse
, you can try the following steps listed below. If you are fine you can skip it.
sudo rm -rf /etc/ssl/caddy*
Setup Test PHP file
Create a new file to output the installed PHP information.
sudo nano /var/www/index.php
Enter the following code inside it and save the file.
<?php phpinfo();
Verify the Caddy Setup with PHP and HTTPS
Once you have restarted Caddy and completed all the setups listed above you can check your domain in your web browser.
You should see the PHP information and your domain loaded with HTTPS.
Conclusion
Now you have learned how to install Caddy with PHP 8.3 and also configure HTTPS using Let’s Encrypt on Ubuntu.
Thanks for your time. If you face any problem or any feedback, please leave a comment below.
Leave your Reply