Article

Build Your Own Database Driven Web Site Using PHP & MySQL, Part 1: Installation

Page: 1 2 3 4 5 6 7 Next

Installing PHP

As mentioned above, PHP is more a web server plugin module than a program. There are actually three ways to install the PHP plugin for Apache:

  • as a CGI program that Apache runs every time it needs to process a PHP-enhanced web page
  • as an Apache module compiled right into the Apache program
  • as an Apache module loaded by Apache each time it starts up

The first option is the easiest to install and set up, but it requires Apache to launch PHP as a program on your computer every time a PHP page is requested. This activity can really slow down the response time of your web server, especially if more than one request needs to be processed at a time.

The second and third options are almost identical in terms of performance, but the third option is the most flexible, since you can add and remove Apache modules without having to recompile it each time. For this reason, we’ll use the third option.

Assuming you don’t already have Apache running on your computer, surf on over to the Apache HTTP Server Project and look for the version of Apache described as “the best available version” (as of this writing it’s version 2.2.11, as shown below).

The best available version -- accept no substitutes!

Once you get to the Download page, scroll down to find the links to the various versions available. The one you want is Unix Source, shown in the figure below. Both the .tar.gz or the .tar.bz2 are the same; just grab whichever archive format you’re used to extracting.

This is the one you need

What you’ve just downloaded is actually the source code for the Apache server. The first step, then, is to compile it into an executable binary installation. Pop open a Terminal, navigate to the directory where the downloaded file is located, then extract it, and navigate into the resulting directory:

user@machine:~$ cd Desktop    
user@machine:~/Desktop$ tar xfz httpd-version.tar.gz    
user@machine:~/Desktop$ cd httpd-version

The first step in compiling Apache is to configure it to your requirements. Most of the defaults will be fine for your purposes, but you’ll need to enable dynamic loading of Apache modules (like PHP), which is off by default. Additionally, you should probably enable the URL rewriting feature, upon which many PHP applications rely (although it’s unnecessary for the examples in this book). To make these configuration changes, type this command:

user@machine:~/Desktop/httpd-version$ ./configure --enable-so --enable-rewrite

A long stream of status messages will parade up your screen. If the process stops with an error message, your system may be missing some critical piece of software that’s required to compile Apache. Some Linux distributions lack the essential development libraries or even a C compiler installed by default. Installing these should enable you to return and run this command successfully. Current versions of Ubuntu, however, should come with everything that’s needed.

After several minutes, the stream of messages should come to an end:

...    
config.status: creating build/rules.mk    
config.status: creating build/pkg/pkginfo    
config.status: creating build/config_vars.sh    
config.status: creating include/ap_config_auto.h    
config.status: executing default commands    
user@machine:~/Desktop/httpd-version$

You’re now ready to compile Apache. The one-word command make is all it takes:

user@machine:~/Desktop/httpd-version$ make

Again, this process will take several minutes to complete, and should end with the following message:

...    
make[1]: Leaving directory `/home/user/Desktop/httpd-version'    
user@machine:~/Desktop/httpd-version$

To install your newly-compiled copy of Apache, type sudo make install (the sudo is required, since you need root access to write to the installation directory).

user@machine:~/Desktop/httpd-version$ sudo make install

Enter your password when prompted.

As soon as this command has finished copying files, your installation of Apache is complete. Navigate to the installation directory and launch Apache using the apachectl script:

user@machine:~/Desktop/httpd-version$ cd /usr/local/apache2    
user@machine:/usr/local/apache2$ sudo bin/apachectl -k start

You’ll likely see a warning message from Apache complaining that it was unable to determine the server’s fully qualified domain name. That’s because most personal computers are without one. Don’t sweat it.

Fire up your browser and type http://localhost into the address bar. If Apache is up and running, you should see a welcome message like the one below.

You can take my word for it!

As with your MySQL server, you’ll probably want to configure Apache to start automatically when your system boots. The procedure to do this is similar; just copy and link the apachectl script from your Apache installation:

user@machine:/usr/local/apache2$ sudo su    
root@machine:/usr/local/apache2# cd /etc    
root@machine:/etc# ln -s /usr/local/apache2/bin/apachectl init.d/    
root@machine:/etc# ln -s /etc/init.d/apachectl rc2.d/S99httpd    
root@machine:/etc# ln -s /etc/init.d/apachectl rc0.d/K01httpd

To test that this works, restart your computer and then hit the http://localhost page in your browser again.

With a shiny new Apache installation up and running, you’re now ready to add PHP support to it. To start, download the PHP Complete Source Code package from the PHP Downloads page. Again, the .tar.gz and .tar.bz2 versions are identical; just download whichever you’re used to extracting.

The file you downloaded should be called php-version.tar.gz (or .bz2). Pop open a new Terminal window, navigate to the directory containing the downloaded file, extract it, and move into the resulting directory:

user@machine:~$ cd Desktop    
user@machine:~/Desktop$ tar xfz php-version.tar.gz    
user@machine:~/Desktop$ cd php-version

To install PHP as an Apache module, you’ll need to use the Apache apxs program. This will have been installed along with the Apache server if you followed the instructions above to compile it yourself; but if you’re using the copy that was installed with your distribution of Linux, you may need to install the Apache development package to access Apache apxs. You should be able to install this package by using the package manager included with your Linux distribution. For example, on Debian Linux, you can use apt-get to install it as follows:

user@machine:~$ sudo apt-get install apache-dev

Now, to install PHP, you must be logged in as root:

user@machine:~/Desktop/php-version$ sudo su    
[sudo] password for user: (type your password)    
root@machine:/home/user/Desktop/php-version#

The first step is to configure the PHP installation program by telling it which options you want to enable, and where it should find the programs it needs to know about (such as Apache apxs and MySQL). The command should look like this (all on one line):

root@machine:/home/user/Desktop/php-version# ./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs --with-mysqli=/usr/local/mysql/bin/mysql_config

The --prefix option tells the installer where you want PHP to be installed (/usr/local/php is a good choice).

The --with-apxs2 option tells the installer where to find the Apache apxs program mentioned above. When installed using your Linux distribution’s package manager, the program is usually found at /usr/sbin/apxs. If you compiled and installed Apache yourself as described above, however, it will be in the Apache binary directory, at /usr/local/apache2/bin/apxs.

The --with-mysqli option tells the installer where to find your MySQL installation. More specifically, it must point to the mysql_config program in your MySQL installation’s bin directory (/usr/local/mysql/bin/mysql_config).

Again, a parade of status messages will appear on your screen. When it stops, check for any error messages and install any files it identifies as missing. On a default Ubuntu 8.10 installation, for example, you’re likely to see an error complaining about an incomplete libxml2 installation. To correct this particular error, open Synaptic Package Manager, then locate and install the libxml2-dev package (libxml2 should already be installed). Once it’s installed, try the configure command again.

After you watch several screens of tests scroll by, you’ll be returned to the command prompt with the comforting message “Thank you for using PHP.” The following two commands will compile and then install PHP:

root@machine:/home/user/Desktop/php-version# make    
root@machine:/home/user/Desktop/php-version# make install

Take a coffee break: this will take some time.

Upon completion of the make install command, PHP will be installed in /usr/local/php (unless you specified a different directory with the --prefix option of the configure script above). Now you just need to configure it!

The PHP configuration file is called php.ini. PHP comes with two sample php.ini files called php.ini-dist and php.ini-recommended. Copy these files from your installation work directory to the /usr/local/php/lib directory, then make a copy of the php.ini-dist file and call it php.ini:

root@machine:/home/user/Desktop/php-version# cp php.ini* /usr/local/ php/lib/    
root@machine:/home/user/Desktop/php-version# cd /usr/local/php/lib    
root@machine:/usr/local/php/lib# cp php.ini-dist php.ini

You may now delete the directory from which you compiled PHP—it’s no longer needed.

We’ll worry about fine-tuning php.ini shortly. For now, we need to tweak Apache’s configuration to make it more PHP-friendly. Locate your Apache httpd.conf configuration file. This file can usually be found in the conf subdirectory of your Apache installation (/usr/local/apache2/conf/httpd.conf).

To edit this file you must be logged in as root, so launch your text editor from the Terminal window where you’re still logged in as root:

root@machine:/usr/local/php/lib# cd /usr/local/apache2/conf    
root@machine:/usr/local/apache2/conf# gedit httpd.conf

In this file, look for the line that begins with DirectoryIndex. This line tells Apache which filenames to use when it looks for the default page for a given directory. You’ll see the usual index.html, but you need to add index.php to the list:

<IfModule dir_module>    
 DirectoryIndex index.html index.php    
</IfModule>

Finally, go right to the bottom of the file and add these lines to tell Apache that files with names ending in .php should be treated as PHP scripts:

<FilesMatch \.php$>    
 SetHandler application/x-httpd-php    
</FilesMatch>

That should do it! Save your changes and restart your Apache server with this command:

root@machine:/usr/local/apache2/conf# /usr/local/apache2/bin/apachectl -k restart

If it all goes according to plan, Apache should start up without any error messages. If you run into any trouble, the helpful individuals in the SitePoint Forums (myself included) will be happy to help.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links