Guide to compile and customize Apache 2, MySQL 5 and PHP 5 on Linux (Fedora Core 5).

Comments/suggestions about this guide is more than welcome: michael [at] ampjuke {dot} org.

- You must be logged in as root (or have root access) to the box you want to put the stuff on.
- Text written with
black, bold types (like this) below are commands you're supposed to punch in using your keyboard.
- This guide was written during an actual installation. I cannot gurantee you won't run into problems in your environment. In other words: I cannot be held responsible...

When you see this icon: opens in a new window, it means that the link will open in a new window.


After trying to get Apache 2, MySQL 5 and PHP 5 installed on my Fedora Core 5 box, I (or was it the update-process?) by accident mocked up the existing built-by-hand installation of Apache, MySQL and PHP.

Maybe I'm an amateur...but at least it gave me the opportunity to go for the latest (almost) versions of Apache, MySQL and PHP (2.0.59, 5.0.24 and 5.1.6 respectively).

opens in a new window According to some sources, performance might be better when you compile something "by hand" rather than just installing some RPM's.
I don't know if that's completely true, but besides the (assumed) advantage in performance, it also gives you the freedom to choose what to install, where it should be installed as well as what modules to run.
So - back to square one - I WROTE this document in order to (finally) be able to get along with the transfer of my open-source music-project, opens in a new windowAmpJuke, to a box with new hardware as well as new software.

It is my sincere hope that others can benefit from this step-by-step guide, in their search for a guide how to install Apache, MySQL and PHP on a Linux box.

The steps we're about to go through are NOT recommendable if you:
- Isn't familiar with Linux/Unix (at least to some extent).
- Don't like the concept of installing something "by hand" (that is: compiling etc.).
- Already have running versions of Apache, MySQL and PHP and don't want to get rid of them.

Ok ? All set ? Got the time to actually DO this ? Here we go...


Before we even begin installing Apache 2, MySQL 5 and PHP 5, it might be a good idea to get rid of any old instances of Apache, MySQL and/or PHP, if they're already present on your box.

I won't go any further into this topic
If you want to know more, take a look at what's offered on opens in a new windowremoval of RPM's elsewhere.

Now, go and get the SOURCE-packages from the various websites:
Apache: opens in a new window
MySQL: opens in a new window
PHP: opens in a new window (that's cool - they seem to be using their own software on their own website !).

A good advice is to put the compressed SOURCE-images (.tar.gz-files) in /usr/local/src (we will assume you did just that below...).


The versions we're about to install on Fedora Core 5 are:
Apache 2.0.59
MySQL 5.0.24
PHP 5.1.6
...if your versions are 0.0.1 off in either direction, I assume it won't change much in relation to this guide, but if you're going for f.ex. Apache 1.2.27 it might not be wise to follow this guide any further.

So, the stuff have been downloaded into /usr/local/src (remember my advice from above?).
You should then extract and unpack the downloaded files.
Do that by entering these three commands for Apache:
cd /usr/local/src
gunzip httpd-2.0.59.tar.gz
tar -xvf httpd-2.0.59.tar
...continue with MySQL:
gunzip mysql-5.0.24.tar.gz
tar -xvf mysql-5.0.24.tar
...and finally PHP:
gunzip php-5.1.6.tar.gz
tar -xvf php-5.1.6.tar
Source is in place, - let's rock on by...

Compiling Apache 2.

First we'll compile Apache.
It's pretty straghtforward, so open a terminal (remember to be super-user aka. root) and type in these two commands:
cd /usr/local/src/httpd-2.0.59
./configure --prefix=/usr/local/apache2 --enable-mods-shared=most --enable-module-rewrite

This will only take a couple of minutes, depending on the speed of your box...

When the prompt comes back, type:
...wait a few moments, and then issue this command:
make install
A lot of stuff is written to the screen during the make / make install.
After a short time, the prompt comes back.

Finally change the security context of .so-modules within Apache:
chcon -t texrel_shlib_t /usr/local/apache2/modules/*.so
...and now Apache 2 is compiled ! You must also configure Apache, so that's what we'll do next.

Configuring Apache 2.

This step involves telling Apache where to serve web-documents from, as well as a couple of other things.
We will accomplish that by editing Apache's configuration file (httpd.conf):
vi /usr/local/apache2/conf/httpd.conf
Note: If you're not familiar with the vi-editor, use something else...

Find the section containing "LoadModule" instructions, and add a line beneath the last entry that reads:
LoadModule php5_module modules/
Find the line starting with "DocumentRoot" and ensure it's says:
DocumentRoot "/usr/local/apache2/htdocs"
...since we will be serving web-content out of that might want something different, so enter whatever is appropriate.
We will, however, assume it's /usr/local/apache2/htdocs for the rest of this guide.

Locate the line with DirectoryIndex and add index.php, so the complete line reads:
DirectoryIndex index.html index.html.var index.php

Finally, find the "AddType" section and add(!) this line:
AddType application/x-httpd-php .php'll probably notice there is a lot of settings to play with in this file, most of it is commented out.
You might take a mental note about going back to Apache's httpd.conf file later and play with these settings...
Or jump straight into http.conf settings.
However, we have accomplished what we want to now, so just save the file and quit vi or whatever editor you we're using.

Compiling MySQL 5.

Just like Apache 2 above, we will compile MySQL in a similar manner.
This is done by entering (still as root):
cd /usr/local/src/mysql-5.0.24

Then run the configure-process...
(Important: The command below is ONE command only, even though it seems to span two or more lines... ):
./configure --prefix=/usr/local/mysql --disable-maintainer-mode --with-mysqld-user=mysql --enable-large-files-without-debug --localstatedir=/usr/local/mysql/data --with-unix-socket-path=/tmp/mysql.sock

This step might actually take a while.
Once the prompt comes back issue a...
...command, then wait a while and - when the prompt finally is back - type in:
make install

Sit back. Watch TV. Check your RSS-feeds. Whatever...
MySQL is by far the most "heavy" application to compile, so be patient.
Once the prompt is back: Done !

Configuring MySQL 5.

If you haven't got a mysql user and mysql group, now it's the time to add them to your system using these two commands:
/usr/sbin/groupadd mysql
/usr/sbin/useradd -g mysql mysql

Then install the database files:
Change ownership to the mysql user/group:
chown -R root:mysql /usr/local/mysql
chown -R mysql:mysql /usr/local/mysql/data

Edit the file
vi /etc/
...and add the following line, if it isn't there already:
Then save the file and quit vi.

Install the database files with:

Copy the appropriate support file to /etc/my.cnf.
"What's appropriate ?", you might ask.
Well, the requested file comes in four flavors: my-small.cnf, my-medium.cnf, my-large.cnf and my-huge.cnf.
I'm normally using my-medium.cnf, so that's what we'll do as well:
cp ./support-files/my-medium.cnf /etc/my.cnf
Feel free to use one of the others, but it's at your own risk.
You might also take a mental note here, and go back and revisit my.cnf later in search of performance tweeks.
If you're unsure what my.cnf is all about, then take a look elsewhere.

We'll start MySQL right away by issuing this command:
/usr/local/mysql/bin/mysqld_safe --user=mysql &

Check that MySQL actually IS running:
ps -e | grep mysqld_safe
...if you see a line that looks like this:
27632 pts/1 00:00:00 mysql_safe
...everything is OK, - it's alive and running.

If MySQL is NOT running, and/or complains about something and stops when you start the mysqld_safe background process (you won't be in doubt if that happens), it's probably something about permissions (yes - we're on a Linux-box - permissions are taken seriously in this environment...).
Do not despair, try and enter the two following commands:
chown -R root:mysql /usr/local/mysql
chown -R mysql:mysql /usr/local/mysql/data
...and go back and start MySQL again (see above how that's done).

Change password for MySQL's root-user:
/usr/local/mysql/bin/mysqladmin -u root password my_password
...where my_password is the password you actually want (and can REMEMBER).

Now we move on to the last chapter: PHP...

Compiling PHP 5.

PHP - the wonderful "glue" that binds together your MySQL databases with web-users - is the last one to build (compile and customize).
We will compile version 5.1.6, so sit tight and enter these two commands:
cd /usr/local/src/php-5.1.6
./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql=/usr/local/mysql --enable-xml --with-gd --with-xml --with-iconv
(if you don't have gd installed, and/or don't want xml, iconv etc., you can skip everything after the --with-mysql=/usr/local/mysql)

This should be quick, so when the prompt comes back, enter:
...and then (yes, you guessed it!):
make install
...and you're done with compiling PHP !

Configuring PHP 5.

Copy the recommended settings:
cp php.ini-recommended /usr/local/php/lib/php.ini
Edit the file:
vi /usr/local/php/lib/php.ini
Change the doc_root setting to:
doc_root= "/usr/local/apache2/htdocs/"'ll probably notice a LOT of other stuff to play with here...leave it for now as we're in a hurry to get finished (yes: make another mental note...or take a quick look).
Save the file and quit vi.

Test the build of Apache 2, MySQL 5 and PHP 5.

Does it all work ??
Say a little prayer and, assuming MySQL is still running, issue the command:
/usr/local/apache2/bin/apachectl start

Now we want to SEE something:
cd /usr/local/apache2/htdocs

Create a file:
vi test.php
Insert this line into the file:
< ?php phpinfo(); ?>
save the file and quit vi.

You can check everything is ok, by opening your favorite browser (have you tried FireFox?) and goto the address:
http://localhost/test.php should display various information about Apache 2, MySQL 5 and PHP 5 as well as other modules included in the installation.
If you want to test it from another machine, you can do that by opening a browser and goto: http://"IP-address or hostname"/test.php
...where "IP-address or hostname" f.ex. can be the IP-address assigned of your box.

Starting it all at boot time.

You might want to start it all automatically at boot time (I like that).
We will first look at automatic start of MySQL 5 at boot:
cp /usr/local/src/mysql-5.0.24/support-files/mysql.server /etc/init.d/mysql

Create links for run levels 3 and 5, - we start with run level 3 using three commands:
cd /etc/rc3.d
ln -s ../init.d/mysql S85mysql
ln -s ../init.d/mysql K85mysql

Do the same for run level 5, again typing in three commands:
cd /etc/rc5.d
ln -s ../init.d/mysql S85mysql
ln -s ../init.d/mysql K85mysql

Change permissions:
cd ../init.d
chmod 755 mysql
MySQL-start done !

Now we move on to Apache, - in a similar way as above:
cd /usr/local/apache2/bin
cp apachectl /etc/init.d/httpd

Repeat the process with linking to Apache 2 (httpd) for run level 3 and 5:
cd /etc/rc3.d
ln -s ../init.d/httpd S85httpd
ln -s ../init.d/httpd K85httpd

...and on to run level 5:
cd /etc/rc5.d
ln -s ../init.d/httpd S85httpd
ln -s ../init.d/httpd K85httpd

Finally set permissions:
cd ../init.d
chmod 755 httpd

That's it ! We're at...

The end... (not the music from the movie "Assault On Pct. 13", but rather this guide)

If you have the courage, reboot your box and (hopefully) everything will work as expected.
At least that was the intention with this guide...

Now it might be a good time to - finally - make that home for your huge collection of musicfiles by - finally - setting up that server to stream music from, so you can enjoy the streamed music anywhere there's internet connection...
What I'm suggesting is of course to take a look at AmpJuke.
It even has a brilliant installation example, a good FAQ, and - if you're just curious - an online demo.

Of course: whatever you will use your box for, is not really my concern.

I wish you good luck and best regards.
Michael Iversen

Copyright 2007 : Michael Iversen.
Feel free to link to this guide from your site, by clearly stating where it can be found (the complete URL to this document must be shown).