A dynamic open source trio has emerged as a favorite for creating interactive, database-powered Web sites. The Apache-PHP-MySQL combination offers robust capabilities and a meager price tag. In my previous article, I showed you how to install the MySQL database server on Linux. In this article, I will show you how to connect your MySQL database to the Web using Apache and PHP.
Apache is the world’s most popular Web server, while PHP functions as the open source middleware that sits between the database server (MySQL) and the Web server (Apache) and provides significant functionality for both. Apache, PHP, and MySQL are all open source applications that are available for free download.
This article assumes that you have MySQL installed, as covered in my previous article. For our purposes here, we will explain the installation of PHP and Apache, detailing how they work together, and then test the applications to make sure that they are functioning correctly. As of this writing, the following versions of the software are available:
- PHP (PHP Hypertext Preprocessor) 4.0.6
- Apache HTTP server 1.3.22
We'll use those versions for our example. To get the most recent Apache and PHP installation files, you can download them from the www.apache.org and the www.php.net Web sites and place them in the /usr/local directory on your Linux server.
Our installation of PHP will make it an Apache-integrated module, which provides a performance boost to the handling of PHP scripts. PHP works like this: When a request is made for a page with PHP code that has a .php extension, Apache hands it over to the PHP scripting engine to handle. In many cases, PHP is used to provide access to a database via its code, so the scripting engine is set up to talk to the local database.
Setting up PHP
Before you can install PHP as an integrated component to Apache, you need to expand the Apache distribution and tell PHP where to find the files. This will give PHP enough information to be built and installed as an Apache module.
As with the MySQL installation, you will need root (superuser) privileges in order to expand, install, and configure Apache and PHP. Become superuser (root) by issuing the su command and entering your root password.
To expand Apache for the PHP installation, you'll first need to switch to the directory containing the downloaded files by typing the command cd /usr/local. Next, type the following command:
gunzip -dc apache_1.3.22.tar.gz | tar xvf
You will see all of the output from the files being extracted, and you will eventually be returned to a command line. Now, switch to the Apache installation directory by typing cd /usr/local/apache_1.3.22. Finally, to configure the Apache installation so that PHP can be properly set up, issue the command ./configure —prefix=/usr/local/apache. This will build the Apache configuration so that Apache will eventually be installed in the /usr/local/apache directory on your Linux server.
For a look at other PHP installation options, type ./configure –help. This gives you a complete list of what PHP can do, such as building PDF pages dynamically.
Once this process completes, we are ready to move on to installing PHP. The PHP installer will be told where all of the Apache files are, where it can find MySQL, and where its own installation location will be.
As with MySQL and Apache, we need to expand the PHP distribution into a usable directory. Issue the command cd /usr/local. Then unzip the PHP file with the command:
gunzip -dc php-4.0.6.tar.gz | tar xvf
Next, we’ll use the configuration program to tell the PHP installer where MySQL and Apache are located. Switch to the PHP installation directory with the command cd /usr/local/php-4.0.6. We’ll use the following command to build the PHP installation:
Again, a lot of information will scroll down the screen. You don’t need to keep track of it unless there is a problem. When everything is finished and you are back at a command line, use the command make to make the actual installation file. Once this process is completed, the command make install will perform the actual software installation.
One final step to perform is copying the PHP configuration file to the proper location on your server. The command cp php.ini-dist /usr/local/lib/php.ini will accomplish this. You have now completed the installation of PHP on your Linux server.
We’re getting closer to having a fully functioning, database-driven Web site complete with dynamic content. We’re missing only one piece—the HTTP server. In the previous step, we partially built Apache to accommodate the installation of PHP. In this step, we’ll be doing that again with different options and then actually installing and starting Apache.
First, let’s build Apache with some specific options to enable PHP support and to tell Apache where it should install itself. Start by switching to the Apache installation directory with the command cd /usr/local/apache_1.3.22. Next, run the configuration program by issuing the command:
Once the configuration program is finished, it’s time to build and install Apache. To build Apache, use the command make. Then use make install to install the program.
Before you can initially start the Apache HTTPD server, you need to tell it how to handle pages with a .php extension. As mentioned above, PHP scripts are normally saved as files with .php extensions, and that’s how we’ll refer to them for the remainder of this article.
To edit the Apache configuration file (httpd.conf), locate the httpd.conf file and use your favorite text editor to open it. For example, pico /usr/local/apache/conf/httpd.conf. Once you are in the httpd.conf file, look for a line that says # And for PHP4.x, use: and uncomment the two lines below it that begin with AddType (remove the # from the beginning of the line).
Now, let’s start Apache and see what we get. Issue the command:
Alternatively, you could use the command:
After you issue one of these commands, you should receive a message indicating the httpd daemon was successfully started. Now, open a Web browser from a machine on the same subnet and type in the Apache server’s IP address or host name. You should see something similar to Figure A. If you see this message, Apache has been installed and is ready to serve.
Does it all work?
Now that all three programs are installed and supposedly working together, it’s time to see if that is really the case. Testing PHP and Apache integration is easy. All you need to do is issue the command cd /usr/local/apache/htdocs to get to your Apache home directory and create a file called test.php with the following contents using a text editor such as vi or pico:
<? phpinfo(); ?>
Now, go to a Web browser again and enter http://<your web server>/test.php. You should see a screen full of information about PHP (Figure B). This verifies that the PHP and Apache installation are working well together and that Apache is able to send the PHP scripts off to the PHP engine with no problems.
If you scroll down the PHP info screen, you'll come to a section on MySQL details that should look similar to Table A. If everything is set up properly, PHP will see the correct client version for your MySQL installation, which means that it is talking to the MySQL server with no problems.
|Active Persistent Links||0|
|Client API version||3.23.43|
You now have a fully functioning system ready to provide dynamic content to your Web audience. Of course, this is just the beginning. There is still the matter of setting up your various databases and writing scripts to make these databases usable from a Web browser.
This Apache-PHP-MySQL installation is a basic one. By using other commands during the configuration phases of the applications and combining them with other apps, you can include a host of functionality. For example, by installing other libraries, you can set up PHP to create PDF files on the fly for display on your Web site. Or you can include SSL functionality to provide a secure connection to your site. Nevertheless, our simple example should get you started using this popular trio to power your site for dynamic functionality.
Have a comment or a question?
We look forward to getting your input and hearing about your experiences regarding this topic. Post a comment or a question about this article.