Do not use, republish, in whole or in part, without the consent of the Author. TheTAZZone policy is that Authors retain the rights to the work they submit and/or post…we do not sell, publish, transmit, or have the right to give permission for such…TheTAZZone merely retains the right to use, retain, and publish submitted work within it’s Network

Written based on experience by: THE Vorlin on 12/4/08 @ 09:00. No plagiarism was done (as far as I know) and if any part looks like something one might have written, it’s purely coincidental. This tutorial is written for those who don’t want any fuss over installing three sources at once and have tons of problems.

In this tutorial, the integration of Postgresql 8, Apache 2, and Php 5 will be discussed. I know that there are tutorials abound out there about how to do it with mySQL (LAMP – Linux Apache MySQL PHP) but I’ve seen few concerning PostgreSQL so that’s what this tutorial is for. I will [b]disclaim[/b] now that there are better people than I to write it, but since I use it all the time and have done setups for it more than I can count, here goes.

I’m performing this installation on my own machine which is running Vector Linux 5.9 Stable.

Linux (of course, pretty much any distro works)
Apache 2 (latest is 2.2.10, available from www dot apache dot org)
PHP 5 (latest is 5.2.6, available from www dot php dot net)
PostgreSQL 8 (latest is 8.3.5, available from www dot postgresql dot org)
gcc (your compiler)
gunzip or bunzip2 AND tar (depending on package, these are the decompression tools)
sudo (needed for root access parts)

Step 0. Download the latest source files for each one from their site. You’ll have three total. You can download either the .tar.gz or the .tar.bz2 types. I go for the bz2 as it’s got better compression than gzip. In the end, you’ll have httpd-2.2.10.tar.bz2, php-5.2.6.tar.bz2, and postgresql-8.3.5.tar.bz2.

Step 1. Using bunzip2 or gunzip (extentions bz2 and gz respectively), unpack each one. That will leave 3 tar files. We’ll use bunzip2 as example.
$ bunzip2 httpd-2.2.10.tar
$ bunzip2 php-5.2.6.tar
$ bunzip2 postgresql-8.3.5.tar
Step 2. Using tar, untar each one.
$ tar xvf httpd-2.2.10.tar
$ tar xvf php-5.2.6.tar
$ tar xvf postgresql-8.3.5.tar.tar

Now, the funny thing about this part is that you can’t just compile things in any order. The correct order is as follows. First, we configure and install apache, then comes postgresql, then lastly, php. PHP is last because the files needed are built when apache and postgresql are built. NOTE: all configurations below, as well as the compiling, can be done as yourself. When you run ‘make install’, though, you’ll have to have root permissions. Generally, this is where sudo comes in, assuming you have sudo capabilities (‘sudo make install’). Also, running ‘make’ will take some time on all of them, and running ‘make test’ after ‘make’ on php will take a significantly long time (there’s over 4000 tests).

[b]Configuring Apache 2.2.10[/b]
There’s a lot of configuration options for apache but we’re only going to be using two of them for brevity. We won’t be including cgi scripting and we’ll be changing the directory where it resides. By default, it will install into [i]/usr/local/apache2[/i]. We’re going to change that to [i]/usr/local/apache[/i] simply because I don’t like the 2 there, hehe, however, you can install it to whatever you want and put it wherever you want.
$ cd httpd-2.2.10
$ ./configure –prefix=/usr/local/apache –disable-cgi
$ make
$ sudo make install

With luck, it will compile and install without any problems. You can now ‘cd /usr/local/apache’ and run an ‘ls’ and see that there’s all kinds of directories there, mainly bin, htdocs, conf, and logs. Once again, you have to have root permission to start the server and if you try to start it as yourself, you’ll get this:
[quote](13)Permission denied: make_sock: could not bind to address
no listening sockets available, shutting down
Unable to open logs[/quote]

Also, if you run the command ‘apachectl’ in your home directory or somewhere other than /usr/local/apache/bin, you’ll probably get this:
sudo: apachectl: command not found

This means you’ll need to add /usr/local/apache/bin to your PATH variable which is easy enough or have symbolic links created from /usr/local/apache/bin to /usr/bin or /bin or something like that. NOTE: if you have sym links created, it’s best to never rename an executable in case you upgrade later as it just causes confusion.
You can create sym links like this, and yes, sudo is required.
$ cd /usr/local/apache/bin
$ for i in `ls`
sudo ln -s /usr/local/apache/bin/$i /usr/local/bin/$i && echo $i created in /usr/local/bin

You’ll get the following:
ab created in /usr/local/bin
apachectl created in /usr/local/bin
apxs created in /usr/local/bin
checkgid created in /usr/local/bin
dbmmanage created in /usr/local/bin
envvars created in /usr/local/bin
envvars-std created in /usr/local/bin
htcacheclean created in /usr/local/bin
htdbm created in /usr/local/bin
htdigest created in /usr/local/bin
htpasswd created in /usr/local/bin
httpd created in /usr/local/bin
httxt2dbm created in /usr/local/bin
logresolve created in /usr/local/bin
rotatelogs created in /usr/local/bin

Finally, to start the server using the default httpd.conf settings (that file is in /usr/local/apache/conf), run the following.
$ sudo /usr/local/apache/bin/apachectl start
You won’t get anything back unless there’s an error, so don’t worry about that. To test it, you can open your browser to localhost:80 or you can telnet to your machine, port 80, and hopefully get the following. If you get a connection refused, something’s wrong with the configuration of apache or something else.
$ /usr/local/apache/logs >telnet localhost 80
Connected to localhost.
Escape character is ‘^]’.
To get out of the telnet, just hit control-] and then type ‘close’ and your telnet session is over.
At this point, you have a fully functional apache web server running on your machine and are now free to create as much content as you want. Next is the Postgresql setup.

[b]Configuring PostgreSQL 8.3.5:[/b]
The configuration for PostgreSQL isn’t so bad, there’s just some switching around especially if you’re compiling from source like we are. To save some trouble before we configure and install, there’s some things we can do first and not worry about later. First, we need to create the postgres user and then create the directory where you want everything installed, mainly the database cluster (IMPORTANT!!). After that we’ll need to create the main directory where Postgres will live. Usually, it’s in /opt/Postgresql-/ but we’re not using that this time. It will be installed in /usr/local/pgsql which doesn’t exist. The data directory in /usr/local/pgsql is where the database cluster will live and has to have very strict permissions, otherwise pg_ctl/postmaster will complain when you try to run initdb or start the server.

To create the postgres user, create the postgres group, and the /usr/local/pgsql directory:
$ sudo useradd -m -c “Postgres user” postgres
$ sudo groupadd postgres
$ sudo passwd postgres
$ sudo mkdir /usr/local/pgsql
$ cd /usr/local/pgsql
$ sudo mkdir data && sudo chown -R postgres.postgres data
$ sudo chmod -R 700 data
At this point, you have a user postgres that will handle all of the startups and shutdowns as well as a group postgres. The passwd command that we used needs to have a password given to postgres so that you can sign in later. The /usr/local/pgsql directory has been created as well as the data directory, which had permissions (chown) changed to 700, which means only the owner and root can ever see anything in there. It’s also owned by group postgres. Now we can go back to the postgres directory in our home directory and get the configuration going.

$ cd $HOME/postgresql-8.3.5

We’ll be doing a very similar ./configure from here like we did with Apache (and will do with PHP later). Some options that I normally use will not be in here as this is for basic configuration. The more you use and learn about PostgreSQL, you’ll figure out just how you want your setup to be. That also means that you’ll have to run ./configure again on the source code to recompile with these new settings. The only setting I’m using is the –with-perl just as an example. In all configure scripts, you can run ./configure –help and it’ll show you all available options for whatever you’re trying to install. We’ll then do the typical ‘make’ and ‘make install’.
$ ./configure –with-perl
$ make
$ sudo make install
After this is successfully done, you’ll have all the binaries you need built and everything’s been installed into /usr/local/pgsql. To make life easier, like with Apache, go into the bin directory and create symbolic links for everything to be in /usr/local/bin.
$ cd /usr/local/pgsql/bin
$ for i in `ls`
ln -s /usr/local/pgsql/bin/$i /usr/local/bin/$i && echo $i created in /usr/local/bin
Now we need to start the server. The password you assigned to user postgres earlier is in need now. We’ll run the which command that shows us which command we’ll be using, in this case, it’s pg_ctl. All the information you should see is listed and you’ll notice I added some additional options to initdb, mainly -E for encoding and –locale for the locale that the database cluster will be using. The UTF-8 encoding and locale is the easiest and causes a lot less problems (cough, phpBB3, cough). NOTE: all databases you create after the cluster has been initialized will have this encoding and locale unless you specify otherwise with the ‘createdb’ options. This is done with ‘initdb’.
$ su – postgres

$ which pg_ctl
postgres:$ initdb -E utf8 –locale=en_US.UTF-8 /usr/local/pgsql/data
The files belonging to this database system will be owned by user “postgres”.
This user must also own the server process.

The database cluster will be initialized with locale en_US.UTF-8.
The default text search configuration will be set to “english”.

fixing permissions on existing directory /usr/local/pgsql/data … ok
creating subdirectories … ok
selecting default max_connections … 100
selecting default shared_buffers/max_fsm_pages … 24MB/153600
creating configuration files … ok
creating template1 database in /usr/local/pgsql/data/base/1 … ok
initializing pg_authid … ok
initializing dependencies … ok
creating system views … ok
loading system objects’ descriptions … ok
creating conversions … ok
creating dictionaries … ok
setting privileges on built-in objects … ok
creating information schema … ok
vacuuming database template1 … ok
copying template1 to template0 … ok
copying template1 to postgres … ok

WARNING: enabling “trust” authentication for local connections
You can change this by editing pg_hba.conf or using the -A option the
next time you run initdb.

Success. You can now start the database server using:

postgres -D /usr/local/pgsql/data
pg_ctl -D /usr/local/pgsql/data -l logfile start
Now you can start the server up as postgres and will see the following.
$ pg_ctl -D /usr/local/pgsql/data -l logfile start
server starting
You can ‘cat logfile’ and see what all was stated but if you get that message about the server starting, you’re ok to go. Now, let’s create your first database. That’s done with ‘createdb’ and we’ll use ‘forum’ as the db we’re creating.
$ createdb forum
If nothing was returned (like an error message about conflicting encoding and locale settings), then you now have a database named ‘forum’. To allow other users able to connect and make changes, you’ll have to use the ‘createuser’ command. We’ll create a superuser ‘vorlin’ for example which means tester can do pretty much anything. I use ‘vorlin’ since that’s my username on the linux server I have and it should be noted that roles created should be the same as those on the box as it makes it easier. You create user roles for the database that don’t exist on the box but it’s advisable to use the option to password-protect them when you create the database (see createdb –help). And since we’re still logged in as postgres, we’ll have to exit out.
$ createuser vorlin
Shall the new role be a superuser? (y/n) y
There’s now a role named ‘vorlin’ that can connect (to any database created, no matter who owns it), create databases, drop databases, change or otherwise modify anything in said database, etc.
$ whoami
$ exit
$ whoami
$ psql forum
Welcome to psql 8.3.5, the PostgreSQL interactive terminal.

Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit

And that’s it, you have a successful database created, a role created, and connected to it. To get rid of the database, all you have to do is ‘dropdb forum’ and as long as nobody’s connected to it or there aren’t any idle connections (see php’s pg_pconnect for persistent connections), it’ll be deleted. NOTE: the postgresql server can’t be started or even stopped by root and it can’t be started or stopped by using sudo. Next is PHP’s configuration.

[b]Configuring PHP:[/b]
PHP doesn’t have to have a lot of configuration options set since the basic ones already at default do pretty good. You can change things later via editing the php.ini file, but once again, ‘./configure –help’ will show everything that you can enable/disable as well as whether or not that setting in particular is set by default.

Run the now-known configure script in the php-5.2.6 directory with any settings you want to set. Since we’re building php with the apache module and postgresql support, we’ll need to set those options. These two settings are important since the –with-pgsql= will tell php what the settings are and such, and –with-apxs2=/usr/local/apache/bin/apxs will tell php where the apache 2 apxs command is that it will need. I’ve listed below the files and directories as to where they would be found using /usr/local as the base directory.

BIG NOTE: php has a ‘make test’ that they recommend you run after a successful compilation. You don’t have to run it, but I did the first time, so I recommend you do too. It’s over 4000 tests and if you have a pretty decent machine, it’ll run pretty fast and take around 10-15 minutes. Yes, you read that right. If you’re on a slow(er) machine, go ahead and go to the mall or watch a movie. Some things will fail, but that’s ok.

Apache: /usr/local/apache/bin/apxs (note: even though php’s setting is –with-apxs2, it’s just apxs)
Postgres: /usr/local/pgsql/bin/pg_conf

$ cd $HOME/php-5.2.6
$ ./configure –with-pgsql=/usr/local/pgsql/bin/pg_config –with-apxs2=/usr/local/apache/bin/apxs2
$ make
$ make test
$ sudo make install

Now that php’s been successfully configured, tested, and installed, it’ll have put three files into /usr/local/bin. Those are the php binary itself, phpize, and php-config. The php binary will be used quite extensively, both with apache as well as any scripts you want to write in php itself on the command line. If you run a secured website and you don’t want users to use php on the command line, just compile it with the added –disable-cli setting. This will build and install the module for apache’s 2.x support and allow postgresql commands to be executed, but no php binary will be built.

To test the php binary, you can do one of two things (or both).

The first option is to check for the version.
$ which php
$ php -V
PHP 5.2.6 (cli) (built: Dec 5 2008 20:55:24)
Copyright (c) 1997-2008 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2008 Zend Technologies

The second option is to create a simple html file in our new directory /usr/local/apache/htdocs and use the ‘phpinfo();’ function. It’ll look like the following given. The file will be listed as ‘test.php’.

That should show a pretty sizable page of php’s configuration. Once shown, you can get rid of that test.php file.

Hopefully this helps anyone who’s looking to make a LAPP setup. I know I’ve done it enough times (with PostgreSQL) and decided just to write up something for me to use in case anything slipped on how I did it before, hehe.

–Vorlin :: proponent of [url=http://tazforum.thetazzone.com]The Taz Security/IT/Programming site[/url]

By admin

Former Freehand Freelance Graphic Illustrator... been online since 2004 ( late starter ), blogging since 2005, presently writing a suspense-thriller e-book that began as a screenplay.