Now that we finally have a complete complement of zero-cost Enterprise Linux 6.0 clones available, I thought it might be time to revisit the idea of building a very small, very lightweight Apache server using nothing but Centos (or Scientific Linux) 6.
My existing article on building such an Apache server uses Ubuntu as its base distro, because I know of nothing that has smaller minimum hardware requirements -but this is a bit annoying, because if you go on to use RHEL-clones for an Oracle server, you have to master both RPM-based and apt-get-based distros. Life would be a lot simpler if you could use the one distro for everything, wouldn’t it?
Well, that’s what I thought too, so here’s my recipe for Apache + PHP talking to an Oracle database with the entire software stack running on two virtual machines running Centos 6 or Scientific Linux 6.
Creating the Server
I won’t detail the actual construction of a suitable web-server-capable virtual machine or the installation of its operating system here. The Ubuntu-based article I mentioned before does that quite adequately, I think. There are a couple of Centos/Scientific-specific issues that arise, though.
First thing to note, then, is that you can’t install Centos 6 (or Scientific Linux 6) in a virtual machine that’s configured with less than 512MB of memory. You can run Centos 6 in just 128MB, but the installer will bomb out at those sorts of RAM allocations. So the basic rule is: install your OS when your VM has 512MB then shut the thing down, reduce the allocated RAM, and re-start.
Second, you only need the 32-bit version of Centos or Scientific: 64-bits for running a trivial web server are completely unnecessary. (Note that the Centos link there has been altered since this article was first written to point to the version 6.1 isos, since version 6.0 is no longer made generally available).
Third, if you’re using Scientific Linux, you’ll have to select to perform a minimal install. If you’re using Centos, that is the default option anyway.
Fourth: once the O/S is installed, make sure the network itself is working (see my last post on how to do this). It won’t be by default, so you need to edit the /etc/sysconfig/network-scripts/ifcfg-eth0 script to make it function properly.
Additionally, and very importantly, you should disable SELinux by editing the /etc/sysconfig/selinux configuration file. By default, it will have a line that says SELINUX=enforcing. Change that to read SELINUX=disabled. You can’t get PHP talking to an Oracle database without doing this. Reboot to make the change take effect.
Finally, the commands you want to install the relevant Apache/PHP software bits and pieces are:
yum -y install httpd php nano unzip make
yum -y install gcc wget openssh php-devel php-pear libaio
The ‘httpd’ bit is the name for the Apache package itself; the other bits and pieces allow Apache to serve up something useful later on!
Configuring the Server
You first need to start the Apache service:
service httpd start
If you ever need to re-start the service (to pick up configuration changes, etc), the command is:
service httpd restart
To make the Apache bits start automatically at every reboot, issue this command:
chkconfig --level 23 httpd on
By default, despite having done all of the above, you won’t be able to connect to your new server from a remote browser: Centos/Scientific slap a firewall on that blocks access. You can completely disable the firewall with the command:
service iptables stop
That only works per re-boot, though, so to switch the firewall off completely, use this:
chkconfig iptables off
The more subtle approach, of course, would be to reconfigure the firewall to allow http traffic through -but that’s beyond the scope of this article and, in any case, I don’t need a firewall when I’m connecting one internal VM to another, so it’s probably overkill at this stage. If you insist on this approach, however, the advice given here would seem useful.
You’ll now be in a position to check that everything is working fine, provided you know your web server’s IP address. Assuming it’s 192.168.0.37 for the moment, then visiting http://192.168.0.37 in a remote browser should net you some sort of “it’s working” page (the Centos version is quite elaborate and is entitled “Apache 2 Test Page”; Scientific’s equivalent is a barebones “it works!” message).
You’ll also need to check that PHP is working OK, and for that I suggest you create a file called phpdata.php in the /var/www/html directory containing the following:
<?php
phpinfo();
?>
…which is just the code needed to display PHP’s configuration data. Then you can visit http://192.168.0.37/phpdata.php and you should see this sort of result:

Connecting the Server to an Oracle database
Installing OCI8 so that you can connect to Oracle via Apache is pretty much the same process as for Ubuntu:
Copy both the instantclient-basic and instantclient-sdk packages (available from the OTN website) to /var/www/html using something like Filezilla. As root, unzip both packages and create a necessary symbolic link:
cd /var/www/html
unzip instantclient-basic-linux32-11.2.0.2.0.zip
unzip instantclient-sdk-linux32-11.2.0.2.0.zip
cd instantclient_11_2
ln -s libclntsh.so.11.1 libclntsh.so
Download the relevant OCI8 library (the version numbers are specific, so check http://pecl.php.net/package/oci8 to make sure you get the latest):
cd /var/www/html
wget http://pecl.php.net/get/oci8-1.4.5.tgz
Install that new download:
pecl install oci8-1.4.5.tgz
This last command will prompt you to specify “the path to the ORACLE_HOME directory…”. At this point, you type in the following:
instantclient,/var/www/html/instantclient_11_2
…which tells the installer (a) that you’re using the instant client and not a full-blown Oracle client; and (b) that the path to the instant client files is /var/www/html/instantclient_11_2. Note that there are no spaces in any of that lot: just a comma separates the two components.
Finish off with some final configuration steps:
echo extension=oci8.so >> /etc/php.d/oci8.ini
echo /var/www/html/instantclient_11_2 >> /etc/ld.so.conf
ldconfig
echo ORACLE_HOME=/var/www/html/instantclient_11_2 >> /etc/profile
export ORACLE_HOME=/var/www/html/instantclient_11_2
service httpd restart
When you now visit your web server’s phpdata.php URL, you should see an OCI8 section appear in the PHP configuration information display.
After that, it should be relatively plain sailing: you just need to create a file which explains how to connect to an Oracle database and fetch some data. I suggest you create a file called oracle.php in the /var/www/html directory, containing this:
<?php
$myselect = "select * from scott.emp";
$oraconn = oci_connect('system', 'password', '//192.168.0.145/lindb');
$doquery = OCIparse($oraconn, $myselect) or die("Couldn't parse statement.");
OCIexecute($doquery) or die("Couldn't execute statement.");
while (OCIfetch($doquery))
{$surname = OCIresult($doquery, ENAME);
$salval = OCIresult($doquery, SAL);
print '<b>'.$surname.'</b> '.$salval.'<br>';
}
?>
That assumes my Oracle server is running at IP address 192.168.0.145; that my SYSTEM password is “password” (which would be extremely dumb if true!); and that I’ve got the SCOTT schema created within that database containing ye olde EMP table. If you now visit the URL http://192.168.0.37/oracle.php, you should get 14 rows returned in your web browser.