In: Categories » Computers and technology » PHP » How to Install PHP into Apache
| In this section, we look at how to install PHP into Apache. The first step is to download it from the PHP web site. There are other sources for PHP around the Web, but it is much easier to get it from the source. Downloading PHPPHP is available as a free download from the PHP web site, http://www.php.net/ downloads.php. The file that you need to download is at the top of the page, in the section labeled Complete Source Code. The current file at the time of this writing was for PHP 4.3.1. Its entry looks like this: PHP 4.3.1(tar.gz) [4,395Kb] – 17 February 2003 Development of PHPNew versions of PHP are released frequently. At the time of this writing, PHP 5 is currently being tested and is likely to be released soon. It is unlikely that Dreamweaver MX will offer extensive support for PHP 5, however, so you should download the most recent version of PHP 4 if you wish to use Dreamweaver's design features with PHP. PHP 5 is something of a new digression in PHP development. Because of this, advances and improvements to PHP 4 will continue, even after the release of PHP 5. Once you've clicked on the file, you'll be taken to a mirror page where you can select a country nearest to you to download from so you can get a faster download. Download the file to your /usr/local directory, as that is where you'll be installing the file. Now that you have the required file, you can move on to extract it and start the installation. Note that you need to install PHP as the Linux root user. Extracting the PHP FileYou are going to install PHP into the location /usr/local/ First, you need to create a php directory in this location, and then you need to move the PHP file to that location. This can be done with the following commands: cd /usr/local/ mkdir php cd php cp /usr/local/php-4.3.1.tar.gz ./ Note that the PHP file contains the version number within it. If you have downloaded a later version, you'll need to change the preceding filename to reference the name of the file you downloaded. You should now be in the directory /usr/local/php/ And there should be a copy of php-4.3.1.tar.gz at that location.To extract this file, enter the following command: tar -xzvf php-4.3.1.tar.gz This will extract the archive, and you should now see another directory at the following location: /usr/local/php/php-4.3.1 As with the Apache installation previously, you're going to create a symbolic link between the php-4.3.0 directory with the name php. In your configuration files, you can then refer to this symbolic link, and in future the link can easily be changed to point to later PHP versions as and when you install them. This allows new versions to be installed without having to change any configuration options. To create the symbolic link, enter the following command: ln -s php-4.3.0 php From now on, you can refer to the directory php as if you were referring to the directory php-4.3.0. You also need to create a new directory, which will be used later by PHP to store temporary files. This directory needs to be called tmp and you'll need to create it at the following location: /usr/local/php/ Do this with the command mkdir /usr/local/php/tmp You are going to be installing PHP as a DSO module. To do this, Apache must have been originally built to allow DSO support, as described in the first section of this tutorial. If you're upgrading a previous version of Apache, you can check that Apache has been built with DSO support with the following command: /usr/local/apache/bin/httpd -l Note that the argument passed is a lowercase l, not a numeric 1, and that you won't need to do this at all if you installed Apache as outlined in the first part of this tutorial. If Apache has been built with DSO support, the preceding command will return something similar to the following: Compiled-in modules: http_core.c mod_so.c If you see the two previously listed files, http_core.c and mod_so.c, it means that Apache has been built with DSO support. If not, you'll need to recompile Apache to allow DSO support. To add PHP as a DSO module for Apache, you must tell PHP the path to the Apache apxs program, which integrates the module with Apache. If you built Apache as per the first part of this guide, then the path to the Apache apxs command will be as follows: /usr/local/apache/httpd/bin/apxs You also need to tell PHP where its php.ini file will be located. Because the two are so closely related, it's convenient to store the php.ini file (a file that PHP will use to set configuration options at start-up) in the same directory as the Apache configuration files. Again, if you followed the first section of the tutorial, this location will be /home/www/conf/ Now that you have the correct paths, you can start the process of compiling PHP, which you can do by using the following command (from the location /usr/local/php/php on your Linux machine's drive system): ./configure --with-apxs=/usr/local/apache/httpd/bin/apxs --with-config- file-path=/home/www/conf --with-mysql There are many options available for configuring PHP. The options shown here are for a basic installation. If you wish, you can add more extensions later on by recompiling PHP. A complete list of options can be found in the online PHP manual at http://www.php.net/. Once you have entered the configure command, you'll see various checks being executed, such as making sure that the path to the apxs command is correct. Once everything has completed, you will be returned to the command prompt. Compiling PHP and Configuring ApacheYou now need to start the actual compile process, which turns the PHP source code into a working program. To start this process, enter the following command: make This process may take a while longer than the time needed to compile Apache in any case, because PHP is a much larger program. As a guideline, we found that on a machine fitted with an AMD K6/2-266 MHz CPU, the process took around 10 minutes, whereas on another Pentium III 500 MHz machine with 256MB of RAM, a time of around 7 minutes was experienced. You'll need to be patient. If everything goes successfully, you'll be returned to the command prompt with no error messages. Now you can complete the installation. First, stop your Apache server: /usr/local/apache/httpd/bin/apachectl stop Now execute the following command: make install Again, this will take a couple of minutes, so be patient while the compile takes place. Once the process is complete, you should see a message indicating that the operation was successful and that PHP is now installed. If There Are Any Errors in the PHP make ProcessIf there are any errors in the process, it will be because PHP cannot find some software that it needs or, alternatively, the version number of installed software is lower than PHP requires. If this is the case, you'll receive an error message that will tell you the problem, which software is needed, and also the version number required. You can then search on the Internet for the required software and install it. Once the correct software is installed, you will need to restart the installation from the configure command. However, before you enter the configure command, you will need to delete a file at the following location: /usr/local/php/php/config.cache You can do this with the command rm /usr/local/php/php/config.cache It's important to do this, as it means that PHP will recheck all the software installed and register the new version number. When you extract PHP into the usr/local/php/ directory, it creates a further directory called php-4.3.0. As with Apache, we've created a symbolic link to it called php. Under the main usr/local/php/ directory go all the different versions of PHP. You just set the symbolic link, php, to point to the relevant version. This means the path is always /usr/local/php/php/. Before you can restart Apache with PHP, you need to configure both Apache and PHP to talk to each other, which you will do next. Configuring ApacheThe Apache configuration file is changed automatically during the PHP installation, but it's important to check that the correct settings have been made. You also have to add some extra information. Open the Apache configuration file, httpd.conf, in a text editor. You will find it at the following location: /home/www/conf/httpd.conf The section that you need to look at is just before the second section of the configuration file and is labeled #Dynamic Shared Object (DSO) Support. You need to check that the following line is present: LoadModule php4_module libexec/libphp4.so This tells Apache to load the PHP module, named libphp4.so and located in the Apache libexec directory. libphp4.so should have been copied to the Apache libexec directory, but it's worth checking that the file has been correctly copied to this location. The full path to check is /usr/local/apache/httpd/libexec/libphp4.so If the file isn't there, you should copy it to this location from the PHP installation directories. You also need to add a new setting to the end of this section: AddType application/x-httpd-php .php This tells Apache to process any files with the file extension .php using the PHP module. If you wish to process other page extensions, you just need to copy the preceding line and change the page extension to the one you wish to use. Another optional setting you may wish to add is as follows: AddType application/x-httpd-php-source .phps This adds a new feature that allows pages with extension .phps to be displayed as PHP source code with syntax highlighting. This is useful if you wish to display PHP code on a web site. You can control the colors used for the formatting in the php.ini file. The lines may already exist, but they may be commented out with a semicolon. If this is the case, just remove the semicolon from the front of the line and the line becomes active. This completes the configuration of Apache, so save httpd.conf. You now need to configure PHP to complete the configuration process. Configuring PHPTo configure PHP, you edit a file called php.ini, which contains all the PHP settings. You need to create this file, but because of the large number of settings involved, you don't create it from scratch. The PHP directory to which you extracted PHP will contain two templates for the php.ini file, each for different uses. php.ini-distThis file is a template for the php.ini file for a development server, and it contains settings that are ideal for code development. php.ini-recommendedThis file is a template for the php.ini file of a production web server. It contains settings that lock down PHP and offer optimum security settings for scripts running on the server. As you are setting up a development server, you are going to use the file php.ini-dist as your base. You need to copy this file from the PHP installation directory to the directory you specified in the configure command, in this case /home/www/conf To do this, use the following command: cp /usr/local/php/php/php.ini-dist /home/www/conf/php.ini This has created your base php.ini file at the following location: /home/www/conf/php.ini You can now open the php.ini file in a text editor and configure it for the server. There are a number of important settings, and we'll look at these one by one, working from the top of the file to the bottom. The first setting is found roughly one quarter of the way into the file. max_execution_timeThis setting defines the maximum time that a PHP script can run before being shut down, specified in seconds. The default setting is max_execution_time = 30 If you're running a PHP script that does a lot of processing, you may need to increase this value to allow the script to complete its work. memory_limitThis setting governs the amount of memory a PHP script can use, and it is specified in megabytes. The default setting is memory_limit = 8M It's important that this setting isn't too high. A number of scripts using a large amount of memory and running at once could overload a server, so this value is best left at its default setting unless you happen to know that you have a particularly large script you need to run. error_reportingThis setting controls the level of error reporting that PHP uses. The default option is error_reporting = E_ALL & ~E_NOTICE This displays all errors and warnings, but not notices. This is usually the best setting, as notices can sometimes cause problems. Notices show noncritical information about a script. For instance, if a PHP script is making reference to a variable for the first time, the variable name will not be recognized by the interpreter. A notice would interrupt the execution of the page to insert a message to this effect. doc_rootThis setting is quite a way down the page from the previous one, so you'll need to scroll down a bit to get to it. It serves the same function as Apache's DocumentRoot setting, and it should be set to the same directory: doc_root = /home/www/webroot extension_dirThis should be set to the path where the PHP extensions folder is located, which is the extension folder at the location where you installed PHP. For this installation, it should be set to extension_dir = /usr/local/php/php/extensions file_uploadsThis option specifies whether to allow file uploads through a web browser. The default setting is file_uploads = On which allows file uploads. Set this to Off if you don't want to allow uploads, although on a development server this setting is often set to On. upload_tmp_dirThis setting tells PHP where it can temporarily store files that have been uploaded through a web browser. The path needs to be changed to the tmp directory that you created earlier: upload_tmp_dir = /usr/local/php/tmp upload_max_filesizeThis sets the maximum size for files that can be uploaded through a web browser, and the default setting is as follows (in megabytes): upload_max_filesize = 2M You should use a setting just larger than the maximum file size you expect to be uploading. session_save_pathThis setting is again some distance down the page, so you'll need to scroll down to get to it. It should be set to the path of the tmp directory created earlier.This will allow PHP to store temporary session data. This should be set as shown here: session_save_path = /usr/local/php/tmp This completes the PHP configuration, so save and close the php.ini file. You now need to restart Apache so that it uses your new settings. Restarting ApacheTo restart Apache, you need to use the apachectl command to stop the server and then start it up again. You do this with the following commands: /usr/local/apache/httpd/bin/apachectl stop which should return the message /usr/local/apache/httpd/bin/apachectl stop: httpd stopped Then to start the server, use /usr/local/apache/httpd/bin/apachectl start which should return the message /usr/local/apache/httpd/bin/apachectl start: httpd started If you get any error messages when you restart Apache, it's usually because of an incorrect setting in either httpd.conf or php.ini. If this is the case, the error message should let you know which file the error is located in and which setting is at fault. You can then correct the error and restart Apache. Once Apache has started successfully, you can test it to make sure it's serving PHP pages. Testing Apache and PHPTo test Apache and PHP, you need to create a new PHP page and add the following PHP function call to the page body: <?php phpinfo(); ?> Save this page as phptest.php in the directory you specified as the documentroot in the httpd.conf file. For the installation we have outlined in this tutorial, the file should be saved at the following location: /home/www/webroot/phptest.php You can now open a new web browser window and use the URL http://ipaddress/phptest.php where ipaddress is the IP address of your Linux server. If everything is running successfully, you should see the PHP Information screen as shown in the screen shot to the right, which shows information about both the Apache and PHP installation. It is, in itself, extremely useful for troubleshooting. If you get a 404 - Page Not Found error, check the troubleshooting guide below. If you get what looks like a blank page, use the View Source option in your browser. It's likely you'll see the PHP command displayed, which means that although Apache is serving your file, it's not being processed by PHP, and again you can refer to the troubleshooting guide. Troubleshooting the PHP InstallationThe first step is to check the Apache httpd.conf file, which you have placed at the following location: /home/www/conf/httpd.conf Make sure that both of the following lines are present in the file: LoadModule php4_module libexec/libphp4.so AddType application/x-httpd-php .php If PHP pages are being served by Apache but are not being processed by PHP, it's likely that the second line is missing. If the preceding lines are correct, then you need to check the php.ini file, which you placed at the following location: /home/www/conf/php.ini Check that the paths for the following settings are correct: doc_root = /home/www/webroot extension_dir = /usr/local/php/php/extensions If you change any of the preceding settings, you'll need to stop and then start Apache, so that the new settings take effect. You can then request the phptest.php page again to see if the installation is working. If PHP is running, but you have problems with file uploading or sessions in your PHP scripts, open php.ini and check that the following paths are correct: upload_tmp_dir = /usr/local/php/tmp session.save_path = /usr/local/php/tmp Check that you created the tmp folder in the preceding location and that Apache has permission to write to that folder. If you still have problems, your first stop should be the online PHP manual (http://www.php.net/docs.php). Here, you'll find many troubleshooting suggestions and checks, as well as comments from real-life users detailing any problems they had and how they solved them. Since PHP and Apache are so popular, it's likely that someone has had a similar problem to your own and documented how he or she fixed it. A search at http://www.google.com/ and http://groups.google.com/ will help you come up with some information that can help solve any problems you may have.
|
legal disclaimer
1) Our website is not responsible for the information contained by this article as well for any and all copyright infringements by authors and writers. E-articles is a free information resource. If you suspect this article for any copyright infringements, please read the Terms of service and contact us to investigate the problem.
2) The E-articles directory team is not responsible for inaccuracies, falsehoods, or any other types of misinformation this tutorial may contain and will not be liable for any loss or damage suffered by a user through the user's reliance on the information gained here. Please read the Terms of service
Useful tools and features
related articles
PHP is finally getting the attention that i deserves, yes I have always believed that PHP is one of those neglected languages, neglected because they are used in abundance but there isn't enough programs or as we call them frameworks to work on PHP. But that was until the release of PHP 5. After the release of PHP, there is a range of Frameworks available. Today we review and understand closely the various frameworks available for PHP. Some of the most popular frameworks for PHP are: ...
2. Common PHP Errors
In this tutorial, we are going to look at some of the common PHP errors that occur and how to solve them. Parse Errors A parse error occurs when the format of your PHP code is incorrect. For example, the following code: <?php for($i=1;$i<10;$i++){ $output = "Current Iteration: " . $i . "<br>" echo $output; } ?> will return an error similar to the following: Parse error: parse error, unexpected T_ECHO in c:\webserver\test2.php on lin...
3. PHP mail
In this article, we are going to look at some of the frequently asked questions regarding e-mail and PHP. We will begin by looking at a more fundamental issue: how to actually send an e-mail as HTML. How Do I Send an E-mail As HTML? As the PHP mail function defaults to sending plain text e-mails unless otherwise specified, a frequent question is how to send HTML e-mails using the mail function. The format for the mail function is as follows: mail($to, $subject, $message, $headers); ...
4. PHP Date and Time
In this tutorial, we look at commonly asked questions regarding the date function in PHP. How Do I Read the Date or Time from the Server? To read the date or time from the server, you need to use the PHP date function. You pass the date function a string, with special tokens in place of the date parameters you require. When the code is run, the tokens will be replaced with the date or time section that they represent. For example: <?php echo date("d/m/Y"); ?> outputs 11/02/2003 &...
5. PHP Random Password
In some applications, it can be useful to generate a random password, such as setting the user's initial password and then letting the user change it if he or she wishes. The function shown in the following code generates a password with a number of random characters, and you can set the length of the password when you call the function: <?php function randomPassword($length) { $possibleCharacters = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"; $characterLength = strlen($possibleCharac...
6. Installing PHP with Apache on Windows
We try to install PHP into Apache so it can process PHP pages and static HTML pages. We assume that you have installed and tested Apache. Downloading PHP The first step is to download PHP, which is available from the PHP downloads page at http://www.php.net/downloads.php. Scroll down the downloads page until you find the section labeled Windows Binaries. The current version at the time of this writing is PHP 4.3.0, and the...










