Fast, Reliable & Affordable Web Services
Highlights
  • email marketing ...
  • content management ...
  • fastest VPS hosting ...
  • new SEO techniques ...
  • Online business drivers ...

Setup of a Linux Web Server in 128Mb of Memory

Centos 5 as a LAMP Server in a Low Memory Situation

The results

Lets get right to the point! I’ve setup a working VPS Centos 5 LAMP server in 128MB of memory and it hasn’t gone into swap yet. It typically runs in just over 100Mb of memory and performs really well. So I thought I’d share the process of setting it up in our blog.

I have been using Gradwell to host our websites and our clients’ sites (and VOIP) for about a year now and I’ve been very happy with their service. However, I wanted the flexibility of control that my own server could provide, but I couldn’t justify the cost of new server and its hosting.

Finding a VPS Hosting Company

The solution of course was a virtual server for a number of reasons:

  1. cost
  2. manageability – no hardware to worry about (SEP)
  3. the green factor. VMs are more energy efficient and this fits with our ethos as a company.
  4. portability

I had look around, but decided to see if it was possible for Gradwell to support the features I needed through their hosting package first. They came back to me suggesting their new VPS hosting package (which isn’t on their website yet) and of course I was happy to with them.

When I had a look around there were quite a few different offerings. At the time of writing this article (July 2008) companies are either using VMWare or Xen, some were using Windows Virtualization.

As I’m already with Gradwell, I decided to go with their offering (Centos 5 on Xen). Not least because they offered a Centos 5 VPS, which is exactly what I wanted.

Other Factors to Consider

You may be looking to buy a VPS on a monthly basis to host your websites and databases, but do you want to host and manage these yourself:

  • email (pop or IMAP boxes)
  • email forwarding
  • DNS
  • backup – where are you going to host your off site backup?

In my case I did not – I have enough to worry about. The IMAP demon can be memory and CPU intensive and SPAM detection even more so. DNS should always be hosted on a machine other than your web server in my opinion and with off-site backup you really do need another machine – not a lot of choice there!

Specification

VPS systems are typically costed as a factor of memory, share of CPU (how many concurrent systems on a real machine) and disk space. Because I’m in the development cycle of the project that this VPS is really going to be used for and I only had a few small, low volume sites to host (and because upgrading memory is easy on a VPS) I’ve gone for their lowest memory spec. machine – 128MB.

The configuration was as follows:

CPU: sitting on a dual quad core machine under Xen
Memory: 128Mb
3GB of disk space
Standard Centos 5 install:
Apache 2.2.3
MySQL 5.0.22
PHP 5.1.6

With a small amount of memory we needed to optimize the set-up so that the system does not page. The rest of this blog covers the set-up and optimization of the new VPS server, service by service.

Optimizations

The first and obvious question to ask was what services do I not need?

I reviewed the running services and dropped the use of some by removing the S links in /etc/rc3.d:

  • I’ve removed the link S26hidd -> ../init.d/hidd located in /etc/rc.d/rc3.d to stop the hidd (bluetooth demon from starting)
  • I’ve removed the link S97yum-updatesd -> ../init.d/yum-updatesd located in /etc/rc.d/rc3.d to stop the yum notification demon running.
  • I’ve removed the link S90xfs -> ../init.d/xfs located in /etc/rc.d/rc3.d to stop the x font server which is not needed.
  • I’ve removed the link S18rpcidmapd -> ../init.d/rpcidmapd in /etc/rc.d/rc3.d to stop NFS server working as I don’t need to use NFS
  • I’ve removed the link S95atd -> ../init.d/atd in /etc/rc.d/rc3.d as not using the “at” command
  • I’ve stopped smartd – haven’t removed the link in rc3.d yet, as I not sure is is needed under Xen – this need more research. However, the server seems to be fine for the moment.

Backup

Normally I would use rsync to a remote machine. I don’t want to run rsyncd on my VPS and it is not running on the machine hosted by Gradwell that I’m backing up to, so I’ve used tar, sftp, cron and some shell scripts to implement the remote backup for the server. A little more bandwidth is used but it is the memory foot print that I am concerned with.

SSHD

There are other implementations of sshd that use less memory, however the ones I considered did not support sftp and I need that, so I’m sticking with the standard install.

DNS (bind)

I’m hosting this elsewhere – this saves memory!

MySQL

Mysql settings for low memory are well documented on their website. Most installs come with some suggest configurations in /usr/share/doc/mysql-server-{mysql-version}/. I actually adapted the file my-medium.cnf found there and incorporated those settings into /etc/my.cnf

here are the resultant settings:

port            = 3306
socket          = /var/lib/mysql/mysql.sock
[mysqld]
datadir=/var/lib/mysql
port = 3306
socket = /var/lib/mysql/mysql.sock
old_passwords = 1
skip-locking
key_buffer = 16M
max_allowed_packet = 1M
table_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[isamchk]
key_buffer = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[myisamchk]
key_buffer = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout

Updates and Yum

I’ve setup a cron job that switches yum-updatesd on for a short period during the night as a memory helper. During that time a cron script emails me if updates are needed on the system using the command `yum check-update`

Apache

I don’t use perl so I’ve have commented out the line:

LoadModule perl_module modules/mod_perl.so

from /etc/httpd/conf.d/perl.conf to save memory

Settings for low memory are changed to:

<IfModule prefork.c>
StartServers 1
MinSpareServers 1
MaxSpareServers 4
ServerLimit 64
MaxClients 64
MaxRequestsPerChild 5000
</IfModule>
<IfModule worker.c>
StartServers 1
MaxClients 15
MinSpareThreads 3
MaxSpareThreads 7
ThreadsPerChild 3
MaxRequestsPerChild 200
</IfModule>

PHP

I have actually increased the min memory from 16M to 40M as many modern PHP applications (SugarCRM, Joomla, WordPress, etc) needs this as a minimum – better it runs and swaps if it needs to than not run at all.

Packages

In addition to the standard install I have installed:

  • emacs (needed for me – I can’t help but love it after all of these years)
  • php-mysql (need for everything and I’m not sure why it wasn’t installed already)
  • php-mbstring (need for SugarCRM)
  • php-imap (needed for sugar and other Webmail packages)
  • php-gd (needed for various PHP apps. for image manipulation)
  • vsftpd (joomla wants to ftp to the Unix platform because of file permissions)

Porting

These are a few notes on things I did to manage stress free porting of applications from the Gradwell hosted server. Some of these points may in principal be useful to you so I’ve left them in this blog.

MySQL based and static websites were dumped and tar(ed) up along with the Webroot and logs directory, sftp(ed) to the new server and setup. In most cases it was necessary to edit config files and .htaccess files (which I brought into httpd.conf as I was moving from Apache 1.3 to Apache 2.2).

Directories

I’ve to mirrored the way Gradwell hosts websites in their directory structure for compatibility, i.e.:

/home/[user]/webs/[domain]/htdocs
/home/[user]/webs/[domain]/logs

I’ve added this to /etc/skel so that new users get this structure.

Users

I’ve copied files over from the Gradwell server and they have my user (UID) with GID 1000, as this is the way Gradwell do things

All users have group user (GID=1000) It did exist as a group and had value 100 on my VPS, so I changed it to have value 1000. And, I have edited /etc/default/useradd and made GROUP=1000. Ported users need to have their UID set, new users don’t need their UID setting manually and the default GID is now set to 1000, so:

# useradd -n –password <password> <newuser>

is used to add a new user.

Checks and Monitors

I have setup an external HTTP monitoring service, a cron script to monitor server load and memory consumption with the ability restart services if needed and something to monitor disk space.

Feedback

I’d be interested to hear what others have done, so please feel free to leave your thoughts and comments.

Article by Malcolm

6 Responses to Setup of a Linux Web Server in 128Mb of Memory

  • thanks for the good article.

    skip-innodb in my.cnf (at least on debian etch) frees up quite a lot of memory

    “By default, the MySQL configuration in Etch will start two database engines, both MyISAM and InnoDB. You only need one engine running for a Drupal site, and making this small tweak can halve MySQL’s memory usage.”

    http://www.debian-administration.org/articles/544

  • Nice article. I found your blog through yahoo while i was searching for web hosting. I will recommend your site to other people and I am sure they will think the same about your work on this site.Cheers

  • Thanks!

    Was useful for me with a 128MB VPS running Ubuntu 11.04 (32 bit).

  • Pingback: LAMP on a low memory Server | Balroks BlogBalroks Blog
  • I see a lot of interesting articles on your blog.
    You have to spend a lot of time writing, i know how to save you a lot
    of time, there is a tool that creates readable, google
    friendly articles in couple of minutes, just type in google – k2 unlimited
    content

  • I read a lot of interesting articles here. Probably you spend
    a lot of time writing, i know how to save you a lot of work, there is an online tool that
    creates readable, SEO friendly posts in seconds, just search in google
    – laranitas free content source

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.