21.4.14

Best configuration for 512mb -1Gb Ram VPS running Wordpress Smoothly

Best configuration for 512mb -1Gb Ram VPS running Wordpress Smoothly

I've found this post from 
As you known, I’ve moved from HostGator to DigitalOcean VPS for two months and I shared some tips to build your own perfect LAMP server optimize for WordPress. With the VPS expansion you can now get a (very) small virtual private server (VPS) for a very affordable price. However, when you get a server with something like 256MB or 512MB RAM and a portion of CPU power, using default MySQL/PHP/Apache settings is a pretty bad idea. Currently, I’m running 3 websites with DigitalOcean‘s lowest plan: 512MB RAM/1 CPU. Not sure for load, but lets say 5-10k visitors per day.





If you follow my guidelines in previous post, RAM usage will be increase up to 400 MB then running out in 1 – 2 weeks even you turned on swap. How to minimize memory usage on LAMP to prevent your server crash?

I’ve setup a working VPS Ubuntu 12.04 LTS, LAMP server in 512MB of memory and it hasn’t gone into swap yet. It typically runs in just over 256 – 378Mb of memory and performs really well. So I thought I’d share the process of setting it up.

Determining Free Memory and Swap Activity

Before start optimizing your server, let’s review memory using on it. You can use the following command to display memory:
$ free -m
To see a list of your running processes sorted by memory use:
$ ps -eo pmem,pcpu,rss,vsize,args | sort -k 1 -r | less

Setup LAMP Server in a Low Memory Situation


Stop, Disable unused services


Here is list of services which I’ve changed:
  • Postfix: This service let you sending & receiving email to your domain. I’m using Google Apps for emails and Mailchimp to manage newsletter’s subscribers. So I stop and disable Postfix server.
  • Bind9: You can manage domain record with bind9 service, it’s stoped & disabled since I used Digital Ocean DNS services.
  • 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.
Don’t run X, shutdown all unneccesary services, and compile Apache, MySQL, and PHP with only essential functionality.

Apache

The biggest problem with Apache is the amount of ram is uses. I’ll discuss the following techniques for speeding up Apache and lowering the ram used.
  • Handle Fewer Simultaneous Requests
  • Loading Fewer Modules
  • Log less

Tune Apache to only have a small number of spare children running.

Prefork is where the real magic happens. This is where we can tell apache to only generate so many processes. The defaults here are high it’s eating your server memory. Make your sure apache2.conf is not configured to start too many servers, or have to many spare server best weight loss sitting around. Reference the example below:

    StartServers          1
    MinSpareServers       1
    MaxSpareServers       3
    MaxClients           10
    MaxRequestsPerChild 3000



    StartServers          1
    MinSpareThreads       5
    MaxSpareThreads      15 
    ThreadLimit          25
    ThreadsPerChild       5
    MaxClients           25
    MaxRequestsPerChild 200
Also, make sure to adjust KeepAliveTimeout to 10 or 15. In my opinion, 15 seconds is longer than a short page view requires, and shorter than a long page view requires.

Only load the modules you require

The default configuration file for apache also frequently loads every module it can. This is an especially big deal with the prefork mpm, as each apache instance will eat up geometrically more memory when unneeded modules are enabled. To check which apache modules are enabled/installed, use command below:
# apache2ctl -M
Default Apache modules (may be difference with yours):
Apache Loaded Modules
Here is list of Apache Modules which must be enabled for running WordPress:
LoadModule dir_module modules/mod_dir.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule mime_module modules/mod_mime.so
LoadModule setenvif_module modules/mod_setenvif.so
LoadModule alias_module modules/mod_alias.so
LoadModule authz_host_module modules/mod_authz_host.so
LoadModule rewrite_module modules/mod_rewrite.so
Comment out any modules that aren’t needed to save yourself some more memory. Or you can change it by use commands below:
To enable a module:
# a2enmod module_name
To disable a module:
# a2dismod module_name
You must restart the server after enable/disable the modules:
# service apache2 restart

Log Less

If you’re trying to maximize performance, you can definitely log less. In my server, I set it to error lever. Also, if you don’t care about looking at certain statistics, you can choose to not log certain things, like theUser-Agent or the http-referer.
# ErrorLog: The location of the error log file.
# If you do not specify an ErrorLog directive within a 
# container, error messages relating to that virtual host will be
# logged here.  If you *do* define an error logfile for a 
# container, that host's errors will be logged there and not here.
#
ErrorLog ${APACHE_LOG_DIR}/error.log

#
# LogLevel: Control the number of messages logged to the error_log.
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
#
LogLevel error
I like seeing those things, but it’s up to you.

Optimize MySQL server

Tweaking MySQL to use small amounts of memory is fairly straightforward. I’m going to try to show you the why instead of the what, so you can hopefully tweak things for your specific server. We’ll look at the following MySQL types of mysql settings:
  • Things We Can Disable
  • Turning MySQL Server Parameters
  • Third Party Configuration Wizard for MySQL
To optimize mySQL we need to edit the mySQL configuration file, it’s /etc/mysql/my.conf

Things We Can Disable

MySQL allows a few different storage engines for its tables. The two most common are InnoDB andMyISAM. The main difference between the two is:
  • MyISAM offers table-level locking, meaning that when data is being written into a table, the whole table is locked, and if there are other writes that must be performed at the same time on the same table, they will have to wait until the first one has finished writing data.
  • InnoDB, on the other hand, offers row-level locking, meaning that when data is being written to a row, only that particular row is locked; the rest of the table is available for writing.
The problems of table-level locking are only noticeable on very busy servers. For the typical website scenario, usually MyISAM offers better performance at a lower server cost.
If you decide to use only MyISAM tables, you must add the following configuration lines to your my.cnffile:
default-storage-engine=MyISAM
default-tmp-storage-engine=MyISAM
If you only have MyISAM tables, you can disable the InnoDB engine, which will save you RAM, by adding the following line to your my.cnf file:
skip-innodb
If you use InnoDB from the past, here is a simple shell script to automatically convert InnoDB tables toMyISAM.
#!/bin/bash

MYSQLCMD=mysql

for db in `echo show databases | $MYSQLCMD | grep -v Database`; do
        for table in `echo show tables | $MYSQLCMD $db | grep -v Tables_in_`; do
                TABLE_TYPE=`echo show create table $table | $MYSQLCMD $db | sed -e's/.*ENGINE=\([[:alnum:]\]\+\)[[:space:]].*/\1/'|grep -v 'Create Table'`
                if [ $TABLE_TYPE = "InnoDB" ] ; then
                        mysqldump $db $table > $db.$table.sql
                        echo "ALTER TABLE $table ENGINE = MyISAM" | $MYSQLCMD $db
                fi
        done
done

Turning MySQL Server Parameters

There are several parameters that can be adjusted on a MySQL server to make it faster.

Key buffer size

This is probably the single most important thing you can tweak to influence MySQL memory usage and performance. MySQL tries to put everything that’s indexed into the key buffer so this is a huge performance speedup. The SQL query will be served directly from RAM. I can’t say what size you should make your key buffer, because only you know how much ram you have free.

The Query Cache

If you do the same query two times in a row, and the result fits in the query cache, mysql doesn’t have to do the query again. If you’re going for performance, this can be a huge benefit, but it can also eat up memory. So, you need setting it’s not too high and low as your website needed.
There are three variables that influence how the query cache works.
  • query_cache_size
  • query_cache_limit
  • query_cache_type

Maximum Number of Connections

It’s option parameter. If you’re already limiting the number of apache processes, then you’ll be fine. If you’re not, and you need to handle thousands of users simultaneously, you need to increase this number.

The Table Cache

Every time you access a table, MySQL loads a reference to a table as one entry in the table cache. This is done for every concurrent access of a table, it’s really important for performance, marginally so for memory usage. You can keep upping the table cache, but you’ll eventually hit a limit on the number of files your operating system can have open, so keep that in mind. If table cache is set too low, mysql will barfon you, and you don’t want that.
Here is current my.conf that I’ve optimized on my VPS with lowest Digital Ocean plan.
[mysqld]
     port            = 3306
     socket          = /var/lib/mysql/mysql.sock
     skip-locking
     key_buffer = 16K
     max_allowed_packet = 1M
     table_cache = 4
     sort_buffer_size = 64K
     read_buffer_size = 256K
     read_rnd_buffer_size = 256K
     net_buffer_length = 2K
     thread_stack = 64K

     # For low memory, InnoDB should not be used so keep skip-innodb uncommented unless required
     skip-innodb

     # Uncomment the following if you are using InnoDB tables
     #innodb_data_home_dir = /var/lib/mysql/
     #innodb_data_file_path = ibdata1:10M:autoextend
     #innodb_log_group_home_dir = /var/lib/mysql/
     #innodb_log_arch_dir = /var/lib/mysql/
     # You can set .._buffer_pool_size up to 50 - 80 %
     # of RAM but beware of setting memory usage too high
     #innodb_buffer_pool_size = 16M
     #innodb_additional_mem_pool_size = 2M
     # Set .._log_file_size to 25 % of buffer pool size
     #innodb_log_file_size = 5M
     #innodb_log_buffer_size = 8M
     #innodb_flush_log_at_trx_commit = 1
     #innodb_lock_wait_timeout = 50

     [mysqldump]
     quick
     max_allowed_packet = 16M

     [mysql]
     no-auto-rehash
     # Remove the next comment character if you are not familiar with SQL
     #safe-updates

     [isamchk]
     key_buffer = 8M
     sort_buffer_size = 8M

     [myisamchk]
     key_buffer = 8M
     sort_buffer_size = 8M

     [mysqlhotcopy]
     interactive-timeout

Third Party Configuration Wizard for MySQL

I’ve found Percona provide a free Configuration Wizard for MySQL that help you choose the best features of MySQL server to achieve better MySQL database performance and avoid the time, complexity, and risk of customizing a my.cnf configuration on your own.

MySQL Server Monitor

MySQL stores statistics that will help you to determine the best values that you must use. Furthermore, there are two handy utilities that can be used to read these statistics and print them on an easy-to-understand format: tuning-primer.sh and mysqltuner.pl.
These scripts will monitoring your MySQL server, at the end of its report, give you a hint of the parameters you should adjust on your server.

Optimize PHP & Caching

PHP is not very memory intensive, so I don’t think you should worry too much about memory usage, unless your app needs it, in which case the memory footprint of PHP won’t be too significant. But I’ve researched then found some tweaks of PHP configuration that decrease memory usage of your webserver.
; Limit the memory to 40M should be fine for barebones WordPress
memory_limit = 48M
realpath_cache_ttl=300
realpath_cache_size=1M

Alternative PHP Cache

Install a PHP Cache such as Alternative PHP Cache. The PHP cache will store compiled PHP scripts so that they can be reused without the overhead of compiling and processing them for each request.
# pecl install apc
And my php.ini configuration
[APC]
extension=apc.so
apc.enabled=1
apc.shm_segments=1

;32M per WordPress install
apc.shm_size=128M

;Relative to the number of cached files (you may need to watch your stats for a day or two to find out a good number)
apc.num_files_hint=7000

;Relative to the size of WordPress
apc.user_entries_hint=4096

;The number of seconds a cache entry is allowed to idle in a slot before APC dumps the cache
apc.ttl=7200
apc.user_ttl=7200
apc.gc_ttl=3600

;Setting this to 0 will give you the best performance, as APC will
;not have to check the IO for changes. However, you must clear 
;the APC cache to recompile already cached files. If you are still
;developing, updating your site daily in WP-ADMIN, and running W3TC
;set this to 1
apc.stat=1

;This MUST be 0, WP can have errors otherwise!
apc.include_once_override=0

;Only set to 1 while debugging
apc.enable_cli=0

;Allow 2 seconds after a file is created before it is cached to prevent users from seeing half-written/weird pages
apc.file_update_protection=2

;Leave at 2M or lower. WordPress does't have any file sizes close to 2M
apc.max_file_size=2M

;Ignore files
apc.filters = "/var/www/apc.php"

apc.cache_by_default=1
apc.use_request_time=1
apc.slam_defense=0
apc.mmap_file_mask=/var/www/temp/apc.XXXXXX
apc.stat_ctime=0
apc.canonicalize=1
apc.write_lock=1
apc.report_autofilter=0
apc.rfc1867=0
apc.rfc1867_prefix =upload_
apc.rfc1867_name=APC_UPLOAD_PROGRESS
apc.rfc1867_freq=0
apc.rfc1867_ttl=3600
apc.lazy_classes=0
apc.lazy_functions=0

Static Cache

Another thing that could be a great idea for a blog on a small server is to put it behind a static-HTTP-cache like Varnish. That can really boost your scalability. Configuring Varnish is a complex and large topic that requires its own blog post, however.

Conclusion

I’ve shared my webserver configuration to get best perfomance with lowest Digital Ocean’s droplet:512MB RAM/1 CPU. I’m using Ubuntu 12.04 LTS, LAMP, Varnish, APC Cache to hosting 3 WordPress website (not Multisite) and alive with 10k visitors per day. Let’s see the Blitz.io test result:



Blitz Test Result
Blitz Test Result

As you see, 0.23% of the users got Connection Timeout when my website has 42,735,587 hits/day (WoW), may I have somethings to do but I’m feel pleasure with my server.

Source: http://www.narga.net/optimizing-apachephpmysql-low-memory-server/

41 comments:

  1. Very Good Article, Lot of good information, working on my website right now!
    VPS

    ReplyDelete
  2. 250TB Dedicated Servers-250TB.com: We provides fully managed virtual dedicated servers, bandwidth solutions, dedicated server web hosting, 100tb dedicated servers & 250tb dedicated servers

    WiFi marketing-Get Restaurant Wifi, WiFi marketing, Social Wifi Free & Social Wifi Hotspot. Vizz WiFi allows your customers to use your internet connection for free while you advertise on their social media profiles.

    top web design dorset-We are Top Web Design Company in Bournemouth. We offer Website Design in Poole, Bournemouth & Hampshire. Visit us for cheap website design services.

    ReplyDelete
  3. I like your post & I will always be coming frequently to read more of your post. Thank you very much for your post once more.
    best domain name registration

    ReplyDelete
  4. Thanks for sharing your thoughts with us.. they are really interesting.. I would like to swervey more from you.
    ebay photo hosting

    ReplyDelete
  5. Want to get affordable VPS Servers? Keep visiting Zolute to get most effective servers.

    ReplyDelete
  6. I like your post & I will always be coming frequently to read more of your post. Thank you very much for your post once more.
    cloud hosting

    ReplyDelete
  7. I like your post & I will always be coming frequently to read more of your post. Thank you very much for your post once more.
    streaming servers

    ReplyDelete
  8. It's really an informative blog .I really like your work.
    Free plesk panel

    ReplyDelete
  9. Webhoster.pk is providing suitable Web Hosting in Pakistan and fulfill your needs according to your requirements , We are Best comapny of Web Hosting Pakistan and offering so many plans like Budget Web Hosting - Business Web Hosting - Reseller Web Hosting - Vps Hosting - Dedicated Server Hosting choose the plan according to your requirement and get benefit from us.

    ReplyDelete
  10. Very interesting topic, can you post some further information on this subject.
    it consulting west palm beach

    ReplyDelete
  11. You need to use &gt; and &lt; in the apache config. The <ifmodule> tags are being interpreted as html tags.

    ReplyDelete
  12. I think this post will be a fine read for my blog readers too, could you please allow me to post a link to my blog. I am sure my guests will find that very useful.
    web hosting

    ReplyDelete
  13. Interesting blog all information are very important for me about the Open source cloud apps and i really need it thank you.
    Open source cloud apps

    ReplyDelete
  14. Interesting blog and I really like your work and must appreciate you work for the online storage nice work..
    online storage

    ReplyDelete
  15. Your blog have allot of detail about the website domains it's good job.
    website domains

    ReplyDelete
  16. Nice work and all information about the programmatic platform that's are very amazing well done.
    programmatic platform

    ReplyDelete
  17. This comment has been removed by the author.

    ReplyDelete
  18. Your work is very good and I appreciate you and hopping for some more informative posts. Thank you for sharing great information to us.
    free vps

    ReplyDelete
  19. Interesting blog all information are very important for me about the local seo and i really need it thank you.
    local seo

    ReplyDelete
  20. Your blog is awesome. You have shared very valuable information to us. Thank you so much for sharing this.




    Domain From The World's Largest Registrar

    ReplyDelete
  21. Thank you for this informative and interesting blog about the web design it's really very important for me.
    web design

    ReplyDelete
  22. Nice blog and all the information are very useful for me thank for sharing it.
    website design ireland

    ReplyDelete
  23. You blog provide a very important details about the Australian Web Hosting and i really appreciate it.
    Australian Web Hosting

    ReplyDelete
  24. You got a really useful blog I have been here reading for about half an hour. I am a newbie and your post is valuable for me.
    lean manufacturing

    ReplyDelete
  25. Most valuable and fantastic blog I really appreciate your work which you have done about the Lime Designer,many thanks and keep it up.
    Lime Designer

    ReplyDelete
  26. digitalocean.com ---> Scam

    ReplyDelete
  27. I'm looking for tutorial on how to optimize LEMP on Linode for my blog.

    Anyone?

    ReplyDelete
  28. Your blog have a very useful and interesting detail about the digital marketing it's so interesting.
    winnipeg web design

    ReplyDelete
  29. Good information and great post about the Web Design and I like the website, and am sure to keep returning.
    Web Design Belfast

    ReplyDelete
  30. Today i seen this blog and also going to bookmark it cause i got many new and informative detail about the seo company well done.
    zoekmachine optimalisatie bureau

    ReplyDelete
  31. Interesting all your detail that i got from your blog just keep it up.

    Website Hosting

    ReplyDelete
  32. Great article on collection of gifts. This article gives an better idea.

    Cash your skills

    ReplyDelete
  33. your blog have a very useful and interesting detail about the digital marketing it's so interesting

    G l o w i t s e l f

    ReplyDelete
  34. You have some honest ideas about the web design share here I really get many information and discovered most peoples will agree with your blog.
    web design company houston

    ReplyDelete
  35. Thank you so much for sharing this great blog.Very inspiring and helpful too.Hope you continue to share more of your ideas.I will definitely love to read. The Difference: ddr3 vd ddr4

    ReplyDelete
  36. I wanted to thank you for this great read!! I definitely enjoying every little bit of it.I have you bookmarked to check out new stuff you post.
    Small Business Services

    ReplyDelete
  37. Today, we are going to a deeper look at the Vultr vs DigitalOcean
    services and compare them with each other. No one can deny that DigitalOcean and Vultr is industry standard cheapest VPS provider.

    ReplyDelete