Linux Caddy Postgresql PHP
#Installation
#Linux
Debian 11
#Caddy
sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
sudo apt update
sudo apt install caddy
#PHP
sudo apt update && sudo apt full-upgrade -y
sudo apt install php php-fpm php-cli php-gd -y
#Postgresql
sudo apt install postgresql php-pgsql -y
#Configuration
#Caddy
sudo nano /etc/caddy/Caddyfile
php.acytoo.com {
root * /var/www/php.acytoo.com
log {
output file /var/log/caddy/php.acytoo.com.log
}
encode gzip
php_fastcgi unix//run/php/php7.4-fpm.sock
}
If you want to uer file_server along with php, please check file_server vs php
Once you configured caddy, restart it, or reload configuration file
sudo systemctl restart caddy
#PHP
sudo nano /etc/php/7.4/fpm/pool.d/www.conf
change the following line
user = caddy
group = caddy
listen.group = caddy
listen.owner = caddy
Then restart php-fpm
sudo systemctl restart php7.4-fpm.service
#Postgresql
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 0.0.0.0/0 md5
Then restart postgresql by
sudo systemctl restart postgresql
#Test
Now navigate to your website's root directory, if not exist, create one.
sudo mkdir -p /var/www/php.acytoo.com
sudo chown -R acytoo:acytoo /var/www/php.acytoo.com
For convenience, I have also changed the owner of website's root directory from root user to myself.
nano /var/www/php.acytoo.com/index.php
<html>
<head>
<title>Test postgresql</title>
</head>
<body>
<?php
$host = "host = 127.0.0.1"; // 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";
}
?>
</body>
</html>
Save file, open browser, visit your website, check the result.