OpenBSD Journal

PHP/PostgreSQL/Apache setup for newbies

Contributed by jose on from the helping-everyone dept.

Robert P. Lessard writes: " PHP, PostgreSQL and Chroot Apache for Newbies

The following is a description of my steps for setting up PHP and PostgreSQL under a chroot'ed Apache environment. This how-to is intended for those new to web-based apps or those who have used PHP and MySQL and would like to try out PostgreSQL as an alternative.

Before I get flamed for "the info is already out there- why do we need another how-to?" The overwhelming tutorials focus on MySQL and most that I came across for PostgreSQL either had older data, didn't address the chroot Apache issues or were not really geared for the beginner audience.

With that said, here's what I did to get things running..." Read on for Robert's instructions.

" 1. Set up a generic OpenBSD 3.3 install without "X-windows system" support. For more information on the minimum install files required:

2. Install the following packages:

  • postgresql-7.3.2.tgz
  • postgresql-docs-7.3.2.tgz
  • php4-core-4.2.3p1.tgz
  • php4-pgsql-4.2.3.tgz
I used an ftp connection and the standard package add functionality such as:

#pkg_add -v

(note: remember to use mixed case)

Or you can grab packages from one of the many mirrors to load balance off the main site :-)

For more details about adding packages:

3. Post package configuration- most of this is posted on the screen after the package additions are complete but here's a summary.

Create your php.ini file:

#cp /usr/local/share/doc/php4/php.ini-dist /var/www/conf/php.ini
Make Apache "aware" of the PHP installation and activate the database extension:
#/usr/local/sbin/phpxs -s

#/usr/local/sbin/phpxs -a pgsql
This will make some modifications to the Apache configuration file to load the appropriate PHP modules when Apache is started. Verify the following line is "uncommented" in /var/www/conf/httpd.conf:
LoadModule php4_module /usr/lib/apache/modules/
In order for Apache to call PHP to parse web pages, we must add the following file-extension association line as well:
AddType application/x-httpd-php .php .html
4. Setup the database admin user and group accounts:
# useradd -c "Postgresql Admin User" -g =uid -m -d /var/postgresql -s /bin/sh postgresql

5. Create the database data folder and initial environment:

#su - postgresql

$mkdir /var/postgresql/data

$initdb -D /var/postgresql/data
PostgreSQL won't let you to anything without being logged in as this user.

6. Create the unix socket folder:

$mkdir /var/www/tmp
By default the database would try to write socket info to /tmp but since Apache will be chroot'ed to /var/www, /var/www/tmp "apears" to be /tmp in a web application scenario.

As far as permissions are concerned, I assigned full permissions to the postgresql account and group, read/list to world and owner deletion only (numerically represented as 1755). Many how-to guides say to set liberal 777 permissions but the above was the most restrictive setting that did not result in errors for me.

7. On the subject of the unix socket folder, open the database configuration file /var/postgresql/data/postgresql.conf and "uncomment" and edit the line:

unix_socket_directory = '/var/www/tmp'

8. Setting Apache and PostgreSQL to start at boot:

I know there are many ways to skin a cat but here's what I did...

Create a rc.conf.local file if you don't have one:

#touch /etc/rc.conf.local
Add the following line to section 1:
(note: that's no space and double quotes)

The above will override the httpd_flags=NO in /etc/rc.conf

And the following line to section 3:

shlib_dirs="$shlib_dirs /usr/local/lib/postgresql"
This makes the system "aware" of the database libraries on startup.

In /etc/rc.local I added the following to start PostgreSQL:

su -l postgresql -c "/usr/local/bin/pg_ctl start
	-D /var/postgresql/data -l /var/postgresql/logfile
	-o '-D /var/postgresql/data'"
	echo -n 'postgresql'

9. To shutdown the database gracefully add the following to /etc/rc.shutdown:

if [ -f /var/postgresql/data/ ]; then
	su -l postgresql -c "/usr/local/bin/pg_ctl stop -m fast -D /var/postgresql/data"
	rm -f /var/postgresql/data/
(note: 2nd line is word wrapped to the 3rd line)

10. Changes to the /var/www/conf/php.ini file:

I secured persistent database connections because I have read it can cause reliability issues (maybe someone can comment on their experience)

pgsql.allow_persistent = Off
To save state between web pages in my application I set
11. Reboot and ensure everything started properly with top or ps
Should see the httpd and postgresql running under their respective users.

12. Test that PHP initialized with Apache:

#touch /var/www/htdocs/info.php
Type the following:
And try to pull up that web page from another computer. You should get a pretty detailed report about the Apache and PHP installation configuration if it worked.

13. Verify that PostgreSQL create unix socket files properly:

Look in /var/www/tmp and you should see something like this:


14. The above steps are all that I did and it worked great for me. I am running a small test application and have had no issues so far.

For those of you thinking about trying an alternative to PHP/MySQL, the php database commands for PostgreSQL are strikingly similar to those used for MySQL. For the basic database function, don't be turned off due to extensive learning of new commands because there isn't.

It was my intention to help people get setup. Actually writing a PHP/PostgreSQL application is beyond the scope but the following have plenty of information: "
Wow, nice notes! Thanks for the help.

(Comments are closed)

  1. By Anonymous Coward () on

    very nice work. thanks for putting this together :)

    1. By Anonymous Coward () on

      Also, FYI, a new PostgreSQL version was just recently released, so change the 7.3.2 in the directions to 7.3.3... :)

    2. By Anonymous Coward () on

      Ditto. Thanks much.

    3. By haynes horne () on

      doesn't this recipe leave a user named postgresql for the db server and a user named www for the httpd server? so as the description stands, they cannot interact.
      how can user www access the db server without being also user postgresql? openBSD notes are explicit about retaining the www user account for httpd. but my www group reads daemon and my postgresql group reads postgresql. where's the catch?

  2. By Anonymous Coward () on

    GREAT ! Another brave man who knows about the advantages of PostgreSQL ;-)

  3. By Anonymous Coward () on

    Beeing an OpenBSD newbie myself, it's nice to have some 'cooking instructions'.

    You might mention that the default Postgresql database user does not have a password after install, and setting one might not be such a bad idea. The database user account used by PHP perhaps should be an account without Postgresql superuser rights i.e. not have the right to create other database users.

    1. By rwx () on

      Not only password need to be set. You also need to modify /var/postgresql/data/pg_hba.conf for access within local machine with authentication, because in default postgresql install even with pgsql superuser password set you still can logon without password via local socket. Of course you need to HUP postmaster after config modification.

      1. By Anonymous Coward () on

        Would be nice if the postgresql post-install message
        mention things like these.

  4. By Anonymous Coward () on

    For a complete newbie reader who uses this cookbook-style, this line: <br> #touch /var/www/htdocs/info.php <br> is wrong, because the reader is told to type PHP code thereafter. Surely you meant to say "fire up your favourite editor and create /var/www/htdocs/info.php with the following contents..." or something along those lines. <br>

  5. By jim () on

    Thanks for the exceptionally detailed instructions! Is anyone familiar with detailed changes that would need to be made to install Python rather than PHP? Again, thanks. This is great.

    1. By Anonymous Coward () on

      yeah - install mod_python from ports and add a line like:

      LoadModule "modules/" to httpd.conf.

      You need to set up .htaccess on the directory containing your scripts but thats about it.

      Check the docs on for more info.

  6. By Anonymous Coward () on

    You quit before getting to the good part. I was hoping for a brief crash course on creating databases.

  7. By KryptoBSD () on

    I also agree that there are substantial MySQL tutorials available, and this one seems to do a great job of explaining details for the less talked about, but still widely used database.

    Glad to see some change.

  8. By kvh () on

    I just followed works great but there are a few typos.
    1. The ftp address should be
    2.Step 5 it should be made explicit when you exit su
    3. I didn't have a /etc/rc.conf.local with newly installed 3.3, so there are no sections in it.
    4. Add at the end of the lines for the /etc/rc.local code
    5. Don't do a touch at #12, use an editor.

    Other than that it is great...

    1. By kvh () on

      #4 is add a backslash

  9. By ac () on

    Very nice. This is a very good start. Thanks again!

  10. By Anonymous Coward () on

    thanks. :)

  11. By Anonymous Coward () on

    Now... who can give me a systrace policy for httpd that works with this mess and a handful of vhosts.

  12. By Anonymous Coward () on

    How to run postgresql on one server and apache/PHP on another?

    When I figure that out, I think I will write a cookbook.

  13. By Anonymous Coward () on

    I followed these great instructions on a fresh 3.3 install and got what looks like a working system. But, when I pull up the info.php page I get two warning messages all they way at the bottom. I tried setting the value to /var/www/tmp, but the warnings were still there (they just said /var/www/tmp instead of /tmp). Anyone have any ideas?

    Warning: open(/tmp/sess_989368bd65448f70985379600fae56b8, O_RDWR) failed: Permission denied (13) in Unknown on line 0

    Warning: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/tmp) in Unknown on line 0

    1. By hillfieldhonest () on

      I could also reproduce the issue, I just wonder if there is any need to change the tmp path of php.inf file.
      currently,/var/www/tmp's permission is set to 1755 as instruction.
      I also need your help! thanks!

    2. By hillfieldhonest () on

      just follow-up.
      worked fine with following modification in php.inf

      session.save_path = /tmp
      session.save_path = /var/www/tmp

      If there is any other way to make it work,
      please share your experience. Thanks.

      1. By Anonymous Coward () on

        Nope - that IS the way to make it work properly.

        Well it works here and makes logical sense.

      2. By anonymous () on

        I cannot locate the 'php.inf' file. Would it be '/var/www/conf/php.ini' ?

        After modifying 'session.save_path' to '=/var/www/tmp', I got error messages like :"Warning: open(/var/www/tmp/sess_85ce0bdcdd89f34933d2825d342e2b13, O_RDWR) failed: No such file or directory (2) in Unknown on line 0

        Warning: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/var/www/tmp) in Unknown on line 0"

        Any suggestion where I should look into ? Thanks.

  14. By Marc Herren () on

    I can't find php4-pgsql-4.2.3 neither in the ports nor in the packages collection.

    Can someone explain me how I can compile it myself ( diden't found the src neither )



  15. By Varyag () on

    Thank you!!!

  16. By Newbie that's lost () on

    Hey I'd appreciate the help, I followed your instructions or at least thought I did. But when i do top... it shows postgres is running however when i go into var/www/tmp nothing is there and when i type "createdb mydb" I get

    pgsql: could not connect to server: No such file or directory
    Is the server running locally and accepting connections on Unix domain socket "/tmp/.s/PGSQL.5432"?
    createdb: database creation failed

    the postgres help says that the server isn't started properly... however it's there when i click top... and there's nothing in the tmp directory like the directions say there should be...

    any clues as to where i've gone wrong???
    -lost Newb

    1. By sailor () on

      I also followed the instructions and got the same error...

      Are we missing something here? :-/

      1. By sailor () on

        Somehow the temp directory I specified in the config file doesn't work.
        postgresql searched /tmp/ rather than /var/www/tmp/ for .s.PGSQL.5432 + *.lock.

        dirty solution:
        cd /var/www/tmp/
        ln -s /var/www/tmp/.s.PGSQL.5432 /tmp/
        ln -s /var/www/tmp/.s.PGSQL.5432.lock /tmp/

        1. By R. Senthil Kumar ( on

          /tmp/.s.PGSQL.5432 if this problem arise first please check your disk space. using df -k will show your disk space. problem arise dute to lack of disk space. clear all the unnecessary datas form the disk or transware the data.

Latest Articles


Copyright © - Daniel Hartmeier. All rights reserved. Articles and comments are copyright their respective authors, submission implies license to publish on this web site. Contents of the archive prior to as well as images and HTML templates were copied from the fabulous original with Jose's and Jim's kind permission. This journal runs as CGI with httpd(8) on OpenBSD, the source code is BSD licensed. undeadly \Un*dead"ly\, a. Not subject to death; immortal. [Obs.]