Apache + PHP + MySQL on Linux Guide

Author
Aron Schatz
Posted
October 11, 2002
Views
159121
A step by step guide to installing apache, php, and MySQL on Linux.
Tags Guides

Page All: Viewing All Pages

Page 1
<center><big><a href="http://www.aseforums.com">Get all your questions answered in the forums</a>!</big></center>

<B>Introduction</B>:

This guide is to help everyone setup a simple webserver with mysql and php support. I must admit that I'm also making this guide for myself. I forget how to setup everything correctly on different builds. Hopefully you will learn something from this article as I have. The Distro used is <a href="http://www.lycoris.com">Lycoris</a> (<a href="articles.php?id=36">Reviewed Here</a>Wink. Everything is compiled from the source. This is the way it should be, don't worry about RPMs. This will be as simple as possible. I made it step by step, and I threw in some pics as well.

<B>Before you get into it</B>:

To me, a test box that you control is easier to develop on than one you need to ftp to. I also crashed this webserver when I had a bad script, at least I can easily restart the test box and debug my script. If you have a website yourself, you probably would like to mimic its configuration. Check with your host to see what versions of MySQL, PHP, and Apache you have. For the purpose of this article, I used the following builds:

PHP: 4.2.0
MySQL: 3.23.43
Apache: 1.3.20

If you just want to have your own test box, download the latest versions of everything.
Page 2
The Steps:

<B>Step 1: Download MySQL, Apache, and PHP</B>

Your first task is to download each of the packages. The following sites will have the source code avaliable to you. Make sure you download the SOURCE CODE (It will be in a .tar.gz file).

PHP: <a href="http://www.php.net/downloads.php">http://www.php.net</a>.
MySQL: <a href="http://www.mysql.com/downloads/index.html">http://www.mysql.com</a>.
Apache: <a href="http://www.apache.org/dist/httpd/">httpd.apache.org</a>.

Download the versions you need. You should end up with these files (with your versions).

<center><img src="http://www.aselabs.com/images/articles/oct02/serverguide/start.gif"></center>

<B>Step 2: Untar the files (Unzip)</B>

Some people will tell you to place the source code into a directly controlled by the root user. I don't care, I'll stick into the home directory. You can use any method to untar the files, I used KDE's build in extractor program. Use the GUI whenever you can. If you want to do it by the command line type this in (assuming your using the same versions as I am):

<tt># tar xfz php-4.2.0.tar.gz
# tar xfz apache_1.3.20.tar.gz
# tar xfz mysql-3.23.43.tar.gz</tt>

Either way, you should end up with 3 folders with the source code. It will look something like this:

<center><img src="http://www.aselabs.com/images/articles/oct02/serverguide/unzipped.gif"></center>

<B>Step 3: Become root</B>

Open up the Konsole or command line, whatever you call it. type <tt># su</tt> and then enter your password for root. You'll be doing everything from this command line now.
Page 3
<B>Step 4: Compile MySQL</B>

From the command line, you need to cd (change directory) to the mysql source directory. There are three steps usually to compiling programs in Linux.

<tt>./configure
make
make install</tt>

This is what you'll need to do in the mysql source directory. Exact steps:

<tt># cd /path/to/mysql-3.23.43
# ./configure --prefix=/usr/local/mysql --with-mysql-user=mysql
# make
# make install</tt>

Okay, you may run into a problem with ./configure, like this one:

<center><img src="http://www.aselabs.com/images/articles/oct02/serverguide/nogcc.gif"></center>

Before you start to panic, calm down! This is easily fixed (hopefully). In Lycoris, you'll need the dev tools cd (CD #3). You'll need to install all the rpms off of the cd. If your running Lycoris, the command is this:

<tt>rlinstall /mnt/cdrom/rl/devel/RPMS/*.rpm</tt>

You'll be prompted for the password. In Mandrake it is also simple enough, just select the development package and it'll load everything you need. If you run into trouble, <a href="/viewforum.php?forumid=15">ask for help in our Linux forum</a>. The most common thing is that you don't have a compiler installed. Goto <a href="http://gcc.gnu.org">gcc.gnu.org</a> to get it.

Once you get everything installed then your ready to do it again.

<tt># cd /path/to/mysql-3.23.43
# ./configure --prefix=/usr/local/mysql --with-mysql-user=mysql
# make
# make install</tt>

Once the ./configure is completed you'll see this:

<center><img src="http://www.aselabs.com/images/articles/oct02/serverguide/confmysql.gif"></center>

Here is the kind of things you'll see when you 'make' the program...:

<center><img src="http://www.aselabs.com/images/articles/oct02/serverguide/make.gif"></center>

Here is the end of the make phase:

<center><img src="http://www.aselabs.com/images/articles/oct02/serverguide/endmake.gif"></center>

The extra part on the ./configure line is to tell the program to stick itself in the /usr/local/mysql directory and to automatically setup a user and group named mysql that can access the databases. Once you compiled MySQL, move on to step 5.

<B>Step 5: Install the database</B>

Your not done with the MySQL installation yet! cd to the newly created directory and install the db using a script given to you.

<tt># cd /usr/local/mysql/bin
# ./mysql_install_db</tt>

<center><img src="http://www.aselabs.com/images/articles/oct02/serverguide/installdb.gif"></center>

<B>Step 5a: Change directory ownership</B>

This step is not really needed, but it is good to know and use. The purpose is to make the database program and its databases not avaliable to anyone except the mysql group and root.

<tt># chown -R mysql /usr/local/mysql
# chgrp -R mysql /usr/local/mysql</tt>

<B>Step 6: Configure Apache</B>

In this step, we are only configuring apache NOT COMPILING IT! The process is similar to mysql, but do not do a make, make install yet! cd to the apache source directory and configure it.

<tt># cd /path/to/apache_1.3.20
# ./configure --prefix=/usr/local/apache</tt>

<B>Step 7: Compile PHP</B>

Now that Apache is configure, we will put PHP 'into' Apache. cd to the PHP source directory and compile it. You make need more things on your ./configure, depending on what PHP options you or your server uses.

<tt># cd /path/to/php-4.2.0
./configure --with-mysql=/usr/local/mysql \<br/>
--with-apache=../apache_1.3.20 \<br/>
--with-xml \<br/>
--enable-track-vars
# make
# make install</tt>

You may need to compile support programs for php. I had to compile pspell myself because php said it didn't have it. The above means: configure PHP with mysql in that directory, and then apache in the directory above it and in the apache_1.3.20 directory, then with xml support, then enabled track vars. This is a very basic install. My configure was much longer than this.
Page 4
<B>Step 8: Compile Apache</B>

Your all set to compile Apache. cd to the source and do it!

<tt># cd /path/to/apache_1.3.20<br/>
# ./configure --prefix=/usr/local/apache --activate-module=src/modules/php4/libphp4.a
# make
# make install</tt>

<B>Step 9: Edit the httpd.conf file</B>

All your compiling is done now! Your next step is to edit a config file for Apache. Open up this file as root (/usr/local/apache/conf/httpd.conf):

<center><img src="http://www.aselabs.com/images/articles/oct02/serverguide/apacheconf.gif"></center>

Add these lines anywhere:

<tt>AddType application/x-httpd-php .php
AddType application/x-httpd-php .php3
AddType application/x-httpd-php .php4</tt>

Find the line DirectoryIndex it'll be in like html tags. Inside it add <tt>index.php</tt>. This tells Apache that index.php can be an index in a directory.

<B>Step 10: Copy the php.ini file</B>

From the php source directory, copy one of the php.ini files (they will be named with stuff like 'dist' on the end of it) to /usr/local/lib/php.ini. Change anything you want in the file to suit your needs.

<B>Step 11: Test your server</B>

Start the server:
<tt># /usr/local/apache/bin/apachectl start</tt>

One of the final tasks. Create a file, test.php and put it into the htdocs of the apache directory. This is the directory where the website is.

test.php:
<tt><?PHP phpinfo(); ?></tt>

Now browse to http://localhost/test.php and you should get a page with a whole lot of things. If you see it, you've got your server working! If not, <a href="/viewforum.php?forumid=15">ask for help in our Linux forum</a>.

<B>Step 12: More MySQL stuff</B>

Now we want to make mysql nice and safe and make sure it works also. cd to the mysql directory and then to the 'bin' directory.

<tt># cd /usr/local/mysql/bin
# ./safe_mysqld --user=mysql &
# ./mysqladmin -u root password somepassword</tt> Change to whatever password you want

<B>Step 13: Auto Start up</B>

Last step. This makes MySQL and Apache start up when the OS is loaded.

<tt># echo "/usr/local/mysql/bin/safe_mysqld --user=mysql &" >> /etc/rc.d/rc.local
# echo "/usr/local/apache/bin/apachectl start" >> /etc/rc.d/rc.local</tt>

Now open up a can/bottle of your favorite drink (Diet Pepsi is my Fav) and kick back and do some programming!

<center><big><a href="http://www.aseforums.com">Get all your questions answered in the forums</a>!></center>

Title

Medium Image View Large