Total Votes: | 1449 |
Avg. Rating: | 9.43/10 |
Compiling PHP and Apache 2 from source on Linux OSBy: Sukhwinder Singh |
|
IntroductionThis 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.
Back to Top Why compile from sourceYou 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 AssumptionsI 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 startBefore 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. Back to Top Compilation ProcessLogin 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 ApacheUsing 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.
{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:
Now you will have a new file named httpd-2.0.44.tar in your current directory. Now to extract its contents use:
Both above steps can be completed with a single command: $ tar -zxvf httpd-2.0.44.tar.gz
A 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:
Now we have to configure apache for compilation. Configure ScriptAlmost 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:
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-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:
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. makeTo 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):
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
This will install Apache to /wwwroot directory. Now test your Apache installation by starting Apache:
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:
It'll show Apache homepage, where you can read Apache documentation to know more about Apache. To stop Apache you can use:
Back to Top Compiling PHPChange to directory where you have downloaded PHP source.
Then we have to extract PHP source files from php-4.3.0.tar.gz file. To decompress the compressed file use:
You will have a file named php-4.3.0.tar in your current directory. Now to extract its contents use:
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:
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:
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 :
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:
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:
Now you can execute make install from within PHP source directory to install PHP to /wwwroot/php directory:
make install will install PHP4 module to Apache's modules sub-directory (/wwwroot/modules) and add a line like this:
{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 <?php and ?> 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:
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):
{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:You can add any file extension in addition to .php if you want to invoke PHP parser for any other file extension also. Like:
will invoke PHP parser whenever any file with phtml extension (.phtml) is accessed. Save this file and then start Apache:
If no error message is displayed and everything is fine, Apache is started and you are returned to shell prompt. Back to Top Testing the PHP InstallationNow 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. 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 <installpath>/lib (if not changed at compile time using --with-config-file-path option).
You can change your PHP settings by editing php.ini file. For information about php.ini and its different settings see http://php.net/manual/en/ini.php. Now restart apache:
Back to Top Enabling some PHP extensionsIf 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 http://php.net/manual/en/configure.about.php. 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:
{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:
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 ConclusionI think now you should feel comfortable about compiling software from source code. It is easy. Now you can download php manual from PHP documentation download page (http://www.php.net/download-docs.php) 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 ResourcesMailing ListsMailing list for installation related issues is php-install@lists.php.net . You can subscribe to general PHP mailing list by sending mail to php-general-subscribe@lists.php.net . Information about mailing lists related to different PHP subjects can be found on PHP support page (http://www.php.net/support.php). There is a page on http://www.php.net 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 http://www.php.net/manual/en/faq.php 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 LinksPHP news server is news://news.php.net/ and can be accessed with a news client. Web interface for the news server is available at http://news.php.net/. Information about latest books related to PHP can be found on amazon.com Other PHP related information can be found on http://www.php.net/links.php. Back to Top About the AuthorAfter doing his graduation in 1996 Sukhwinder started learning about computers and this is the thing he is still doing. He used to spend most of his time in trying to learn languages and technologies. He has tried to learn almost all popular languages, OSes and software on his own, by experimenting or by getting information from books or web. This article is the result of that. He wrote it to help others like him. These days he doesn't have any time for experimenting because he is working as a freelancer full time. If you have any freelancing work related web development using php / mysql, asp / sql server, java servlets or jsp, you can contact Sukhwinder (+91-98158-00532). See his CV here. If you liked the article and want to support author, please visit some of the google sponsers. Back to Top Copyright @ 2002-2024 Sukhwinder Singh | Reprints | Back to Top | Web Designing and web Development (Last reviewed on Wednesday, September 03, 2014 2:30 PM) (First posted in May, 2002)
|