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