Compile and setup PHP and Apache in Linux

This is a very good article that I came across written by a person called Sukhwinder Singh. In fact the article was so good that it helped me setup PHP and Apache on my Debian Box with minimum hassles. This is recommended read for all those having trouble setting up PHP and Apache on their Linux boxes.


This article will provide beginners and others new to Apache 2 and PHP compilation, detailed information about how to compile PHP and Apache 2 on Linux system. Even if you just want to learn how to compile software from source on Linux like OS, this article will provide enough information to get you started. After reading this article any person, who hasn’t yet compiled any software from source, will be able to compile and run PHP and Apache on his Linux Machine (hopefully).

In this article we are going to compile PHP as Apache 2 shared module because there is no option, in case of Apache 2, to compile PHP as Apache static module. It was possible with Apache versions prior to version 2.

If you just want steps, those are given here.

{Note} Compiling PHP as Apache 2 filter (shared module) is experimental at this time.

Back to Top

Why compile from source

You may ask, why we need to compile PHP and Apache from source when binary RPMs are already available and which don’t take much effort to setup. The reason is, it provides you much more flexibility. Instead of being dependent on other people’s RPMs you can decide what PHP and Apache functionality you want to enable. Another reason is that these RPMs are weeks or months out of date. When compiling from source, you can also specify the directory where software will be installed. Also, after you see your compiled software working, it really provides great satisfaction. Compilation process is really very straight forward.

Back to Top


I assume that you have basic skills required to operate Linux Operating System and you have root privileges. No prior knowledge of compiling software from source code is assumed.Back to Top

What you need

Back to Top

Before you start

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.

Then download apache 2.0 source and PHP source . These files have .tar.gz extension. Save these files to any directory of your choice. I am using PHP 4.3.0 and Apache 2.0.44 and have these files in /usr/src directory.

{Note} I faced no problems with Apache 2.0.44 and PHP 4.3.0. When you try to use other latest versions of PHP and Apache 2 or try to use older versions of PHP with latest version of Apache 2 or vice-versa, which are not compatible, you may face problems. Some did face these problems, three of which are mentioned here.

Most others faced no problems with Apache 2.0.40 / 2.0.43 and PHP 4.2.3 and Apache 2.0.44 and PHP 4.3.0. So, you shouldn’t face any compatibility problem, if using Apache 2.0.44 and PHP 4.3.0.

Back to Top

Compilation Process

Login as root to follow these steps.

As I earlier said, we are going to build PHP as Apache 2 shared module. Apache provides a facility to extend its functionality using separate modules. When PHP is compiled as Apache shared module its object code is not included in httpd binary. Instead, it works as a separate module which can be loaded by Apache and can also be disabled.

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

{Note} Beginners please note that $ is the just shell prompt symbol and it shouldn’t be typed. It is used just to show that these lines should be typed at shell prompt.

Then we have to extract Apache source code from compressed httpd-2.0.44.tar.gz. To decompress this file use:

$ gunzip httpd-2.0.44.tar.gz or gzip -d httpd-2.0.44.tar.gz

Now you will have a new file named httpd-2.0.44.tar in your current directory. Now to extract its contents use:

$ tar -xvf httpd-2.0.44.tar

Both above steps can be completed with a single command:

$ tar -zxvf httpd-2.0.44.tar.gzA new directory httpd-2.0.44 will be created in your current directory. For me it is /usr/src/httpd-2.0.44.

Now change to this directory:

$ cd /usr/src/httpd-2.0.44

Now we have to configure apache for compilation.

Configure Script

Almost every software which comes with its source code contains a configure script, which performs many checks and then prepares software for compilation. For example, it checks which kind of compiler is available and whether required libraries are installed or not. It also enables us to customize software according to our requirements. For example, we can specify where software will be installed.

Name of this script is normally configure. Apache also provides a configure script file named configure in its source directory (/usr/src/httpd-2.0.44 in my case). Using this script we can decide where Apache will be installed and which optional modules we want to compile etc. There are many options which can be supplied to configure. To see a list of options supported by configure, type:

$ ./configure --help

It will show a complete list of all options supported by the version of Apache that you have downloaded.

These options normally start with a “–with-xxx” and “–enable-xxx” and are separated by space. Where xxx is the name of the option like –enable-so (for apache configure script) and –with-mysql (for PHP configure script). Options we will be using for Apache are –prefix and –enable-so.

Execute this command in the Apache source directory:

$ ./configure --prefix=/wwwroot --enable-soFirst 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.


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

Back to Top

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.gzA 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.

Example PHP configure command line looks like this.

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/

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.

{Note} All lines that start with a # are comments and are ignored by Apache.

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

{Note} If you are unable to find any AddType line, then add the above line at the end of the file.

{Note} With Apache 2_0_28-beta line to be added at the end of httpd.conf file was:
SetOutputFilter PHP
SetInputFilter PHP

But with Apache 2.0.44 AddType syntax has to be used.

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

If no error message is displayed and everything is fine, Apache is started and you are returned to shell prompt.

{Note} With version 2.0.43 / 2.0.44 of Apache I have noticed a small problem. When you are experimenting with various PHP settings and trying to stop and start Apache again and again, then sometimes when you stop Apache all httpd processes are not stopped. Then if you try to start Apache again it displays error message like port is already used etc. and it doesn’t start. It happens only sometimes and not always. To solve this problem you have to manually kill that httpd process, using its process id (pid). To see the pid of that httpd process you can use:

ps -A

it will display processes that are running. If there is any httpd process in the list displayed, then note its pid and then use:

kill -9

with the actual process id of the httpd process. Also sometimes more than one httpd processes are displayed and you have to kill all httpd process otherwise you won’t be able to start Apache.

Back to Top

Testing the PHP Installation

Now create a PHP file named info.php, using any editor like vi or emacs, in the /wwwroot/htdocs directory and enter three lines, shown below, in this file. Directory /wwwroot/htdocs is Apache root directory. Apache publishes all files present in this directory to web users. This directory can be changed by editing httpd.conf file and changing DocumentRoot value to some other directory.


Save this file and then access this file using a web browser like lynx or Netscape Navigator by entering a url like this:


If PHP was installed successfully a page, shown in the image below, will be displayed containing detailed information about your PHP installation, Apache environment and PHP extensions loaded etc.

Screenshot of result of info.php. Click to enlarge.
Click for a larger image.
Now you can copy /usr/src/php-4.3.0/php.ini-dist to /wwwroot/php/ directory as php.ini.

{Note} Default location of php.ini is /lib (if not changed at compile time using –with-config-file-path option).

$ cp /usr/src/php-4.3.0/php.ini-dist /wwwroot/php/php.ini

You can change your PHP settings by editing php.ini file. For information about php.ini and its different settings see

Now restart apache:$ /wwwroot/bin/apachectl restart

Back to Top

Enabling some PHP extensions

If you are feeling comfortable about compiling PHP and Apache and everything is working fine then you can enable some PHP extensions or use some other configure options. First you have to run PHP configure script again and add relevant –with-xxx or –enable-xxx option, where xxx is the name of the extension or option you want to enable. For a list of core configure options supported by PHP see To find out all configure options supported by version of PHP source code that you have downloaded, you have to run ./configure –help in your PHP source directory. It will display a list of all the configure options supported by that PHP version. As I already said, for any extension to work its library must have been installed otherwise configure will fail. These extensions just provide an interface to the actual library. Then, after stopping Apache, you’ll have to run make followed by make install again in PHP source directory.

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.

PHP configure will try to find directory where gd and other libraries and their header files are installed by searching some default library and include paths, like /usr and /usr/local. Header files are normally found inside include directory, like /usr/local/include, and library files are found inside lib directory e.g. /usr/local/lib. If configure is unable to find gd library then you can specify path where you installed gd library like this:

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

If you compile gd from source then it’ll be installed by default in /usr/local. There was no configure script provided with gd library I used, and I had to edit Makefile to change default install location.

Since PHP version 4.3.0, gd library is bundled with PHP distribution. This version of gd has support for some more image related features. PHP documentation suggests that this bundled version should be used in preference to the external gd library which you may install yourself.

So, if you are using PHP 4.3.0, you just have to use –with-gd option without specifying any path and PHP will use the bundled gd library. But not all libraries on which gd depends are bundled with PHP 4.3.0. So, you still need to install some other libraries like jpeg , png and zlib (for png) to create jpeg and png images with gd. In my case configure was unable to find zlib, so I had to use –with-zlib-dir=/usr/local option. More detailed information about installing this bundled gd library can be found here.
Back to Top

Problems with swf and java extensions

If you are using PHP 4.3.0 or later then you can ignore this section.

SWF extension can be used to create shockwave flash movies using PHP code. There was a bug in swf extension of some 4.1.x and 4.2.x versions of PHP, which caused segmentation fault and PHP also failed. But this bug has been fixed in PHP 4.3.0.

Java extension allows us to create java objects from within PHP. Java extension also had some bugs which caused segmentation fault when used with JDK 1.4.0 (may be JDK 1.3.0 also). It worked fine with JDK 1.2.2. This bug seems to have been fixed in this (4.3.0) version of PHP. But, when using JDK 1.4.0 some effort still has to be made to enable this Java extension.

If you face problems with java extension, like those mentioned at, then do report them using the same page.

Back to Top


I think now you should feel comfortable about compiling software from sources. It is easy. Now you can download php manual from PHP documentation download page ( and start exploring PHP. If you faced problems when trying to compile PHP then you can subscribe to many related mailing lists (given below ) and post your problems there.

Do tell me if you were able to compile PHP and Apache 2 from source after reading this information and whether you found this information useful. I do check each and every mail I receive and update this page according to any feedback provided. Also write to me if any links etc. don’t work or about any other problem with this page.

Back to Top


Mailing Lists

Mailing list for installation related issues is . You can subscribe to general PHP mailing list by sending mail to . Information about mailing lists related to different PHP subjects can be found on PHP support page ( There is a page on where you can subscribe to and unsubscribe from various mailing lists using web interface.

But before posting your problems at those mailing lists you should read faq at because solution to many problems is already given there. After reading faq if solution isn’t found, then try to search mailing list archives to see if question that you have isn’t already answered. Links to archive sites are given at PHP support page. If you still cannot find an answer only then post your problem at those mailing lists.

But you must remember that you have to provide complete details about what problem you are facing,  what you were doing when problem occurred, what was your configure line etc. You should copy program’s output, like error messages, from command line or log files and append it to your message, only then someone will be able to help you on these mailing lists.

If you don’t know how mailing list works, click here.

Other Links

PHP news server is news:// and can be accessed with a news client. Web interface for the news server is available at

Information about latest books related to PHP can be found on Other PHP related information can be found on

Back to Top


About synapse
Programming, motorcycles and photography. Want to do more, but only have time for so much!

One Response to Compile and setup PHP and Apache in Linux

  1. Pingback: bearcoat pei shar

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: