How to create your own Caddy web server in Linux
Caddy is a modern, easy-to-use web server for Linux. It works by streamlining the process of creating website configuration files and SSL certificates.
Caddy is a modern, easy-to-use web server for Linux. It works by streamlining the process of creating website configuration files and SSL certificates. This article will guide you through the process of installing Caddy on an Ubuntu Linux server and how to use Caddy to deploy a simple website as well as an SSL reverse proxy.
Why should you use Caddy as a web server?
Although Nginx and Apache are powerful web server daemons, they can be difficult and complicated to use for new users. Caddy cuts that complexity by providing 'Caddyfile', a single flat file with a simple syntax that's easy to learn even for beginners.
Another attractive feature of Caddy is that it provides HTTPS to your web server instantly. This helps users who find setting up SSL for their website difficult and complicated. So, Caddy is the perfect choice if you are looking for a 'noisy' web server in Linux that is both easy to maintain and easy to use.
Install Caddy
The first step in installing Caddy on Ubuntu Linux is to ensure that you have the tools to import its repository information and keys:
sudo apt install curl debian-keyring debian-archive-keyring
Fetch the key repository for Caddy from the developer's website:
curl -fsSL 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
Download and save the Caddy project repository file to the 'sources.list.d' folder on your device:
curl -fsSL 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy.list
Reload your system's apt repository by running the following command:
sudo apt update && sudo apt upgrade
Install the Caddy package to your system using apt install:
sudo apt install caddy
Launch your first caddy site
To run your first website, create your website's root directory in your home directory:
mkdir ~/my-first-website && cd ~/my-first-website
Create an index.html file using your favorite text editor:
nano ./index.html
Paste the following block of code into your new HTML file:
Hello World!
Hello World!
Hello MakeTechEasier!
Save your index.html file, then run the following command:
caddy file-server --listen :8080
Confirm that your web server is working properly by navigating to it using your browser.
Create a website using Caddyfile
While the CLI tool is great for serving simple websites, Caddy also offers an easy-to-use 'Caddyfile' for more complex setups. To get started, create a new Caddyfile in '/etc/caddy' using your favorite text editor:
sudo rm /etc/caddy/Caddyfile && sudo nano /etc/caddy/Caddyfile
Paste the following block of code into your new Caddyfile:
your-domain.com { redir https://www.{host}{uri} } www.your-domain.com { root * /var/www/html file_server }
Note : You can also host a LAN-only website using Caddyfile by replacing 'your-domain.com' with 'localhost'.
Copy the index.html file from the home directory to the system's '/var/www' :
sudo mkdir /var/www/html/ sudo cp ~/my-first-website/index.html /var/www/html
Go to your DNS registry and make sure that your www and root subdomains have A or AAAA records pointing to your machine's IPv4 and IPv6 addresses.
Note : You do not need to adjust any DNS settings for LAN-only sites because Caddy will generate a self-signed certificate for that site.
Activate the integrated Caddy service on your device:
sudo systemctl enable --now caddy
Check if your site is working properly by navigating to your domain.
Create SSL Reverse Proxy with Caddy
Like Nginx and Apache, you can also use Caddy as a reverse proxy for internal services on your machine. To do this, open Caddyfile in your system:
sudo nano /etc/caddy/Caddyfile
Paste the following code block into your Caddyfile:
your-domain.com { reverse_proxy 127.0.0.1:LOCAL-PORT }
Replace 'LOCAL-PORT' with your web application port. In this case, the code will be replaced with 3001 to redirect all traffic to the Kuma Uptime server.
Save your Caddyfile, then reload the Caddy service to apply your new settings:
sudo systemctl reload caddy
Check if your reverse proxy is working properly by navigating to your domain using a web browser.
You should read it
- 14 interesting Linux commands in Terminal
- Compare the most popular Linux distributions today
- 7 best Linux distributions based on Red Hat
- Basic Linux commands everyone needs to know
- What's new in Linux Kernel 5.18?
- 6 reasons why the Linux operating system often fails
- 8 best Linux distros for pentest
- Why are Linux computers still important?
- 10 reasons to switch to Linux right in 2012
- What is Puppy Linux? Everything you need to know about Puppy Linux
- The 5 most awaited things in Linux in 2019
- How does the Linux Kernel work?
Maybe you are interested
Instructions for coding the game 'Snake of Prey' in Python Windows is now available for iPhone, iPad, Mac, and PC Steps to check iPad battery status quickly iPad How to fix Code 10 errors that prevent the device from booting How to pin a drive to the Taskbar to open it quickly Instructions to stamp photos, add watermark to images on Android phones