Build Your Skills: Dynamic Web site installation tutorial

Explains how to install and configure PHP, Perl, Apache, and MySQL

In the first Daily Drill Down of this series, I took a cursory glance at some of the tools you can use to build your dynamic Web site. I looked at two Web servers, Apache and Roxen, two RDMS (Relational Database Management System) packages, MySQL and PostgreSQL, and two programming languages ideal for use with Web sites, Perl and PHP. After weighing these choices, I decided to use the more popular combination of Apache, MySQL, and both Perl and PHP.

Of course, more choices are available than those I looked at. This is Linux I’m talking about, and there are always more than one or two ways to do any given task. Those I examined are probably the most popular tools for the job. If you’re inclined to dig into more esoteric packages, feel free. That can make the experience more interesting.

For the purposes of this tutorial, however, I’ll look specifically at Apache, MySQL, Perl, and PHP as the basic tools for what I want to do, and I’ll complete the entire installation on a Linux Mandrake 7.1 system.

Installation of MySQL
The first thing you’ll want to do is install MySQL. Depending on your distribution, you may be able to simply install a set of RPM or DEB packages. If there are no packages for your particular distribution, you'll have to build it yourself. That isn't very difficult either, so take heart if you're one of the few who may have to do it.

If you're using an RPM-based distribution, grab the RPMs from the installation CD or a distribution’s mirror site. After you have obtained the RPM packages, install them like this:
rpm _ivh MySQL_3.22.32-5mdk.i586.rpm
rpm -ivh MySQL-devel-3.22.32-5mdk.i586.rpm
rpm -ivh MySQL-shared-libs-3.22.32-5mdk.i586.rpm
rpm -ivh MySQL-client-3.22.32-5mdk.i586.rpm
rpm -ivh MySQL-bench-3.22.32-5mdk.i586.rpm

If you need to download the package, simply obtain it from the MySQL Web site. After you have downloaded MySQL_3.22.32.tar.gz, save it to your /usr/src directory and then prepare to compile the following:
[/usr/src]# tar xvzf MySQL_3.22.32.tar.gz
cd MySQL_3.22.32
make install

The next thing you want to do is add Perl support for MySQL. Since most Linux systems come with Perl pre-installed, you don’t have to worry about having the prerequisite Perl program installed. If this isn’t the case, you’ll need to install Perl before continuing. Please note that the installation of the DBI/DBD client interface modules is required only for Perl programs to interact with the MySQL server. You do not need these modules installed if you have no intention of using Perl with MySQL.

You’ll need to download three files from the MySQL contribution downloads Web page, which you can find at MySQL site. As of this writing, the three modules you’ll have to download are Data-Dumper-2.101.tar.gz, DBI-1.13.tar.gz, and Msql-Mysql-modules-1.2214.tar.gz. The compilation steps are the same for each, so we’ll look only at installing the Data-Dumper module, and you can replace the path names for the other modules. Save each file to your /usr/src directory. To install the Data-Dumper module, execute the following steps:
[/usr/src]# tar xvzf Data-Dumper-2.101.tar.gz
cd Data-Dumper-2.101
perl Makefile.PL
make test
make install

Then, do the same for the other modules. Be sure you execute make test as that will ensure that the modules are operating correctly and can interface with the MySQL server. You must also install the modules in a specific order. Install Data-Dumper first, then DBI, and finally Msql-Mysql-modules.

Now, you must initialize MySQL by providing the root password to access the server. However, the first thing you need to do is start the MySQL daemon. If you install via RPM, you will most likely have a script in your /etc/rc.d/init.d directory called mysql. If this is the case, execute
/etc/rc.d/init.d/mysql start

If, however, you built the binaries from a source distribution, your startup script will be in the /usr/local/mysql/bin directory (which is the default, unless you changed it with configure), and you will then have to execute
/usr/local/mysql/bin/safe_mysqld &

After that, run the following command to set the root password. Keep in mind that the root password for the MySQL server does not necessarily need to be the same password you use with root for logging into your system. In fact, it might be a good idea to keep the two different for security reasons.
mysqladmin _u root _p password

Change the password above to whatever password you decide to use for the root MySQL user. Keep this password safe, because the root user in MySQL is the same as the root user in Linux. As far as the program is concerned, root is God and all root's commands will be obeyed without question, good or bad.

Now, barring any problems with the installation or initialization, your MySQL server is complete.

Installation of Apache
The next step is to install Apache. Again, if you have an RPM- or DEB-based distribution, you're on easy street. Most distributions have packaged Apache, so it should just be a matter of grabbing your installation CD and installing the RPMs if you don't already have them installed.

There’s nothing special about the Apache installation. It's extremely straightforward. If, however, you need to compile and install Apache from a tar archive, follow these steps: Download the Apache archive, apache_1.3.12.tar.gz from the official Apache site and save it in your /usr/src directory.
[/usr/src]# tar xvzf apache_1.3.12.tar.gz
cd apache_1.3.12
./configure –prefix=/usr –with-perl=/usr/bin/perl –enable-suexec –suexec-caller=nobody –suexec-docroot=/home \
make install

Be sure to point –with-perl= to the appropriate location for your Perl program. For the sake of sanity, you may want to rename /home/httpd/htdocs to /home/httpd/html. This makes things a little easier to read. Make certain you edit /etc/httpd/conf/httpd.conf and have it reflect your system settings. Configuring Apache completely is, obviously, beyond the scope of this article, but httpd.conf is well documented, so it should be easy enough to do.

Now, your Apache installation is complete.

Installation of Perl
Perl ships with every distribution, so you needn’t worry about installing it. Perl is one of the most widely used interpretive scripting languages available, so it's probably installed already. If it isn't, simply install the Perl package from your installation CD.

To integrate Perl and Apache, you’ll need the Apache module called mod_perl. You can obtain the mod_perl package from the official Apache Web site, as the file mod_perl-1.24.tar.gz. If you’re using an RPM- or DEB-based distribution, I strongly recommend seeing if a package already exists for mod_perl, just to make life easier. If not, you’ll need a copy of the Apache source against which to build mod_perl. Most distributions that package Apache also package mod_perl, but if you had to build Apache from source, you’ll most likely have to do the same for mod_perl. Assuming that the Apache source is in /usr/src/apache_1.3.12, follow these instructions:
[/usr/src]# tar xvzf mod_perl-1.24.tar.gz
cd mod_perl-1.24
perl Makefile.PL NO_HTTPD=1
make test
make install

This will build mod_perl against the Apache source found in the ../apache_xxx/src/ directory, which is where mod_perl will find the Apache header files it requires. Adding the NO_HTTPD=1 to the perl Makefile.PL statement will prevent mod_perl from recompiling and re-installing Apache.

Next, edit your /etc/httpd/conf/httpd.conf file and add the following lines if they don’t exist already:
LoadModule perl_module /usr/lib/apache/
AddModule mod_perl.c
Alias /perl/ “/home/httpd/html/perl/”
<Location /perl>
  SetHandler perl-script
  PerlHandler Apache::Registry
  PerlSendHeader On
  Options +ExecCGI

This will allow you to use embedded Perl only in the /home/httpd/html/perl directory, which makes things a little safer since you want to have some sort of a controlled environment. Having user-written Perl pages can be a huge security hole if you permit Perl scripts and pages to be run just anywhere.

Installation of PHP
Now we come to installing the Hypertext Pre-Processor (PHP). Many newer distributions are packaging PHP as well, simply because it is such a popular language. Some distributions may only provide packages for PHP3, while others also provide the new PHP4. The differences between the two versions of PHP are numerous, far too many to describe here. However, most of these differences are in performance. PHP3 has worked well for a long time, so if you are familiar with and already use PHP3, you may opt to stay with it. However, if you are new to PHP, you should really try PHP4. Your distribution may package PHP4, and if it does, you’ll have an easy installation.

The unique thing about PHP is that it can be used as an interpreted scripting language as well as an embedded Web scripting language, which means you can write PHP programs that run on the command line by using the PHP interpreter. This capability can come in handy when scheduling tasks or doing behind-the-scenes maintenance where you would otherwise have to use Perl. It makes PHP even more powerful as a Web scripting language, as it will permit you to remain consistent with the code on your site. If you don't know Perl or PHP, you may only need to learn one language instead of two.

If you don't have a package for PHP, or you have a package for PHP3 but wish to try PHP4, download the source package from the official PHP site. Save it to your /usr/src directory and execute the following commands:
[/usr/src]$ tar xvzf php_4.0.1pl2.tar.gz
cd php_4.0.1pl2
./configure -–with-mysql=/path/to/mysql -–with-apxs -–enable-track-vars –-with-gd
make install

You need to tell PHP4 where to find MySQL on your system (if you compiled it yourself, it will most likely be in /usr/local/mysql) so that it doesn’t use its built-in MySQL support. This will prevent any potential conflicts with mod_perl or any other Apache module that also uses MySQL. It will also create the mod_php4 module required by Apache to interpret embedded PHP code in your Web pages. To complete the installation, insert the following lines into your /etc/httpd/conf/httpd.conf file:
LoadModule php4_module libexec/
AddModule mod_php4.c

You also need to associate mod_php4 with certain suffixes. For example, you don't want the PHP interpreter to be run on a file called index.shtml, but rather on something like index.php, index.php3, index.phtml, and so forth. Again, edit /etc/httpd/conf/httpd.conf and add the following:
AddType application/x-httpd-php .php .phtml

This will process PHP code in HTML files ending in .php and .phtml only. Any other file with PHP code in it will not be processed.

Test the installation
Now that all of our required tools are installed, you should test the installation by writing a quick piece of dummy code in PHP and view it through your Web server.

The first thing you need to do is start Apache. If you’re using a distribution like Linux Mandrake or Red Hat, the initialization script will be in the /etc/rc.d/init.d directory. If you’re using SuSE, it will be in the /etc/init.d directory. It will typically be called httpd. Start the Web service by running this command:
/etc/rc.d/init.d/httpd start

If you don’t have a start script like this (usually because you compiled Apache yourself), you can start Apache with this command:
/usr/sbin/httpd -f /etc/httpd/conf/httpd.conf

where the -f command-line parameter points to your httpd.conf configuration file. If you have to start Apache this way, I should also tell you how to stop it:
kill -TERM `cat /etc/httpd/logs/`

These commands will start Apache in the background. Just to make sure it's running, issue the following:
ps aux

Check to see if a program called httpd is running, either as user nobody, httpd, or something similar. After binding to the TCP port 80 as root, Apache should switch to a nonprivileged user, such as nobody or httpd. If you see httpd being run as root, you have a problem. If this is the case, edit your /etc/httpd/conf/httpd.conf file and change the value of User and Group to nobody, as shown here:
User nobody
Group nobody

Then, you will need to restart the server by issuing either of these commands:
/etc/rc.d/init.d/httpd restart

killall httpd _HUP

Now, go into the root directory for the Web server, usually /home/httpd/html or /home/httpd/htdocs; depending on the distribution and package, the location may vary. Take your favorite editor and edit the file /home/httpd/html/test.php, which should consist of the following:
echo "Hello world!";

Then, fire up your Web browser and point it to http://localhost/test.php. You should see two words on the page, "Hello world!" and nothing else. If this is what you see, congratulations! You have Apache properly interpreting PHP code. If you don’t see this, but you see an error of some sort or a completely blank page, go back over the steps and make sure that you followed each of them correctly.

In this second part of my Daily Drill Down series, I have explained how to install and configure the tools you will need to build your dynamic Web site. Each component is equally important; you need the Web server, the database, and the means to glue the two together, which means PHP and/or Perl. In subsequent Daily Drill Downs in this series, I will be looking at some ways to begin building your dynamic Web site and provide you with real examples that you can put to use on your own site.

Vincent Danen, a native Canadian in Edmonton, Alberta, has been computing since the age of 10, and he’s been using Linux for nearly two years. Prior to that, he used OS/2 exclusively for approximately four years. Vincent is a firm believer in the philosophy behind the Linux "revolution,” and heattempts to contribute to the Linux causein as many ways as possible—from his FreezerBurn Web site to building and submitting custom RPMs for the Linux Mandrake project. Vincent also has obtained his Linux Administrator certification from Brainbench .He hopes to tackle the RHCE once it can be taken in Canada.

The authors and editors have taken care in preparation of the content contained herein, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for any damages. Always have a verified backup before making any changes.


Vincent Danen works on the Red Hat Security Response Team and lives in Canada. He has been writing about and developing on Linux for over 10 years and is a veteran Mac user.

Editor's Picks