Linux

How to Install Caddy Web Server on Ubuntu 22.04

How to Install Caddy Web Server on Ubuntu 22.04

 

Caddy is a modern web server that is easy to configure and comes with automatic HTTPS by default. It is an excellent choice for developers and system administrators who want to serve websites quickly and securely. In this guide, we will walk through the installation process of Caddy on Ubuntu 22.04, along with basic configuration and usage tips.

Why Choose Caddy?
Automatic HTTPS: Caddy automatically obtains and renews SSL/TLS certificates for your websites, making it incredibly easy to serve secure sites.
Simple Configuration: With its intuitive Caddyfile, configuring sites and reverse proxies is straightforward.
High Performance: Caddy is built on top of a high-performance Go runtime, making it fast and efficient.
Rich Ecosystem: Caddy supports plugins that can extend its functionality for various use cases.
Prerequisites
Before you begin, make sure you have:

An Ubuntu 22.04 server with sudo access.
A domain name pointing to your server (if you intend to use HTTPS).
Step 1: Update Your System
It is always a good idea to update your package lists and upgrade your installed packages before installing new software.

sudo apt update
sudo apt upgrade -y

Step 2: Install Caddy
Caddy can be installed via the official APT repository, which is the recommended method for installation. Follow these steps:

Step 2.1: Add the Caddy APT Repository
First, you need to install the necessary packages to allow APT to use HTTPS.

sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https

Next, import the Caddy GPG key:

curl -fsSL https://apt.caddyserver.com/api/gpg.key | sudo gpg –dearmor -o /usr/share/keyrings/caddy-archive-keyring.gpg

Now, add the Caddy repository to your sources list:

echo “deb [signed-by=/usr/share/keyrings/caddy-archive-keyring.gpg] https://apt.caddyserver.com/ubuntu/ focal main” | sudo tee /etc/apt/sources.list.d/caddy.list

Step 2.2: Install Caddy
After adding the repository, update your package lists again:

sudo apt update

Now, install Caddy:

sudo apt install caddy

Step 3: Verify the Installation
To verify that Caddy was installed successfully, check the version:

caddy version

You should see the version of Caddy displayed in the terminal.

Step 4: Configure Caddy
Caddy’s configuration is managed through a simple file called the Caddyfile. By default, it is located at /etc/caddy/Caddyfile.

Step 4.1: Edit the Caddyfile
Open the Caddyfile in your preferred text editor:

sudo nano /etc/caddy/Caddyfile

A basic configuration for a single domain would look like this:

your-domain.com {
root * /var/www/html
file_server
}

Replace your-domain.com with your actual domain name, and adjust the document root (/var/www/html) to point to the directory where your website files are stored.

Step 4.2: Create the Document Root Directory
If you haven’t already created the directory for your website, do so now:

sudo mkdir -p /var/www/html

You can add a simple HTML file to test:

echo “

 

Hello, Caddy!

” | sudo tee /var/www/html/index.html

Step 5: Start and Enable Caddy
Caddy is installed as a systemd service, making it easy to manage.

Step 5.1: Start Caddy
You can start Caddy with the following command:

sudo systemctl start caddy

Step 5.2: Enable Caddy to Start at Boot
To ensure that Caddy starts automatically when the server boots, enable the service:

sudo systemctl enable caddy

Step 6: Check Caddy Status
To check the status of the Caddy service, run:

sudo systemctl status caddy

You should see that the service is active (running).

Step 7: Configure Firewall (if applicable)
If you have a firewall enabled, you need to allow HTTP and HTTPS traffic. Use the following commands to enable these services in UFW (Uncomplicated Firewall):

sudo ufw allow ‘Nginx Full’

Step 8: Test Your Installation
Now, open a web browser and navigate to your domain (or your server’s IP address). You should see the “Hello, Caddy!” message displayed, indicating that Caddy is serving your website correctly.

Conclusion

Congratulations! You have successfully installed and configured Caddy on Ubuntu 22.04. With its automatic HTTPS feature, Caddy simplifies the process of serving secure websites. You can further explore the capabilities of Caddy by reading the official documentation and discovering its rich set of plugins.

For more information and advanced configurations, visit the official Caddy documentation.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button