#development #laravel #linux #php #sysadmin

Using the Caddy web server with Laravel is really easy and straightforward.

I find it way easier to setup and manage than Nginx and CertBot). I initially installed Nginx using this bgui and this tutorial, but I didn't like it as the configuration was too complex and cumbersome.

Let's show how easy it can be to get it up and running with Caddy instead, so that we don't have to care about the renewal of certificates or complex configuration files.

So, I decided to replace Nginx with Caddy.

Just for the reference, we're running Ubuntu here. If you are using another distribution, you might want to check the Caddy installation guide for the proper install procedure.

I first stopped and disabled Nginx and certbot:

1$ sudo systemctl stop nginx
2$ sudo systemctl stop certbot
3$ sudo systemctl stop certbot.timer
4$ sudo systemctl disable nginx
5$ sudo systemctl disable certbot
6$ sudo systemctl disable certbot.timer

Let's first install Caddy itself as described in the documentation:

1$ sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https
2$ curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
3$ curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
4$ sudo apt update
5$ sudo apt install caddy

Next up is to edit the /etc/caddy/Caddyfile, adding the mysite.com website:

1mysite.com, www.mysite.com {
2        root * /var/www/mysite.com/public
3        encode zstd gzip
4        file_server
5        php_fastcgi unix//var/run/php/php8.1-fpm.sock
6}

As you can see, the config is really easy. Let's go over this line by line:

This first line indicates which hostnames we want to serve. We're serving both the domain with and without the www. prefix.

The root directive tells Caddy where the files for that site can be found. It's important to point to the public folder here.

The encode directive is used to compress the responses. In this case, we're enabling zstd and gzip compression.

The file_server directive sets up a static file server.

The php_fastcgi directive is a so-called "opiniated directive" that directs to a php-fpm FastCGI server. It's basically a shortcut for the following configuration:

 1route {
 2    # Add trailing slash for directory requests
 3    @canonicalPath {
 4        file {path}/index.php
 5        not path */
 6    }
 7    redir @canonicalPath {path}/ 308
 8
 9    # If the requested file does not exist, try index files
10    @indexFiles file {
11        try_files {path} {path}/index.php index.php
12        split_path .php
13    }
14    rewrite @indexFiles {http.matchers.file.relative}
15
16    # Proxy PHP files to the FastCGI responder
17    @phpFiles path *.php
18    reverse_proxy @phpFiles <php-fpm_gateway> {
19        transport fastcgi {
20            split .php
21        }
22    }
23}

Once that is done, all that is left is to restart Caddy:

1$ sudo systemctl restart caddy

You can check if it's up and running like using the status command:

1$ sudo systemctl status caddy

That's it, it's as easy as that. All the rest is done automatically. It will automatically generate the Let's Encrypt SSL certificate and serve the site.

You can automate it slightly more by using snippets as explained here.