All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.
Tutorial: CentOS 6.4 Apache+MySQL+PHP+mod_pagespeed+Varnish
In a kickoff for my entry into the blogging world (for real this time lol), I share with you guys the latest tutorial that I wrote up for http://jarland.me.
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 at one time 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.
Apache
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.
HTTPD=/usr/sbin/httpd.itk
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 [email protected] 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 website website </VirtualHost>
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
MySQL
Now we will install and configure MySQL.
yum install mysql-server chkconfig mysqld on
Then run the following to secure the base installation.
mysql_secure_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
[mysql] user=root password=your MySQL root password [mysqldump] user=root password=your MySQL root password
PHP
Now on to installing PHP.
yum install php php-pear
Then replace the settings in /etc/php.ini with the values below.
error_reporting = E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR 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/conf.d/php.conf
<IfModule itk.c> LoadModule php5_module modules/libphp5.so </IfModule>
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.
mod_pagespeed
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.
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:
VARNISH_LISTEN_PORT=80
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.
Comments
Why CentOS?!
One goal of my project was to recreate an environment which assists in my continued learning of the finer details of Apache on a cPanel server, but without cPanel itself, so I wanted to keep the environment familiar. However, CentOS is a beast, and with all of these RAM heavy VPS going around, I think it's time people ditch minimal Debian OpenVZ templates and start using those resources to do something other than Minecraft
Any love for Debian?
Is that one of those hipster operating systems?
I'm actually use both os depend on situation. So tutorial for both os would be great for me. And to promote low end spirit too.
Maybe my next project will be a balance between low usage and high performance at high traffic. My big thing is high performance at high traffic and insane performance at low traffic. There's still a lot of apache optimization that I didn't do on this as well, but varnish pretty well renders most of the mpm tweaking useless except for lowering base memory usage.
Great write up, Thank you.
Thanks for taking the time to do a tutorial for newbies like me. Will definitely try this out!
rpm -U mod-pagespeed*.rpm
warning: mod-pagespeed-stable_current_x86_64.rpm: Header V4 DSA signature: NOKEY, key ID 7fac5991
error: Failed dependencies:
at is needed by mod-pagespeed-stable-1.4.26.5-3533.x86_64
Good luck with your website Jarland.
@ZeroCool
yum -y install at && rpm -U mod-pagespeed-stable_current_x86_64.rpm
mod-pagespeed does cache adsence codes? or rewrite them? if so is there a rule not to rewrite adsense codes?
Thanks brother!
Weird I thought 6.4 had at installed default.
Hmm good thought. Currently using reinvigorate.net myself. I'm sure I can get an answer and post it here. Off work in 9.5h
Small suggestion, instead of using "rpm -U" or "rpm -i", you can use "yum localinstall filename.rpm". It will handle all dependencies automatically.
You just taught me something that makes me very happy
@jarland Nice tutorial. good luck for your website as well
Thanks brother!
I've written a custom C web server for serving flat files (but it does a bit fetching of content in the background too)... I wrote it to fit onto these LEB boxes and it'll never use double figure MB's.
I know you stated in the OP you wanted to be more liberal with resource/memory usage but ) ....
I'd installed (L)AMP on a server for a work colleague and it took something of the order of 200MB by default. All those bells and whistles you never need, but might one day.
In that respect it is definitely worth playing around with my.cnf for MySQL and apache2.conf. If you're using InnoDB tables then the 16MB dedicated to the key buffer cache isn't needed. The query cache can be tweaked down to 1 or 2 MB, etc. Plenty threads out there that cover this optimisation, but worth mentioning.
Same for apache and PHP.
A fork of PHP takes something of the order of 200K/300K, depending on how you compiled it. It's definitely worth knowing a bit more about.
If you have deep pockets the little inefficiencies won't matter, buy more hardware ...
For anyone interested, this has become a script that has developed quite nicely. Still a lot to do in the way of optimization, but this installs a complete LAMP stack with Varnish and MPM-ITK on CentOS 6.5 with minimal effort. Run the script, it asks for values, it runs, then it's done.
https://github.com/jarland/ServerScripts/blob/master/centos65stack.sh
Currently consuming 216MB of memory after a fresh boot and Wordpress installation on Kimsufi.
Why not use nginx?
Jeez. Use cat to output multiple lines.
Edit: And what about Apache and MySQL optimization? OOTB they are major resource hoaders and can kill even an 1GB instance very easily.
Don't like it, prefer varnish to lighten apache load
Everyone has their own logical flow, it's light and it works.
Still work to do on optimization but this won't kill a 1GB instance easily. I run this on 512MB all the time. Optimize your web applications and their queries if default settings are tearing you up that bad
You can also get 6GB for less than $10 at vpsdime. Death to minimalism! Lol