How to Install Caddy Web Server with PHP-FPM on Ubuntu 16.04

Caddy or Caddy web server is an open source HTTP/2 enabled web server written in Go. Caddy can run on various systems, including those running Linux, Windows, Mac OS, Android, and BSD operating systems. Caddy has a lot of features and can be installed as a web server and as a reverse proxy for your application. The most notable Caddy web server features are automatic HTTPS enabled by default with no extra configuration.

In this tutorial, I will show you step by step how to install and configure the Caddy web server with PHP-FPM 7.x on Ubuntu 16.04. We will install caddy in the local environment, and then configure PHP-FPM for PHP based applications.

Prerequisites

  • Ubuntu 16.04 server
  • Root privileges

What we will do

  1. Install Caddy web server
  2. Configure Caddy web server
  3. Running Caddy as a Service on Systemd Ubuntu 16.04
  4. Install PHP and PHP-FPM 7.0
  5. Configure Caddy web server with PHP-FPM
  6. Testing

Step 1 – Install Caddy web server

In this step, we will install the Caddy web server using the installer script. The script will automatically download the binary file and extract it to the bin directory.

Download the installer script and make it executable.

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

Now run the installer script with sudo.

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

Note:

  • getcaddy = caddy installer script.
  • personal = license for our installation.
  • http.ipfileter … = include caddy plugins installation.

Wait for the caddy web server installation and you will get the result as below.

Install Caddy web server

Caddy web server has been installed on Ubuntu 16.04.

Step 2 – Basic Caddy web server configuration

In this step, we will configure the Caddy web server. We will create a new user named ‘caddy’, create a new directory for caddy configuration file, a new directory for caddy log files and a new directory for caddy web root directory.

Create a new ‘caddy’ user using the ‘/bin/false’ option as shown below.

useradd -M -s /bin/false caddy

Now run the following mkdir commands to create the caddy directory.

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

And change the ownership to the caddy user.

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

Next, we will create new caddy configuration file named ‘Caddyfile’. Go to the ‘/etc/caddy’ directory and create the configuration with the vim command.

cd /etc/caddy/
vim Caddyfile

Paste basic caddy configuration there.

http://hakase-labs.co {
    root /var/www/html
    log /var/log/caddy/hakase-labs.log
    tls off
    gzip
}

Save and exit.

Note:

  • tls off = we’re running caddy on the local server, if you’re on the live server, enable the option.

Caddy web server configuration

Basic configuration for Caddy web server has been completed.

Step 3 – Running Caddy as a Service on Systemd Ubuntu 16.04

In this step, we will run the caddy web server as service on systemd system and create the index.html file for our caddy web server.

Create new service file named ‘caddy.service’ using vim.

vim /etc/systemd/system/caddy.service

Paste the following configuration there.

[Unit]
Description=Caddy HTTP/2 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.

Now reload the systemd system and start the caddy service using the systemctl command as shown below.

systemctl daemon-reload
systemctl start caddy

Enable it to launch everytime at system boot.

systemctl enable caddy

The Caddy web server is now running as a service on Ubuntu 16.04.

Running Caddy as a Service on Systemd Ubuntu 16.04

Next, we will create a new index.html file under the web root directory ‘/var/www/html’.

Go to that directory and create the index.html file using commands below.

cd /var/www/html
echo ‘<h1><center>Caddy web server</center></h1>’ > index.html

Now change the owner of the file to the ‘caddy’ user and group.

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

The index.html file has been created. Open the web browser and type the domain name address that you typed on the ‘Caddyfile’ configuration, mine is:

http://hakase-labs.co/

And you will get the index page that we’ve created.

Caddy web server is running

Step 4 – Install PHP and PHP-FPM 7.0

In this step, we will install PHP-FPM packages from the Ubuntu repository.

Run the apt command below.

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

After the installation is complete, we will configure the configuration file for PHP-FPM.

Go to the ‘/etc/php/7.0/fpm’ directory and edit the pool configuration file ‘www’conf’ using vim.

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

Uncomment lines below.

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

Save and exit.

Now add the ‘caddy’ user to the ‘www-data’ group.

usermod -a -G www-data caddy

Start the PHP-FPM service and enable it to launch everytime at system boot.

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

PHP-FPM installation and configuration for our Caddy web server installation have been completed.

Install PHP and PHP-FPM 7.0

Check the PHP-FPM socket file process using the netstat command below.

netstat -pl | grep php

Step 5 – Configure Caddy web server with PHP-FPM

After the PHP-FPM installation and configuration, we will add PHP-FPM support to the Caddy web server.

Go to the ‘/etc/caddy’ configuration directory and edit the configuration file ‘Caddyfile’ using vim.

cd /etc/caddy
vim Caddyfile

Now add the following PHP-FPM configuration for caddy under your domain name bracket as below.

http://hakase-labs.co {
    root /var/www/html
    log /var/log/caddy/hakase-labs.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.

Restart the caddy web server and the PHP-FPM service.

systemctl restart caddy
systemctl restart php7.0-fpm

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

Configure Caddy web server with PHP-FPM

Step 6 – Testing

Go to the web root directory ‘/var/ww/html’ and create the phpinfo file ‘info.php’.

cd /var/www/html
echo ‘<?php phpinfo(); ?>’ > info.php

Open your web browser and type the Caddy web server URL installation as below.

http://hakase-labs.co/info.php

And you will get the PHP information page as shown below.

Testing Caddy Web Server

Installation for Caddy web server with PHP-FPM on Ubuntu 16.04 has been completed successfully.

Reference

Share this page:

نوشته های مشابه