Compiling PHP + Apache to access remote postgreSQL database?

Where Penguins and Daemons chill together in the warmth of the Sun.

Moderators: SecretSquirrel, notfred

Compiling PHP + Apache to access remote postgreSQL database?

Postposted on Thu Dec 06, 2007 9:14 pm

My goal is to enable PHP-based web applications to use postgreSQL database. Thus, I tried to compile Apache, PHP, and postgreSQL using this instruction.

I've been quite successful so far. However, the instruction above is to enable web applications to access postgreSQL database on the same machine. Apache, PHP, and postgreSQL are indeed compiled on the same machine.

Code: Select all

   Web Server

================
|              |
|    Apache    |
|      +       |
|     PHP      |
|      +       |
|  postgreSQL  |
|    server    |
|              |
================



Figure 1: Apache, PHP, and postgreSQL on the same machine


However, is it possible to compile Apache and PHP to access postgreSQL database on remote database server? I'm thinking about the following configuration:

Code: Select all

   Web Server            Database Server
================        ================
|              |        |              |
|    Apache    |        |              |
|      +       |--------|  postgreSQL  |
|     PHP      |        |    server    |
|              |        |              |
================        ================


Figure 2: Apache and PHP accessing remote postgreSQL database: is it possible?

I wonder if such configuration above is possible, because from what I read from the instruction, it seems that PHP needs certain postgreSQL libraries to be installed anyway, in order for the web applications to access postgreSQL database.

Note that I'm just a newbie; I merely guess from the following compilation steps:

(1) From the two steps below, it seems to me that certain postgreSQL libraries need to be shared (correct me if I'm wrong).
3. Append the following line to /etc/ld.so.conf:

/usr/local/pgsql/lib

4. Run the dynamic linker:

ldconfig -v


(2) And then, PHP need to be compiled in certain way to access the shared postgreSQL libraries (emphasizes mine):
Configure PHP with Apache and PostgreSQL in mind:
./configure --with-apache=/usr/local/src/apache_1.3.12
--with-pgsql=share


--enable-track-vars



So the ultimate question is: do I really need to compile postgreSQL on my web server, despite the it is to access remote postgreSQL database anyway? Ergo, do I need to have the following configuration?


Code: Select all

   Web Server            Database Server
================        ================
|              |        |              |
|    Apache    |        |              |
|      +       |        |              |
|     PHP      |--------|  postgreSQL  |
|      +       |        |    server    |
|  postgreSQL  |        | (production) |
|    server    |        |              |
|   (unused)   |        |              |
|              |        |              |
================        ================


Figure 3: the postgreSQL database on the web server is effectively unused, because it is only compiled simply because PHP needs it

Based on the instruction I mentioned, it seems that I need to compile postgreSQL server on the web server anyway, simply because PHP needs its library. However, I don't quite like the configuration, because the postgreSQL server on the web server is unused anyway; the production database is the one on the remote database server.

Alternatively, if PHP really needs postgreSQL libraries to access a postgreSQL database (remote or otherwise), can I just compile the postgreSQL client on the web server? Can I have the following configuration?

Code: Select all

   Web Server            Database Server
================        ================
|              |        |              |
|    Apache    |        |              |
|      +       |        |              |
|     PHP      |--------|  postgreSQL  |
|      +       |        |    server    |
|  postgreSQL  |        | (production) |
|(client-only) |        |              |
|              |        |              |
================        ================


Figure 4: is it possible?

Is it possible to do the above configuration? If I compile postgreSQL client, will it include the libraries required by PHP? And where to find compiling instruction for postgreSQL client only?

And how about MySQL? Is it possible to have Apache and PHP to access remote MySQL database?
Kreshna Aryaguna Nurzaman
Gerbil XP
 
Posts: 388
Joined: Mon Apr 11, 2005 2:05 am

Postposted on Thu Dec 06, 2007 9:29 pm

It is definitely possible; both postgreSQL and MySQL are designed to be used in client-server environments, communicating over TCP/IP sockets. I don't know specifically how to set it up though, as I personally haven't tried using PHP with remote databases.
(this space intentionally left blank)
just brew it!
Administrator
Gold subscriber
 
 
Posts: 37410
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Postposted on Thu Dec 06, 2007 10:09 pm

As JBI mentioned this was what it's made for :)

You'll need a host that will do this for you, or you'll have to keep it local to your network on a separate machine. The files that make use of the database should be local to that server anyhow. Not sure how much different PostgreSQL is from MS SQL, but that's what I use - though with ASP mostly, not php. I'd imagine there's a different way to setup your connections strings and what not.
Corsair 600T | ASUS P8P67 PRO | Intel 2500k @ 4.4Ghz | EVGA 560 TI | G.SKILL Ripjaws Series 8GB | Corsair HX650 650W
steelcity_ballin
Gerbilus Supremus
Silver subscriber
 
 
Posts: 11907
Joined: Mon May 26, 2003 5:55 am
Location: Pittsburgh PA

Re: Compiling PHP + Apache to access remote postgreSQL datab

Postposted on Thu Dec 06, 2007 10:23 pm

Kreshna Aryaguna Nurzaman wrote:So the ultimate question is: do I really need to compile postgreSQL on my web server, despite the it is to access remote postgreSQL database anyway? Ergo, do I need to have the following configuration?

You don't need the entire PostgreSQL database on your webserver; all Apache and PHP need is access to the Postgres client libraries. Those provide the functionality for it to interact with a local or remote DB. The problem here is that you're compiling all of this yourself, so you're making it hard on yourself.

If you were using a your distro's packages, they'd most likely have the Postgres client and server stuff separated into different packages, even though they're build from the same source. For instance, on Debian, apache2 uses the libpq5 Postgres client libraries, which is way smaller than all of Postgres. libpq5 is built from the monolithic Postgres source tree, however. Furthermore, to compile Apache or PHP against the client libraries you not only need the runtime libraries, but you also need the header files and such. Generally speaking you need a lot more dependencies to compile something than you do to run it.
Last edited by bitvector on Thu Dec 06, 2007 10:28 pm, edited 1 time in total.
bitvector
Grand Gerbil Poohbah
 
Posts: 3234
Joined: Wed Jun 22, 2005 4:39 pm
Location: Mountain View, CA

Postposted on Thu Dec 06, 2007 10:28 pm

Yes, the whole point of db engines is to offload the work to a separate machine, so this is the default scenario (at least for any large applications). You only need the client on the user-facing machine. Once you have the client compiled, switching to a different server is simply a matter of specifying different information (server address, database, login info) when you connect. You could even decide to connect to a different database at runtime based on user input, or whatever (of course be careful about passing user input through to dbs, and also about how you handle things like passwords).
UberGerbil
Gerbil Khan
 
Posts: 9966
Joined: Thu Jun 19, 2003 3:11 pm

Postposted on Thu Dec 06, 2007 10:59 pm

As others have said, you should be able to set it up as shown in your last diagram with just the client libraries on the web machine and those will then call across the network to the server.


You don't say what distribution you are using, it's almost always easier to use the distribution supplied versions and only recompile if they are missing something that you absolutely need. The other advantage is that they then track security updates rather than you needing to track them. In all likelihood your distribution supplies packages something like "postgreSQL-server" and "postgreSQL-client".
notfred
Grand Gerbil Poohbah
 
Posts: 3701
Joined: Tue Aug 10, 2004 10:10 am
Location: Ottawa, Canada

Re: Compiling PHP + Apache to access remote postgreSQL datab

Postposted on Fri Dec 07, 2007 1:23 am

bitvector wrote:all Apache and PHP need is access to the Postgres client libraries.


I see. Thanks.


bitvector wrote:If you were using a your distro's packages, they'd most likely have the Postgres client and server stuff separated into different packages, even though they're build from the same source. For instance, on Debian, apache2 uses the libpq5 Postgres client libraries, which is way smaller than all of Postgres. libpq5 is built from the monolithic Postgres source tree, however.


I see. So if I merely use Apache and PHP that comes with my distro's packages, they already come with postgreSQL client library "out of the box", am I correct?


bitvector wrote:Furthermore, to compile Apache or PHP against the client libraries you not only need the runtime libraries, but you also need the header files and such. Generally speaking you need a lot more dependencies to compile something than you do to run it.


I see. What if I compile postgreSQL client with these commands:
gmake -C src/bin install
gmake -C src/include install
gmake -C src/interfaces install
gmake -C doc install


Would the steps above install the header files needed to compile Apache and PHP? Could I just follow those steps with these?
/usr/local/pgsql/lib
ldconfig -v


and then compiling PHP with --with-pgsql=SHARED?
Kreshna Aryaguna Nurzaman
Gerbil XP
 
Posts: 388
Joined: Mon Apr 11, 2005 2:05 am


Return to Linux, Unix, and Assorted Madness

Who is online

Users browsing this forum: No registered users and 2 guests