How to Install Caddy Web Server to Host a Website on CentOS 7

Caddy is one of the webserver. It is simple enough to be used as a quick development server and strong enough to be used in productions.

Basic Features of Caddy :
An easy, intuitive way to configure your site. It’s not scripting, and not hard to memorize.
Caddy can be extended with plugins. All server types, directives, DNS providers, and more features are plugins! They’re easy to write and get compiled in directly.
When the going gets tough, Caddy gets going on more CPUs. Go’s scheduler understands Go code, and goroutines are more lightweight than system threads. So yeah, it’s fast.
Caddy can be configured to run system commands at startup and shutdown. Useful when your site requires other processes running.
Caddy can write a log of all its significant events, especially errors. Log to a file, stdout/stderr, or a local or remote system log!

Step 1 -> Installing the Caddy on CentOS :

Type and execute the above command to install caddy server’s binary files.

The command output will look like this:

Downloading Caddy for linux/amd64…
Putting caddy in /usr/local/bin (may require password)
[sudo] password for sammy:
Caddy 0.10.2
Successfully installed

To verify the caddy binary files location, use the below command

$ which caddy

Steps 2 -> Creating User and Group for Caddy

To create user named caddy let’s type:

$ sudo adduser -r -d /var/www -s /sbin/nologin caddy

Here /var/www is the home directory for the user caddy.

Step 3 -> Configuring Directories for Caddy’s files

First, create a directory that will house the main Caddyfile, which is a configuration file that tells Caddy what websites should it serve and how.

First, create a directory for the main Caddyfile, which is a configuration file that tells Caddy what websites should it serve and how.

sudo mkdir /etc/caddy

Change the owner of this directory to the root user and its group to www-data so Caddy can read it.

sudo chown -R root:caddy /etc/caddy

In this directory, create an empty Caddyfile which we’ll edit later.

sudo touch /etc/caddy/Caddyfile

Create another directory in /etc/ssl to store the SSL private keys and certificates that it automatically obtains from Let’s Encrypt.

sudo mkdir /etc/ssl/caddy

Caddy needs to be able to write to this directory when it obtains the certificate, so make the owner the caddy user

sudo chown -R caddy:root /etc/ssl/caddy

Then make sure no one else can read those files by removing all the access rights for others.

sudo chmod 0770 /etc/ssl/caddy

Make a directory that should be completely owned by caddy.

sudo mkdir /var/www

sudo chown caddy:caddy /var/www

Step 4 -> Configuring Caddy as a System Service

While Caddy does not install itself as a service, the project provides an official systemd unit file. This file does assume the directory structure we set up in the previous step, so make sure your configuration matches.

Download the file from the official Caddy repository. The additional -o parameter to the curl command will save the file in the /etc/systemd/system/ directory and make it visible to systemd.


$ sudo curl -s -o /etc/systemd/system/caddy.service


Let’s open the file with vi or your favourite text editor to modify the file slightly to make it use our unprivileged caddy user to run the server.

$ sudo vi /etc/systemd/system/caddy.service

; User and group the process will run as.

Find the above lines and Change both values to caddy as follows:

; User and group the process will run as.

Save and close the file to exit.

To make systemd aware of the new service file.

$ sudo systemctl daemon-reload

Enable Caddy to run on boot.

$ sudo systemctl enable caddy.service

To verify that the service has been properly loaded and enabled to start on boot by checking its status.

$ sudo systemctl status caddy.service

The output should look as follows:

‚óŹ caddy.service – Caddy HTTP/2 web server
Loaded: loaded (/etc/systemd/system/caddy.service; enabled; vendor preset: disabled)
Active: inactive (dead)

You have now configured Caddy as a system service which will start automatically on boot without the need to run it manually.

Step 5 -> Host a Test Website

This command will create an index.html file in the website directory we created earlier with just the one line of text, <h1>CADDY WEBSITE</h1>, inside.

echo ‘<h1>CADDY WEBSITE</h1>’ | sudo tee /var/www/index.html

Open the Caddyfile you created in Step 2 using vi or your favorite text editor.

http:// {
root /var/www

Then save the file and exit. Let’s explain what this specific Caddyfile does.

Once the configuration file is ready, start the Caddy service.

$ sudo systemctl start caddy

First, replace the address definition of http:// with your domain. This removes the insecure connection forced by HTTP and provides a domain name for the TLS certificate. Second, provide Caddy with an email address using the tls directive inside the server block.

The modified Caddyfile should look as follows, with your domain and email address substituted in: {
root /var/www

Save the file and exit the editor. To apply the changes, restart Caddy.












View all posts by

Leave a Reply

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