Install & Configure the Caddy web server on a CentOS 7 VPS

Caddy is an open source web server that is quite popular with the developer community. Caddy is secure by default! Yes you heard that right, it uses HTTPS by default thus it preserves your privacy as well as prevents man in the middle (MITM) attacks.

In this how-to article, we shall show you how to install Caddy web server on CentOS 7 VPS.

Overview

With Caddy’s excellent features described below,am sure you want to switch your web server.

  • Caddy’s easy to use interface- whether you are a seasoned professional or a fresh beginner, Caddy always has something for you.
  • Command line interface – Caddy can efficiently run from its own CLI. It's cross-platform hence no worries when using different systems. The commands can be configured to run on startup or shutdown.
  • Plugins- if you want more control over Caddy, you can download as many plugins as you wish. The good thing is that Caddy plugins are easier to write.
  • Logs – Caddy stores its log files systematically hence helping you whenever you want to troubleshoot errors. If your log files become big, Caddy will automatically rotate them so that it can conserve space.
  • Custom builds- with Caddy when downloading, you can choose which plugins you want, and your custom build will be made for you in just a few seconds.
  • Speed- did I mention that Caddy is fast? Yes, it is, with its technology, it makes use of idle CPU cores whenever more power is needed.

Prerequisites

  • A VPS server running latest version of CentOS;CentOS 7.
  • An SSH client to connect to the VPS.

How to Install & Configure the Caddy web server on a CentOS 7 VPS

Update packages in CentOS 7

We have to make sure that we update all available packages on our server. Therefore, type the following command.

$ sudo yum clean all
$ sudo yum -y update

Install Caddy

Next, we are going to install Caddy server using the following command;

$ sudo curl https://getcaddy.com | bash -s personal

That installs the Caddy server. To verify the installation type the following.

$ which caddy

Create user

Now the next step includes us creating a user named Caddy. So in your shell, type the following;

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

Next, since Caddy needs a new directory for hosting Caddy files, let's create one. So type the following;

$ sudo mkdir /etc/caddy

Now to change the user to the root user, type;

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

Install Caddy as a service

Next, we have to install Caddy as a System Service. Therefore type the following command.

$ sudo  curl -s https://raw.githubusercontent.com/mholt/caddy/master/dist/init/linux-
systemd/caddy.service -o /etc/systemd/system/caddy.service

Then open the Caddy service file

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

and make the following changes.

User=caddy
Group=caddy

Now save the file and exit vim then restart Caddy service to enable the changes.

$ sudo systemctl daemon-reload
$ sudo systemctl enable caddy.service
$ sudo systemctl status caddy.service

Test Caddy web server

Our next step will be to make a small test web page for Caddy. The test web page is to help us confirm the installation. So go ahead and create the following.

# /var/www/index.html
<h1>Hello World!</h1>

Next up, open the Caddy file and add the following code to it.

$ sudo  vi /etc/caddy/Caddyfile


http:// {
root /var/www
gzip
}

The above code will help Caddy to bind the HTTP port 80.

Start and enable the Caddy service.

$ sudo systemctl start caddy
$ sudo systemctl enable caddy

Then add both HTTP and HTTPS service to the server firewall.

$ firewall-cmd -- permanent -- zone=public -- add-service=http
$ firewall-cmd -- permanent -- zone=public -- add-service=https
$ firewall-cmd -reload

Conclusion

You now have the world's first secure server at your disposal. If CentOS is your operating system get started with using Caddy web server. Enjoy Caddy's excellent and secure technology.

 

Check out these top 3 VPS services:

Was this article helpful?