How to Install Caddy on NixOS Latest

Caddy is a web server that is easy to use and supports HTTPS by default. It is a great server for hosting static websites, APIs, and dynamic applications. This tutorial will guide you through the process of installing Caddy on NixOS Latest.

Prerequisites

Before you can install Caddy, you need to have the following:

Installation Steps

Follow these steps to install Caddy on NixOS Latest:

Step 1: Update your system

Before installing Caddy, you need to update your system to ensure that all packages are up-to-date.

sudo nix-channel --update
sudo nixos-rebuild switch

Step 2: Install Caddy

You can install Caddy on NixOS Latest using the following command:

sudo nix-env -iA nixpkgs.caddy

Step 3: Run Caddy

To run Caddy, you need to create a Caddyfile that defines your site configuration. For example, to serve a static site on your domain name, create a file named Caddyfile with the following content:

example.com {
    root /var/www
}

This Caddyfile tells Caddy to serve files from the /var/www directory when a request is made to example.com. Replace example.com with your own domain name.

Start Caddy using the following command:

sudo caddy start

Step 4: Test Caddy

To test if Caddy is running correctly, visit your domain name or public IP address in a web browser. You should see your static site being served by Caddy.

Step 5: Configure Caddy to Run on System Startup

To ensure that Caddy runs on system startup, you need to create a systemd unit file.

Create a file named caddy.service in the /etc/systemd/system/ directory with the following content:

[Unit]
Description=Caddy HTTP/2 web server
After=network.target

[Service]
User=root
Group=root
ExecStart=/usr/bin/caddy run --environ --config /etc/caddy/Caddyfile
ExecReload=/usr/bin/caddy reload --config /etc/caddy/Caddyfile
TimeoutStopSec=5s
LimitNOFILE=1048576
LimitNPROC=512
PrivateTmp=true
ProtectSystem=full
AmbientCapabilities=CAP_NET_BIND_SERVICE

[Install]
WantedBy=multi-user.target

This systemd unit file specifies that Caddy should run as the root user and group, and the Caddyfile should be located in the /etc/caddy/ directory.

Enable and start the systemd unit using the following command:

sudo systemctl enable caddy.service
sudo systemctl start caddy.service

Conclusion

That's it! You now have Caddy installed and running on NixOS Latest. With Caddy, you can easily serve static sites, APIs, and dynamic applications over HTTPS.

If you want to self-host in an easy, hands free way, need an external IP address, or simply want your data in your own hands, give IPv6.rs a try!