Da3mon Computer

CADDY, MySQL (MariaDB) AND PHP-FPM 8.1 ON UBUNTU 22.04

Install Caddy Webserver

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. It 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.

Set up the repository for Caddy and install using the following command:

sudo 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

Once the Caddy has been installed, you can verify the Caddy version using the following command:

sudo caddy version

Next, you will need to set some permission to allow the caddy binary to connect to privileged ports like 80 and 443. You can set it with the following command:

sudo setcap 'cap_net_bind_service=+ep' /usr/bin/caddy

Now, open your web browser and access the Caddy default page using the URL http://your-server-ip. You should see the following page:

Caddy Default Page
CADDY, MySQL (MariaDB) AND PHP-FPM 8.1 ON ubuntu 22.04 jammy jellyfish 4

To start the Caddy service, run the following command:

sudo systemctl start caddy

To stop the Caddy service, run the following command:

sudo systemctl stop caddy

To enable the Caddy service to start at system reboot, run the following command:

sudo systemctl enable caddy

Enable PHP 8.1 Support in Caddy

PHP 8.1 was released on November 25, 2021, and is a minor release of the PHP 8 series. It comes with its own unique set of features and deprecations.

First, install PHP and other necessary extensions using the following command:

sudo apt install php8.1-{bcmath,xml,fpm,mysql,zip,intl,ldap,gd,cli,bz2,curl,mbstring,common,pgsql,opcache,soap,cgi} -y

After installing PHP, edit the PHP-FPM configuration file and change the default user and group with caddy:

sudo nano /etc/php/8.1/fpm/pool.d/www.conf

Find and replace user and group name from www-data to caddy:

user = caddy
group = caddy
listen.owner = caddy
listen.group = caddy

Save and close the file then restart the PHP-FPM service to apply the changes:

sudo systemctl restart php8.1-fpm

Make sure PHP 8.1 starts at system reboot, run the following command:

sudo systemctl enable php8.1-fpm

Optionally install MySQL (MariaDB)

MariaDB Server is fast, scalable and supports more storage engines than MySQL.  It’s made by the original developers of MySQL and guaranteed to stay open source.

Install MySQL (MariaDB) using the following command:

sudo apt install mariadb-server mariadb-client -y

Configure and secure MariaDB using the following command:

sudo mysql_secure_installation

You will be asked the following:

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  

Make sure MariaDB starts at system reboot, run the following command:

sudo systemctl enable mariadb

Create Caddy Virtual Host Configuration File

The Caddy default virtual host configuration file is located at /etc/caddy/Caddyfile.

Edit the /etc/caddy/Caddyfile file with the following command:

sudo nano /etc/caddy/Caddyfile

Remove all lines and add the following lines:

caddy.example.com:80 {
    root * /usr/share/caddy/
    encode gzip zstd
    php_fastcgi unix//run/php/php8.1-fpm.sock
}

Save and close the file then restart the Caddy service to apply the changes:

sudo systemctl restart caddy

Next, create a sample PHP file for Caddy with the following command:

sudo nano /usr/share/caddy/info.php

Add the following lines:

<?php

phpinfo();
?>

Save and close the file when you are finished.

Verify Caddy PHP Support

Now, open your web browser and access the Caddy website using the URL http://caddy.example.com/info.php. You should see the PHP page on the following screen:

Caddy Verify PHP 8.1
CADDY, MySQL (MariaDB) AND PHP-FPM 8.1 ON ubuntu 22.04 jammy jellyfish 5

Optionally install Adminer with Caddy

Make a directory for Adminer and use Wget to download it using the following commands:

sudo mkdir /usr/share/caddy/adminer
sudo wget https://www.adminer.org/latest.php -O /usr/share/caddy/adminer/index.php

Set root Database password for Adminer:
To manage the complete database system using the Adminer we need to log in with the root database user. Therefore set a password for that. This will help us to manage all available databases using Adminer.

Login to MariaDB or MySQL:

sudo mysql

Now, set the password for the root user:

Note: Replace my-password with the password you want to set for your root database user.

SET PASSWORD FOR 'root'@'localhost' = PASSWORD("my-password");

Exit the MariaDB shell:

FLUSH PRIVILEGES;
exit;

Open the port for Caddy to allow access to Adminer:

sudo ufw allow from any to any port 8080 proto tcp

Edit the /etc/caddy/Caddyfile file with the following command:

sudo nano /etc/caddy/Caddyfile

Add the following lines:

:8080 {
        # Set this path to your site's directory.
        root * /usr/share/caddy/adminer
        encode gzip zstd
        php_fastcgi unix//run/php/php8.1-fpm.sock

        # Enable the static file server.
        file_server
}

Save and close the file then restart the Caddy service to apply the changes:

sudo systemctl restart caddy

Now, open your web browser and access the Adminer using the URL http://caddy.example.com:8080


adminer
CADDY, MySQL (MariaDB) AND PHP-FPM 8.1 ON ubuntu 22.04 jammy jellyfish 6
wpChatIcon