This tutorial covers step by step instructions to install Caddy web server on Ubuntu, configuring it with a Caddyfile, and setting up a basic website, focusing on features like automatic HTTPS and simple commands for beginners.
Caddy is an open-source web server written in Go. It's known for its simplicity and ease of use, and it's particularly famous for its automatic HTTPS support. Here are some of its key features:
- Automatic HTTPS: Caddy automatically obtains and renews SSL/TLS certificates from Let's Encrypt, enabling secure, encrypted HTTP connections without manual intervention.
- Simplicity: Caddy is designed with simplicity in mind, making it easy to configure and deploy.
- Performance: Written in Go, Caddy is fast and efficient, suitable for both small and large-scale web applications.
- Extensibility: It supports various plugins, allowing additional functionality like webmail, Git support, and more.
- Modern Protocol Support: Caddy supports modern protocols like HTTP/2 and QUIC, offering improved performance over traditional HTTP.
Steps to install Caddy Web Server on Ubuntu
1. Update Package List
Before installing Caddy, it's good practice to update your system. This ensures you have the latest security patches and software versions. Use the following apt command with sudo privilege:
sudo apt update
This command updates the list of available packages and their versions, but it does not install or upgrade any packages.
2. Install Required Keyrings and HTTPS Transport
The debian-keyring
and debian-archive-keyring
packages contain the archive keys of the Debian archive, ensuring the authenticity of packages downloaded from Debian repositories. The apt-transport-https
package allows the APT package manager to retrieve packages over HTTPS, ensuring secure communication.
sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https
3. Add Caddy's GPG Key
Next we need to download and add the GPG Key required to add and access the repository which will add in next step.
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
: Downloads data from a URL.-1sLf
flags ensure the operation follows redirects (L
), fails silently on server errors (f
), and uses secure connections (1s
).
- URL
'https://dl.cloudsmith.io/public/caddy/stable/gpg.key'
: This is the address where the GPG key for the Caddy repository is located. |
: Pipes the downloaded key to the next command.sudo gpg --dearmor
: Converts the downloaded GPG key into a format that APT can understand.-o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
: Specifies the output file for the converted key.
4. Add Caddy Repository to APT Sources
This command will add Caddy's official repository to your system. This ensures you get the latest version and updates.
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
curl -1sLf
: Similar to the previous step, this command downloads the repository source list.- URL
'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt'
: Contains the APT source list for the Caddy repository. | sudo tee
: This command is used to write the output of the previous command to a file while also displaying it./etc/apt/sources.list.d/caddy-stable.list
: The file where the Caddy repository source list is stored.
5. Install Caddy
Now, update the package list and install Caddy:
sudo apt update sudo apt install caddy
This will install the Caddy web server on your system.
6. Verify Installation
To check if Caddy is installed correctly, you can run:
$ caddy version v2.7.6 h1:w0NymbG2m9PcvKWsrXO6EEkY9Ru4FJK8uQbYcev1p3A=
The installation of Caddy Web Server is now complete. You have successfully downloaded and installed Caddy, and it is ready to be configured for hosting websites
7. Allow HTTP and HTTPS Connections in Firewall
To enable Caddy to serve websites using HTTP and HTTPS protocols, you must grant access to ports 80 and 443. Execute the following commands to allow the required ports in UFW firewall which is the default firewall in Ubuntu:
sudo ufw allow proto tcp from any to any port 80,443
8. Enable and start Caddy Web Server Service
Now that Caddy is installed on your Ubuntu server, the next step is to start the Caddy service. We will enable the service to auto start on reboot and also start the service for the current session using systemctl command:
sudo systemctl enable caddy --now
The enable
argument will enable the service to auto start during reboot event while --now
will start the service for current session.
9. Verify Service Status
Check the caddy web server service status to make sure it un running properly:
sudo systemctl status caddy
10. Access the Caddy Welcome Page (Optional)
Caddy provides a simple welcome page that you can use to verify its initial functionality. Open your web browser and navigate to `http://localhost ` or `http://your-server-ip `. If you see the Caddy welcome page, it means Caddy is running successfully.
Caddy is now up and running on your Ubuntu server. It's actively listening for incoming requests and ready to serve content. With the service started, you can proceed to configure Caddy for hosting your website and leveraging its advanced features.
Continue to the next steps to configure Caddy using the Caddyfile, deploy your first website, and explore the rich capabilities that Caddy offers in the realm of web hosting.
Steps to Set Up a Website with Caddy Web Server
Setting up a website with Caddy is an elegant and straightforward process. In this section, I'll take you through each step, providing examples and code to help you understand the process. By the end, you will have a basic website running on your Ubuntu server using Caddy.
1. Create the Website Directory
First, you need to create a directory where your website's files will reside. Replace mywebsite
with your desired directory name.
sudo mkdir -p /var/www/mywebsite
Set Proper Permissions
sudo chown -R www-data:www-data /var/www/mywebsite
This command changes the ownership of the directory to the web server user, www-data
, ensuring Caddy can access and serve the files.
2. Adding Content to Your Website
Now let’s create a basic HTML page to serve as your website’s homepage.
sudo vim /var/www/mywebsite/index.html
In the vim editor, write a basic HTML structure. For example
<!DOCTYPE html> <html> <head> <title>My First Caddy Site</title> </head> <body> <h1>Welcome to My Caddy-Powered Site!</h1> <p>This is a basic HTML page served by Caddy.</p> </body> </html>
3. Configuring Caddy to Serve the Website
Caddy uses a configuration file called Caddyfile
to manage website settings.
sudo nano /etc/caddy/Caddyfile
In the Caddyfile, you’ll define how Caddy should serve your site. A simple configuration for a basic site looks like this:
mywebsite.com { root * /var/www/mywebsite file_server }
Replace mywebsite.com
with your domain. If you don’t have a domain, you can use your server’s IP address.
If you have an application running on a different port that you want to serve through Caddy, you can use the reverse_proxy
directive.
reverse_proxy localhost:8080
If you're running a PHP site, you can use the php_fastcgi
directive.
php_fastcgi localhost:9000
To apply the new configuration, restart Caddy:
sudo systemctl reload caddy
4. Testing Your Website
Now, it's time to see your website in action.
- Open a web browser.
- Navigate to
http://mywebsite.com
orhttp://<your_server_ip>
.
5. Troubleshooting Issues
- Verify DNS Settings:
- Make sure your domain's DNS records (A and/or AAAA records) correctly point to the public IP address of the server where Caddy is running.
- This change might take some time to propagate (up to 48 hours in some cases).
- Check Domain Status:
- Ensure your domain is not parked and is active.
- If you recently purchased or transferred the domain, wait until the domain becomes fully operational.
- Retry Certificate Generation:
- Once you're sure the DNS settings are correct, restart Caddy to trigger the SSL certificate request process again
sudo systemctl restart caddy
- Monitor Logs for New Errors:
- Check the logs again after restarting Caddy for any new error message
journalctl -u caddy --since "10 minutes ago"
journalctl -u caddy --since today
You have successfully set up a basic website with Caddy on Ubuntu. This setup is ideal for personal projects, portfolios, and small websites. As you become more familiar with Caddy, you can explore its extensive documentation to leverage its full potential.
Caddy's official documentation is an excellent resource for deepening your knowledge and understanding of its advanced capabilities.