Tuesday, January 17, 2012

Building a LAMP Server




Building a LAMP Server

Code Convention

Italics Command
x Number
X Alphanumeric characters.

Building a LAMP Server
This document provides a walk through the installation of Linux, Apache, MySQL and PHP (LAMP). This document is targeted at installation on Red Hat Enterprise Linux 5 32 bit machine but can also be adopted for earlier or later release of Red Hat, other RPM-Based distribution (CentOS/Red Hat/Fedora/SUSE) as well as Debian-based systems.

Initial Steps
Most out-of-the-box Red Hat Linux installations will have one or more of the LAMP components installed via RPM files. For simplicity sake, installing applications from source code was avoided; hence we use the site like http://www.rpmfind.net/, http://www.rpmseek.com/, http://www.rpm.pbone.net/ to locate the correct RPM version for the system.

Log in as root
Because we will be installing software to directories that "regular" users don't have write access to, and also possibly uninstalling RPM versions of some applications, we'll log in as root

Check RPM Versions of the Applications
Before we start, we need to check if any LAMP component is already. To find out what RPMs are already installed, use the RPM query command:

rpm -qa
in conjunction with grep to filter your results:

rpm -qa grep -i apache
rpm -qa grep -i httpd
rpm -qa grep -i php
rpm -qa grep -i mysql

Check RPM Versions of the Applications
To remove the RPMs on the system, use the RPM remove command:

rpm -e filename

for each RPM you found in the query. If you have any content in your MySQL database already, the RPM removal step should not delete the database files. When you reinstall MySQL, you should be able to move all those files to your new MySQL data directory and have access to them all again.

Getting the latest RPM.
It is wish ideal to use the latest RPM or the last stable release. Get the RPM from the site specified above or other RPM finder site. Get the RPM for:
· Apache/httpd
· PHP
· MySQL
Put it and other libraries and dependencies in a central location, for this purpose, I put them in the folder LAMP as show below:


Each folder inside LAMP folder represent location of the respective RPM’s.

NOTE: All installation must be done through log-in as root user.

Install and configure Apache/httpd
Apache installation must be done before that of PHP, all dependencies required by Apache and other RPM are included in the dependencies folder. Install Apache as follows:

rpm –ivh httpd-2.x.x-XXX.rpm

Stop and Start Apache 2
To start Apache2, type:
/usr/local/apache2/bin/apachectl start

To stop Apache2, type:
/usr/local/apache2/bin/apachectl stop

To Auto stop Apache2
To enable auto start pf Apache2, type
cp /usr/local/apache2/bin/apachectl /etc/init.d
chmod 755 /etc/init.d/apachectl
/sbin/chkconfig –add apachectl
/sbin/chkconfig –level 35 apachectl on

Test Apache
Launch the browser and type http://localhost/ The default port is 80 it would display index.html file shipped with Apache 2.

Install and configure PHP 5
After Apache installation, install PHP 5 as follows;

rpm –ivh php5x.-x.x.x.XXX.rpm
If any dependencies are unsatisfied, install the required packages from the PHP library folder and then retry the above command.

cp php-ini-dist /usr/local/lib/php.ini
gedit /usr/local/apache2/conf/httpd.conf

To ensure that PHP files are properly interpreted, and not just downloaded as text files, add the following line to httpd.conf file or remove the #ath the beginning of the line which reads

AddHandler application/x-httpd-php .php .phtml .php3 .php4
AddHandler application/x-httpd-php .php-source .phps

Modify the AddType lines like this:

AddType application/x-tar .tgz
AddType application/x-httpd-php .php .html .htm
AddType application/x-httpd-php-source .phps .html .htm

If you wish to use other/additional extensions/filetypes for your PHP scripts instead of just .php, add them to the AddType directive:

AddType application/x-httpd-php .php .php3 .php4

Add index.php to the list of valid Directory Index files so that your "default page" in a directory can be named index.php.


DirectoryIndex index.php index.htm index.html


However you may change the order if you want Apache to check htm first followed by html and then php to :


DirectoryIndex index.htm index.html index.php


Stop and restart Apache2

Test Apache 2 and PHP 5 are working
Create and add following content to /var/www/html/phpinfo.php file.

Launch the browser and type http://localhost/phpinfo.php
If it displays the page, your installation is successful.

Install and configure MySQL 5
The minimum MySQL components that must be installed are:
MySQL-client- 5.x.xx-XXX.i386.rpm
MySQL-server- 5.x.xx-XXX.i386.rpm
Others that could be installed based on the environment are
MySQL-devel- 5.x.xx-XXX.i386.rpm
MySQL-embedded-community-5.x.xx-XXX.i386.rpm
MySQL-shared-compact-5.x.xx-XXX.i386.rpm
MySQL-shared-5.x.xx-XXX.i386.rpm
MySQL-test--5.x.xx-XXX.i386.rpm

Some RPM include –community before the major version number like this
MySQL-test-community-5.x.xx-XXX.i386.rpm

To install MySQL and it component type:
rpm -ivh MySQL-server-5.x.xx-x.XXX.
rpm MySQL-client-community--5.x.xx-x.XXX.rpm
or

rpm -ivh MySQL-*.rpm
Install the header and libraries that are part of the MySQL-devel- 5.x.xx-XXX.i386.rpm

rpm –ivh MySQL-devel- 5.x.xx-XXX.i386.rpm

Set the password for the MySQL root user as follows:
/usr/bin/mysqladmin -u root password 'r00tWord'

Test MySQL
Run a quick test on, use the command line program mysql:

mysql –u root –p

Then enter the root password. Look at the database shipped with the installation

show databases;



Test Apache, PHP and MySQL
Log in to MySQL, create a database test if none exist:

create database test;

use test;

Create table contact as follows:

CREATE TABLE contacts (
id int(6) NOT NULL auto_increment,
firstname varchar(15) NOT NULL,
lastname varchar(15) NOT NULL,
phone varchar(20) NOT NULL,
PRIMARY KEY (id));

Insert into contacts values(NULL,"Bill","Gates","+2349191");
Insert into contacts values(NULL,"Larry","Eliision","+2348181");


Create and add following content to /var/www/html/test.php file.

echo "
" . $row['lastname'] . "";
echo "
" . $row['Phone'] . "";
echo "";
}
echo "";

mysql_close($con);
?>

Where user name is the database owner username, password is the login password for the user and the database (in this case test) you want to query.
Launch the browser and type http://localhost/test.php
If it displays the page, your installation is successful.

Friday, September 18, 2009

Welcome to my blogspot

Dear All,

I just sign up on blogspot, in recent days, I will be posting topics of interest on my page. Thanks for visiting my page.

ENJOY