Optimize Apache and MySQL for a 256MB VPS

For small websites or not so popular WordPress blogs a small VPS with only 256MB of RAM should be enough. If you’ve followed this guide to install a Ubuntu web server you need to optimize your server a little bit.

Start installing MySQLtuner

Download the Perl script to your (admin) home directory:

wget http://mysqltuner.pl/mysqltuner.pl

Create also a file nano .my.cnf and add this code:

[client]
user=someusername
pass=thatuserspassword

After running MySQLtuner script perl mysqltuner.pl you should get this warning:

Reduce your overall MySQL memory footprint for system stability

To resolve this an other memory related issues we need to optimize the MySQL database settings.

Open your MySQL settings file using (don’t forget to backup your settings):

sudo nano /etc/mysql/my.cnf

Add the following line into the section [mysqld]:

skip-innodb

Next locate the line skip-external-locking and add skip-locking below that line.

Now find the section labeled “Fine Tuning”. Change/add the settings in that section to match those below:

key_buffer = 16K
max_allowed_packet = 1M
thread_stack = 64K
thread_cache_size = 4
sort_buffer = 64K
net_buffer_length = 2K

Save the file (ctrl+x) and restart MySQL: sudo /etc/init.d/mysql restart
If you run MySQLtuner again you will see that the “memory” warning is gone, ignore the aother warnings for the moment (you need to run the script after a few days again to get exact test results)

Optimize Apache in prefork mode

If you followed the Ubuntu tutorial we’ve mentioned in the first paragraph, your Apache setup should run in prefork mode. The default settings are much to high, open the file sudo nano /etc/apache2/apache2.conf andchange the following settings:

Timeout 45
KeepAlive On
MaxKeepAliveRequests 200
KeepAliveTimeout 3
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxClients 30
MaxRequestsPerChild 2000

That’s all, restart Apache using sudo /etc/init.d/apache2 restart. If you know more tweaks, please share.

4 thoughts on “Optimize Apache and MySQL for a 256MB VPS”

  1. You are absolutely wrong if you are using 256 MB, using your settings your application will consume too much memory.

    Setting MySQL
    ===========
    You can use MySQLtuner as reference, but your settings need to depend on the application you’re running.

    sudo nano /etc/my.cnf
    ———————————
    [mysqld]
    datadir=/var/lib/mysql
    socket=/var/lib/mysql/mysql.sock
    user=mysql

    symbolic-links=0
    skip-innodb
    #innodb_file_per_table = 1

    [mysqld_safe]
    log-error=/var/log/mysqld.log
    pid-file=/var/run/mysqld/mysqld.pid

    query_cache_type = 1
    query_cache_limit = 1M
    query_cache_size = 8M

    set-variable = max_connections = 30
    set-variable = max_user_connections = 30
    set-variable = table_cache=1000
    set-variable = max_allowed_packet=1M
    set-variable = max_connect_errors=999999

    key_buffer = 16K
    thread_stack = 64K
    table_cache = 4
    sort_buffer = 64K
    net_buffer_length = 2K

    Setting APACHE (/etc/apache2/apache2.conf or /etc/httpd/conf/httpd.conf)
    ============
    1. modify your conf
    sudo nano /etc/apache2/apache2.conf or sudo nano /etc/httpd/conf/httpd.conf
    —————————————————————————————————————–
    KeepAlive Off
    Timeout 30
    MaxKeepAliveRequests 50
    KeepAliveTimeout 2

    StartServers 1
    MinSpareServers 1
    MaxSpareServers 2
    ServerLimit 10
    MaxClients 10
    MaxRequestsPerChild 1000

    StartServers 1
    MaxClients 15
    MinSpareThreads 2
    MaxSpareThreads 15
    ThreadsPerChild 2
    MaxRequestsPerChild 200

    2. disable unused module

    #LoadModule authn_alias_module modules/mod_authn_alias.so
    #LoadModule authn_anon_module modules/mod_authn_anon.so
    #LoadModule authn_dbm_module modules/mod_authn_dbm.so
    #LoadModule authn_default_module modules/mod_authn_default.so
    #LoadModule authz_owner_module modules/mod_authz_owner.so
    #LoadModule authz_groupfile_module modules/mod_authz_groupfile.so
    #LoadModule authz_dbm_module modules/mod_authz_dbm.so
    #LoadModule ldap_module modules/mod_ldap.so
    #LoadModule authnz_ldap_module modules/mod_authnz_ldap.so
    #LoadModule logio_module modules/mod_logio.so
    #LoadModule env_module modules/mod_env.so
    #LoadModule ext_filter_module modules/mod_ext_filter.so
    #LoadModule usertrack_module modules/mod_usertrack.so
    #LoadModule dav_module modules/mod_dav.so
    #LoadModule status_module modules/mod_status.so
    #LoadModule speling_module modules/mod_speling.so
    #LoadModule userdir_module modules/mod_userdir.so
    #LoadModule substitute_module modules/mod_substitute.so
    #LoadModule proxy_module modules/mod_proxy.so
    #LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
    #LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
    #LoadModule proxy_http_module modules/mod_proxy_http.so
    #LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
    #LoadModule proxy_connect_module modules/mod_proxy_connect.so
    #LoadModule cgi_module modules/mod_cgi.so

    Setting SYSCTL
    ============
    sudo nano /etc/sysctl.conf

    net.ipv4.tcp_fin_timeout = 35
    net.ipv4.tcp_keepalive_time = 1800
    net.ipv4.tcp_keepalive_intvl = 35
    net.ipv4.tcp_tw_recycle = 1
    net.ipv4.tcp_tw_reuse = 1

    Create Swap
    =========

    cd /var
    touch swap.img
    chmod 600 swap.img
    dd if=/dev/zero of=/var/swap.img bs=512k count=500
    mkswap /var/swap.img
    swapon /var/swap.img
    echo “/var/swap.img none swap sw 0 0” >> /etc/fstab
    ysctl -w vm.swappiness=30
    reboot

    if you want disable swap. You can use swapoff /var/swap.img to turn it off.

    now, see it by your self how fast your Server

    you can check using :

    top
    free -m

  2. Thanks for your comment, I’m sure I will try it next time.
    What kind of website do you run on this kind of small server?

  3. When you say to set `key_buffer` do you not mean `key_buffer_size`?

Comments are closed.