Installing Apache, MySQL 4.7 and PHP 5.0.3

on Fedora Core 3

This small tutorial may help others trying to install MySQL 4.1.X and PHP 5.X on FC3
I am assuming that most of you know some basic Linux commands and have successfully
installed FC3 on your computer.  I have also assumed that the Apache 2.X web server
is not
currently installed on your FC3 machine. 

I have  performed this installation using my notes a few time with the same result
all the time........ it works. 

Feel free to email me at stephanj@melbpc.org.au with any queries or comments.

Without any further delay, let's get started.


1. Installing Apache

Log on to your FC3 machine as root user.  For the sake of this tutorial I have called my machine
victor. 
Let's first see if Apache in installed on my machine.  Open a terminal window and type
the following.

  image-1


You should notice that I don't seem to have Apache installed.  That is, no httpd
service, so let's install it.

From the Menu select System Settings -> Add/Remove Applications.  
Shown below.

  s2


Make sure you select Web Server.  Shown below.

  i3


You will be asked to insert Disk 1 of FC3.  So do that. 

NOTE if you get an error message that indicates
the application cannot be
installed simple keep  FC disk 1 in the CDROM drive and repeat the installation
process again.  It should work the second time.

Try the rpm query command again to see if Apache is installed.  Hopefully you will get the a similar output
show below.


  s6


Before we start the Apache Web Server we are going to edit its configuration
file
httpd.conf found at

  /etc/httpd/conf/httpd.conf


Using a simple text editor, I use Kate,  open the file /etc/httpd/conf/httpd.conf and navigate to the
section that says

#ServerName new.host.name:80


I have added an extra line as you can see from the screen dump below which says

ServerName victor:80

Since my Linux FC3 machine is called victor I will set the ServerName directive to victor
You choose your own computer name for his section.
 If you don't know your computer's name type hostname at the command prompt, or
you could use localhost.
 
s7


The next step is to allow users to have access to their own webpages on your machine. 
I prefer to do this since I only use the root account for administrative features.

Navigate to the section that says UserDir disable.  You should see the screen dump below as the default.
  s8


Change the sections UserDir disable and UserDir public_html to the screen dump below.
 ie comment out
UserDir disable  with a # and uncomment UserDir public_html by removing the #.

  s9


Next, we will uncomment the section <Directory /home/*/public_html>.  Make sure your file
looks like to one below.
  s11

Great.  We have finished with this file.  SAVE IT!!!!!

We now need to restart the Apache service (httpd daemon) to instigate the
features we have edited.  Simply type:

/etc/rc.d/init.d/httpd restart

At the command prompt. 

See the screen dump below
  s12

The first fail is probably due to the fact that httpd wasn't started in the first place, hence the fail.  But there
is nothing stopping you typing
/etc/arc.d/init.d/httpd restart again.  At this stage we should make sure
that httpd is started each time the computer is started.  For this you will need to use the services command.

Open the services command from the menu.  ie
System Settings -> Server Settings. Shown below.
  s13

You will be presented with the screen dump shown below.  Make sure you

1.  Check the box next to httpd.
2.  Click the SAVE ICON.  This is important DON'T FORGET IT!

Make sure the service is running by looking at the Status pane.  Your service needs to run if you
want to use it.
Also you can restart the service and SAVE the configuration each time you edit a configuration file.
  s14


Our next step is to create a user. 


2. Creating a user

Let's create the user stephanj with password letmeinWe will place the user
stephanj in a group called home.

From the menu select System Settings -> Users and Groups as shown below.

  s15


Select the New Group Icon and type home.  Then click OK.

image16


Click  the Add User icon and type in the user details.  The details for user stephanj is shown
below.  I have unchecked the Create a private group for user check box since I
want my user to be a member of the home group.

image17

When your happy click OK.

Highlight the user you have just created, in my case stephanj and click the
Properties Icon.  We are going to make the user stephanj primary group
home, rather than the default user.

Go ahead a do this, then click OK.

s18


All you need to do now is open a browser and type http://machine_name, in my case
its http://victor.  You should see the Fedora Core Apache test page below.  If not, try
restarting the httpd service again.

s19


3.  Creating a user's public_html directory

Since you have created a user called stephanj, or what ever you may have called your
your user, you will need to created a public_html directory under the user's home
directory to that the user can display their web pages. 

So log into FC3 as the user, in my case stephanj, and create a public_html directory. 

NOTE you must log in as the user!

Once you have created the 
public_html with the command mkdir public_html, you
will need to change the permissions of the user's home directory along with the user's
public_html directory.
The home directory requires permission 711, while the user's public_html directory
requires permissions 755.  Do this recursively using the -R option. 

See the screen dump below for all the commands I used.

image111


The tricky bit is that FC3 has introduced new security content with users' public_html
directories.  So let's change it with the chcon command.  Type in the command
below.  IF YOU DON'T DO THIS YOU WILL NOT BE ABLE TO VIEW ANY
HTML FILES IN THE USER'S HOME DIRECTORY!

image112


All you  need to do now is open a browser and point to the user's home page. 

http://victor/~stephanj     (ie http://machine_name/~user_name)

There is nothing in there at the moment but you should still see a screen dump similar to the
one below. (use the ~username to access the user's directory)

image113



4.  Using PHP

The chances are that when you installed Apache you would have automatically installed
PHP as well.  Let's see if it is installed.  Open a terminal window and type the following

image114

Looks like PHP is installed along with php-pear and php-ldap.

Log in as a user, in my case stephanj, and create the following file called
phptest.php (it needs a php extension) and save it in the public_html directory
Don't forget the  ; at the end of the line phpinfo().

image115

Open a browser and point to that file with the URL

http://victor/~stephanj/phptest/php.


You should get the screen dump below.  If so you are 1/2 way there.  Congratulations!
image115

PHP is working................... now onto MySQL.


5.  Installing MySQL

Make sure you are logged in as root for this section

You probably have not installed a database server on your machine.  Just as a check, open a
terminal window and type the following.

image117

Looks like I don't have MySQL installed on victor.

Since MySQL is not installed on my machine will will need to install the packages.

From the Menu select System Settings -> Add/Remove Applications.   Find
the section for Database Server and make sure you select the packages indicated
below
.
image118

Go ahead an install all the packages.

Once you have done this, perform another package check for
MySQL. 
image1110


At this stage MySQL has not been started as a service so you will need to start it
using the Service Configuration.  Recall that this was also the case with Apache!

So open Service Configuration and select
MySQL.  Don't forget to SAVE the new configuration.

image1111


Now start MySQL.  It should now be running as a service.
Don't forget to SAVE the new configuration!

image112


Our next step is to create a root password for MySQL root user.  Open a terminal
window and at a command prompt type

mysqladmin -u root password 'portpower'

Where portpower is the password I have chosen for the root user.  We are using
MySQL's powerful mysqladmin command to set a root password.  I STRONGLY
advise you do this!

Once you have set the root password for the MySQL server you can now
log into the MySQL database server.  Open a terminal window and type

mysql -u root -p

The screen dump below should be displayed.  If so............. FANTASTIC!


image113


Notice that the prompt changes to mysql>.  This means you are now logged into the
MySQL server.  To exit the server type  \q.

Just like your FC3 machine we only use the root account for MySQL for administrative
purposes.  So your next step is to create a MySQL user account and a database for the new MySQL
user.

I am going to create a new database called sj_gym for a new user called stephanj with password
letmein.  It's not necessary that the user have the same username/password as the Linux username
/password but it make your ever increasing password list just a bit shorter.  The user stephanj
will have FULL ACCESS to the database sj_gym, that means he can edit, delete, alter etc. the
database.  However user stephanj will not have access to other users databases.

Open a text editor, I use Kate, and type the following.  I have included a HTML and screen dump
version of the file  usersetup-gym.sql.


# quick check
FLUSH PRIVILEGES;
DROP DATABASE IF EXISTS sj_pm;
CREATE DATABASE sj_pm;
GRANT ALL ON sj_pm.* TO stephanj@localhost IDENTIFIED BY 'letmein';
GRANT ALL ON sj_pm.* TO stephanj@'%' IDENTIFIED BY '
letmein';

                                                        usersetup-gym.sql
sj1


Save this file, say in your root directory and then open a terminal window and
navigate to the same directory where you saved the the file
usersetup-gym.sql.

Now log into the MySQL server as root user as shown below.

sj2

(I should mention here that FC3 is shipped with MySQL 3.23.58, we will upgrade
this to 4.1.X next)

At the mysql> command prompt type

\.
usersetup-gym.sql

The whitespace is required after the \.  You are telling MySQL to run the script you have
just created.  Hopefully you should see something similar to the screen dump below

sj3

Looks like I have success.  The only way to tell is to LOG OUT as root from MySQL and
log in as the new user.

We will now log in as the new user stephanj using the new database we have created
called sj_gym.  At the command prompt type.

mysql  sj_gym -u stephanj -p

We are telling the MySQL server to use the database sj_gym with user
stephanj using a password.  The screen dump below is what  should be displayed

sj5


We're in!  So far so good.  Next step is to create a table and populate it with data for the
sj_gym database.

Log out of MySQL (use \q)

Log onto the Linux machine as user stephanj and create the following script that creates
a table and populates it.  Save this file as
usersetup-gym.sql in home home directory of the
user.

DROP TABLE IF EXISTS school;

CREATE TABLE school
(
    school_id        smallint(3)        NOT NULL AUTO_INCREMENT PRIMARY KEY,
    school_name        varchar(20)      NOT NULL
)TYPE=InnoDB;

#*** delete data from tables if exists
DELETE FROM school;

INSERT INTO school VALUES(1,'Wantirna');
INSERT INTO school VALUES(2,'Bayswater');
INSERT INTO school VALUES(3,'Scoresby');
INSERT INTO school VALUES(4,'Vermont');
INSERT INTO school VALUES(5,'Boronia');

                                                                        usersetup-gym.sql

sj8


As the user, in my case stephanj, log onto the MySQL server a n run the
script as shown below.  (You should now how to log into the MySQL server
and run a script!)

sj9

At the MySQL prompt type in a simple SQL statement to see if everything is
OK.  Type the command below


sj10


6. Using PHP and MySQL

Our next step is to connect to MySQL from a PHP script.  In order to do this you will
need to log in as the user stephanj.  As user stephanj  navigate to the public_html directory
and create the following PHP file.  I have called this file connect.php.

NOTE:  The variable in the script called $db_host is the IP address of my computer victor.
use your own IP address of your machine or try using  localhost.  Use the username, password
and database name you have created.

<?php
        //connects to database for gym program
      $db_host = "192.168.1.10";
   //  $db_host = "localhost";
     $db_user = "stephanj";
     $db_password = "letmein";
     $db_name = "sj_gym";
     $connection = @mysql_connect($db_host, $db_user, $db_password) or die ("error connecting");
     mysql_select_db($db_name, $connection) or die("Could not open database"); 
       
?>

                                                                            connect.php

sj11

Next, create a php script called displayschools.php that will connect to this database and access the table school.
It's not necessary that you understand the code, we are simply trying to see if PHP can talk to MySQL.  I
have taken this snippet of code from an existing PHP application so please ignore any additional bits
and pieces.

<html>
<?php>
    require_once("connect.php");
?>
<head>
    <title>Gymnastics Program - 2005</title>
</head>
<body>
    <div id="topContent">
        <h1>School Gymnastics Program</h1>
        <form action="studentresults.php" method="get" name="Student" target="mainFrame">
              <fieldset>
                  <legend>Select a school to view its students</legend>
                <label>School: </label>
                <select class="text"name="schoolselect" >
                <?php
                    //Query database to find school names for drop down box
                    $query = "SELECT * FROM school";
                    $result = mysql_query($query, $connection);
                    //error report
                    if(!$result)
                        die("The query has failed with the following error : ".mysql_error($connection));
                    //loop through school names
                    for($i=0; $i < mysql_num_rows($result); $i++)
                    {
                        //store school name in variable
                        $schoolname = mysql_result($result, $i, "school_name");
                        //store school number in variable
                        $schoolnumber = mysql_result($result, $i, "school_id");
                        //display html
                        print "<option value='" .$schoolnumber."'>" .$schoolname. "</option>";       
                    }
                    //close connection
                    mysql_free_result($result);
                    mysql_close($connection);
                ?>
                </select><br />
            </fieldset>
        </form>
    </div>
</body>
</html>

                                                                                                        displayschools.php

sj12

Now th big test to see if we can connect to the database and display some information.
Open a browser and type

http://machine_name/~username/displayschools.php

I am hoping you get the display shown below.  If that's the case GREAT!  You how
have LAMP (Linux Apache MySQL PHP )working!  If not try again, I have done this about 8 times with the
same result....................... it works.  You can email me and I may be able to help.
sj13


7. Upgrading MySQL 3.23.X to MySQL 4.1.X

Log in as root for this section

MySQL 3.23.58 is good BUT MySQL 4.1.X is better. Especially if you need to use sub queries
which I do all the time.  So let's perform the upgrade.  Firstly, we will just see what MySQL
packages are installed on victor.  Type the following below as shown on the
screen dump

sj14

I have 4 MySQL packages installed on victor.  Next, navigate to http://www.mysql.com
and download the latest MySQL 4.1.X rpms for Linux.  At the time I did this the
latest MySQL rpm was 4.1.7.  Download these files in a separate directory.
Below are the files i have downloaded.  I know I don't need all of them but you can
always rpm -e the ones you don't need letter

sj16


Next, let's install them.  Open a terminal window and type the following at the
command prompt. 

rpm -Uivh MySQL-*

sj17

Looks like we have success..........................

sj18


Now the big test, log into the MySQL server as root. 
Type the command shown on the screen dump below

sj19

Oh no................ we can't log in.  I don't know what MySQL has done during the upgrade but if
you RE-START your machine you will find that MySQL will now work,  So REBOOT

Now we have success.  Also PHP works with the new version of MySQL
sj20

sj21


8. Upgrading to PHP 5

Log in as root for this section

The final step is to upgrade to PHP 5.  Before we do that let's see what
PHP packages we have on our machine.  Type the following at the command
prompt

sj113

Navigate to the following URL

http://download.fedora.redhat.com/pub/fedora/linux/core/development/i386/Fedora/RPMS/

and download the following packages into their own directory.

sj1114


Next type in the following to upgrade

rpm -Uivh php-*

You will probably find that you have a dependency failure.
sj115

So we also need to download the latest db4-4.3.21-1.i386.rpm.  You may already have this
file installed on your machine but you need to install the latest version.  Go back to

http://download.fedora.redhat.com/pub/fedora/linux/core/development/i386/Fedora/RPMS/

and download this rpm and install it as shown below.


sj117


Restart Apache..........................

sj118


Ammended 30-03-2005

We haven't finished just yet.................  I have just noticed that Pear (the abstraction layer for PHP) doesn't
work without the php-DB-1.6.5-1ark.noarch.rpm rpm.  This RPM is not given to use at

http://download.fedora.redhat.com/pub/fedora/linux/core/development/i386/Fedora/RPMS/
so we will need to download it from

http://rpm.pbone.net/index.php3/stat/4/idpl/1732489/com/php-DB-1.6.5-1ark.noarch.rpm.html

Go ahead and download the file and install it with the command

rpm -ivh php-DB-1.6.5-1ark.noarch.rpm

Then restart Apache with

/etc/rc.d/init.d/httpd restart


Everything should work NOW (as well as Pear)....................................



Last edited by
Stephan Jusypiw
stephanj@melbpc.org.au
28-03-2005