You are here

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

  1. yum update

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

  1. echo "" >> /etc/sysconfig/network
  2. hostname ""

Update /etc/hosts

  1. localhost.localdomain localhost
  2. X.X.X.X

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.

  1. 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.

  1. date

It should display something like this.

  1. 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.

  1. useradd webuser

Change the password for newly created user

  1. passwd webuser

Now allow this user to run root commands via sudo

  1. yum install sudo
  2. visudo

After doing visudo find the line like this

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

and add the following line after that

  1. 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

  1. sudo yum install epel-release
  2. sudo yum install fail2ban

Start the service

  1. sudo service fail2ban start

Start fail2ban on boot

  1. sudo /sbin/chkconfig fail2ban on

Checklist - Check available services

  1. ls /etc/init.d/

Setup Apache Web server

Install Apache 2.2

  1. 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

  1. 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.

  1. KeepAlive Off
  2. ...
  3. <IfModule prefork.c>
  4. StartServers 2
  5. MinSpareServers 6
  6. MaxSpareServers 12
  7. MaxClients 80
  8. MaxRequestsPerChild 3000
  9. </IfModule>

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
    1. NameVirtualHost *:80
    3. <VirtualHost *:80>
    4. ServerAdmin
    5. ServerName
    6. ServerAlias
    7. DocumentRoot /srv/www/
    8. ErrorLog /srv/www/
    9. CustomLog /srv/www/ combined
    10. <Directory /srv/www/abc/public_html/>
    11. AllowOverride All
    12. Order allow,deny
    13. Allow from all
    14. </Directory>
    15. </VirtualHost>
    17. <VirtualHost *:80>
    18. ServerAdmin
    19. ServerName
    20. ServerAlias
    21. DocumentRoot /srv/www/
    22. ErrorLog /srv/www/
    23. CustomLog /srv/www/ combined
    24. <Directory /srv/www/>
    25. AllowOverride All
    26. Order allow,deny
    27. Allow from all
    28. </Directory>
    29. </VirtualHost>

    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.

  2. Now create the respective directories referenced above
    1. mkdir -p /srv/www/
    2. mkdir /srv/www/
    3. mkdir -p /srv/www/
    4. mkdir /srv/www/

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

    1. <h1>my apache web server</h1>
  3. Start Apache for the first time, and set it to run at boot
    1. sudo service httpd start
    2. 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

  1. sudo yum install mysql-server

Start MySQL, and set it to run at boot:

  1. sudo service mysqld start
  2. 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.

  1. 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
    1. mysql -u root -p
  2. Create a database and user
    1. create database webdata;
    2. 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

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

    1. quit

    login as webuser

    1. mysql -u webuser -p
  5. After checking the access logout from mysql
    1. quit

Install and Configure PHP

Install PHP

  1. sudo yum install php php-pear
  2. 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

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

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

  1. sudo mkdir /var/log/php
  2. sudo chown apache /var/log/php

Restart or Reload Apache

For restarting use

  1. sudo service httpd restart

For reloading use

  1. 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.