Linux Caddy Postgresql PHP - LCPP

Linux Caddy Postgresql PHP



Debian 11

sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https
curl -1sLf '' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf '' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
sudo apt update
sudo apt install caddy
sudo apt update && sudo apt full-upgrade -y
sudo apt install php php-fpm php-cli php-gd -y
sudo apt install postgresql php-pgsql -y


sudo nano /etc/caddy/Caddyfile {
	root * /var/www/
	log {
		output file /var/log/caddy/
	encode gzip
	php_fastcgi unix//run/php/php7.4-fpm.sock

file_server vs php

Once you configured caddy, restart it, or reload configuration file

sudo systemctl restart caddy
sudo nano /etc/php/7.4/fpm/pool.d/www.conf

change the following line

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

Then restart php-fpm

sudo systemctl restart php7.4-fpm.service

Allow connecting from local using password:

sudo nano /etc/postgresql/13/main/pg_hba.conf

Change (or add if not exist) this line, the original value is peer, change it to md5

local all all md5

You can give user 'sometwo' permission to connect from remote, by add

host all sometwo md5

Then restart postgresql by

sudo systemctl restart postgresql


Now navigate to your website's root directory, if not exist, create one.

sudo mkdir -p /var/www/
sudo chown -R acytoo:acytoo /var/www/

For convenience, I have also changed the owner of website's root directory from root user to myself.

nano /var/www/
    <title>Test postgresql</title>

$host = "host ="; // host address
$port = "port = 5432"; 			// postgresql port
$dbname = "dbname = database_name"; 	// database name
$credentials = "user = sometwo password=notmypassword"; // credentials

$db = pg_connect("$host $port $dbname $credentials");
if (!$db) {
    echo "Error : Unable to open database\n";
} else {
    echo "Opened database successfully\n";


Save file, open browser, visit your website, check the result.