How to Install Caddy Web Server with PHP 8.1 on Ubuntu 22.04
This tutorial exists for these OS versions
- Ubuntu 22.04 (Jammy Jellyfish)
- Ubuntu 20.04 (Focal Fossa)
- Ubuntu 16.04 (Xenial Xerus)
On this page
Caddy is a modern web server built in the GO language. It is a simple, user-friendly, lightweight, and commercially supported web server. It supports HTTP/2 and experimental HTTP/3 protocols and runs anywhere with no external dependencies It can be expanded via plugins. Caddy is the only web server to use HTTPS automatically and by default. Caddy obtains and renews TLS certificates for your sites automatically. It is designed with security in mind and provides a number of features that are useful for hosting websites.
In this post, we will explain how to install the Caddy web server on Ubuntu 22.04.
Prerequisites
- A server running Ubuntu 22.04.
- A valid domain name is pointed to your server IP.
- A root password is configured on the server.
Install Caddy Web Server
By default, the Caddy package is not included in the Ubuntu 22.04 default repository. So you will need to add the Caddy repository to your system.
First, install all required dependencies using the following command:
apt-get install -y curl debian-keyring debian-archive-keyring apt-transport-https
Next, download and add the GPG key with the following command:
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
Next, add a Caddy repository to the APT using the following command:
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | tee /etc/apt/sources.list.d/caddy-stable.list
Next, update the repository and install the Caddy web server by running the following command:
apt-get update
apt-get install caddy -y
Once the Caddy has been installed, you can verify the Caddy version using the following command:
caddy version
You will get the following output:
v2.6.2 h1:wKoFIxpmOJLGl3QXoo6PNbYvGW4xLEgo32GPBEjWL8o=
Next, you will need to set some permission to allow the caddy binary to connect to privileged ports like 80 and 443. You can set it with the following command:
setcap 'cap_net_bind_service=+ep' /usr/bin/caddy
Now, open your web browser and access the Caddy default page using the URL http://your-server-ip. You should see the following page:
How to Start and Stop Caddy Service
You can manage the Caddy service via the systemd. You can start and stop it easily using the systemctl command.
To start the Caddy service, run the following command:
systemctl start caddy
To stop the Caddy service, run the following command:
systemctl stop caddy
To enable the Caddy service to start at system reboot, run the following command:
systemctl enable caddy
To check the status of the Caddy service, run the following command:
systemctl status caddy
You will get the following output:
? caddy.service - Caddy Loaded: loaded (/lib/systemd/system/caddy.service; enabled; vendor preset: enabled) Active: active (running) since Sun 2022-11-20 11:03:31 UTC; 42s ago Docs: https://caddyserver.com/docs/ Main PID: 2356 (caddy) Tasks: 8 (limit: 2242) Memory: 7.8M CPU: 31ms CGroup: /system.slice/caddy.service ??2356 /usr/bin/caddy run --environ --config /etc/caddy/Caddyfile Nov 20 11:03:31 ubuntu2204 caddy[2356]: {"level":"info","ts":1668942211.4061167,"msg":"using provided configuration","config_file":"/etc/cadd> Nov 20 11:03:31 ubuntu2204 caddy[2356]: {"level":"info","ts":1668942211.4079537,"logger":"admin","msg":"admin endpoint started","address":"lo> Nov 20 11:03:31 ubuntu2204 caddy[2356]: {"level":"warn","ts":1668942211.4082074,"logger":"http","msg":"server is listening only on the HTTP p> Nov 20 11:03:31 ubuntu2204 caddy[2356]: {"level":"info","ts":1668942211.4085367,"logger":"http.log","msg":"server running","name":"srv0","pro> Nov 20 11:03:31 ubuntu2204 caddy[2356]: {"level":"info","ts":1668942211.4088042,"msg":"autosaved config (load with --resume flag)","file":"/v> Nov 20 11:03:31 ubuntu2204 systemd[1]: Started Caddy. Nov 20 11:03:31 ubuntu2204 caddy[2356]: {"level":"info","ts":1668942211.419063,"logger":"tls.cache.maintenance","msg":"started background cer> Nov 20 11:03:31 ubuntu2204 caddy[2356]: {"level":"info","ts":1668942211.4192455,"logger":"tls","msg":"cleaning storage unit","description":"F> Nov 20 11:03:31 ubuntu2204 caddy[2356]: {"level":"info","ts":1668942211.419398,"logger":"tls","msg":"finished cleaning storage units"} Nov 20 11:03:31 ubuntu2204 caddy[2356]: {"level":"info","ts":1668942211.4197013,"msg":"serving initial configuration"}
Enable PHP Support in Caddy
First, install PHP and other necessary extensions using the following command:
apt-get install php-fpm php-mysql php-curl php-gd php-mbstring php-common php-xml php-xmlrpc -y
After installing PHP, edit the PHP-FPM configuration file and change the default user and group with caddy:
nano /etc/php/8.1/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:
systemctl restart php8.1-fpm
Create Caddy Virtual Host Configuration File
The Caddy default virtual host configuration file is located at /etc/caddy/Caddyfile.
Edit the /etc/caddy/Caddyfile file with the following command:
nano /etc/caddy/Caddyfile
Remove all lines and add the following lines:
caddy.example.com:80 { root * /usr/share/caddy/ encode gzip zstd php_fastcgi unix//run/php/php8.1-fpm.sock }
Save and close the file then restart the Caddy service to apply the changes:
systemctl restart caddy
Next, create a sample PHP file for Caddy with the following command:
nano /usr/share/caddy/info.php
Add the following lines:
<?php phpinfo(); ?>
Save and close the file when you are finished.
Verify Caddy PHP Support
Now, open your web browser and access the Caddy website using the URL http://caddy.example.com/info.php. You should see the PHP page on the following screen:
Conclusion
In this post, we explained how to install and configure the Caddy web server on Ubuntu 22.04. You can now start creating and hosting your own website using the Caddy web server. Feel free to ask me if you have any questions.