MySQL + Apache + PHP + PHPLIB Quick Installation Guide
(Version 1.0. Last modified: 2000-09-18)
(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)).
There are two specific objectives we want to achieve:
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).
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.
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:
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:
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:
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.
184.108.40.206 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).
220.127.116.11 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:
In the /home/www/securefoldername folder, create the .htaccess file (don't forget the dot in front of htaccess) which contains the following lines:
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.
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:
Add the appropriate entries in MySQL:
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:
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).
Copyright. 1996-2000. Jun Da. email@example.com. Last updated: 2000-09-18