Thursday, January 29, 2009

Setting up a LAMP Server

It is a common task for Linux System Administrators to set up and to establish a web server. On Linux Systems it is common to have Linux, Apache, MySQL and PHP for production. In this tutorial I'll show you on how to set up LAMP with the latest Httpd (Apache), MySQL, and PHP. Here I'll be using CentOS 4.7.

Actually you can have a LAMP running Apache 2.0, PHP 5.1 and MySQL 5.0 on CentOS 4/RH4 easily with yum. How?
Here it is:

To install apache 2.0
yum --enablerepo=centosplus install httpd
To install mysql 5.1
yum --enablerepo=centosplus install mysql mysql-server
To install PHP 5.1
yum --enablerepo=centosplus install php

Then you are done you have a CentOS 4.x running Apache 2.0 MySQL 5.1 and PHP 5.1. Hmmm quite nice isn't it.

But how about if you really insist to have the latest. Well then here is how.

Of course you have to remove all the old Apache, PHP and MySQL instances. Here's how to do it.

1. First check if they are currently installed
rpm -qa|grep httpd ;to check if Apache is installed
rpm -qa|grep mysql ; to check if mysql is installed
rpm -qa|grep php ; to check if php is installed

2. If they are installed then remove them
yum remove httpd
yum remove mysql
yum remove php

3. Remove the remaining traces or dependencies of it
rpm -qa|grep php

you will see here like

php-common
php-pdo

remove them via

rpm -e php-common php-pdo

do the same thing on httpd and mysql

rpm -qa|grep (packagename)

then remove remaining traces

rpm -e (packagename)

4. Once all is clean we can start downloading all the latest packages for our new LAMP

Installing Apache

1. Goto /usr/src

cd /usr/src

1. Get the latest apache server say 2.2.x

wget http://apache.mirrors.biblionix.com/httpd/httpd-2.2.11.tar.gz

2. Extract it

tar xvzf httpd-2.2.11.tar.gz

3. cd httpd-2.2.11

4. then under the httpd folder do the following

./configure
make
make install

By default the apache is installed at /usr/local/apache2 folder

5. Set the necessary configuration you want for your web server

vi /usr/local/apache2/conf/httpd.conf

in my case I set my document root on /var/www/html
to avoid directoy listing do this
On directory directive you will see this

Options Indexes FollowSymLinks

make it like this

Options -Indexes FollowSymLinks

well enough on httpd configurations we'll have more on our next discussions to follow

6. To make the httd start at boot, we'll put it on Linux services. How?

- copy first the apachectl script from the httpd folder

cp /usr/local/apache2/bin/apachectl /etc/init.d/httpd

- we put it on under init.d since all start up scripts are under this folder and being executed upon boot

- we're not done yet. Open httpd startup script.

vi /etc/init.d/httpd

- Add the following entries after the second line

# chkconfig: - 85 15
# description: Apache Web server

- let us add it on the Linux services

/sbin/chkconfig --add httpd

- then set it to start on running levels 345

/sbin/chkconfig --level 345 httpd on

- check if it is set well

/sbin/chkconfig --list|grep httpd

- you should see this

httpd 0:off 1:off 2:off 3:on 4:on 5:on 6:off

7. Now start the httpd service by

/sbin/service httpd start

8. to check if it is running

/sbin/service httpd status (only works for apache installed via rpm or yum)

or do

ps aux|grep httpd (works for all so just do this in our case)

- you should see either of the following

root 27093 0.0 0.1 4108 1808 ? Ss 14:31 0:00 /usr/local/apache2/bin/httpd -k start
apache 27114 0.0 0.1 4108 1640 ? S 14:32 0:00 /usr/local/apache2/bin/httpd -k start
apache 27115 0.0 0.1 4108 1300 ? S 14:32 0:00 /usr/local/apache2/bin/httpd -k start
apache 27116 0.0 0.1 4120 1648 ? S 14:32 0:00 /usr/local/apache2/bin/httpd -k start
apache 27117 0.0 0.1 4108 1648 ? S 14:32 0:00 /usr/local/apache2/bin/httpd -k start
apache 27118 0.0 0.1 4108 1300 ? S 14:32 0:00 /usr/local/apache2/bin/httpd -k start
apache 27119 0.0 0.1 4108 1300 ? S 14:32 0:00 /usr/local/apache2/bin/httpd -k start

-- now you have a running Apache server version 2.2

Installing MySQL

We'll do the RPM way of installing this one.

1. First you have to know what version of CentOS you have and what platform are you working in with.
- to know what platform are you working with issue the command

uname -i ; this will display or echo the platfrom in my case its i386

- to know what OS version are you running with open this file or just do cat

cat /etc/redhat-release ; this will display the OS version in my case it's CentOS release 4.7 (Final)

2. Since we're running i386 platform and version 4.x CentOS we'll download the necessary MySQL rpm packages suitable to this platfrom. In this case it is x86 or Red Hat Enterprise Linux 4 RPM (x86) downloads. Here are the things we need.

- MySQL-server-community-5.1.30-0.rhel4.i386.rpm
- MySQL-client-community-5.1.30-0.rhel4.i386.rpm
- MySQL-devel-community-5.1.30-0.rhel4.i386.rpm
- MySQL-shared-community-5.1.30-0.rhel4.i386.rpm

- so let's download them

wget http://(Mirror-linkname)/(rpmpackagename)

3. Once all neede packages are downloaded we can now install them. Issue the following command

- to install each of the packages issue this

rpm -ivh (packagename)

Ex. rpm -ivh MySQL-server-community-5.1.30-0.rhel4.i386.rpm

4. Then ensure that mysql service can start at boot.

/sbin/chkconfig --list|grep mysql

- you should see this

mysql 0:off 1:off 2:on 3:on 4:on 5:on 6:off

- if it is not then make sure it runs / start on run level 345

/sbin/chkconfig --level 345 mysql on

5. Start MySQL service

/sbin/service mysql start

6. By default MySQL root has no password so just set its password say by installing first phpMyAdmin then reset password there or just go using the mysqladmin or mysql console command to reset password.

-- Now you have a running MySQL server version 5.1.x

Installation of PHP

Before we proceed on installing PHP we need the following to be installed first. These packages are known and commonly used on web development such as XML, web service and others

- curl (compile from source or you can do yum)
- curl-devel (if you did yum on curl)
- zlib (compile from source or do yum)
- libxml2 (compile from source or do yum)
- libxml2-devel (if you did yum on libxml2)
- openssl (you can do yum on this one)
- libpng (do yum here)
- libpng-devel (do yum here)
- gd (do yum)
- gd-devel (do yum)

Once all of these package are installed we can proceed on php installation.

1. Get the latest PHP packages from php.net

2. Goto /usr/src

3. Extract it there

4. cd php-5.x/

5. then issue this

./configure --with-mysql --enable-soap --with-apxs2=/usr/local/apache2/bin/apxs --enable-ftp --with-curl --with-curlwrappers --with-zlib --with-gettext --with-gd

make

make install

6. You now have installed PHP 5.x

7. There are things to verify to make PHP5 really running. How? Create you phpinfo.

8. If phpinfo is not displaying any content at all then there must have any problem on the httpd configuration. Here are the things that you should ensure to have on you httpd.conf

LoadModule php5_module modules/libphp5.so ; by default this is placed by PHP installer

DirectoryIndex index.php index.html


AddType application/x-httpd-php .php

AddHandler php5-script .php

AddType text/html .shtml .php

9. Save your changes on the httpd.conf and restart apache.

10. Check again your phpinfo it should display the current PHP and apache settings.

11. By the way sometimes php.ini is not set up so just copy the php.ini to the configuration path check phpinfo for the right path

- on phpinfo you will see

Configuration File (php.ini) Path /usr/local/lib

- copy the recommended php.ini settings

cp /usr/src/php-5.X/php.ini-recommended /usr/local/lib/php.ini

-by the way you should always restart your Apache service after saving any changes you applied on php.ini.

=== Now you have the latest LAMP running.

Hope you find these useful. Thanks, Cheers and God Bless!!!

No comments: