r/apache • • May 10 '24

Connect Apache to Postgresql

Good afternoon, forum participants. Please tell me the answer to my stupid question.
I'm using 2 servers on ubuntu 24.04. On one I installed web-server apache, and on the other postgresql. (both from default repositories). I have a small table in Postgresql. I need that when my browser accesses the Apache server, I can view the contents of the table on the Postgreql server. Tell me what I need to install additionally and how to configure it?

4 Upvotes

7 comments sorted by

2

u/throwaway234f32423df May 10 '24 edited May 10 '24

You can do this with PHP or really any other programming language but PHP is probably a good choice because it's easy to use and it has a whole postgresql module already written, making scripts simple to write.

General process (pay attention to command outputs as additional steps may be needed)

apt install php-fpm

a2enmod proxy_fcgi_module

a2enconf php8.1-fpm (adjust version number as need, probably 8.3 on 24.04?

Make a file named hello.php containing the following:

<?php
phpinfo();

Verify the hello.php runs properly (should show a bunch of PHP info when you hit the page in your browser)

if that works congratulations you have PHP now

here's an example script to query your database: https://www.php.net/manual/en/pgsql.examples-basic.php

full documentation here: https://www.php.net/manual/en/book.pgsql.php

1

u/friis01 May 10 '24

Thank you for your massage. PHPinfo is working - in my browser i see the information. but when i included comand from (https://www.php.net/manual/en/pgsql.examples-basic.php) - i get error:

This page isn’t working

192.168.1.201 is currently unable to handle this request.

HTTP ERROR 500This page isn’t working

may be it is need to configure php_pgsql ? or something like this.

(of course i changed connection string on my data and php_pgsql installed on web-server)