How to install Caddy with HTTPS&PHP on Ubuntu VPS

A web server is a piece of software that accepts a network request from a user agent, typically a web browser, and returns either the response for the request or an error message. Two dominant solutions for HTTP servers today are Apache and Nginx. However, a new player in the space, Caddy Web Server, is gaining traction for its ease of use. This tutorial shows you how to install Caddy with PHP on an Ubuntu 20.04 based VPS server.

What's Caddy

Caddy Web Server is a modern open-source web server written in GO language. It doesn’t have any dependencies and runs off of a static binary file and generates and renews SSL certificates automatically. Caddy can work as a static file server, scalable reverse proxy or a powerful dynamic server and can be expanded via plugins. It also includes support for HTTP/2 and experimental HTTP/3 protocols.

Nginx is a very popular web server, with 16K+ Stars on Github, whille Caddy has 38.7K Stars and is much more elegant and powerful than Nginx.

caddy server github

Caddy has the following key features:

  • The simplicity of its original Caddyfile configuration compared to the complex configuration of Nginx.
  • the ability to dynamically modify the configuration through its provided Admin API.
  • automatic HTTPS configuration support by default, with the ability to automatically request HTTPS certificates and configure them.
  • the ability to scale to tens of thousands of sites
  • can be implemented anywhere, with no additional dependencies.
  • written in Go language, memory security is more guaranteed.

In this tutorial, you’ll install and configure Caddy to run along with PHP on an Ubuntu 20.04 based VPS server.

Prerequisites

  • An Ubuntu 20.04 VPS server with a non-root account having sudo privileges.
  • A fully registered domain name pointed to your server’s IP address.

Before you start to set up Caddy on your server, you need to log in to your server.

$ ssh user_name@server_ip
$ ssh user_name@server_ip

Install Caddy Server

By default, Caddy is not available in the default Ubuntu repository. So you need to add the Caddy repository on Ubuntu 20.04 with the commands below:

 

$ sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https 
$ curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo tee /etc/apt/trusted.gpg.d/caddy-stable.asc 
$ curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list

Then, update your local package index with the following command:

sudo apt update

Now use the command below to install Caddy:

sudo apt install caddy

Here you can verify your Caddy installation with the following command:

sudo systemctl status caddy

In your output you will see:

Output
● caddy.service - Caddy
     Loaded: loaded (/lib/systemd/system/caddy.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2022-04-14 01:21:14 UTC; 1min 17s ago
       Docs: https://caddyserver.com/docs/
   Main PID: 17750 (caddy)
      Tasks: 8 (limit: 2264)
     Memory: 7.5M
     CGroup: /system.slice/caddy.service
             └─17750 /usr/bin/caddy run --environ --config /etc/caddy/Caddyfile

 

Configure Firewall

On ubuntu UFW is disabled by default. You can check the status of the UFW service with the following command:

$ sudo ufw status
Status: inactive

The upper output shows that the firewall status is inactive. If UFW is activated, you should see something like the following:

Status: active
To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)

 

Caddy serves websites using HTTP and HTTPS protocols, so you need to allow access to ports 80, and 443.

# Allow HTTP and HTTPs ports.
$ sudo ufw allow http
$ sudo ufw allow https

Check if Caddy Server is working

Now you can type your server’s IP address in your web browser to see that your Caddy web server is installed on your server correctly.

https://server-IP-address

You will see the following page:

caddy default index page
caddy-with-free-ssl

With all the above ready to go, it’s time to experience the power of Caddy!

Install PHP

Update System Packages

Update all system packages to the latest by running the commands below.

# downloads package information from all configured sources
$ sudo apt update
# upgrade all installed packages to their latest versions
$ sudo apt -y upgrade
# A reboot is important after any upgrade.
$ sudo systemctl reboot


# Ubuntu 20.04|18.04 (Not needed on Ubuntu 22.04)
$ sudo apt install lsb-release ca-certificates apt-transport-https software-properties-common -y

Next, Use the following command to enable PHP PPA and update apt cache:

$ sudo add-apt-repository ppa:ondrej/php 

Install PHP8.0

Then install PHP 8.0:

$ sudo apt install php8.0 

Press ‘Y’ for any confirmation asked by the installer.

Once completed the above commands, you have successfully installed PHP 8.0 on the Ubuntu system.

Check PHP version

 
$ php -v
PHP 8.1.4 (cli) (built: Apr  4 2022 13:30:17) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.4, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.4, Copyright (c), by Zend Technologies

 

Note: The apt packaging system has a set of trusted keys that determine whether a package can be authenticated and therefore trusted to be installed on the system. Sometimes the system does not have all the keys it needs and runs into this issue. Fortunately, there is a quick fix. Each key that is listed as missing needs to be added to the apt key manager so that it can authenticate the packages. To add these keys, run the following commands:

$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 40976EAF437D05B5

If the keys that your system is missing differs, simply replace the key at the end of the above command with your key and run it.

Installing PHP Extensions

Now, install the required PHP modules for your application. Use the following command to search all available PHP 8.0 modules.

$ sudo apt search php8.0-* 

Then install the required PHP modules. The following command will install PHP fpm and some frequently used PHP modules on your system.

$ sudo apt install php8.0-fpm php8.0-curl php8.0-gd php8.0-xml php8.0-common php8.0-mbstring php8.0-mysql -y 

You may also need to install other PHP modules recommended by the application.

Start PHP-FPM Service

There is no official PHP module for the Caddy server. In that case, we will use PHP-FPM to deploy PHP applications over Caddy web servers. Once the installation is finished, enable and start the PHP-FPM service.

$ sudo systemctl enable php8.0-fpm 
$ sudo systemctl start php8.0-fpm 

Configure Caddy

Here you need to make some configuration changes to the Caddy web server main configuration file.

$ sudo vim /etc/caddy/Caddyfile
# [file content]
your-domain-name {
    root * /var/www/example.com
    log {
        output file /var/log/caddy/example.com.access.log {
        	roll_size 10MiB
	        roll_keep 7
	        roll_keep_for 48h
        }
        format console
        level info
    }
    encode gzip zstd
    php_fastcgi unix//run/php/php8.0-fpm.sock
    
    tls info@example.com {
    	protocols tls1.2 tls1.3    	
    }
}

When you are done, save and close the file. To apply the changes, restart the Caddy web server with the command below:

$ sudo systemctl reload caddy

Then, let us create a test page to verify that Caddy can render PHP with the following command:

$ sudo vim /usr/share/caddy/info.php

Add the following content to the file:

<?php


phpinfo();
?>

When you are done, save and close the file.

Verify the Setup

Here you can access the Caddy website by typing your domain name in your web browser followed by info.php:

https://your-domain-name/info.php

php info page

When you have finished reading your PHP info, it’s better to remove your PHP file for more security.

$ sudo rm /usr/share/caddy/info.php

You can always rebuild your file every time you need it.

Conclusion

This concludes our tutorial where we installed Caddy web server along with PHP 8.0. The tutorial is tested on VPS Mart, so it works fine on the Ubuntu VPS Hosting. Caddy can work as a static file server, scalable reverse proxy or a powerful dynamic server and can be expanded via plugins. If you face any problem or any feedback, please leave a comment below. Hope you enjoy it.

Table of Contents
    Add a header to begin generating the table of contents
    Scroll to Top