Building-Up CentOS 5 Linux Operating System

Wed Aug 5 21:57:59 2009 EDT (-0400 GMT)

HyperVM admin pageWhen setting up my VPS server there were a number of options for building it. The VPS admin tool gives you “virtual” control over the power switch and the ability to rebuild the server with a fresh OS image at any time. A2 Web Hosting has a few flavours of Linux to choose from: CentOS (the GPL-based rebuild of Red Hat’s Enterprise Server 5), Gentoo and Ubuntu. I went with CentOS as I use it at work, and Enterprise-Grade always seems good to me.

From here on I’ll assume that you know something about the command line, and that you’ve got a good SSH client like the OS X Terminal or Putty and a way to upload files via an SFTP client like Cyberduck or Filezilla.

One my first steps with a pristine Linux operating system is to create a folder called “backup” and an “etc_original” folder in there and copy all of the original etc folder files there for reference.
mkdir /backup
mkdir /backup_original
cp -r /etc/ /backup/etc_original

What you’ll need on your server

Depending on the install/image you use you may have everything you need right there, but here are the packages I install out of the gate:

First off I install the screen tool (more about screen at so that I can walk away and reconnect to this process, rsync for moving things and backing things up and telnet for testing ports/servers:
yum install screen rsync telnet
Make sure Apache’s installed and that we’ve got all the PHP modules we need:
yum install httpd php php-cli php-zip php-mysql php-mcrypt php-mbstring
Also get some SSL support:
yum install mod_ssl openssl
Install MySQL client and server:
yum install mysql mysql-server
Install the firewall
yum install iptables

Once all of those packages are installed you’ll need to set them up.

Securing SSH and the rest of the server

Here’s a great a guide to securing SSH itself and setting up iptables, so I will deffer to it:

Be sure to allow the appropriate ports you’ll need later (http 80, https 443, and your current ssh port because you’d never keep it at 22, right?).

Setting up Apache

For Apache I like to edit the httpd.conf nano -w /etc/httpd/conf/httpd.conf and turn off the following modules by commenting them out with a #:

LoadModule auth_basic_module modules/
LoadModule auth_digest_module modules/
LoadModule authn_file_module modules/
LoadModule authn_alias_module modules/
LoadModule authn_anon_module modules/
#LoadModule authn_dbm_module modules/
LoadModule authn_default_module modules/
LoadModule authz_host_module modules/
LoadModule authz_user_module modules/
LoadModule authz_owner_module modules/
#LoadModule authz_groupfile_module modules/
#LoadModule authz_dbm_module modules/
#LoadModule authz_default_module modules/
#LoadModule ldap_module modules/
#LoadModule authnz_ldap_module modules/
#LoadModule include_module modules/
LoadModule log_config_module modules/
LoadModule logio_module modules/
LoadModule env_module modules/
LoadModule ext_filter_module modules/
LoadModule mime_magic_module modules/
LoadModule expires_module modules/
LoadModule deflate_module modules/
LoadModule headers_module modules/
LoadModule usertrack_module modules/
LoadModule setenvif_module modules/
LoadModule env_module modules/
LoadModule ext_filter_module modules/
LoadModule mime_magic_module modules/
LoadModule expires_module modules/
LoadModule deflate_module modules/
LoadModule headers_module modules/
LoadModule usertrack_module modules/
LoadModule setenvif_module modules/
LoadModule mime_module modules/
#LoadModule dav_module modules/
#LoadModule status_module modules/
LoadModule autoindex_module modules/
#LoadModule info_module modules/
#LoadModule dav_fs_module modules/
LoadModule vhost_alias_module modules/
LoadModule negotiation_module modules/
LoadModule dir_module modules/
LoadModule actions_module modules/
LoadModule speling_module modules/
LoadModule userdir_module modules/
LoadModule alias_module modules/
LoadModule rewrite_module modules/
#LoadModule proxy_module modules/
#LoadModule proxy_balancer_module modules/
#LoadModule proxy_ftp_module modules/
#LoadModule proxy_http_module modules/
#LoadModule proxy_connect_module modules/
LoadModule cache_module modules/
LoadModule suexec_module modules/
LoadModule disk_cache_module modules/
LoadModule file_cache_module modules/
LoadModule mem_cache_module modules/
LoadModule cgi_module modules/

Other than a few tweaks like commenting out the cgi-bin there isn’t much else I do to the httpd.conf. You may also need to remove the /etc/httpd/conf.d/proxy_ajp.conf file.

What I like to do for the rest of my Apache configurations is create a file with a name/location like “/etc/httpd/conf.d/current_server_name.conf”. This is where I put the rest of my modifications. This file will get loaded and applied when Apache starts and is protected from updates etc by being placed in the conf.d folder.

Some examples of configurations I often add are:
DirectoryIndex index.rss index.php index.html

# Buffering logs - Not good for dev, very good for production
BufferedLogs On
HostnameLookups Off

When you’re ready start apache:
/etc/init.d/httpd start

and to make sure it always starts with the server:
/sbin/chkconfig --add httpd

Turbo Charge PHP

I’m a big fan of the APC PHP Pre-Compiler/Accelerator. It works by caching the text-based PHP file’s compiled opcode and runs that until the original file is modified. This greatly speeds up the process of PHP files and speeds up larger PHP-based applications with a lot of files, like WordPress. You can even tell APC to not even check the drive for updates, just served the cached page, until the server is restarted or the cached cleared. I also like the persistent shared memory it can introduce to PHP.

It requires the developer tools to be installed:
install gcc make mlocate autoconf

Then I’d recommend install APC based on this guide:

For the record, here’s what my “/etc/php.d/apc.ini” file currently looks like. I’ve assigned 48mb of ram, and I’m telling APC not to check the disk for updates, just serve the cached page:
apc.enabled = 1
apc.shm_size = 48
apc.ttl = 7200
apc.user_ttl = 7200
apc.optimization = 1


Start MySQL:
/etc/init.d/mysql start

and to make sure it always starts with the server:
/sbin/chkconfig --add mysql

You’ll have to set a root password for MySQL:
mysqladmin -u root password yourrootsqlpassword

and then proceed to add whatever accounts and database you’d like to add however you’d like to do that. In my case I download PHPMyAdmin and configuring it. What I also do is add to my own Apache config file some IP restrictions like this (with some example IPs):

<Location "/phpmyadmin">
Order deny,allow
deny from ALL
Allow from
Allow from 192.168.
# Force clients from the Internet to use HTTPS
RewriteEngine on
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^phpmyadmin/*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

That’s the really geeky part covered, next I’ll look at the more interesting element: Setting-up Google Apps including Gmail for the whole system’s mail.

3 Responses to “Building-Up CentOS 5 Linux Operating System”

  1. Bill Bartmann Says:

    Hey good stuff…keep up the good work! 🙂

  2. Daniel Mooney Says:

    Wow, I wish I had found this a long time ago. While I’ve already had a web server up and running for a while, I just spent hours banging my head against a problem where APC file uploads worked on Ubuntu but not on Centos.

    I ran into two huge issues in solving this problem:
    – Make and Locate were not installed by default (wtf?!). Your blog post would’ve keyed me into this, if I had found it in time.
    – The Yum installable PHP version is 5.1.x. The APC RFC.1867-specific options (apc.rfc1867, apc.rfc1867_freq, apc.rfc1867_name, apc.rfc1867_prefix) don’t show up until PHP 5.2.x. I kept banging my head against a wall until I hit up “php -v” and saw my problem. Oh, and then getting the new version of PHP…

    The lack of standardization between the Linux variants is my single biggest computing impediment, these days. I have abandoned Windows servers a while back, but things like this sometimes make me regret it.

    ANYways, thanks for the great blog post.

  3. Matt Clare Says:

    Thanks for the positive words. I’m almost ready to post the follow-up of how integrate Google apps for the rest of the job. I’m just waiting for the third person I’ve convinced to make the switch to complete the process so that I’ve got a large enough frame of reference.

    I can’t conceive of ever running a windows server, but I admit that’s because I’ve got no experience with it and the complexity of the security side just scares me — but Linux really needs to push towards standardization. I’m not sure of common starting points like the Red Hat derived distros and the Debian derived distros are making thing more standardized or making them deceptively similar.