Jul 02

Recently i am working with nginx and apache on wpmu. I am trying to test and bencmark both web server. Yes i have browse and heard that nginx is more fastest than apache but i also having difficulties with the nginx mod rewrite setting to wpmu supercache on the other side there were no problem with apache mod rewrite but i have a lot memory and resource when i use apache. Okay then when i stumble on this article http://tech.nocr.at/tech/how-to-speed-up-wordpress-with-nginx-and-wp-super-cache/ it will be a great idea if i can apply the article on the wpmu, i already have wpmu , wp supercache , nginx and apache. The main idea with that article is to split the static and dynamic content process, nginx will process the static content and the apache will process the dynamic content.

System requirement:

– Centos 5.3

– Apache ( httpd-2.2.8 )

– Nginx (nginx-0.6.36-1.el5)

– wp super cache plugin Version: 0.9.4.3

Okay let’s start the changes

1.  I assume you have installed apache and wpmu first of all we need to changes the apache(httpd) port firstwe can changes it on the httpd.conf at /etc/httpd/conf/httpd.conf

# vi /etc/httpd/conf/httpd.conf
Listen 80
Listen 8080

2. Next we are going to turn KeepAlives off on Apache since Nginx will be dealing with all of the actual browser connections. This will ensure that all of the child processes in Apache die quickly and don’t hang around chewing up memory. Find the KeepAlive entry in your /etc/httpd/conf/httpd.conf file and set it to off

# vi /etc/httpd/conf/httpd.conf
KeepAlive Off

3. You need to changes your virtual directory to match the nginx config. Here is my config

# vi /etc/httpd/conf/httpd.conf

NameVirtualHost localhost:8080
ServerName *.blog.myblog.org
< VirtualHost localhost:8080>
DocumentRoot /blog.myblog.org/public_html/
< Directory "/blog.myblog.org/public_html" >
AllowOverride All
Options All
Order allow,deny
Allow from all
< / Directory>
ErrorLog logs/blog.myblog.org-error_log
CustomLog logs/blog.myblog.org-access_log combined
< / VirtualHost>
< VirtualHost localhost:8080 >
ServerName myotherweb.com
DocumentRoot /myotherweb.com/public_html/
< Directory "/myotherweb.com/public_html" >
AllowOverride All
Options All
Order allow,deny
Allow from all
< / Directory >
ErrorLog logs/myotherweb.com-error_log
CustomLog logs/myotherweb.com-access_log common
< / VirtualHost>

5. You neew to reload your httpd service
# /etc/init.d/httpd reload

6.  Okay we will set the nginx , if you did not have nginx yet on centos you can insstall it from EPEL repository you can refer to here http://www.cyberciti.biz/faq/rhel-fedora-install-configure-nginx-php5/

Okay we will set the nginx.conf on /etc/nginx/nginx.conf
# vi /etc/nginx/nginx.conf

#user nginx;
user apache;
worker_processes 10;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] $request '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
gzip on;
server {
listen 80;
server_name blog.myblog.org *.blog.myblog.org;
# Main location
location / {
proxy_pass http://127.0.0.1:8080/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
location ~ wp-login.php$ {
root /blog.myblog.org/public_html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index wp-login.php;
fastcgi_param SCRIPT_FILENAME /blog.myblog.org/public_html$fastcgi_script_name;
include fastcgi_params;
}
# Static files location
location ~*^.+^.+^.+.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$ {
root /blog.myblog.org/public_html;
# if the requested file exists, return it immediately
if (-f $request_filename) {
break;
}
set $supercache_file '';
set $supercache_uri $request_uri;
if ($request_method = POST) {
set $supercache_uri '';
}
# Using pretty permalinks, so bypass the cache for any query string
if ($query_string) {
set $supercache_uri '';
}
if ($http_cookie ~* "comment_author_|wordpress|wp-postpass_" ) {
set $supercache_uri '';
}
# if we haven't bypassed the cache, specify our supercache file
if ($supercache_uri ~ ^(.+)$) {
set $supercache_file /wp-content/cache/supercache/$http_host/$1index.html;
}
# only rewrite to the supercache file if it actually exists
if (-f $document_root$supercache_file) {
rewrite ^(.*)$ $supercache_file break;
}
# all other requests go to WordPress
if (!-e $request_filename) {
rewrite . /index.php last;
}
}
# Static files location
location ~*^.+^.+^.+ (jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$ {
root /myotherweb.com/public_html;
}

}
}

you can remove the wp-login.php because i set my wp-login.php to be process with the nginx php_cgi ( i use ldap and ldap session will refer to blog.myblog.org:8080 and i need blog.myblog.org port 80 )

location ~ wp-login.php$ {
root /blog.myblog.org/public_html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index wp-login.php;
fastcgi_param SCRIPT_FILENAME /blog.myblog.org/public_html$fastcgi_script_name;
include fastcgi_params;
}

7. And the final steps is to start your nginx
# /etc/init.d/nginx start
and in my case since i process the wp-login.php on nginx i need to turm on the spawn-fcgi /php_cgi
# /etc/init.d/php_cgi start

8. And that’s it you can start test your sites http://blog.myblog.org

Leave a Reply