简体   繁体   中英

Connect PHP to Postgresql on OpenBSD

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.";
    }
?>

Did you activate the PG PHP module by copying or symlinking its .ini file to /etc/php-8.0/ ? If not, take a look at the "Extension modules" section of /usr/local/share/doc/pkg-readmes/php-8.0 :

For all extensions packaged separately (and for opcache), you will find a file named /etc/php-8.0.sample/(MODULE_NAME).ini. To enable it, add a symlink into /etc/php-8.0 and restart:

 ln -sf ../php-8.0.sample/MODULE_NAME.ini /etc/php-8.0/

To disable, remove the symlink from /etc/php-8.0 and restart:

 rm /etc/php-8.0/MODULE_NAME.ini

If you have installed a number of extensions and wish to enable them all, you can use these shell commands:

 # cd /etc/php-8.0.sample # for i in *; do ln -sf ../php-8.0.sample/$i ../php-8.0/; done

After enabling or disabling extensions (or otherwise modifying php's configuration), use rcctl(8) to restart php80_fpm or Apache.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM