Tutorial: CentOS 6.4 Apache+MySQL+PHP+mod_pagespeed+Varnish

Posted by in Tutorials

Title a bit long there? Yeah buddy. I’m not going to tell you how to set up a web server that sits idle with 3MB of RAM usage. No sir. I’m going to tell you how to set up a web server that laughs in the face of high traffic and serves it with dignity. I hover at around 380-400MB of memory in use at all times on this server. The trade off? A few thousand page views has less of an impact on how it scales beyond that much more efficiently.

When I set out on this project, I had a list of demands. I’m going to share these with you, because it also doubles as a list of what we’re accomplishing with this tutorial.

Setup: Must be Apache, MySQL, PHP. Layers on top of that are fine, but no hipster web servers or NoSQL crap. When something isn’t doing the job, I fix it, I don’t just switch to the flavor of the month.

Security: Must have each site executed as a different Unix user account. When one website gets exploited, the user executing the PHP should not have permission to alter the files of another website. I want to isolate future unfortunate events.

Features: Speed. A lot of it. Reasonable compliance with PageSpeed. Minimal impact from high traffic.

To accomplish this we will be using CentOS 6.4 64-bit. This guide assumes you have already installed and updated your OS. Let’s get started.


First, we’re going to install and configure Apache.

yum install httpd

Let’s create a new user for your first website.

adduser website

Then set a password.

passwd website

Now we need to install the EPEL repo.

wget http://epel.mirror.constant.com/6/i386/epel-release-6-8.noarch.rpm
rpm -i epel*.rpm

Then we install mpm-itk.

yum install httpd-itk.x86_64

Then edit /etc/sysconfig/httpd and uncomment this line and change anything necessary to make it match.


We need to set up virtual hosts. Create the following file in /etc/httpd/conf.d and name it something like virtualhosts.conf.

NameVirtualHost *:82

<VirtualHost *:82>
ServerAdmin youremail@yourdomain.tld
ServerName yourdomain.tld
ServerAlias www.yourdomain.tld
DocumentRoot /home/yourdomain.tld/public_html/
ErrorLog /home/yourdomain.tld/logs/error.log
CustomLog /home/yourdomain.tld/logs/access.log combined
AssignUserId user group

Obviously you will want to replace the pieces above that fit your needs. Let’s go ahead and make the folders.

mkdir -p /home/yourdomain.tld/{public_html,logs}
chown -R website.website /home/yourdomain.tld

Next, change this in /etc/httpd/conf/httpd.conf

Listen 80

Change to:

Listen 82

Now start up Apache and set it to start at boot.

service httpd start
chkconfig httpd on


Now we will install and configure MySQL.

yum install mysql-server
chkconfig mysqld on
service mysqld start

Then run the following to secure the base installation.


Set the root password, disable remote root login, remove anonymous users. remove test database, reload tables. Now let’s make sure you can execute MySQL from command line later without any trouble. Create the following file as /root/.my.cnf

password=your MySQL root password
password=your MySQL root password


Now on to installing PHP.

yum install php php-pear php-xml

Then replace the settings in /etc/php.ini with the values below.

display_errors = Off
log_errors = On
error_log = /var/log/php.log
max_execution_time = 300
memory_limit = 256M
register_globals = Off

Then run the following.

yum install php-mysql

Then add the following to the top of /etc/httpd/conf.d/php.conf

<IfModule itk.c>
LoadModule php5_module modules/libphp5.so

Your LAMP server is configured, but your Apache is listening on port 82. That’s fine, because we’re installing Varnish on port 80. We’re almost there, I promise.


Let’s go ahead and install mod_pagespeed.

Download and install mod_pagespeed to/from your current directory.

wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-stable_current_x86_64.rpm
rpm -U mod-pagespeed*.rpm

Then restart Apache.

service httpd restart

That was easy, right? On to Varnish.


Let’s get Varnish installed.

Run the following to install the Atomic repo and Varnish.

rpm -Uvh http://www6.atomicorp.com/channels/atomic/centos/6/i386/RPMS/atomic-release-1.0-16.el6.art.noarch.rpm
yum install varnish

Then edit /etc/varnish/default.vcl and replace the following:

.port = “80”;

With this:

.port = “82”;

Then edit /etc/sysconfig/varnish and make sure that the following is set as specified:


Now just restart Varnish.

service varnish restart

Enjoy your new amazing web server. Don’t be running this on 256MB of memory. Give it 2GB for room to grow.