How to Install & Configure the Caddy web server on an Ubuntu 18.04 VPS

How to Install & Configure the Caddy web server on an Ubuntu 18.04 VPS

Caddy web server is a free open source web server written in go language. It is mostly known for its HTTP functionality and enabling HTPPS by default. It can run on most common systems including Windows, OS X and Linux.

In this how-to article, we cover the steps of how to install Caddy web server on Ubuntu 18.04 VPS.

Overview

It means that you can move your site from Windows to Linux without worrying about compatibility issues.

Some of its features include;

  • It is self-contained hence no compilation is needed – since it’s written in the go language, it has no external dependencies.
  • Easy to use – it avoids using regular expressions since they are a bit tricky.
  • Secure by default
  • Use of plugins can extend the features offered by caddy.
  • It writes all its errors in a log file, thus enabling you to troubleshoot quickly.
  • Caddy can be used as a reverse proxy or a library in your Go program.
  • Caddy is fast; it can utilise more CPUs.

Prerequisite

  • VPS server running Ubuntu 18.04
  • SSH client

How to Install & Configure the Caddy web server on an Ubuntu 18.04 VPS

SSH to VPS server

Log in to your VPS via SSH

by using the following.

$ ssh user@vps_IP

Update packages

Update the packages by running the following commands in the terminal.

$ sudo apt-get update && apt-get -y upgrade
$ sudo apt-get install curl

Install Caddy web server

Then to install the caddy web server run the following command;

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

You can also install caddy with some additional features, use the –s switch with a comma

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

Then we can now add cap_net_bind_servicecapability for caddy to bind to ports less than 1024

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

Configure Caddy

We now have to set up the directories that will be used to store the caddy configuration files and SSL certificates. Therefore, run the following commands.

$ 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 0770 /etc/ssl/caddy
$ sudo touch /etc/caddy/Caddyfile
$ sudo mkdir /var/www
$ sudo chown www-data: /var/www

After that, we need to create a systemD configuration script, therefore, run the following;

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

Then add the following to the caddy service file;

[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 the file by pressing control key + X then overwriting the original caddy file. Then, to enable caddy to run on boot, execute the following commands;

sudo systemctl enable caddy.service

Testing the installation

We are going to create a small webpage and a caddyfile. Therefore, type the following in your shell.

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

Then to add our domain to the caddy file, type the following;

$ sudo nano /etc/caddy/Caddyfile

Then add the following;

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

Then save the file, close the editor and restart caddy.

$ sudo systemctl restart caddy.service

Now, using your browser, go to the https:// ateamagencies.000webhostapp.com the test page we just created.

Conclusion

While caddy isn’t truly unique, it does offer significant advantages over other web servers.

These among other features make caddy an excellent alternative to common web servers. Now that we have installed caddy on our system, we hope you enjoy these features.

Check out these top 3 Linux hosting services

FastComet
$1.79 /mo
Starting price
Visit FastComet
Rating based on expert review
  • User Friendly
    4.7
  • Support
    5.0
  • Features
    4.8
  • Reliability
    4.5
  • Pricing
    5.0
Kamatera
$4.00 /mo
Starting price
Visit Kamatera
Rating based on expert review
  • User Friendly
    3.5
  • Support
    3.0
  • Features
    3.9
  • Reliability
    4.0
  • Pricing
    4.3
HostArmada
$2.49 /mo
Starting price
Visit HostArmada
Rating based on expert review
  • User Friendly
    4.5
  • Support
    4.5
  • Features
    4.5
  • Reliability
    4.5
  • Pricing
    4.0
  • Check the recommendations for the best VPS and get a suitable one.

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

Caddy is the only new server’s that secure by default. It’s growing in popularit
2 min read
Max Ostryzhko
Max Ostryzhko
Senior Web Developer, HostAdvice CTO

How to Set Up SSH for your Ubuntu 18.04 VPS or Dedicated Server

This how-to article shows you how to create a public and a private key for acces
4 min read
Bruno Mirchevski
Bruno Mirchevski
Hosting Expert

How to Install a Self-Signed SSL Certificate on Your Ubuntu 18.04 VPS or Dedicated Server

This how-to article will teach you how to create a self-signed SSL certificate o
3 min read
Michael Levanduski
Michael Levanduski
Expert Hosting Writer & Tester

How to Set Up SSH for your Ubuntu 18.04 VPS or Dedicated Server

This how-to article shows you how to create a public and a private key for acces
3 min read
Michael Levanduski
Michael Levanduski
Expert Hosting Writer & Tester
HostAdvice.com provides professional web hosting reviews fully independent of any other entity. Our reviews are unbiased, honest, and apply the same evaluation standards to all those reviewed. While monetary compensation is received from a few of the companies listed on this site, compensation of services and products have no influence on the direction or conclusions of our reviews. Nor does the compensation influence our rankings for certain host companies. This compensation covers account purchasing costs, testing costs and royalties paid to reviewers.
Click to go to the top of the page
Go To Top