MySQL + Apache + PHP + PHPLIB  Quick Installation Guide

(Version 1.0. Last modified: 2000-09-18)

If you are looking for PHP+MySQL sample code, click here!

(Notes: PHP3 will be installed as a STATIC module within Apache if you follow the instructions provided in this tutorial. Further, installation of the mod_ssl module is not included. If you are new to this web server + database game, please follow the instructions in the newer version of this tutorial.)

This is a quick guide to install the three (great) packages on a Unix system such as Linux or FreeBSD. It is meant in particular for first-time Unix users or system administrators who would like to know how to set up a web server with database support. This tutorial uses RedHat Linux 6.0 as the reference OS. With minor modifications such as folder paths or system init files, the instructions provided here should be applicable to other Unix systems as well.

All the information provided in this tutorial is available online at different places, especially those INSTALL/README docs that come along with the packages. It took me a while to put the different pieces of the puzzle together. Hence here is a quick tutorial to save you some time. Please note that this tutorial is by no means a substitution of the original documentation from those packages. It is suggested that you consult those them whenever necessary.

Disclaimer: This tutorial is provided for you reference only. Use it at your own risk. The author is not responsible for any possible damage that may happen to your computer.

Recommended: Finish the installation and configuration described in this document in one session. It shouldn't take more than three hours on a properly configured system (i.e., with the OS and standard GCC library installed) and resonably fast system (e.g., a Pentium II 266 system with 64M+ RAM and a good hard disk (either a SCSI disk or 5400+ rpm IDE Disk)).

1. Objectives

There are two specific objectives we want to achieve:

  1. A web server with database support on a Unix system. Specifically, we are talking about running the Apache web server with MySQL as the database backend and PHP as the server-side scripting language on a Unix system;
  2. A web-based interface for performing most system administration tasks including Apache and MySQL configuration/administration.

2. Operating system

Instructions provided in this tutorial is fully tested on a RedHat 6.0 Linux box (as well as FreeBSD 3.4 and 4.0).

  • Make sure you have C/C++ compilers (gcc and lib files) installed.
  • If you installed a previous version of Apache from the RedHat CD, either uninstall the package (recommended) or make sure you know where the executables and conf files are located. By default, conf files from a RedHat CD installation are in the /etc/httpd folder.

3. Software to download

Grab the following packages (source code only (no pre-compiled binaries (e.g, RPMs)) onto your system. Save them in the same folder, e.g., /usr/src. Note that by the time you read this tutorial, version numbers may be different.

  • Apache 1.3.12 (www.apache.org): Web server software;
  • MySQL 3.22.32 (www.mysql.com): Database server engineer. Download the full tarball;
  • PHP 3.0.16 (www.php.net): server-side scripting language (to be installed as an Apache module);
  • PHPLIB 7.2 (phplib.netuse.de): A library of PHP codes for clean and efficient PHP scripting and extra functionality such as session management, authentication, etc.. A must-have if you are serious about web application development;
  • DBI 1.13 www.cpan.org/modules/by-module/DBI/): A Perl module which enables Perl scripts to interface with MySQL;
  • phpMyAdmin (phpwizard.net/phpMyAdmin): A web-based tool for administering MySQL:
  • Webmin 0.79 (www.webmin.com): Web-based administration tools for Unix systems. A great handy tool.

4. Installation

We will use /usr/src as the folder which contains all the downloaded packages. Before installation, you need to become the superuser:

% su root 
# cd /usr/src

To install, follow the steps in the order given below:

4.1 Unpack all the gzip'ed and tarred files. E.g.;

# gunzip apache_1.3.12.tar.gz | tar xvf -

Repeat the above procedure for every gzipped and tarred file you've downloaded.

4.2 Install Webmin for system administration

Start installation using the following command:

# cd webmin-0.79
# ./setup.sh

During installation, just accept all the defaults when prompted (except, of course, the administrator's username and password).

With the package installed, you can perform many system tasks such as user management, Samba and Apache configuration, etc. through a web browser.

Don't forget to restrict access priledges to the IPs that you use. To do this, use the Webmin Configuration module and set up restricted IP addresses. You can do this after everything is installed.

4.3 Install MySQL

Install MySQL is as easy as taking the following three steps:

# ./configure --prefix=/usr/local/mysql
# make
# make install

Note: the option --prefix=/usr/local/mysql will get all MySQL files installed under that particular folder. Another common option is to use --prefix=/usr/local.

After MySQL is installed, run the following commands only ONCE:

# /usr/local/mysql/bin/mysql_install_db
# /usr/local/mysql/bin/safe_mysqld &
# /usr/local/mysql/bin/mysqladmin -u root password newpassword

Copy the file whatever-your-mysql-unpacked-folder-is/support/mysql.server into the rc.d/init.d/ folder and create a softlink in the rc3.d folder. e.g.

# ln -s /etc/rc.d/init.d/mysql.server /etc/rc.d/rc3.d/S91mysql

We need to make mysql.server excutable so that it will be called up when the system reboots:

# chmod a+x /etc/rc.d/init.d/mysql.server

4.4 Install Apache + PHP 

PHP can be built into Apache in two modes: 1) Statically linked; 2) Dynamically linked. In this tutorial, we build a statically linked PHP.

4.4.1 Install the software

Suppose that both Apache and PHP files are untarred under the same parent folder. Do the following (copied from the PHP README file):

# cd apache_1.3.12
# ./configure --prefix=/usr/local/apache
# cd ../php-3.0.16
# ./configure --with-mysql=/usr/local/mysql --with-apache=../apache_1.3.12 --enable-track-vars
# make
# make install
# cd ../apache_1.3.12
# ./configure --prefix=/usr/local/apache --activate-module=src/modules/php3/libphp3.a
# make
# make install
# cd ../php-3.0.16
# cp php3.ini-dist /usr/local/lib/php3.ini

Note the use of --prefix=/usr/local/apache option, which will get all Apache files installed in the /usr/local/apache folder.

If you want to enable other modules such as 'log_referer, proxy, rewrite' etc. in httpd, add relevant options in the last configuration command (all in one line)

# ./configure --prefix=/usr/local/apache --activate-module=src/modules/php3/libphp3.a --enable-module=proxy

The command './configure --help' will give you a list of all the modules available.

4.4.2 Configure Apache to enable PHP scripting

This is done by modifying the /usr/local/apache/conf/httpd.conf file and make sure the following line is present exactly as below (What you need to do is to uncomment (i.e., delete the # sign at the beginning of) the two lines in the default httpd.conf file):

AddType application/x-httpd-php3 .php3
AddType application/x-httpd-php3-source .phps

4.4.3 Modifying system init file

The /etc/rc.d/rc.local file can be modified so that Apache will be fired at system reboot. Add the following line at the very end of the file:

/usr/local/apache/bin/apachectl start

4.4.4 Manually start Apache

# /usr/local/apache/bin/apachectl start

4.4.5 Test your new server configuration

You can test all the configurations by creating a file (name it info.php3, for example) and place it under the root directory of your web server. (If you haven't modified the DocumentRoot option in the httpd.conf file, the web server root is at /usr/local/apache/htdocs folder.) The file should just contains one line:

<?php phpinfo() ?>

Run the script from a web browser and you should see a list of web server configuration parameters.

4.4.6 Optional rebooting

At this time, you can optionally reboot your Linux box to make sure that the two servers (MySQL and Apache) are fired up at system reboot.

4.4.7. Create a protected folder (for installing phpMyAdmin)

By default, all the files and folder under the web root folder is viewable by everyone. If you need to restrict access to a certain web folder, you need to create username for authentication. Note that this username is different from your Linux system user accounts. You can choose any username.

4.4.7.1 Create a username for authentication

The following command will create a username 'mynewuser' which can be used for authentication to access a protected folder.

# /usr/local/apache/bin/htpasswd -c /home/www/userdb/userfile mynewuser

where /home/www/userdb is the folder which contains the file userfile which in turn contains the username mynewuser. With the option -c, username will be created. Otherwise the useerfile will be updated (when, for example, you want to change the password associated with the username).

4.4.7.2 Set up a password protected folder

Suppose the folder /home/www/securefoldername needs to be protected. In the /usr/local/apache/conf/httpd.conf file, add the following:

<Directory "/home/www/securefoldername">
AllowOverride AuthConfig
AuthName "MYCOMMUNITY"
AuthType Basic
AuthUserFile /home/www/userdb/userfile
Order deny,allow
Deny from all
Allow from .mydomain.com
</Directory>

In the /home/www/securefoldername folder, create the .htaccess file (don't forget the dot in front of htaccess) which contains the following lines:

AuthName "MYCOMMUNITY"
AuthType Basic
AuthUserFile /home/www/userdb/userfile
AuthGroupFile /dev/null
Require user mynewuser

Restart the Apache server:

# /usr/local/apache/bin/apachectl restart

The /home/www/securefoldername will be protected with basic authentication. Only the username 'mynewuser' from a computer within .mydomain.com will be able to view pages hosted within that folder. We will use this folder to install the phpMyAdmin files.

4.5 Install phpMyAdmin for MySQL administration

Untar the phpMyAdmin file in a protected folder such as the one we just created above. It is suggested that you password protect the folder and implement IP access restriction at the same time.

Modify the config.inc.php3 file to add the root password of your MySQL server.

Now you can administer your MySQL database from a web browser.

Let us create our first database for use with the PHPLIB installation.

  1. Create a database. It's easy with phpMyAdmin's web interface.
  2. Create a user in the mysql database by adding a user to the user table.
    E.g., username=mydbuser, password=secret, host=localhost.
    There is no need to change any permissions in that table. That is, leave anything else to N.
  3. Add the user info to the db table of the mysql database and change permissions for select, insert, delete and update to Y. Leave other permissions untouched.

You need to consult the MySQL manual for further information about permissions, etc.. The latest Webmin (0.78) also has limited support to MySQL administration.

4.6 Install PHPLIB

Installing PHPLIB is optional but STRONGLY recommended if you are serious about PHP scripting. PHPLIB is an excellent library that will enable you to code in PHP efficiently and cleanly.

To install the library, untar the downloaded file into a folder parallel to your web server's document root. For example, if your web root is at /export/home/www/DocRoot, you can place PHPLIB files under /export/home/www/phplib72.

Edit the /usr/local/lib/php3.ini file by adding two lines:

include = "/path/to/the/phpfolder"
auto_prepend_file =  "/path/to/thefile/prepend.php3"

Add the appropriate entries in MySQL:

Host: localhost
Database: mydb
Username: mydbuser
Password: secrete

Modify the DB_Example lines in the local.inc file to add the above information.

Run the following command:

$ /usr/local/mysql/bin/mysql -h localhost -u root -p mydb < /whateverpath/to/create_database.mysql

Now you should be able to use the PHPLIB for your coding. Check out this sample code page to see how to connect to a database with or without PHPLIB support.

PHPLIB can be customized for each virtual web server that is running on your unix box. If you need information or help with configuration, please contact me.

4.7 Install DBI

DBI is a Perl module that enables interfacing with MySQL from a Perl script. One way or the other, you will need it for writing Perl scripts to interact with MySQL.

To install it, follow the instructions below:

# perl Makefile.PL
# make
# make test
# make install

You can ignore the error message about using proxy servers. You will unlikely need this feature if you only use Perl script to interface with the MySQL database on a localhost.

5 After installation

Sit back and take a break! You've done a great job within a very short period of time.

Now it is time for you to read those manuals to get started with scripting. The two must-haves are:

  1. MySQL Reference Manual;
  2. PHP Manual.

If you want to learn SQL, read this informative tutorial. Tutorials on PHP scripting can be found by following links provided on the official PHP website. If you are serious about PHP scripting, www.phpbuilder.com is the place to check out. www.webmonkey.com is also a great place for tutorials.

If you want to code cleanly and quickly and develop serious web applications, please read the PHPLIB document. One way or the other, you will like it.

All the documents are available online on their official sites.

6. Seek help

If you have any questions concerning this tutorial, feel free to contact me through the online form (which uses PHP).

Happy coding!

 


Copyright. 1996-2000. Jun Da. jda@mtsu.edu. Last updated: 2000-09-18