How to setup LAMP Stack on CentOS 6.4/6.5/6.6

CentOS is a very popular Linux distribution mainly used as hosting server. This guide will show you step by step how to setup LAMP stack on CentOS 6.x version including CentOS 6.4, 6.5, and 6.6.

The guide starts by installing Apache web service, MySQL database, and PHP / suPHP. After completing this guide, the server is ready to host your web hosting services such as WordPress, Joomla, Drupal and more.


If you have a dedicated or VPS server and don’t want to buy cPanel, you can manually install and configure it as a WordPress, Joomla and Drupal web server. I use CentOS 6.4 running on a DigitalOcean VPS cloud servers. This should be applicable for any version of Centos such as Centos 5.x.

Install Apache

As root / superuser, update the system and install the web server (Apache)

yum -y update
yum -y groupinstall "Web Server"
chkconfig httpd on
chown apache.apache /var/www/html

Install MySQL database server

As root / superuser, install the MySQL database server

yum -y groupinstall "MySQL Database server"
service mysqld start
mysql_secure_installation

Install PHP and suPHP

PHP is used as the programming language in most CMSes such as WordPress, Magento, and Joomla, so we need to install PHP on our server.
suPHP is a tool for executing PHP scripts with the permissions of their owners. With suPHP you are able, for example, to manage php applications in user’s home directories without worry about permissions problems (when creating directories, files or uploads)

Run the following command to install the PHP

yum -y install php php-devel php-mysql apr-devel httpd-devel

We will install suPHP from source file so we need to install ‘Development Tools’ packages.

yum -y groupinstall "Development Tools"

wget http://www.suphp.org/download/suphp-0.7.1.tar.gz

tar zxv suphp-0.7.1.tar.gz

cd suphp-0.7.1
./configure --prefix=/usr --sysconfdir=/etc --with-apache-user=apache --with-setid-mode=paranoid  
--with-apxs=/usr/sbin/apxs --with-php=/usr/bin/php-cgi --with-logfile=/var/log/httpd/suphp_log  
--enable-SUPHP_USE_USERGROUP=yes --with-apr=/usr/bin/apr-1-config 

make && make install

Still in the current directory, copy the suphp.conf-example file configuration located in the doc folder to /etc/ directory.

cp doc/suphp.conf-example /etc/suphp.conf

Edit the /etc/suphp.conf file to be like below!

[global]
;Path to logfile
logfile=/var/log/httpd/suphp.log

;Loglevel
loglevel=info

;User Apache is running as
webserver_user=apache

;Path all scripts have to be in
docroot=/var/www:${HOME}/public_html

;Path to chroot() to before executing script
;chroot=/mychroot

; Security options
allow_file_group_writeable=false
allow_file_others_writeable=false
allow_directory_group_writeable=false
allow_directory_others_writeable=false

;Check wheter script is within DOCUMENT_ROOT
check_vhost_docroot=true

;Send minor error messages to browser
errors_to_browser=false

;PATH environment variable
env_path=/bin:/usr/bin

;Umask to set, specify in octal notation
umask=0077

; Minimum UID
min_uid=100

; Minimum GID
min_gid=100


[handlers]
;Handler for php-scripts
;x-httpd-php="php:/usr/bin/php"
application/x-httpd-suphp="php:/usr/bin/php-cgi"

;Handler for CGI-scripts
x-suphp-cgi="execute:!self"

Add users / websites

To make it easy, websites in a server are divided by users. Users can upload their data or documents inside their HOME, mostly under public_html directory.

adduser website01
passwd website01
mkdir /home/website01/public_html
chown website01.website01 /home/website01
chown website01.apache /home/website01/public_html
chmod 711 /home/website01
chmod 750 /home/website01/public_html

You can simply repeat the above commands to add more users / websites

Configure the Apache HTTPD

– Edit the /etc/httpd/conf/httpd.conf file

ServerName 11.22.33.44:80 <<<< --- change with your IP address and port
Listen 11.22.33.44:80  <<<< --- change with your IP address and port

NameVirtualHost *:80
#
# NOTE: NameVirtualHost cannot be used without a port specifier 
# (e.g. :80) if mod_ssl is being used, due to the nature of the
# SSL protocol.
#

#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for requests without a known
# server name.
#
<VirtualHost *:80>
    ServerAdmin webmaster@website01.com
    DocumentRoot /home/website01/public_html
    ServerName website01.com
    ServerAlias www.website01.com
    ErrorLog logs/website01.com-error_log
    CustomLog logs/website01.com-access_log common

	<Directory /home/website01/public_html/>
		Options Indexes FollowSymLinks MultiViews
		AllowOverride All
		Order allow,deny
		allow from all
	</Directory>

<IfModule mod_suphp.c>
  suPHP_UserGroup website01 website01
</IfModule>

</VirtualHost>

– Edit the /etc/httpd/conf.d/php.conf file to make like below!

LoadModule suphp_module modules/mod_suphp.so
 
suPHP_Engine on
AddType application/x-httpd-suphp .php5 .php .php3 .php2 .phtml
<Directory />
    suPHP_AddHandler application/x-httpd-suphp
</Directory>

DirectoryIndex index.php

– Restart Apache and the server is ready to host WordPress/Joomla/Drupal and other CMSes.

/etc/init.d/httpd restart

– Test suPHP using a simple script file (test.php file) and locate it right under public_html directory.

<?php echo 'whoim = '.exec('/usr/bin/whoami');?>

– Run Firefox/Chrome and access to http://website01.com/test.php, it should echo who you are.