Searching...
Sunday 2 June 2013

Installing Apache and PHP on Linux


Linux Apache php mysql


Most developers prefer to develop applications on a local workstation/laptop or on a dedicated development server, you’ll probably need to know how to at least install and configure PHP and a web server (apache and ms IIS). Asking "Why should i learn to install php and a web server" is the same as asking "Why should a soldier need to load/handle a gun?". It is something you will never regret about. 
In this lecture you will be guided to install apache and PHP on Linux Platform.

Look for necessary links in the footer

DOWNLOADING APACHE

These days, Apache is packaged with all mainstream Linux distributions. So if you’re using one of these platforms, chances are quite good you already have it installed or can easily install it through your distribution’s packaging service (e.g., by running the apt-get command on Ubuntu). However, if you’d like to install Apache manually, follow along with this section.

Because of tremendous daily download traffic, it’s suggested you choose a download location most closely situated to your geographical location (known as a mirror). Apache will attempt to identify the mirror closest to your location simply by navigating to http://httpd.apache.org/download.cgi. Here you’ll be presented with a list of several Apache versions. I suggest clicking on the latest stable version, which will prompt you to select the source code in tar.gz or bz2 formats, or downloading one of several operating system-specific binaries. If you’re running a Linux distribution and plan on building from source, then download one of the source code archives.

DOWNLOADING PHP

Like Apache, PHP is available through all Linux distributions’ package environments nowadays, and it is installed on OS X by default. If either case applies to you, I strongly recommend following the installation and configuration instructions specific to your environment. Otherwise, you should download the latest stable version by clicking on the Downloads link at the top of the PHP web site and then choosing from one of the available distributions. After selecting a distribution, choose the download mirror closest to your location (recommended).

INSTALLING APACHE AND PHP on LINUX

You need a respectable ANSI-C compiler and build system, two items that are available through all of the major distributions’ package managers. In addition, PHP requires both Flex (http://flex.sourceforge.net) and Bison (www.gnu.org/software/bison/bison.html), while Apache requires at least Perl version 5.003. Finally, you’ll need root access to the target server to complete the build process.

For the sake of convenience, before beginning the installation process, consider moving both packages to a common location such as /usr/src/. 

The installation process follows:

1. Unzip and untar Apache and PHP. In the following code, the X represents the latest stable version numbers of the distributions you downloaded in the previous section:

%>gunzip httpd-2_X_XX.tar.gz
%>tar xvf httpd-2_X_XX.tar
%>gunzip php-XX.tar.gz
%>tar xvf php-XX.tar

2. Configure and build Apache. At a minimum, you’ll want to pass the option -- enable-so, which tells Apache to enable the ability to load shared modules:

%>cd httpd-2_X_XX
%>./configure --enable-so [other options]
%>make

3. Install Apache (which you will need to do as the system superuser/administrator):

%>make install

4. Configure, build, and install PHP. In the following steps, APACHE_INSTALL_DIR is a placeholder for the path to Apache’s installed location, for instance /usr/local/apache2:

%>cd ../php-X_XX
%>./configure --with-apxs2=APACHE_INSTALL_DIR/bin/apxs [other options]
%>make
%>make install

5. PHP comes bundled with a configuration file that controls many aspects of PHP’s behavior. This file is known as php.ini, but it was originally named php.ini-dist. You need to copy this file to its appropriate location and rename it php.ini. The later section “Configuring PHP” examines php.ini’s purpose and contents in detail. Note that you can place this configuration file anywhere you please, but if you choose a non default location, you also need to configure PHP using the --with-config-file-path option. Also note that there is another default configuration file at your disposal, php.ini-recommended. This file sets various non standard settings and is intended to better secure and optimize your installation, although this configuration may not be fully compatible with some of the legacy applications. Consider using this file in lieu of php.inidist. To use this file, execute the following command:

%>cp php.ini-recommended /usr/local/lib/php.ini

6. Open Apache’s configuration file, known as httpd.conf, and verify that the following lines exist. (The httpd.conf file is located at APACHE_INSTALL_DIR/conf/httpd.conf.) If they don’t exist, go ahead and add them. Consider adding each alongside the other LoadModule and AddType entries, respectively:

LoadModule php5_module modules/libphp5.so
AddType application/x-httpd-php .php

Believe it or not, that’s it. Restart the Apache server with the following command:

%>/usr/local/apache/bin/apachectl restart

TESTING YOUR INSTALLATION

The best way to verify your PHP installation is by attempting to execute a PHP script. Open a text editor and add the following lines to a new file:

<?php
phpinfo();
?>


Save this file as phpinfo.php. If you’re running Apache, save it to the htdocs directory. If you’re running IIS, save the file to C:\inetpub\wwwroot. Now open a browser and access this file by entering the following URL: http://localhost/phpinfo.php.

Please note that you cannot just open the script by navigating through your browser’s File | Open feature, because in doing so this script will not be passed through the web server and therefore will not be parsed.
If all goes well, you should see output similar to that shown in Figure.

Output from PHP’s phpinfo() function
Figure. Output from PHP’s phpinfo() function


If you’re attempting to run this script on a web hosting provider’s server, and you receive an error message stating phpinfo() has been disabled for security reasons, you’ll need to create another test script. Try executing this one instead, which should produce some simple output:

<?php
echo "A simple but effective PHP test!";
?>


If you are not seeing the appropriate output, it may be due to one or more of the following reasons:
  • If you manually configured Apache, changes to its configuration file do not take effect until Apache has been restarted. Therefore, be sure to restart Apache after adding the necessary PHP-specific lines to the httpd.conf file.
  • Invalid characters or incorrect statements will cause Apache’s restart attempt to fail. If Apache will not start, go back and review your changes.
  • Verify that any PHP-enabled file ends in the same PHP-specific extension as defined in the httpd.conf file. For example, if you've defined only .php as the recognizable extension, don’t try to embed PHP code in an .html file.
  • Make sure that you've delimited the PHP code within the file using the <?php and ?> constructs. Neglecting to do this will cause the code to output to the browser.
  • You've created a file named index.php and are trying unsuccessfully to call it as you would a default directory index file (done by just referencing a directory within the URL sans the specific file name you’d like to request, for instance http://www.example.com/about/ versus http://www.example.com/about/index.php). However, Apache only recognizes index.html as the default directory index file. Therefore, you need to add index.php to Apache’s Directory Index directive.

0 comments:

Post a Comment

 
Back to top!