budak Leutik webLog

December 13, 2005

PHP, PostgreSQL and Chroot Apache for Newbies in OpenBSD

Filed under: OpenBSD — budakleutik @ 8:13 am

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:

http://www.openbsd.org/faq/faq4.html

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
ftp://ftp.openbsd.org/pub/3.3/OpenBSD/packages/i386/postgresql-7.3.2.tgz

(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:

http://www.openbsd.org/ports.html

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/libphp4.so

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:

httpd_flags=””

(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/postmaster.pid ]; then
su -l postgresql -c “/usr/local/bin/pg_ctl stop -m fast -D /var/postgresql/data”
rm -f /var/postgresql/data/postmaster.pid
fi

(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)

[Postgresql]
pgsql.allow_persistent = Off

To save state between web pages in my application I set

[Sessions]
session.auto_start=1

11. Reboot and ensure everything started properly with top or ps

#top

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:

.s.PGSQL.5432
.s.PGSQL.5432.lock

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:

http://www.apache.org/
http://www.php.net/
http://www.postgresql.org/ ” Wow, nice notes! Thanks for the help.

Original Source

Robert P. Lessard

http://undeadly.org/cgi?action=article&sid=20030603105000

Blog at WordPress.com.