How to setup LAMP on Cent OS 6

At sparxsys we use different flavors of Linux operating system like ubuntu, centos and fedora. Most of our servers where we host our Drupal sites are run on Cent OS machines. On all these operating systems for website development there is a need to have Apache, PHP and MySQL installed. Today let us see you how to setup a lamp stack on Cent OS6. Well here I am assuming you already have installed Cent OS on your machine so I will provide info on how to start configuration on your blank machine.

Basic configurations

First and foremost thing is to make sure that the system has the right tools necessary to become a web server. Apart from LAMP stack we need to install few important tools and perform some configurations.

Update your system
Firstly you need to update your newly installed machine so use this command to update your system

yum update

Setup Hostname
You need to setup the hostname to provide a name to your machine.

echo "" >> /etc/sysconfig/network
hostname ""

Update /etc/hosts localhost.localdomain localhost

X.X.X.X is the ip address of your server.

No need to modify if similar values are already there.

Setup the timezone
You need to set correct time and timezone on your system. So use this command.

ln -sf /usr/share/zoneinfo/Asia/Calcutta /etc/localtime

Of course you can check inside this folder /usr/share/zoneinfo/ to check for the time zone for your country.

After that just check the time.


It should display something like this.

Mon Apr 6 15:23:38 IST 2015

Add User
You should create a new user to use your system. You should avoid using root user because a wrong command run carelessly could easily break your system so create a new user using this command.

useradd webuser

Change the password for newly created user

passwd webuser

Now allow this user to run root commands via sudo

yum install sudo

After doing visudo find the line like this

## Allow root to run any commands anywhere
root ALL=(ALL) ALL

and add the following line after that

webuser ALL=(ALL) ALL

Now logout from system and login as this new user. All the further commands will be given by using this user. After this point do not login with root user until and unless it is absolutely necessary.

Prevent Dictionary attacks

Some security measures. Install Fail2ban

sudo yum install epel-release
sudo yum install fail2ban

Start the service

sudo service fail2ban start

Start fail2ban on boot

sudo /sbin/chkconfig fail2ban on

Checklist - Check available services

ls /etc/init.d/

Setup Apache Web server

Install Apache 2.2

sudo yum install httpd

Edit the /etc/httpd/conf/httpd.conf to adjust the resource usage settings
Before you edit it is best to make a backup of your file like this

cp /etc/httpd/conf/httpd.conf ~/httpd.conf.backup

Now make changes to the file /etc/httpd/conf/ are some good settings for your server. The settings mentioned below are for fine tuning your server performance.

KeepAlive Off

StartServers 2
MinSpareServers 6
MaxSpareServers 12
MaxClients 80
MaxRequestsPerChild 3000

Configure Apache Virtual Hosts
We will manage the sites that we want to manage using vhost file and not the httpd.conf which is used only for global configurations but the actual sites will be configured using vhost.conf file under /etc/httpd/conf.d directory.

  1. Create a file under /etc/httpd/conf.d called vhost.conf. add the following contents. replace with you domain name

  2. NameVirtualHost *:80

    DocumentRoot /srv/www/
    ErrorLog /srv/www/
    CustomLog /srv/www/ combined

    AllowOverride All
    Order allow,deny
    Allow from all

    DocumentRoot /srv/www/
    ErrorLog /srv/www/
    CustomLog /srv/www/ combined

    AllowOverride All
    Order allow,deny
    Allow from all

    As per the vhost.conf file we will have two sites running on our server and, of course you need to make sure that these two domains point to your server. Either by modifying their A record or complete DNS.

  3. Now create the respective directories referenced above

  4. mkdir -p /srv/www/
    mkdir /srv/www/
    mkdir -p /srv/www/
    mkdir /srv/www/

    include a index.html file in /srv/www/ folder with following contents

    my apache web server

  5. Start Apache for the first time, and set it to run at boot

  6. sudo service httpd start
    sudo /sbin/chkconfig --levels 235 httpd on

You should new be able to view a Apache page on your website with following message "my apache web server". You can do the same for website.

Install and Configure MySQL

Since we will be using Drupal so we need to have MySQL as well installed on the server.

Install the MySQL package

sudo yum install mysql-server

Start MySQL, and set it to run at boot:

sudo service mysqld start
sudo /sbin/chkconfig --levels 235 mysqld on

Run mysql_secure_installation to secure MySQL

You will be given the option to change the root password, remove anonymous user accounts, disable root logins outside of localhost, and remove test databases and reload privileges. It is recommended that you answer yes to these options.


Login with root and create a new database

  1. Log in to MySQL and then you will be presented with a MySQL prompt

  2. mysql -u root -p

  3. Create a database and user

  4. create database webdata;
    grant all on webdata.* to 'webuser' identified by 'password';

    In the above example webdata is the name of the database, webuser the user, and password a strong password

  5. Now logout from root and login with this webuser and check if this user can access webdata database
  6. logout from root


    login as webuser

    mysql -u webuser -p

  7. After checking the access logout from mysql

  8. quit

Install and Configure PHP

Install PHP

sudo yum install php php-pear
sudo yum install php-mysql

Edit /etc/php.ini
You must edit /etc/php.ini for better error messages and logs, and upgraded performance.Find the error reporting part and add the following lines

error_log = /var/log/php/error.log
max_input_time = 30

Create the log directory for PHP and give the Apache user ownership

sudo mkdir /var/log/php
sudo chown apache /var/log/php

Restart or Reload Apache

For restarting use

sudo service httpd restart

For reloading use

sudo service httpd reload

Reload will apply the changes in the configuration without terminating/interrupting any connections. Simple the Apache will first process the request, complete it and then restart, so server will eventually restart but users would not see downtime.In any case this is better than full restart.

Here is it all the pointers you need to follow to setup your lamp server on Centos6.

We host our websites on Linode, which offers reliable VPS solutions.

We have been using Linode since two years now and we must say that we are extremely satisfied with their service.