Showing posts with label Apache. Show all posts
Showing posts with label Apache. Show all posts

Wednesday, January 6, 2010

Installing Oracle, PHP, and Apache on Linux

Let's walk through the steps required to install the Oracle Database, Apache HTTP Server, and PHP as an Apache module on Linux. Check out the OTN Linux Technology Center to see what versions of Linux are certified for use with the Oracle Database. We will be using Oracle Enterpise Linux for this example.

Software Requirements:

Software Version URL
Oracle Database 10g Express Edition 10.2 http://www.oracle.com/technology/products/database/xe/
Apache HTTP Server 2.0 http://httpd.apache.org/download.cgi
PHP Hypertext Processor 5.2 http://www.php.net/downloads/

Installing Oracle

You have a choice here. You may either install the database locally on this Linux machine, or you may decide to use an Oracle server located on another machine on your network. If your database is remote, jump to the article on Installing PHP and the Oracle Instant Client for Linux and Windows.

Otherwise, if this is your first time with Oracle, installing the Oracle Database 10g Express Edition only takes a few minutes. Download the Express Edition (commonly known as "XE") RPM package, log in as root and run:

    # rpm -ivh oracle-xe-univ-10.2.0.1-1.0.i386.rpm

After the software package is installed on the machine, configure a database by running this and answering its four questions:

    # /etc/init.d/oracle-xe configure

For Debian users, a .deb file is also available.

Starting and Stopping Oracle

Oracle XE will be running after installation. You can test it by opening your browser to the Database home page http://localhost:8080/apex. Use the username "SYSTEM" and the password you chose during installation.

Note: You may need to replace "localhost" with the IP address 127.0.0.1 or your machine's DNS name if you are behind a firewall or if localhost does not resolve for some other reason.

If you need to restart the database at any time use the Start Database and Stop Database items on the operating system's "Oracle Database 10g Express Edition" menu. To run these you will need to add yourself to the operating system "dba" group and re-login to the machine.

Alternatively you can call the oracle-xe script as the root user:

    # /etc/init.d/oracle-xe stop

To restart:

    # /etc/init.d/oracle-xe start

Installing Apache HTTP Server

Now that the Oracle Database is installed, you should install Apache. You must install Apache before you can install PHP, since PHP will be installed into Apache.

If you don't want to mess about compiling Apache or PHP, Oracle has pre-built PHP RPM packages that use the default Apache package.

If your operating system doesn't already have Apache, download httpd-2.0.63.tar.bz2 from the Apache web site, log in as the root user and execute these commands:
    # tar -jxvf httpd-2.0.58.tar.bz2

# cd httpd-2.0.58
# ./configure --prefix=/usr/local/apache --enable-module=so
# make
# make install

When configuring the web server, the option "--enable-module=so" allows PHP to be compiled as a Dynamic Shared Object (DSO). Also, the "--prefix=" option sets where Apache will be installed during the command "make install"

If you are familiar with the tar command on UNIX systems, you may be wondering why we did not need to invoke bunzip2 to extract the tar file. Linux includes the GNU version of tar which has a new 'j' flag to automatically uncompress a bzipped tar file. If you downloaded the gzipped file you could have used the 'z' flag instead.

Note: With Apache 2 you should use the default pre-fork MPM ("Multi-Processing Module") because many of the PHP extentions are not known to be thread-safe.

Starting and Stopping Apache

Start and stop Apache with the apachectl script:

    # /usr/local/apache/bin/apachectl start

You should test that Apache is up and running on your machine by opening your web browser to http://localhost/.

Now stop Apache so it can be configured for PHP:

    # /usr/local/apache/bin/apachectl stop

On Oracle Enterprise Linux, the apachectl script for the default Apache package is /usr/sbin/apachectl.

Installing PHP

To build PHP yourself, download the file php-5.2.9.tar.bz2 from the PHP downloads page.

Installation Steps

  1. Log in as the root user and execute these commands:

        # tar -jxvf php-5.2.9.tar.bz2
    
    # cd php-5.2.9
    # export ORACLE_HOME=/usr/lib/oracle/xe/app/oracle/product/10.2.0/server
    # ./configure \
    --with-oci8=$ORACLE_HOME \
    --with-apxs2=/usr/local/apache/bin/apxs \
    --with-config-file-path=/usr/local/apache/conf \
    --enable-sigchild
    # make
    # make install

    Note: if you are behind a firewall, you may need to set the environment variable http_proxy to your proxy server before running make install. This enables PHP's PEAR components to be installed.

  2. Copy PHP's supplied initialization file:

        # cp php.ini-recommended /usr/local/apache/conf/php.ini
    

    For testing it is helpful to edit php.ini and set display_errors to On so you see any problems in your code.

  3. Edit Apache's configuration file /usr/local/apache/conf/httpd.conf and add the following lines:

        #
    
    # This next section will call PHP for .php, .phtml, and .phps files
    #
    AddType application/x-httpd-php .php
    AddType application/x-httpd-php .phtml
    AddType application/x-httpd-php-source .phps

    #
    # This is the directory containing php.ini
    #
    PHPIniDir "/usr/local/apache/conf"

    If a LoadModule line was not already inserted by the PHP install, add it too:

        LoadModule php5_module modules/libphp5.so
    

Restart the Apache HTTP Server

You must now restart the Apache Server so that you can test your PHP installation.

    # /usr/local/apache/bin/apachectl start

Note: If you are using Oracle 10.2 but not the Express Edition, you must give the "nobody" user access to the Oracle directory. With Oracle 10.2.0.2 there is a script $ORACLE_HOME/install/changePerm.sh to do this.

If there are errors, they will display on your screen. They may also be recorded in /usr/local/apache/logs/error_log. If you have problems, double check your httpd.conf and php.ini, and make corrections.

When you start Apache, you must at least have ORACLE_HOME defined. Any other required Oracle environment variables must be set before Apache starts too. These are the same variables set by the $ORACLE_HOME/bin/oracle_env.sh or the /usr/local/bin/oraenv scripts.

To simplify things, you may create a script to start Apache. I did this and named it start_apache:

    #!/bin/sh


ORACLE_HOME=/usr/lib/oracle/xe/app/oracle/product/10.2.0/server
export ORACLE_HOME
echo "Oracle Home: $ORACLE_HOME"
echo Starting Apache
/usr/local/apache/bin/apachectl start
Testing Apache and PHP with Oracle

Testing PHP with Oracle is easy. You simply need to place a PHP file into your htdocs directory; normally /usr/local/apache/htdocs.

Here are two files, the first is used to test basic PHP installation. Open it in a browser with http://localhost/phpinfo.php. If PHP is installed you should see a large page full of PHP configuration information.

phpinfo.php
   

Check there is a section titled "oci8".

oci8test.php

The second file will display name and salary columns from the EMPLOYEES table owned by the HR user. This requires the HR schema be installed otherwise you will need to modify the script. The HR schema comes with Oracle XE. You can unlock access and set a password using the Adminstration section of the Database home page.

For Oracle XE the database connection string is simply "127.0.0.1/XE". If you are not using Oracle XE then change the connection string (third parameter) to the Oracle Net entry for your database.

  Oracle PHP Test";

echo "

Oracle PHP Test


";
echo "\n\n";
echo "\n\n\n";

for ($i = 0; $i < $nrows; $i++ ) { echo "\n";
echo "";
echo "";
echo "\n";
}

echo "
NameSalary
" . $results["LAST_NAME"][$i] . "$" . number_format($results["SALARY"][$i], 2). "
Number of Rows: $nrows
";
echo "
If you see data, then it works!
\n";

?>

Friday, December 11, 2009

Building Apache with PHP, MySQL, OCI8 instant client support

There are many howtos on building Apache with PHP, MYSQL support, but it is very rare to find some document on OCI8 support with Apache Build, recently I was building a web server in my data center and I was really in trouble when I was searching some installation document, there are few documents , but most of them are using either RPM version of packages or they are missing some of the essential steps. I am trying to write a howto that will help you to at least understand the installation with step-by-step method.

Plateform:

I am using IBM x system x86_64 bit server with RHEL 4 Update Level 4.

So it begins :

1.Download Oracle Instant Client.

First of all you have to download Oracle Instant Client, here is the link to download it, I am downloading for Linux x86_64, you can adjust according to your hardware.

Here is the link where you can find exact Installer for your hardware.

http://www.oracle.com/technology/software/tech/oci/instantclient/index.html

As I am doing it on Linux x86_64, so i will use following link:

http://www.oracle.com/technology/software/tech/oci/instantclient/htdocs/linuxx86_64soft.html

Here you have to download following packges.

1. instantclient-basic-linux-x86-64-10.2.0.2-20060228.zip

2. instantclient-sqlplus-linux-x86-64-10.2.0.2-20060228.zip

3. instantclient-sdk-linux-x86-64-10.2.0.2-20060228.zip

After downloading to your local directory you have to unzip them, as:

$ cd $HOME

assuming you have downloaded in your user home.

$ unzip instantclient-basic-linux-x86-64-10.2.0.2-20060228.zip

$ unzip instantclient-basic-linux-x86-64-10.2.0.2-20060228.zip

$ unzip instantclient-sdk-linux-x86-64-10.2.0.2-20060228.zip

These will unzip to $HOME/instantclient_10_2.

Create the client library symbolic link:

$ ln -s $HOME/instantclient_10_2/libclntsh.so.10.1 $HOME/instantclient_10_2/libclntsh.so

2. Setting up enviroment:

If you are installing all this from oracle user who is suppose to be owner of your database server then you have to make changes in oracle user .bash_profile, it is most likely that you have DB Server and Apache on same server then you might have set some of the variables otherwise you have to write them in oracle .bash_profile, but some time you may have a situation where your Web Server i.e. Apache Server is seperate then Oracle DB Server, I mean both of them are on seperate servers (machines) then you will just need to add LD_LIBRARY_PATH variable in your current user .bash_profile, i.e. the user who is installing all this here.

Now if you are installing through oracle user :

$ vi $HOME/.bash_profile

and add/edit following lines,

export ORACLE_HOME=/path/to/your/installdir
export ORACLE_BASE=/base/dir
export ORACLE_OWNER=user
export ORACLE_SID=database
export ORACLE_TERM=xterm
export TNS_ADMIN=/path/to/your/installdir/network/admin
export LD_LIBRARY_PATH=$HOME/instantclient_10_2/

But if you are not installing from oracle user, the case above discussed then:

$ vi $HOME/.bash_profile

and just this line:

export LD_LIBRARY_PATH=$HOME/instantclient_10_2/

Now are done with environment setup.

3. Installing Apache:

Now you have download Apache source to compile on your machine, here is the link to download:

http://httpd.apache.org/download.cgi

I am using Apache1.3. Now it depends on you, which apache version you wish to download, but still people recommend Apache1.3, and I will stick to this recommendation.

To open the Apache tar, change filename according to your apache release:

$ tar zxvf apache1.3....tar.gz
$ cd apache1.3...
$ ./configure --prefix=$HOME/apache --enable-so --with-mpm=prefork --with-port=8888

You can change port according to your need, even 80, but never try this on production server.

$ make
$ make install

Now we are done with Apache for now.

4. Installing PHP:

You can download your selected version of php source from,

http://www.php.net/downloads.php

My recommedation is PHP4.3.X I am using PHP 4.3.4 for my server. Now to open tar, do this:

$ tar zxvf php4.3.4..tar.gz
$ cd php4.3.4
$ ./configure --prefix=$HOME/php --with-apxs=$HOME/apache/bin/apxs --with-mysql=/usr --with-config-file-path=$HOME/apache/conf --with-oci8-instant-client=$HOME/instantclient_10_2 --enable-sigchild
$ make
$ make install
$ cp php.ini-recommended $HOME/apache/conf/php.ini

Add these lines to the $HOME/apache/conf/httpd.conf file:

      AddType application/x-httpd-php         .php
AddType application/x-httpd-php-source .phps

Restart Apache:

$ $HOME/apache/bin/apachectl start

To check the extension is configured, create a simple PHP script phpinfo.php where the web server can read it.

Load the script into a browser using an "http://" URL. The browser page should contain an "oci8" section saying "OCI8 Support enabled".