wordpress with nginx

by Ruchira on October 2, 2010

Well If you read my article about installing Nginx+Mysql+PHP-FPM on debian or linux server you might interested in knowing how to setup wordpress on your newly installed lnmp serve. If you didnt read my article about how to install lnmp its here-http://www.ruchirablog.com/nginxmysqlphpfpm-auto-installer-centos-5-debian/

LNMP is really nice installer program I only discovered only 1 bug in it. Actually its not about security, LNMP asks your domain when installing so if you provide www.yourdomain.com for lnmp it will only setup your domain with www. So if some one access yourdomain.com ( Without www) it will not placed on the server and your web browser wont resolve that domain and sidplay Not Found error. So don’t worry now im going to write about solving this error and configuring nginx.conf correctly for complete install for wordpress. I have modified nginx.conf forWordPress Permalinks + WP Super Cache + Non WWW to WWW. , So to setup this I have included modified nginx.conf below

You need to open your nginx.conf to put this if this is a single domain lnmp server setup just login to ssh and your nginx.conf file is located in

  • /usr/local/nginx/conf/nginx.conf

To edit in type ,

  • nano /usr/local/nginx/conf/nginx.conf

It will open nginx.conf file in nano text editor like this-
Nginx lnmp

So backup current data in nginx.conf in case something go wrong! You can select the text in the nano text editor by like you do in notepad and Ctrl+C will copy the selected data to your clip board and then open notepad and paste it. After pasting all the text to notepad take a look at my modified nginx.conf below!

I have highlighted non www to www redirect in Red color while current text in black. (You need to replace ruchirablog.com with your own domain name)
And I have highlighted text that need for supercache to work in greeen ( You dont need to modify this)
And I have highlighted text which need for wordpress permalinks to work in blue ( You dont need to modify this)
So Just delete the text in your current nginx.conf file (Do backup like i said if something goes wrong) And copy and paste included nginx.conf text to your nginx.conf file (After modifying with your own domain name)

And after pasting just press Ctrl+O (Not zero) to save the data to your nginx.conf file and then press Ctrl+X to exit.

Thats it folks. Note that this edits your main nginx.conf file and only edit like this if you only host just single wordpress website on your server and its configured with lnmp installer (http://www.ruchirablog.com/nginxmysqlphpfpm-auto-installer-centos-5-debian/) . For multiple web sites and other nginx installs you need to modify some codes differently!

After saving your nginx.conf file you need to restart lnmp by

/root/lnmp restart

Thats it folks you have complete nginx setup for wordpress!

user www www;

worker_processes 1;

error_log /home/wwwroot/logs/nginx_error.log crit;

pid /usr/local/nginx/logs/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;

use epoll;
worker_connections 51200;

include mime.types;
default_type application/octet-stream;

server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;

sendfile on;
tcp_nopush on;

keepalive_timeout 60;

tcp_nodelay on;

fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 256k;

gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;

#limit_zone crawler $binary_remote_addr 10m;

listen 80;

server_name www.ruchirablog.com ruchirablog.com;
rewrite ^(.*) http://www.ruchirablog.com$1 permanent;

index index.html index.htm index.php;
root /home/wwwroot;

location / {
# enable search for precompressed files ending in .gz
# nginx needs to be complied using .-with-http_gzip_static_module
# for this to work, comment out if using nginx from aptitude

# if the requested file exists, return it immediately
if (-f $request_filename) {

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;

location ~ .*.(php|php5)?$
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;

include fcgi.conf;
location / {
if (!-e $request_filename) {
rewrite ^.*$ /index.php last;

location /status {
stub_status on;
access_log off;

location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
expires 30d;

location ~ .*.(js|css)?$
expires 12h;

log_format access ‘$remote_addr – $remote_user [$time_local] “$request” ‘
‘$status $body_bytes_sent “$http_referer” ‘
‘”$http_user_agent” $http_x_forwarded_for’;
access_log /home/wwwroot/logs/access.log access;
include vhost/*.conf;

I'm Ruchira Sahan and all posts on this blog are completely my thoughts and writings. I love DIY and Technology. So feel free to contact me for anything about this blog and don't forget to add a comment if this blog helped you! Thanks
View all posts by Ruchira

Last 5 posts by Ruchira

  • bagus

    hi…could you guide me on how to edit the code for multiple website…I plan to host 2 domain on my VPS


    • http://www.ruchirablog.com Ruchira

      if you add this code to main nginx.conf it will be effective for all of your wordpress sites! :)

      • bagus

        thank you for your reply…I’ll try it on my websites and update the result :)

  • Albi

    Hey Ruchira!

    I’ve followed this but upon lnmp restart I’m getting the following error:

    Reload Nginx configure…
    [emerg]: “location” directive is not allowed here in /usr/local/nginx/conf/nginx .conf:64
    configuration file /usr/local/nginx/conf/nginx.conf test failed

    I’m using the Debian version and had adjusted the paths accordingly, but this is beyond me. Any ideas how to fix this?

    • http://www.ruchirablog.com Ruchira

      looks like you have changed the nginx.conf file dont you?

      • Albi

        Well…yes, I did as the path to my nginx_error.log and access.log are slightly different in my box than the one you provided. Initially, I just edited the server names, but it was throwing up errors, so I changed the paths but that didn’t help either and I’m stuck.

        Not sure what you’re asking me since the purpose of this post was to change the nginx.conf file.

        I appreciate any insight into this as I’m not too familiar with server admin and am learning as much as I can.

  • Albi

    That’s right, I’m running your installer. It’s the best one I’ve come across thus far, for myself that is.

    • http://www.ruchirablog.com Ruchira

      and how the error log and access log paths are different in your vps if you are using my modified script? Thats not possible! Can you give me the path names in your box? I mean the paths that you got from original installation.

      And do not mess with additional stuff on nginx.conf if you are new to these. It will bring down your nginx instance when you reload or restart nginx.

      Best thing for now is do a complete reinstallation of LNMP0.5.1RB and modify nginx.conf carefully with only necessary things to work with supercache! And why dont you consider w3 total cache instead of supercache? Im using it with memcached and its running great and doesnt require nginx.conf mods to run.


      You can install memcached by

       apt-get install memcached

      and memcached php support is build in to LNMP0.5.1RB :)

  • Albi

    These are my actual paths:



    • http://www.ruchirablog.com Ruchira

      you dont have to change those! Just only change rewrites if you really want to use supercache instead of w3 total cache!

Previous post:

Next post: