@JS's Notes

Site with notes from my work.

Caddy Web Server with PHP and MariaDB on Ubuntu 20.04 in Intranet

2020-07-05 System @JS
Requirements:
  • A non-root user with sudo privileges.
  • DNS server in local network. In my case, the domain name for Caddy Web Server on the local network is ctx07vm.local.lnxorg.

Update your operating system packages and install some essential packages for basic administration of the operating system:

$ sudo apt update; sudo apt upgrade -y
$ sudo apt install mc nano vim wget curl git tree unzip -y
Caddy, PHP, MariaDB - installation and configuration
$ echo "deb [trusted=yes] https://apt.fury.io/caddy/ /" | sudo tee -a /etc/apt/sources.list.d/caddy-fury.list
$ sudo apt update
$ sudo apt install caddy -y
$ sudo systemctl is-active caddy.service
$ sudo systemctl is-enabled caddy.service
$ sudo ufw allow http
$ sudo apt install php-cli php-fpm php-mysql -y
$ php --version
$ sudo systemctl is-active php7.4-fpm.service
$ sudo systemctl is-enabled php7.4-fpm.service
$ sudo apt install mariadb-server -y
$ sudo systemctl is-active mariadb.service
$ sudo systemctl is-enabled mariadb.service
$ sudo mysql_secure_installation

Answer all the questions as shown below:

    Enter current password for root (enter for none):
    Set root password? [Y/n]: Y
    Remove anonymous users? [Y/n]: Y
    Disallow root login remotely? [Y/n]: Y
    Remove test database and access to it? [Y/n]:  Y
    Reload privilege tables now? [Y/n]:  Y

Log in to MariaDB shell, create a test database and test user with access permission:

$ sudo mysql

MariaDB [(none)]> CREATE DATABASE testmdb;
MariaDB [(none)]> CREATE USER 'usermdb' IDENTIFIED BY 'pass4usermdb';
MariaDB [(none)]> GRANT ALL PRIVILEGES ON testmdb.* TO 'usermdb';
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> EXIT;

Create the root directory for .html files and directory to store the log files for Caddy:

$ sudo mkdir -p /var/www/ctx07vm.local.lnxorg/html
$ sudo mkdir /var/log/caddy
$ sudo chown -R caddy: /var/log/caddy

Replace content a default Caddyfile /etc/caddy/Caddyfile with the following content:

http://ctx07vm.local.lnxorg {
    root * /var/www/ctx07vm.local.lnxorg/html
    log {
        output file /var/log/caddy/ctx07vm.local.lnxorg.access.log {
                roll_size 5MiB
                roll_keep 5
                roll_keep_for 96h
        }
        format console
    }
    encode gzip zstd
    php_fastcgi unix//run/php/php7.4-fpm.sock
}

Replace ctx07vm.local.lnxorg with domain name corresponding to your case.

Change the username for the PHP process. Edit the file /etc/php-fpm.d/www.conf. Find the user=www-data and group=www-data lines in the file and change them to caddy. Also, find the lines listen.owner=www-data and listen.group=www-data in the file and change them to caddy.

Restart the php-fpm process and Caddy Web Server:

$ sudo systemctl restart php7.4-fpm.service
$ sudo systemctl status php7.4-fpm.service
$ sudo systemctl restart caddy.service
$ sudo systemctl status caddy.service

Create a sample test page to check the PHP operation of the MariaDB database:

<html>
<head>
    <title>Caddy Test Site</title>
</head>
<body>
    <h2 style="margin-top: 2%; text-align: center;">Caddy Test Site</h2>

    <?php
    $servername = "localhost";
    $username = "usermdb";
    $password = "serwis";

    $conn = mysqli_connect($servername, $username, $password);

    if (!$conn) {
        exit('<p style="margin-bottom: 2%; text-align: center; ">Your connection has failed.<p>' .  mysqli_connect_error());
    }
    echo '<p style="margin-bottom: 2%; text-align: center; ">You have connected successfully.</p>';
    phpinfo();
    ?>
</body>
</html>

Remove the test file once you are satisfied:

$ sudo rm /var/www/example.com/html/test.php

Reference: Caddy DOCS.