ABHIONLINUX
Site useful for linux administration and web hosting

Showing posts with label Compilation of apache and php. Show all posts
Showing posts with label Compilation of apache and php. Show all posts

2010/03/27

Compilation of apache and php

Apache 2 source distribution available from http://www.apache.org.
PHP source distribution available from http://www.php.net/.
For the compilation make and gcc must be installed in the server. If it is not installed you can download it and install from  ftp://mirrors.kernel.org/gnu/

Before starting to compile Apache and PHP you must uninstall all previous Apache and PHP related RPMs installed on your system. To uninstall these packages you can use Red Hat Package Manager (rpm) utility. If you are using X Windows you can also use GUI utilities like gnorpm  and kpackge, if installed, to uninstall these RPMs.


Compiling Apache

Using shell, (or open a new virtual console window if working in X Windows) change to directory where you have downloaded Apache 2 source. In my case it is /usr/src.

$ cd /usr/src
$ gunzip httpd-2.0.44.tar.gz or gzip -d httpd-2.0.44.tar.gz
$ tar -zxvf httpd-2.0.44.tar.gz
$ cd /usr/src/httpd-2.0.44
Now we have to configure apache for compilation.

./configure --help

$ ./configure --prefix=/wwwroot --enable-so

First option --prefix tells configure script that we want Apache to be installed in directory /wwwroot. If we don't provide a prefix option than it'll be installed in default location, which is /usr/local/apache2.

I am installing everything in /wwwroot because:

   1. when a new version of PHP and Apache is released, I only have to rename /wwwroot directory to some other name like /wwwrootold and then I can install new versions in /wwwroot directory again. If new installation works properly then I can simply copy configuration files from old directory to /wwwroot.


   2. users new to compiling software from source, after compiling and installing it, try to find a way to uninstall the software. So, benefit of keeping everything at one place is; if someone wants to uninstall Apache and PHP then he just has to delete /wwwroot directory (After stopping Apache, if it is running).

Second option --enable-so tells configure to enable module so, which allows Apache to load shared modules. We need this option because we are compiling PHP as Apache shared module.

Example Apache configure command line looks like this. After configure finishes we have to compile Apache.
make


To compile Apache a utility called make is used. make reads a file named Makefile in the source directory. In the Makefile step by step instructions are written about how to compile the software. Benefit of using make is that if some of the source files are changed and we compile software again, then only files which are changed and files which depend on changed files are recompiled.

To compile Apache source we have to issue this command in the Apache source directory (/usr/src/httpd-2.0.44):

$ make

When you type make It will start compiling Apache. It will take several minutes depending upon the speed of your computer. After make finishes, shell prompt is available. Now source has been compiled. We will use make install command to install Apache

$ make install

This will install Apache to /wwwroot directory. Now test your Apache installation by starting Apache:

$ /wwwroot/bin/apachectl start

If you are returned to shell prompt and no error etc. is displayed then Apache is started.

Now you can open a web browser like lynx and visit Apache homepage:

$ lynx http://localhost

It'll show Apache homepage, where you can read Apache documentation to know more about Apache.

To stop Apache you can use:

$ /wwwroot/bin/apachectl stop


Compiling PHP

Change to directory where you have downloaded PHP source.

$ cd /usr/src

Then we have to extract PHP source files from php-4.3.0.tar.gz file.

To decompress the compressed file use:

$ gunzip php-4.3.0.tar.gz

You will have a file named php-4.3.0.tar in your current directory. Now to extract its contents use:

$ tar -xvf php-4.3.0.tar

Both above steps can be completed by using this command:
$ tar -zxvf php-4.3.0.tar.gz

A new directory php-4.3.0 will be created in your current directory. For me its /usr/src/php-4.3.0.

Now change to this directory:

$ cd /usr/src/php-4.3.0

Now we have to configure PHP for compilation process. There are hundreds of options which can be provided to configure script. These options include the option to specify where PHP should be installed, which functionality should be enabled, like functionality to access mysql databases from PHP and which extensions have to be compiled etc. To see a list of options supported by PHP configure, type:

$ ./configure --help

It'll show a list of all options supported by the version of PHP that you are using.

Extensions provide additional functionality which core PHP doesn't provide. For example to create images --with-gd option can be used. But for these extensions to work, appropriate libraries must have been installed. If you use some --with option and that library isn't installed on your system then configure will fail. So, my advice is, for the first time don't try to use any extension.

To compile PHP as Apache shared module we have to provide path to apache apxs utility, which in our case was installed in /wwwroot/bin/ when we installed Apache. So, in PHP source directory (/usr/src/php-4.3.0) execute this command :

$ ./configure --prefix=/wwwroot/php --with-apxs2=/wwwroot/bin/apxs --with-config-file-path=/wwwroot/php --with-mysql

First option --prefix=/wwwroot/php tells configure script that we want PHP to be installed in /wwwroot/php directory. Otherwise it'll be installed in some default location (/usr/local).

Second option --with-apxs2 specifies that we want to install PHP as Apache 2 shared module.

Third option --with-config-file-path specifies that PHP should look for php.ini file in /wwwroot/php directory. Php.ini file contains various settings, which can be used to configure PHP after it has been installed. Settings like path to directory where php extensions are installed. Options like max_execution_time in php.ini specifies maximum time a script is allowed to run before it is terminated by PHP parser.

    {Note} You don't have to specify name of the php.ini file when using --with-config-file-path option. Only directory path where php.ini file will be stored has to be specified. So, don't use --with-config-file-path=/wwwroot/php/php.ini, but instead use --with-config-file-path=/wwwroot/php.

Fourth option --with-mysql enables support to access mysql databases through PHP. After --with-mysql we can optionally specify directory where mysql is installed like --with-mysql=/usr/local/mysql. To use mysql database functions you must have mysql database installed on your system. If you don't have mysql installed you can remove this option. If this option is not used then library, which is bundled with PHP is used to access mysql databases.

After configure finishes. You have to type make to compile PHP:

$ make

It will take several minutes to compile. After make finishes and, no error etc. is displayed then PHP has been compiled successfully. If any warning is displayed then, normally, you can ignore it.

After this, if Apache is running stop Apache:

$ /wwwroot/bin/apachectl stop

Now you can execute make install from within PHP source directory to install PHP to /wwwroot/php directory:

$ make install

make install will install PHP4 module to Apache's modules sub-directory (/wwwroot/modules) and add a line like this:

LoadModule php4_module modules/libphp4.so

to apache configuration file (/wwwroot/conf/httpd.conf). This line allows Apache to automatically load PHP module when Apache starts. If this line is not added by PHP install, which in my case wasn't,  then you can add it yourself. To add this line yourself, search for a word LoadModule in /wwwroot/conf/httpd.conf file. This word will be somewhere under section "Dynamic Shared Object (DSO) Support". Under this section, on a new line, add the above line.

Now you have to add another line to this httpd.conf file so that Apache invokes PHP parser whenever a file with extension php (.php) is accessed. When PHP parser is invoked by Apache it reads .php file which contains PHP code blocks, html tags and other text. Parser then executes PHP code found inside blocks and then merges PHP code results and other html content (as is). Resulting output is then sent back to Apache which in turn sends it to web browser which requested the file.

The line to be added is:

AddType application/x-httpd-php .php

To add this line search for word AddType in httpd.conf file. There will be a line like this:

AddType application/x-tar .tgz

Below this line add (on a new line):

AddType application/x-httpd-php .php

You can add any file extension in addition to .php if you want to invoke PHP parser for any other file extension also. Like:

AddType application/x-httpd-php .phtml

will invoke PHP parser whenever any file with phtml extension (.phtml) is accessed.

Save this file and then start Apache:

$ /wwwroot/bin/apachectl start



Enabling some PHP extensions:

You can get list of core configure options supported by PHP see http://www.php.net/manual/en/configure.php.

For example if you want to create images using PHP, you first need to install gd library using RPMs or by compiling from source. Then you can use --with-gd option to enable gd support from PHP like this:

$ ./configure --prefix=/wwwroot/php --with-apxs2=/wwwroot/bin/apxs --with-config-file-path=/wwwroot/php --with-gd

    {Note} GD depends on some other libraries to create images in different formats. So, some other libraries like png, jpeg , zlib also have to be installed.




.