How to optimize WordPress in EC2 in very overkill way!


When i wrote this thing, i’m actually put my sites for testing. I’m running micro (612 MB) instance EC2 which there are Drupal, CodeIgniter and WordPress in same servers. I thought that WordPress have more resource than anothers. So i start to optimize this server and WordPress in Overkill way!

First, i assume you already have NGINX + PHP5-FPM + APC installed. If not, you can check at my Drupal 7 sites which talking about Configuring & Optimizing EC2 Instance.

Now, here the steps for optimizing wordpress.

1. Editing /etc/mysql/my.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#
# * Query Cache Configuration
#
query_cache_type = 1
query_cache_limit = 105M
query_cache_size = 146M
join_buffer_size = 100M

#
#
# * Logging and Replication
#
# Both location gets rotated by the cronjob.
# Be aware that this log type is a performance killer.
# As of 5.1 you can enable the log at runtime!
#general_log_file = /var/log/mysql/mysql.log
#general_log = 1

log_error = /var/log/mysql/error.log

# Here you can see queries with especially long duration
#log_slow_queries = /var/log/mysql/mysql-slow.log
#long_query_time = 2
#log-queries-not-using-indexes
#
# The following can be used as easy to replay backup logs or for replication.
# note: if you are setting up a replication slave, see README.Debian about
# other settings you may need to change.
#server-id = 1
#log_bin = /var/log/mysql/mysql-bin.log
expire_logs_days = 10
max_binlog_size = 100M
#binlog_do_db = include_database_name
#binlog_ignore_db = include_database_name
#
# * InnoDB
#
# InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.
# Read the manual for more InnoDB related options. There are many!
#
# * Security Features
#
# Read the manual, too, if you want chroot!
# chroot = /var/lib/mysql/
#
# For generating SSL certificates I recommend the OpenSSL GUI "tinyca".
#
# ssl-ca=/etc/mysql/cacert.pem
# ssl-cert=/etc/mysql/server-cert.pem
# ssl-key=/etc/mysql/server-key.pem
innodb_buffer_pool_size = 300M
innodb_flush_method = O_DIRECT
innodb_file_per_table = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 4M
innodb_additional_mem_pool_size = 20M
# num cpu’s/cores *2 is a good base line for innodb_thread_concurrency
innodb_thread_concurrency = 4

2. Installing Eaccelerator

1
2
3
4
5
6
7
8
9
10
11
12
sudo apt-get install subversion git build-essential
wget -c http://nchc.dl.sourceforge.net/project/eaccelerator/eaccelerator/eAccelerator%200.9.6.1/eaccelerator-0.9.6.1.zip
unzip eaccelerator-0.9.6.1.zip
cd eaccelerator-0.9.6.1
phpize
./configure –enable-eaccelerator=shared
sudo make
sudo make install
cd /var/cache/
sudo mkdir eaccelerator
sudo chown www-data:www-data eaccelerator/
sudo chmod 777 eaccelerator/

Remember where eaccelerator installed when you install it.

3. Edit /etc/php5/fpm/php.ini and configure Eaccelerator with PHP5-FPM:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
zend_extension = "/usr/lib/php5/20090626+lfs/eaccelerator.so"
eaccelerator.shm_size = "16"
eaccelerator.cache_dir = "/var/cache/eaccelerator"
eaccelerator.enable = "1"
eaccelerator.optimizer = "1"
eaccelerator.check_mtime = "1"
eaccelerator.debug = "0"
eaccelerator.filter = ""
eaccelerator.shm_max = "0"
eaccelerator.shm_ttl = "0"
eaccelerator.shm_prune_period = "0"
eaccelerator.shm_only = "0"
eaccelerator.compress = "1"
eaccelerator.compress_level = "9"

4. Use Memcached Plugin by download object-cache and put in /wp-content/

1
2
cd wp-content/
wget -c http://plugins.trac.wordpress.org/export/412520/memcached/trunk/object-cache.php

Also, editing your wp-config.php

1
2
global $memcached_servers;
$memcached_servers = array(‘default’ => array(‘127.0.0.1:11211’));

/* That’s all, stop editing! Happy blogging. */

5. Enabling WordPress’s internal caching

Uncomment or add this to your wp-config.php:

1
define( ‘ENABLE_CACHE’, true );

Make sure that the wp-content/cache folder is writable.

6. Installing Plugin W3-Total-Cache
Download plugin here. You should installed a few Ubuntu Package to make w3 cache works.

1
2
sudo apt-get install php5-curl php5-tidy
sudo service php5-fpm restart

Chmod 777 your /wp-content and start to activate this plugin. Don’t forget to chmod 755 your /wp-content after installing this plugin.

7. Editing NGINX Configuration for enabling W3-Total-Cache

Create w3-cache.conf in /etc/nginx/ :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# BEGIN W3TC Page Cache cache
location ~ /wp-content/w3tc/pgcache.*html$ {
    add_header X-Powered-By "W3 Total Cache/0.9.2.3";
    add_header Vary "Accept-Encoding, Cookie";
}
location ~ /wp-content/w3tc/pgcache.*gzip$ {
    gzip off;
    types {}
    default_type text/html;
    add_header X-Powered-By "W3 Total Cache/0.9.2.3";
    add_header Vary "Accept-Encoding, Cookie";
    add_header Content-Encoding gzip;
}
# END W3TC Page Cache cache
# BEGIN W3TC Browser Cache
gzip on;
gzip_types text/css application/x-javascript text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon;
location ~ .(css|js)$ {
    add_header X-Powered-By "W3 Total Cache/0.9.2.3";
}
location ~ .(html|htm|rtf|rtx|svg|svgz|txt|xsd|xsl|xml)$ {
    add_header X-Powered-By "W3 Total Cache/0.9.2.3";
}
location ~ .(asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|png|pot|pps|ppt|pptx|ra|ram|swf|tar|tif|tiff|wav|wma|wri|xla|xls|xlsx|xlt|xlw|zip)$ {
    add_header X-Powered-By "W3 Total Cache/0.9.2.3";
}
# END W3TC Browser Cache
# BEGIN W3TC Minify core
rewrite ^/wp-content/w3tc/min/w3tc_rewrite_test$ /wp-content/w3tc/min/index.php?w3tc_rewrite_test=1 last;
rewrite ^/wp-content/w3tc/min/(.+.(css|js))$ /wp-content/w3tc/min/index.php?file=$1 last;
# END W3TC Minify core
# BEGIN W3TC Page Cache core
rewrite ^(.*/)?w3tc_rewrite_test$ $1?w3tc_rewrite_test=1 last;
set $w3tc_rewrite 1;
if ($request_method = POST) {
    set $w3tc_rewrite 0;
}
if ($query_string != "") {
    set $w3tc_rewrite 0;
}
set $w3tc_rewrite2 1;
if ($request_uri !~ /$) {
    set $w3tc_rewrite2 0;
}
if ($request_uri ~* "(sitemap.xml(.gz)?)") {
    set $w3tc_rewrite2 1;
}
if ($w3tc_rewrite2 != 1) {
    set $w3tc_rewrite 0;
}
set $w3tc_rewrite3 1;
if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|index.php)") {
    set $w3tc_rewrite3 0;
}
if ($request_uri ~* "(wp-comments-popup.php|wp-links-opml.php|wp-locations.php)") {
    set $w3tc_rewrite3 1;
}
if ($w3tc_rewrite3 != 1) {
    set $w3tc_rewrite 0;
}
if ($http_cookie ~* "(comment_author|wp-postpass|wordpress_[a-f0-9]+|wordpress_logged_in)") {
    set $w3tc_rewrite 0;
}
if ($http_user_agent ~* "(W3 Total Cache/0.9.2.3)") {
    set $w3tc_rewrite 0;
}
set $w3tc_ua "";
if ($http_user_agent ~* "(2.0 mmp|240×320|alcatel|amoi|asus|au-mic|audiovox|avantgo|benq|bird|blackberry|blazer|cdm|cellphone|danger|ddipocket|docomo|dopod|elaine/3.0|ericsson|eudoraweb|fly|haier|hiptop|hp.ipaq|htc|huawei|i-mobile|iemobile|j-phone|kddi|konka|kwc|kyocera/wx310k|lenovo|lg|lg/u990|lge vx|midp|midp-2.0|mmef20|mmp|mobilephone|mot-v|motorola|netfront|newgen|newt|nintendo ds|nintendo wii|nitro|nokia|novarra|o2|openweb|opera mobi|opera.mobi|palm|panasonic|pantech|pdxgw|pg|philips|phone|playstation portable|portalmmm|bppcb|proxinet|psp|qtek|sagem|samsung|sanyo|sch|sec|sendo|sgh|sharp|sharp-tq-gx10|small|smartphone|softbank|sonyericsson|sph|symbian|symbian os|symbianos|toshiba|treo|ts21i-10|up.browser|up.link|uts|vertu|vodafone|wap|willcome|windows ce|windows.ce|winwap|xda|zte)") {
    set $w3tc_ua _low;
}
if ($http_user_agent ~* "(acer s100|android|archos5|blackberry9500|blackberry9530|blackberry9550|blackberry 9800|cupcake|docomo ht-03a|dream|htc hero|htc magic|htc_dream|htc_magic|incognito|ipad|iphone|ipod|kindle|lg-gw620|liquid build|maemo|mot-mb200|mot-mb300|nexus one|opera mini|samsung-s8000|series60.*webkit|series60/5.0|sonyericssone10|sonyericssonu20|sonyericssonx10|t-mobile mytouch 3g|t-mobile opal|tattoo|webmate|webos)") {
    set $w3tc_ua _high;
}
set $w3tc_ref "";
if ($http_cookie ~* "w3tc_referrer=.*(google.com|yahoo.com|bing.com|ask.com|msn.com)") {
    set $w3tc_ref _search_engines;
}
set $w3tc_ssl "";
if ($scheme = https) {
    set $w3tc_ssl _ssl;
}
set $w3tc_enc "";
if ($http_accept_encoding ~ gzip) {
    set $w3tc_enc .gzip;
}
if (!-f "$document_root/wp-content/w3tc/pgcache/$request_uri/_index$w3tc_ua$w3tc_ref$w3tc_ssl.html$w3tc_enc") {
    set $w3tc_rewrite 0;
}
if ($w3tc_rewrite = 1) {
    rewrite .* "/wp-content/w3tc/pgcache/$request_uri/_index$w3tc_ua$w3tc_ref$w3tc_ssl.html$w3tc_enc" last;
}
# END W3TC Page Cache core

Then edit your /etc/nginx/sites-available/sites.conf and load this w3-cache.conf, for example :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
server {

        …..

        index  index.php index.html index.htm;

        if (!-e $request_filename) {
                rewrite ^(.+)$ /index.php?q=$1 last;
        }
 
        # catch all
        error_page 404 /index.php;

        # Load w3-cache nginx.conf here
        include /etc/nginx/w3-cache.conf;

        location ~ .php$ {
        …..

This the result multiple sites cisight.com and wpscale.com


Leave a Reply

Your email address will not be published. Required fields are marked *

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