Install Caddy Web Server on Ubuntu

Posted on July 16th, 2020

Caddy web server is also known as the Caddy, and it is a free, open-source HTTP/2-enabled web server written in Go language. The Caddy web server uses the Go standard library to perform its HTTP functionalities. By default, the Caddy web server enables HTTPS. The Caddy can also act as a load balancer and reverse proxy server. This web server can run on multiplatform, such as Windows, Linux, and OS X. This multiplatform web server also supports virtual hosting, WebSockets, IPV6, Markdown, FastCGI, templates, automatic HTTPS via Let’s Encrypt, and more. With the help of the Caddy web server, it is easy to transfer a website from Windows to Linux without any compatible issue.

Some of the main features of the Caddy web server are:

  1. The Caddy web server is self-contained.
  2. The Caddy does not have any external dependencies as it uses the Golang codes.
  3. The Caddy web server is secure and easy to use. It avoids the use of regular expressions.
  4. You can use plugins to enhance the features of the Caddy web server.
  5. The troubleshooting is easy with Caddy web servers as it writes all the error details in a log file.
  6. You can use the Caddy web server as a reverse proxy server or load balancer.
  7. You can also use this web server as a library in your Go program.
  8. The Caddy web server utilizes more CPU, and therefore, it is fast when compared to other web servers.

In this knowledge base, we learn how to install and configure the Caddy web server on Ubuntu 18.04 VPS server.

 

Prerequisites

  1. Server running Ubuntu 18.04 VPS.
  2. An SSH user with sudo privileges.

 

Install the Caddy Web Server on an Ubuntu 18.04 VPS

To install the Caddy web server on an Ubuntu server, follow the below steps:

First, log in to the server as an ssh user with sudo privileges.

Then, update and upgrade the apt packages by executing the following command.

$ sudo apt-get update

$ sudo apt-get upgrade

After that, run the following command to install cURL package.

$ sudo apt-get install curl

Run the following command to check if the installation of cURL is successful.

$ sudo curl --version

The above command should show an output similar to the one shown below if the installation was successful.

curl 7.29.0 (x86_64-redhat-linux-gnu) libcurl/7.29.0 NSS/3.15.4 zlib/1.2.7 libidn/1.28 libssh2/1.4.3

Protocols: dict file ftp http ftps https imap gopher imaps ldap ldaps pop3 pop3s rtsp scp sftp smtp smtps telnet tftp

Features: AsynchDNS GSS-Negotiate IDN IPv6 Largefile NTLM NTLM_WB SSL libz

Execute the following command to install the Caddy web server on your server.

$ sudo curl https://getcaddy.com | bash

To add any additional features to your Caddy web server, then you can use the ‘-s’ switch in the above command. These features should separate with a comma. For example, to install the Caddy web server with additional features, execute the following command.

$ sudo curl https://getcaddy.com | bash -s personal,realip,upload,expires

After that, run the following command to make sure that Caddy binds only to the ports less than 1024.

$ sudo setcap cap_net_bind_service=+ep /usr/local/bin/caddy

 

Configure the Caddy Web Server

First, we have to set up all the directories that are essential to store the configuration files and SSL certificates. Run the following commands to create and set permission for these files.

$ sudo mkdir /etc/caddy
$ sudo chown -R root:www-data /etc/caddy
$ sudo mkdir /etc/ssl/caddy
$ sudo chown -R www-data:root /etc/ssl/caddy
$ sudo chmod 770 /etc/ssl/caddy
$ sudo touch /etc/caddy/Caddyfile
$ sudo mkdir /var/www
$ sudo chown www-data: /var/www

Then, create a systemD configuration script by using the following steps:

Open the caddy.service file using any of the text editors.

$ sudo nano /lib/systemd/system/caddy.service

Add the following script to the caddy.service file by replacing the existing contents.

[Unit]
Description=Caddy HTTP/2 web server
Documentation=https://caddyserver.com/docs
After=network-online.target
Wants=network-online.target

[Service]
Restart=on-failure
StartLimitInterval=86400
StartLimitBurst=5

User=www-data
Group=www-data

; Letsencrypt-issued certificates will be written to here in this directory

Environment=CADDYPATH=/etc/ssl/caddy

ExecStart=/usr/local/bin/caddy -log stdout -agree=true -conf=/etc/caddy/Caddyfile -root=/var/tmp

ExecReload=/bin/kill -USR1 $MAINPID

LimitNOFILE=1048576

LimitNPROC=64

PrivateTmp=true

PrivateDevices=true

ProtectHome=true

ProtectSystem=full

ReadWriteDirectories=/etc/ssl/caddy

; The following additional security directives only work with systemd v229 or later.
; They further restrict privileges that can be gained by caddy. Uncomment if you want.
; You may need to add capabilities required by the plugins in use.
;CapabilityBoundingSet=CAP_NET_BIND_SERVICE
;AmbientCapabilities=CAP_NET_BIND_SERVICE
;NoNewPrivileges=true

[Install]

WantedBy=multi-user.target

Save and close the file.

Then, run the following systemctl command to start the Caddy web server as a service during system boot.

$ sudo systemctl enable caddy.service

 

Test the Caddy Web Server Installation

To test the Caddy installation, we are going to create a sample webpage and a Caddy file. For the same, follow the below steps:

To create a sample web page and set permission for the same, run the following commands.

$ sudo mkdir -p /var/www/example-domain.com
$ sudo echo “Caddy” > /var/www/example-domain.com/index.html
$ sudo chown -R www-data: /var/www/example-domain.com

Open the Caddyfile using any text editors.

$ sudo nano /etc/caddy/Caddyfile

Then, add the following line to add the domain to the file. After that, save and close the file.

example-domain.com {
    root /var/www/ ateamagencies.000webhostapp.com
}

Run the following command to restart the Caddy service.

$ sudo systemctl restart caddy.service

Now, you can hit the following URL from any of the browsers to view the test page.

https://example-domain.com

 

Conclusion

Caddy web server is also known as the Caddy, and it is a free, open-source HTTP/2-enabled web server written in Go language. It offers some significant advantages over the other web servers. Now, we have installed and configured Caddy on your Ubuntu VPS, we hope that this knowledge base was helpful to you. Please comment below for any questions or queries. If you are an InterServer customer, please reach out to our support team for further help.

Leave a Reply