Install Caddy with PHP & HTTPS using Let’sEncrypt on Ubuntu. In this guide you are going to learn how to install Caddy with PHP 7.4 and also configure HTTPs on Ubuntu 18.04.
Caddy is a 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
Once the update is done you can start the installation of Caddy.
Install Caddy
Once you have your server setup and domain name pointed to your server you can proceed to install Caddy.
Execute the following commands to install Caddy.
curl https://getcaddy.com | sudo bash -s personal
Once the installation is completed you will get an output similar to the one below.
Output
Putting caddy in /usr/local/bin (may require password)
Caddy v1.0.4 (h1:wwuGSkUHo6RZ3oMpeTt7J09WBB87X5o+IZN4dKehcQE=)
Successfully installed
This output shows Caddy is installed in /usr/local/bin
.
You can check the version of Caddy installed using this command.
caddy -version
Configure Caddy
Now you need to allow Caddy binary to bind to ports 80
and 443
.
Setup directories for Caddy.
sudo mkdir /etc/caddy sudo mkdir /etc/ssl/caddy sudo mkdir /var/log/caddy
Configure correct permissions.
sudo chown -R root:root /etc/caddy sudo chown -R root:www-data /etc/ssl/caddy sudo chown -R root:www-data /var/log/caddy sudo chmod 0770 /etc/ssl/caddy
Configure Caddy Systemd service unit
Now you can create a systemd service file for Caddy which is available in the official repository and reload the demon for the changes to be available.
wget https://raw.githubusercontent.com/caddyserver/caddy/master/dist/init/linux-systemd/caddy.service sudo cp caddy.service /etc/systemd/system/ sudo chown root:root /etc/systemd/system/caddy.service sudo chmod 644 /etc/systemd/system/caddy.service sudo systemctl daemon-reload
Install PHP 7.4 FPM
Add the ondrej/php
which has PHP 7.4 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 7.4 and some common extensions.
sudo apt install php7.4-fpm php7.4-common php7.4-mysql php7.4-xml php7.4-xmlrpc php7.4-curl php7.4-gd php7.4-imagick php7.4-cli php7.4-dev php7.4-imap php7.4-mbstring php7.4-opcache php7.4-soap php7.4-zip php7.4-intl php7.4-bcmath unzip -y
Once PHP 7.4 FPM is installed you can configure your domain name with Caddy.
Configure Domain and Webroot in Caddy
Create a new directory for your website files and configure correct permissions.
sudo mkdir /var/www sudo chown www-data:www-data /var/www sudo nano /var/www/index.html
Create a Caddy file named Caddyfile
inside /etc/caddy/
and configure your domain name with HTTPS
.
sudo nano /etc/caddy/Caddyfile
Copy the below configuration and paste it inside this file.
https://domain.com { root /var/www/ log /var/log/caddy/domain.log tls on gzip fastcgi / /run/php/php7.4-fpm.sock { ext .php 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 start
If you have your Caddy server started before you can restart using the following command.
sudo service caddy restart
Now you can 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 HTTP/2 web server Loaded: loaded (/etc/systemd/system/caddy.service; disabled; vendor preset: enabled) Active: active (running) since Thu 2020-01-30 09:34:54 UTC; 3s ago Docs: https://caddyserver.com/docs Main PID: 24533 (caddy) Tasks: 6 (limit: 661) CGroup: /system.slice/caddy.service └─24533 /usr/local/bin/caddy -log stdout -log-timestamps=false -agree=true -conf=/etc/caddy/Caddyfile -root=/var/tmp
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
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.
Prepare yourself for a role working as an Information Technology Professional with Linux operating system
Conclusion
Now you have learned how to install Caddy with PHP 7.4 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.