Share your love
Install Caddy Web Server on Ubuntu 22.04
In this tutorial, we want to teach you How To Install Caddy Web Server on Ubuntu 22.04.
Caddy is a unique web server with a modern feature set. Think Nginx or Apache, but written in Go. With Caddy, you can serve your websites over HTTP/2. It can act as a reverse proxy and load balancer. Front your PHP apps with it. You can even deploy your site with git push.
How To Install Caddy Web Server on Ubuntu 22.04
To complete this guide, you must log in to your server as a non-root user with sudo privileges and set up a basic firewall. To do this, you can follow our guide the Initial Server Setup with Ubuntu 22.04.
Install Caddy on Ubuntu 22.04
By default, Caddy is not available in the default Ubuntu repository. So you need to add the Caddy repository on Ubuntu 22.04 with the commands below:
# 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
Then, update your local package index with the following command:
sudo apt update
Now use the command below to install Caddy on Ubuntu 22.04:
sudo apt install caddy
Here you can verify your Caddy installation with the following command:
sudo systemctl status caddy
In your output you will see:
Output caddy.service - Caddy Loaded: loaded (/lib/systemd/system/caddy.service; enabled; vendor preset:> Active: active (running) since Sun 2022-08-13 11:07:46 CET; 27s ago Docs: https://caddyserver.com/docs/ Main PID: 3308 (caddy) Tasks: 8 (limit: 2282) Memory: 7.1M CGroup: /system.slice/caddy.service └─3308 /usr/bin/caddy run --environ --config /etc/caddy/Caddyfile
Caddy serves websites using HTTP and HTTPS protocols, so you need to allow access to ports 80, and 443.
sudo ufw allow proto tcp from any to any port 80,443
Now you can type your server’s IP address in your web browser to see that your Caddy web server is installed on your server correctly.
http://server-IP-address
You will see the following page:
Enable PHP for Caddy Web server on Ubuntu 22.04
It is normal for a web server to have PHP support, at least.
First, install PHP and the required packages on Ubuntu 22.04 with the command below:
sudo apt install php-fpm php-mysql php-curl php-gd php-mbstring php-common php-xml php-xmlrpc -y
Next, you need to edit the PHP-FPM configuration file. Open the file with your favorite text editor, here we use vi:
sudo vi /etc/php/8.1/fpm/pool.d/www.conf
At the file, find the lines below and change them to Caddy as shown below:
user = caddy group = caddy listen.owner = caddy listen.group = caddy
When you are done, save and close the file.
To apply the changes, restart the PHP-FPM service:
sudo systemctl restart php8.1-fpm
Here you need to make some configuration changes to the Caddy web server main configuration file. Open the file with your favorite text editor, here we use vi:
sudo vi /etc/caddy/Caddyfile
By default, the file is very basic. Most of the file is commented on. Comment the few lines that are not by adding the # at the beginning of the lines and adding the following content to the file. Just remember to replace the domain with your domain name.
your-domain-name:80 {
root * /usr/share/caddy/
encode gzip zstd
php_fastcgi unix//run/php/php8.1-fpm.sock
}
When you are done, save and close the file.
To apply the changes, restart the Caddy web server with the command below:
sudo systemctl restart caddy
Then, create a sample PHP file for Caddy with the following command:
sudo vi /usr/share/caddy/info.php
Add the following content to the file:
<?php phpinfo(); ?>
When you are done, save and close the file.
Here you can access the Caddy website by typing your domain name in your web browser followed by info.php:
http://your-domain-name/info.php
When you have finished reading your PHP info, it’s better to remove your PHP file for more security.
sudo rm -rf /usr/share/caddy/info.php
You can always rebuild your file every time you need it.
For more information, you can visit the Caddy Documentation page.
Conclusion
At this point, you learn to Install Caddy Web Server on Ubuntu 22.04.
Hope you enjoy it.
You may be interested in these articles:
How To Install XAMPP on Ubuntu 22.04
Install and Configure Apache Tomcat on Ubuntu 22.04