Home » Misc Tech Stuff, Planet Ubuntu, Ubuntu Tutorials

Configuring a lightweight Apache / MySQL install on Debian/Ubuntu

5 October 2008 17,548 views 29 Comments

I’m going to explain how to setup a lightweight Apache and MySQL to work on a smaller server such as SliceHost’s 256mb VPS. This will work on both Debian and Ubuntu servers. You are going to install Apache 2 and MySQL like you normally would. After you install it, we are going to edit the config files to better utilize your available memory. To do this you need to be logged in via SSH to your server.

First enter:

$top

Look for the MySQL command. (If you don’t see it, press <Shift> + M to sort by the amount of memory used) Once you locate MySQL, make note of the percentage of memory it is using. Now exit top by hitting ‘q’.

We are now going to edit your MySQL configuration. First you want to make a backup by entering:

$sudo cp /etc/mysql/my.cnf /etc/mysql/my.cnf.orig

Now open my.cnf using your preferred editor. We will use nano for this tutorial.

$sudo nano /etc/mysql/my.cnf

Find the following line in your my.cnf file:

#skip-innodb

Uncomment that line. (Note: if after changing this line your database no longer works, come back and comment this line out again and restart MySQL.)

Next locate the line:

skip-external-locking

and add:

skip-locking

below it. Next find the section labeled Fine Tuning. Change the settings in that section to match:

key_buffer = 16K
max_allowed_packet = 1M
thread_stack = 64K
thread_cache_size = 4
sort_buffer = 64K
net_buffer_length = 2K
#max_connections = 100
#table_cache = 64
#thread_concurrency = 10

Now restart MySQL:

$sudo /etc/init.d/mysql restart

Now run ‘top’ again, and see what your percentage of memory is. If all is right, it should be much lower. Now we will change the apache configuration. This will not show you a lower memory usage per se, however, it should prevent apache from using too much memory. First lets make a backup of your apache config.

$sudo cp /etc/apache2/apache2.conf /etc/apache2/apache2.conf.orig

Now lets open the apache config file for editing:

$sudo nano /etc/apache2/apache2.conf

Now make your entries match the following:

Timeout 45
KeepAlive On
MaxKeepAliveRequests 200
KeepAliveTimeout 3
<IfModule mpm_prefork_module>
StartServers          5
MinSpareServers       5
MaxSpareServers      10
MaxClients          30
MaxRequestsPerChild   2000
</IfModule>

Finally restart apache:

$sudo /etc/init.d/apache2 restart

If you have problems with memory still, try lowering your “MaxClients” to 25.

This setup should work on most Debian and Ubuntu VPS’s. Note that your results may very. If you have any questions, or need help, feel free to post a comment and I will try to get back to you.

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading ... Loading ...

29 Comments »

  • Raoul said:

    Chris, after following the directions and even disabling most of the plugins, as discussed in the SH chatroom (only Akismet and WP Stats were enabled), the free memory still hovered around 30-35MB.

    I then re-enabled WP Super Cache, and the free memory still hovered around 30-40MB. Now I see that it’s stabilized around 70-100MB or so.

    I’ll still need to re-enable some of my plugins, so I’ll keep monitoring the free memory to see when it tanks. Still not sure myself where the problems lie, but I’ll keep looking at it.

    Thanks for your help!

  • Raoul said:

    I gave up this morning and upgraded to the 512MB slice.

    My sites were down again, and this with only three plugins enabled: Akismet, WP Stats and WP Super Cache. I don’t know, maybe I’m getting more traffic than your sites and I don’t know it. I don’t count search engines when I look at the traffic. All bots and crawlers are excluded, and I only look at people.

    At any rate, my sites are doing great now, even while getting crawled by search engines, and free memory is around 270MB, which is a lot better than 20-30MB or less.

    I did leave your tuning config in there. I may up some parameters if the need arises, but my slice is doing great for now. NO DOWNTIME! Yes!

  • Sorry about the growing pains by Raoul Pop said:

    [...] got it back up and re-tuned Apache and MySQL with Chris Johnston’s help, but at some point during the night, it went down hard, and stayed down. When I woke, [...]

  • ORNYX » Blog Archive » Lightweight Apache/MySQL Configuration for Debian said:

    [...] has an excellent post on configuration parameters for Apache and MySQL to help save memory. This was specifically used be [...]

  • links for 2008-12-12 | Nathan and his Open Ideals said:

    [...] Configuring a lightweight Apache / MySQL install on Debian/Ubuntu | Chris Johnston I’m going to explain how to setup a lightweight Apache and MySQL to work on a smaller server such as SliceHost’s 256mb VPS. This will work on both Debian and Ubuntu servers. (tags: performance mysql hosting slicehost) [...]

  • Greg Brown said:

    Thanks for this, it’s proved really handy. Do you know much about the query_cache_size and query_cache_limit parameters? To be honest I’m not really too familiar with the mysql query cache but reducing these seemed to free up a fair bit of memory.

  • MINEG said:

    Hello! Chris could u tell me hwo i can set up a web mail? pls help me. Thank you.

  • cJ (author) said:

    I have never setup web mail, or any sort of eMail for that matter, but it may be something for a future article.

  • m3nt0r said:

    Following the instructions for mysql on a mysql5/debian install i raised memory usage by 5% on idle..

    I guess the most comes from skipping innoDB. Unfortunatly i need innodb because my rows change alot.

    Just to let you know. Bye

  • Setting up a LLMP Stack (Linux, Lighttpd, MySQL, PHP5) on Ubuntu 8.10 | Chris Johnston said:

    [...] That is it.. Your LLMP install is now complete! Stay tuned for more articles on how to setup your websites using Lighttpd as your web server. Because MySQL does not come optimized, please follow the MySQL part of the tutorial on Configuring a lightweight Apache / MySQL install on Debian/Ubuntu. [...]

  • Dan Jones said:

    Thanks for the tips. They helped a lot, but it may be that 128 MB just isn’t enough RAM for WordPress. I may have to upgrade.

  • cJ (author) said:

    @Dan Jones Probably not.. I would try either Nginx or Lighttpd.. Or take a look at Mosso and Slicehost for a better VPS.

  • jay said:

    Thank you so much for this tutorial It has made all the difference on the performance of my slicehost apache2/MySQL installation. A quick question: what do you think about creating a tmpfs with these commands and would it be detrimental to a slicehost 256 VPS?

    mkdir -p /my/home/directory/ramdrive

    mount -t tmpfs -o size=500M,mode=0777 tmpfs /my/home/directory/ramdrive

    Then pointing the tmp directory in my.cnf to the ramdrive?

    Thank you.

  • Eric Marden said:

    Thanks for the great article. I use it as a starting place for every slice I generate.

    Cheers,

    ~e

  • gollyg said:

    Thanks for the tips. I have a staging server running on a teeny weeny 128MB slice that was performing very poorly.

    It is now quite snappy. It is a good low traffic environment to optimise in this way. I am guessing these settings cease to be as optimal for a busy slice (no such thing as a free lunch).

    The slicehost article add some details for some of these settings and might be useful for someone.
    http://articles.slicehost.com/2008/4/28/ubuntu-hardy-apache-configuration-1

    Thanks again

  • links for 2009-12-19 » Pbur’s Adventures said:

    [...] Configuring a lightweight Apache / MySQL install on Debian/Ubuntu | chrisjohnston.org (tags: apache config tips)   « links for 2009-12-02 |   [...]

  • John Rockefeller said:

    This is a great article. It has significantly reduced the amount of RAM required to run both MySQL and Apache. I’ll let you know if the VPS lockups continue to seize :)

  • Research on MySQL performance with WordPress on Slicehost | Inside CoPress said:

    [...] posts related to MySQL optimization including a conversation on Stack Overflow, explanation on a lightweight configuration on Debian, and a lengthy WordPress optimization tutorial. I also came across a forum thread on WordPress.org [...]

  • Running Wordpress on a Budget Slice. • A post from Full Stop Interactive's Blog said:

    [...] get some kind of kickback. I don’t even know how much it is. [↩]Take a look at “Configuring a Lightweight Apache / MySQL Install on Debian / Ubuntu” and “Introducing LAMP Tuning Techniques” via this thread on Slicehost’s [...]

  • Deep said:

    Great tips, but I’m wondering if this is valid for all kind of apps running. I have to optimize my server for an open source crm application, vtiger crm, running on php/mysql.

    right now the apache process is taking close to 50MB per process, which I hope to reduce down to 20MB. Its standard installation on Centos 5.4 (including running tomcat, which I don’t require), but I’m afraid how to remove extra packages, without affecting my running application.

    any help will be highly appreciated.

    regards,
    Deep

  • Matt said:

    This was great – Could you mention what the downsides are?

  • Chris Johnston (author) said:

    I have yet to run into any downsides.

  • Slavi said:

    Thanks for the steps.

    the free memory of my VPS increased from 3-5 to 150 MB!
    nice!
    I hope there aren’t huge drawbacks when I am hit with a huge load of traffic.

  • Slavi said:

    Oh, forgot to ask has anybody experienced any issues with the settings in this article when the traffic of your site has increased ?

  • kate89 said:

    First enter:Look for the MySQL command. (If you don

  • Gennaro said:

    I have overwritten my.cnf with a wrong one.
    How may I rebuild it? I do not know what are the right path of all the service… I’m using a VPS CentOS 5 a MySQL 5.0.9. May amyone help me?

  • zwebx said:

    here is a copy of mine:

    [mysqld]
    default-storage-engine = myisam
    key_buffer = 8M
    query_cache_size = 8M
    query_cache_limit = 4M
    max_connections=25
    thread_cache=1
    skip-innodb
    query_cache_min_res_unit=0
    tmp_table_size = 4M
    max_heap_table_size = 4M
    table_cache=256
    concurrent_insert=2

    give it a go and ignore any errors you get during start up it still works for me…

  • zwebx said:

    “Oh, forgot to ask has anybody experienced any issues with the settings in this article when the traffic of your site has increased ?”
    i know the owner of freevps.us (i think about 100k of hits per month)
    he uses the mysql setting i posted above but he runs nginx and he keeps his server at under 55MB.

  • SAM said:

    whatever is described in article : tried by me and still my apache consuming all of memory. each connection is taking at least 30mb.

    i am looking to move to nginix. ca you help me convert .htaccess to nginx one?

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.