How to setup LAMP on Cent OS 6

Submitted by hemant.gupta on Fri, 09/25/2015 - 07:48

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

cent
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 "HOSTNAME=example.com" >> /etc/sysconfig/network
hostname "example.com"

Update /etc/hosts


127.0.0.1 localhost.localdomain localhost
X.X.X.X example.com

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.


date

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
visudo

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/httpd.conf.here 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 example.com with you domain name

  2. NameVirtualHost *:80


    ServerAdmin webmaster@example.com
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /srv/www/example.com/public_html/
    ErrorLog /srv/www/example.com/logs/error.log
    CustomLog /srv/www/example.com/logs/access.log combined

    AllowOverride All
    Order allow,deny
    Allow from all


    ServerAdmin webmaster@example.com
    ServerName abc.com
    ServerAlias www.abc.com
    DocumentRoot /srv/www/abc.com/public_html/
    ErrorLog /srv/www/abc.com/logs/error.log
    CustomLog /srv/www/abc.com/logs/access.log combined

    AllowOverride All
    Order allow,deny
    Allow from all


    As per the vhost.conf file we will have two sites running on our server example.com and abc.com, 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/example.com/public_html
    mkdir /srv/www/example.com/logs
    mkdir -p /srv/www/abc.com/public_html
    mkdir /srv/www/abc.com/logs

    include a index.html file in /srv/www/example.com/public_html 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 abc.com website.

Install and Configure MySQL

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.


mysql_secure_installation

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';
    FLUSH PRIVILEGES;

    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


    quit

    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_reporting = E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR
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.