View Full Version : Optimizing a server for low memory use
matta
06-28-2005, 03:28 PM
Below are a few tips on how to optimize a server to conserve as much memory as possible.
1) Run services under inetd/xinetd - Services such as SSH, SMTP servers, FTP servers, and almost any other service can be spawned by inetd/xinetd. In this case they only use memory when they are actually being used.
2) Tune Apache to only have a small number of spare children running. An example of the Apache configuration section:
StartServers 1
MinSpareServers 1
MaxSpareServers 5
ServerLimit 64
MaxClients 64
MaxRequestsPerChild 4000
Also, only load the modules you require. If you do do not use PHP, mod_perl, etc then do not install them.
3) Tune MySQL to use less memory for cache. The best is to use the my-small.cnf sample config as /etc/my.cnf. Below is an example of what to put in /etc/my.cnf.
[mysqld]
port = 3306
socket = /var/lib/mysql/mysql.sock
skip-locking
set-variable = key_buffer=16K
set-variable = max_allowed_packet=1M
set-variable = thread_stack=64K
set-variable = table_cache=4
set-variable = sort_buffer=64K
set-variable = net_buffer_length=2K
Please feel free to reply to this post with your own methods for conserving memory under a VM.
use squid in httpd accelerator mode. even for the most basic dynamic website with a couple of images and a stylesheet this will reduce the load on apache by at least 90% (meaning fewer apache processes are needed)
in httpd.conf:
BindAddress 127.0.0.1
Listen 81
Port 81
----
in squid.conf (applies to debian default -- change example.com as appropriate -- order does matter for acl directives)
http_port 80
cache_replacement_policy heap GDSF
emulate_httpd_log on
acl Safe_ports 81
acl okdomains dstdomain example.com www.example.com
http_access deny !okdomains
#http_access deny all (comment out!)
http_access allow all
visible_hostname example.com
httpd_accel_host virtual
httpd_accel_port 81
http_accel_single_host off
httpd_accel_uses_host_header on
You can download my complete squid.conf file here: http://www.corpuselectronica.com/blog/aws/2005-06-27-httpd_acceleration_with_squid
dorogoy
06-29-2005, 07:40 AM
Try to use lighttpd (http://www.lighttpd.net/) instead of Apache. You will be wondering why you have been suffering Apache for so long.
fmccoey
06-30-2005, 02:12 PM
You can also reduce the number of gettys that are spawned.
Run sshd, ftp (pure-ftp), subversion, etc... from inetd.
I personally use lighttpd, mysql, php, courier-pop and postfix on a 32 hosting 3 email domains and it seems to be ok.
-felix
gioeleb
07-09-2005, 11:39 PM
[QUOTE=matta]
3) Tune MySQL to use less memory for cache. The best is to use the my-small.cnf sample config as /etc/my.cnf. Below is an example of what to put in /etc/my.cnf.
[/QUOTE]
If you use mysql only for small lookups and you don't need support for ACID transactions or foreign keys, you could use TYPE=MyISAM to create tables and insert [FONT=monospace]skip-innodb[/FONT] under the [FONT=monospace][mysqld][/FONT] section.
Skipping InnoDB saved me ~10 MB of memory.
gioeleb
07-21-2005, 12:44 PM
[QUOTE=dorogoy]Try to use lighttpd (http://www.lighttpd.net/) instead of Apache. You will be wondering why you have been suffering Apache for so long.[/QUOTE]
Too bad that lighttpd is not packaged in Debian sarge :(
DJ_Max
07-26-2005, 10:40 PM
[QUOTE=dorogoy]Try to use lighttpd (http://www.lighttpd.net/) instead of Apache. You will be wondering why you have been suffering Apache for so long.[/QUOTE]
I wish I would have heard of LightHTTPD before I setup all my sites on Apache. I've been runnig LightHTTPD on a local machine, and I prefer it over Apache. It's very easy to install as well.
hoffman_c
07-29-2005, 02:07 AM
How do I reduce the getty's? Not a simple killall right?
brett
07-29-2005, 06:46 AM
[QUOTE=gioeleb]Too bad that lighttpd is not packaged in Debian sarge :([/QUOTE]
It's easy to get it going...
Download the source from http://lighttpd.net/download/lighttpd-1.3.15.tar.gz
Uncompress it and then cd lighttpd-1.3.15.
Install any deps you might need...
Run dpkg-buildpackage.
dpkg -i the resulting .deb file (look behind you, in ../)
Enjoy...
Brett
gioeleb
07-29-2005, 09:18 AM
[QUOTE=hoffman_c]How do I reduce the getty's?[/QUOTE]
You can remove them from [font=monospace]/etc/inittab[/font].
hoffman_c
07-29-2005, 04:32 PM
I know this is a bit off topic, but I think it's actually been a good experience so far to have a server with such limited resources. It really gives me the opportunity to scruitinize every process, asking myself "well, what does this one do, and do I really need it?"
Very valuable experience being gained methinks. Not that I would mind getting an extra 32mb of memory for free or anything ;)
EDIT: oh, and speaking of memory for free, it would be uber cool if you could give us like 8mb of memory extra or something for putting a button for unixshell on our sites. I would do that in a second.
dballant
07-29-2005, 07:13 PM
Although a nice idea, one of the brilliant things about XEN is you can't just add memory here and there!! It ensures that resources are correctly allocated to the number of units you have been given.
This is one of the reasons I moved off another VPS provider because resources where a bit hit and miss.
hoffman_c
07-30-2005, 02:53 PM
Well, I think I'll put up a button anyway, once I get my main site up. Hell, the more users you guys get, the better the chances of it staying up, right?
Has anyone made some hot buttons?
dvdkhlng
08-15-2005, 12:38 PM
Hi,
I, too am having problems with a low-memory server (Debian Sarge on 32Mb RAM 6x86 200MHz). Anybody mentioned, that bash can usually be replaced by Ash/Dash which will save some memory on shell-script execution (cron etc.)?
Make sure you update the link /sbin/sh to point to /sbin/dash.
BTW my computer suffered quite often from the OOM killer killing named at bootup, although there always seemed to be sufficient memory (at least swap space)... The only way to fix that was to edit /etc/sysctl.conf to contain:
vm.overcommit_memory=2
vm.overcommit_ratio=70
regards,
David
brett
08-15-2005, 06:22 PM
About dash, though it can be faster, you have to be careful.
If you make it your default shell, some scripts that depend on bash's echo may start failing.
bash:
$ echo "testing\n1...2...3..."
testing\n1...2...3...
$ echo -e "testing\n1...2...3..."
testing
1...2...3...
dash:
$ echo "testing\n1...2...3..."
testing
1...2...3...
$ echo -e "testing\n1...2...3..."
-e testing
1...2...3...
Can be maddening when echo doesn't work right, and you've forgotten you're using dash...
dvdkhlng
08-16-2005, 01:16 PM
At least as far as Debian is concerned, all system scripts should be free of bash-specifc code (AFAIK). But you are right, it seems that switching to dash broke at least one of my own scripts in /etc/ppp/ip-up.d...
BUT, I now have 12 Mb of free RAM :) (well, some memory is swapped out):
total used free shared buffers cached
Mem: 29588 27304 2284 0 2140 7512
-/+ buffers/cache: 17652 11936
Swap: 72568 4176 68392
I use Boa, which is in the Debian repository. Also, have a look at Wikipedia:Boa (http://en.wikipedia.org/wiki/Boa_(web_server)) for others.
ferrix
09-24-2005, 06:48 PM
I've seen lighttpd and Boa mentioned here as thrifty web servers, and the wikipedia page for Boa lists many others.
I think it would be valuable for someone who has experience with several of these to post a comparison based on features (or lack of) vs. memory usage, ease of administration, performance, etc.
As others here have stated, I too have found it quite satisfying to see how much can actually be packed into a 32MB server. Yay linux!
hezekiah
09-24-2005, 09:47 PM
From my (very) limited experience, Boa seems to work very well for static+Perl/Python/etc CGI only sites.
Lighttpd adds the ability to, relatively easily, use PHP as well.
I have found that for my personal setup, Boa is a little easier to setup only because there are fewer options to tweak. If you use a similarly stipped down Lighttpd configuration, they seem to be of roughly equal complexity. Without any extra plugins enabled, the configuration files for both are pretty similar.
WRT memory use, both Boa and Lighttpd seem pretty similar until FastCGI+PHP is added to the mix. This uses the most RAM of anything on my VM, and I would happily do away with it if I could find a decent Perl/Python/Ruby CGI webmail program that works as well as SquirrelMail or Hastymail.
Finally, Lighttpd has a decent selection of plugins to allow it to act as a proxy to other servers, different authentication schemes, and many other goodies.
Again, this is all based on my limited experience with my happy little 32 account. As I mentioned, I'm currently using lighttpd+FastCGI/PHP for webmail. It performs reasonably for a single user system, though it can be rather slow at times.
What about using SQLite (http://www.sqlite.org/) instead of MySQL, if whatever you're doing supports that? Is that likely to produce significant memory savings? It seems like it should, since it means you'd have one less daemon process running...
perlmonkee
11-15-2005, 01:18 AM
[QUOTE=DJ_Max]I wish I would have heard of LightHTTPD before I setup all my sites on Apache. I've been runnig LightHTTPD on a local machine, and I prefer it over Apache. It's very easy to install as well.[/QUOTE]
LigHTTPD does not *really* support SSI - which may be important to some of you. It supports pretty much everything except exec tags - which to me is the most important feature of SSI.
This may or may not be important to you.
brett
11-15-2005, 03:19 AM
[QUOTE=perlmonkee]LigHTTPD does not *really* support SSI - which may be important to some of you. It supports pretty much everything except exec tags - which to me is the most important feature of SSI.[/QUOTE]
I'm not sure what you mean by "*really*" support SSI, but simple exec tags work fine in my installation of lighttpd-1.3.15...
<!--#exec cmd="date"-->
does exactly what you would expect.
That said, using exec can be pretty unsecure, and so it is often disabled altogether by the security-minded/paranoid admin...
Brett
vBulletin v3.0.6, Copyright ©2000-2009, Jelsoft Enterprises Ltd.