Web Hosting Blog – Dewlance

How install Caddy Webserver on Ubuntu 16.04 VPS – Step by step

Here is a guide for installing Caddy Web Server (with PHP-FPM) on Ubuntu 16.04:

Preliminary

The Caddy Web Server is an HTTP / 2-based open-source server. This server can run on a variety of systems, making it very preferred by programmers. Whatever your operating system, you can use this server without experiencing any constraints. With the ease of running on Windows, Mac OS, Linux, Android to the BSD operating system, this server is one of the most favorite servers to this day. One that is considered the most fun feature of the Caddy Web Server is the HTTPS automation feature (by default) without requiring additional configuration.

 

If you are a Ubuntu 16.04 user, installing this server on your computer is probably something difficult. But do not worry because in this article we provide a step by step guide how to install Caddy Web Server with PHP-FPM on your Ubuntu 16.04. First of all we will do Caddy installation on local environment followed by PHP-FPM configuration for PHP based application.

 

Before starting the installation process, make sure we meet the following requirements:

 

Installation steps

Install Caddy Web Server

In this first step we will use a installer script which can automatically download binary files and also execute (against those files) to the bin directory. So we need to find the installer script.

Open: wget https://getcaddy.com -O getcaddy
chmod + x getcaddy

 

After getting the installer script, we need to run it with sudo.

Open: sudo ./getcaddy personal http.ipfilter, http.ratelimit

 

To be known:

 

Basic Configuration of Caddy Web Server

This is the second step we should do. We will create a new user, a new directory for the configuration file, a new directory for the caddy log file and finally, the new directory as the container for the caddy web root directory.
To create a new user, we can use the ‘/bin.false’ option as described below:

useradd -M -s /bin/false caddy

 

Then we need to run the following mkdir command. The goal is to create a caddy directory.

mkdir -p /etc/caddy
mkdir -p /var/log/caddy
mkdir -p /var/www/html

 

The next step is to change ownership to the caddy user as follows:

chown -R caddy:root /etc/caddy /var/log/caddy

 

After this step is done, we will create a new configuration file that we will call ‘caddyfile’. We need to open the ‘/etc/caddy‘ directory followed by a new configuration with vim command.

cd /etc/caddy/
nano Caddyfile

 

Next we need to stick to the basic configuration there as shown below:

http://dewlance.com {
     root /var/www/html
     log /var/log/caddy/caddy.log
     tls off
     gzip
 }

Finally, save and exit!

 

To note, ‘tls off’ means we are running caddy on the local server. You do not need to enable this option if you are on a live server.

 

Running caddy on Ubuntu 16.04

This is the third step and in this step we will run Caddy Web Server. We also need to create an index.html file for it. As before, use vim to create a new service file. This new file will be called ‘caddy.service’.

vim /etc/systemd/system/caddy.service

 

Use the following configuration:

[Unit]
 Description=Caddy HTTP/2 Unit Web Server
 
 [Service]
 User=caddy
 Group=caddy
 Environment=CADDYPATH=/etc/caddy
 ExecStart=/usr/local/bin/caddy -agree=true -log=/var/log/caddy/caddy.log -conf=/etc/caddy/Caddyfile -root=/dev/null
 ExecReload=/bin/kill -USR1 $MAINPID
 LimitNOFILE=1048576
 LimitNPROC=64
 
 [Install]
 WantedBy=multi-user.target

Save and exit.

 

Then use the systemctl command to start our caddy service. Use the following procedure:

systemctl re-daemon
systemctl started caddy

 

Whenever we are on system boot, enable them

systemctl activate caddy

 

Now we find your new server running on Ubuntu 16.04

Create a new index.html file:

 

We have to create a new index.html file in the web root directory ‘/var/www/html’. To make the file we need to go to that directory first. Use the command as follows:

cd /var/www/html
echo 'Caddy webserver is working' > index.html

 

The final step in making the index.html file is to change the file owner status to the file user. Use the command as follows:

chown -R caddy:caddy /var/www/html

We now have a new index.html file. Open the browser and type the domain name that has been set in the ‘Caddyfile’ configuration. We will be taken to our new index page.

 

 

Install PHP and PHP-FPM 7.0

This is the fourth step and in this step we will install PHP-FPM from the Ubuntu repository.

 

First run the command as follows:

sudo apt install -y php7.0-fpm php7.0-cli curl

 

After we complete the installation, the configuration file for PHP-FPM is the next step. Now we have to open the directory ‘/etc/php/7.0/fpm’ and edit the configuration file pool ‘www’conf’. Use vim again.

cd /etc/php/7.0/fpm
vim pool.d/www.conf

 

Clear the signs below:

listen.owner = www-data
listen.group = www-data
listen.mode = 0660

Save and exit.

 

Upon exit we need to add the caddy user to the ‘www-data’ group. Do the commands below:

usermod -a -G www-data caddy

 

Enable PHP-FPM service every time we are on system boot. How to activate it is as follows:

systemctl start php7.0-fpm
systemctl enable php7.0-fpm

 

After the activation, all installation and configuration process of PHP-FPM has been completed. Now we can use PHP-FPM on our Caddy server.

We may need to check the PHP-FPM socket file with the following command:

netstat -pl | grep php

 

Add PHP-FPM support to the Caddy server

This is the fifth step and in this step we need to open the ‘/etc/caddy’ configuration directory. To edit the ‘Caddyfile’ configuration file we need to use vim.

cd /etc/caddy
nano Caddyfile

 

After the configuration file has been edited, we need to add PHP-FPM configuration according to our domain name. Examples are as follows:

https://www.dewlance.com {
     root /var/www/html
     log /var/log/caddy/caddy.log
     errors /var/log/caddy/errors.log
     tls off
     gzip
 
     # PHP-FPM Configuration for Caddy
     fastcgi / /run/php/php7.0-fpm.sock php {
         ext .php
         split .php
         index index.php
     }
 }

Save and exit.

 

After logging out, we need to restart the server to start the PHP-FPM service correctly.

systemctl restart caddy
systemctl restart php7.0-fpm

The Caddy web server configuration with PHP-FPM has been done perfectly.

 

Final test

This is the last step. We need to make sure everything that has been done is functioning as we expect it to be. First we need to go to the web root directory ‘/var/www/html’ and then create a new file we’ll call ‘info.php’.

cd /var/www/html
echo '' > phpinfo.php

 

Access our browser and we type as follows:

http://example.com/phpinfo.php
(Replace example.com with your domain name)

 

Bingo! We now get your PHP page.

 

Caddy Webserver successfully installed on Ubuntu 16.04.

Exit mobile version