I have PHP and Postgresql installed on OpenBSD 7.0 (VPS). PHP is working (I can echo from an index.php file). And my Postgresql server is running (I can connect and create tables from the command line) but I can't get PHP to connect.
I suspect it has something to do with PHP not being able to access the DB socket (because it operates in chroot). Or it's related to my postgresql.conf not using "localhost" (explained below). But I'm not lost on trying to solve for those.
/etc/httpd.conf
(port 80 blocks are also there and redirect to 443)
server "db.example.com" {
listen on * tls port 443
root "/htdocs/db"
directory index index.php
authenticate with ".htpasswd"
tls {
certificate "/etc/ssl/db.example.com.fullchain.pem"
key "/etc/ssl/private/db.example.com.key"
}
location "*.php" {
fastcgi socket "/run/php-fpm.sock"
}
}
(and /etc/acme-client.conf is aligned and certificate installed)
PHP install
# pkg_add php php-pgsql (version 8 selected)
# rcctl enable php80_fpm
# rcctl start php80_fpm
# rcctl restart httpd
Postgres (part 1)
# pkg_add postgresql-server postgresql-contrib
# su - _postgresql
$ mkdir /var/postgresql/data
$ initdb -D /var/postgresql/data -U postgres -A scram-sha-256 -E UTF-8 -W
[password created]
Postgres (part 2)
/var/postgresql/data/postgresql.conf
(Localhost doesn't resolve to my VPS IP so I add my actual IP here)
listen_addresses = '123.45.67.89'
Postgres (part 3)
$ pg_ctl -D /var/postgresql/data -l logfile start
$ exit
# rcctl enable postgresql
# rcctl start postgresql
# psql -U postgres
# CREATE DATABASE test;
# CREATE USER tester WITH PASSWORD '12345';
# GRANT ALL PRIVILEGES ON DATABASE test TO tester;
/var/www/htdocs/db/index.php
<?php
echo "Hello world!"; (this part works, then no output after this)
$db = pg_connect("host=123.45.67.89 port=5432 dbname=test user=tester password=12345");
if ($db) {
echo "I'm in.";
} else {
echo "No luck.";
}
?>