Installing WordPress + Nginx Server (Minimal Configuration)

easyengine (ee) note: If you are using easyengine, you can accomplish everything in this article using following commands:

ee system install nginx
ee site create example.com --wp

If this is your first Nginx setup, most likely you are moving from Apache to a WordPress-Nginx setup. I will cover moving process in next article.

In this article we will see fresh WordPress installation and its configuration with Nginx.

Installing Fresh WordPress

Download and unzip latest WordPress

Following will download and extract latest copy of WordPress for example.com domain

mkdir -p /var/www/example.com/htdocs/ /var/www/example.com/logs/
cd /var/www/example.com/htdocs/
wget http://wordpress.org/latest.tar.gz
tar --strip-components=1 -xvf latest.tar.gz
rm latest.tar.gz
chown -R www-data:www-data /var/www/example.com/

Create a database for new WordPress

Following command will create a fresh database for the new WordPress you are installing.

mysql -u USERNAME -pPASSWORD -e 'create database DB_NAME'

In above command, replace USERNAME, PASSWORD & DB_NAME with appropriate values. You will also need these values during WordPress installation wizard.

You can open example.com in browser to complete WordPress installation as soon as we finish next step!

Simplest Nginx Configuration for WordPress

I am posting below a simple Nginx configuration for WordPress.

It doesn’t have support for any caching plugin or things like SSL, WordPress multisite, etc. It also uses Nginx’s default access.log & error.log files.

Create Nginx-config file for example.com

vim /etc/nginx/sites-available/example.com

Add following lines to it:

server {
        server_name example.com www.example.com;

	access_log   /var/log/nginx/example.com.access.log;
	error_log    /var/log/nginx/example.com.error.log;

        root /var/www/example.com/htdocs;
        index index.php;

        location / {
                try_files $uri $uri/ /index.php?$args; 
        }

        location ~ \.php$ {
                try_files $uri =404;
                include fastcgi_params;
                fastcgi_pass 127.0.0.1:9000;
        }
}

Next, add example.com to Nginx sites-enabled folder:

ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

Then test Nginx-configuration before you ask Nginx to load it:

nginx -t

It will show an output like below:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

It you see any error in output, most likely you have made mistakes while creating Nginx config files. Try to fix them yourself first. Help is always available here.

Finally reload Nginx configuration:

service nginx reload

Create symbolic links for example.com log files:

This will be helpful (as explained before)

ln -s /var/log/nginx/example.com.access.log /var/www/example.com/logs/access.log
ln -s /var/log/nginx/example.com.error.log /var/www/example.com/logs/error.log

Complete WordPress installation:

You can now open example.com in your favorite web-browser. You will see a WordPress installation wizard.

What’s Next…

In next chapter, we will explore different WordPress configurations for Multisite, caching plugins, SSL, etc. You can also find the complete list of WordPress-Nginx tutorials here.

44 thoughts on “Installing WordPress + Nginx Server (Minimal Configuration)

  1. Create symbolic links for example.com log files:

    ln -s /var/log/nginx/example.com.access.log /var/www/example.com/log/access.log
    ln -s /var/log/nginx/example.com.error.log /var/www/example.com/log/error.log

  2. Hello Rahul,

    I can get this setup working fine – even with W3 Total Cache, but every time I try to move my Apache hosted wordpress database to the new Nginx setup it stops working. Is there something special about moving an Apache wordpress install that I’m missing?

  3. after finished follow this guide, and set the domain A record, why there are no wordpress instalation page when i open my domain?
    there are only shown:
    It works!
    This is the default web page for this server.
    The web server software is running but no content has been added, yet.
    –> when i enter my vps ip address in the browser, there are the same page appear:
    It works!
    This is the default web page for this server.
    The web server software is running but no content has been added, yet.
    –> why it’s happen???

    1. @skidnew

      Sorry for delay in reply. Most likely your Nginx configuration is not correctly loaded.

      Please check if you have “enabled” site-config for WordPress site. Also hit SHIFT+CTRL+REFRESH so that browser’s local cache may get flushed (in case issue is with local-cache)

  4. I am having the same problem as skidnew. Domain A record was set up pointing to the IP address of my VPS. I’ve also added this domain to /etc/hosts file but I guess my domain still redirects to the /var/www/ catalog.

    I am trying to figure it out on my own but any tip would be appreciated.

    Thanks,
    Przemek

  5. I’ve figured out what I did wrong. It appeared that I did not start nginx at all so I’ve run the command below:
    sudo service nginx start
    I’ve also removed apache before (it was preinstalled on my VPS) but it was probably not necessary, right? Anyway the message:

    It works!
    This is the default web page for this server.
    The web server software is running but no content has been added, yet.
    was from apache not from nginx.

    So on my end it is solved now. This series of tutorials are really helpful and easy to get through. Perhaps it is obvious that nginx needs to be started but I think it would be worth mentioning that in this tutorial: http://rtcamp.com/tutorials/installing-phpapc-mysql-postfix-nginx-and-wordpress-on-ubuntu/ or perhaps it is somewhere there but I did not noticed.

    Thanks 🙂
    Przemek

      1. Just a small update. Rather than restarting nginx, try using service nginx reload more frequently.

        Restarting can disrupt service for users currently active on site.

        1. thank you, it’s work for me after i restart my nginx.
          One problem, i want to install a plugin, but the plugin need to write some text in the .htaccess
          As i understood, Rahul said that with nginx there are no need .htaccess
          So, what can i do??

  6. Thanks for the hint. I will be careful with that.

    I did not expect the difference in speed will be so noticeable after I replaced apache with nginx. Especially because the website is still in development phase (it is going to be a store based on woocommerce) and it is not yet published so only a few people have access to it. It might be also a matter of a fresh install but on the other hand I’ve already re installed it once on apache. Also both database and all files are the same. Perhaps the reason is in my VPS. When I run my website locally the speed was always fine (but of course it is always faster locally).

    I am surprised because after reading a few articles about nginx I got an impression that the difference can be noticed only on high traffic websites.

    Anyway I’m happy I gave it a try 🙂 I will go through the rest of your series as well.

    Thanks 🙂
    Przemek

    1. We are also using woocommerce here. 🙂

      I have some extra nginx config rules for woocommerce. They are useful when using Nginx’s fastcgi_cache. I will publish them soon as its still work in progress. 🙂

      1. Thanks for the advice about the server, but since the website will be strictly targeted on polish users I will stick to the servers in Poland. But yes I will be looking for something else that I have now.

        1. @Przemek

          Good point. But I will still recommend finding a good dedicated server in or around your country.

          There are many companies which provides dedicated server at low prices. They just don’t have support-team like big companies. But with Nginx we are mostly on our own, so why pay extra for support that we may not need anyway! Just my opinion.

  7. I noticed that when you create the htdocs directory you also create a logs directory at /var/www/example.com/logs/. Then in the Nginx virtual host configuration you set the log directory to /var/log/nginx/. By default /var/log/nginx should be created when Nginx is installed, so you should not need to create a directory to store the logs. Also it is definitely not a good idea to store your web servers logs in a publicly accessible directory. If you do your system can easily become an easier target for someone to attack.

    1. Okay it makes sense because you created a separate htdocs directory. But you should change the logging directives in the configuration file then.

  8. I want to setup WordPress on a sub directory. Let’s say example.com/blog. What do I need to do? Do I need different rewrite rules?

    1. There are many ways. If you have a WordPress or some other app in root-dir (at example.com/) then following may work:

      Add lines:

      location /blog/ {
                      try_files $uri $uri/ /blog/index.php?$args; 
              }
      

      This will go in nginx config for root-domain. Assuming you have a PHP app or may be WordPress in webroot-dir.

      All you need to do is change PATH in few places.

  9. There’s rails unicorn app in the root. How ever I managed to get it working. Pretty links are ok. But .css .js and images files are giving 403 errors. Permissions are ok. I guess static files are going through fastcgi. Any ideas?

    1. I am not sure how your location / { } is coded. But you can try something like:

      location / {
                      try_files $uri $uri/ /blog/$uri /blog/$uri/ /index.php?$args; 
              }
      

      If you have a dedicated CSS/JS block, you will need to edit try_files in that section. May be you can use our free support forum – http://rtcamp.com/support/forum/wordpress-nginx/

      Send your complete nginx config and I will have a look into it.

        1. We are maintaing almost 10+ configs here. Some tutorial contains try_files $uri =404 while some contains try_files $uri /index.php. Both are safe.

          try_files inherently check if a file exist for real before passing it to fastcgi gateway. Though on this site we use try_files $uri /index.php. You can try hacking this site as we allow uploads in our forum. 😉

      1. … but… you do realize, that:

        try_files $uri =404;

        … does not exist within this tutorial… as of this date; 2013/08/14. ???

        So the response makes little sense and does not address the security point at all.

        Furthermore, if one has not addressed the PHP PATHINFO settings, the settings within this tutorial, … try_files $uri /index.php; … could result in the very security vulnerability being discussed afflicting whomever uses such a config without addressing the PATHINFO situation.

        Be responsible and fix this stuff.

        1. Do u know how try_files works?

          Yep. Tutorial is not consistent with my comment above but try_files $uri /index.php and try_files $uri =404 both are SAFE.

          In case of try_files $uri /index.php rather than serving 404 page for hacking attempt, it shows index.php which means wordpress homepage in this tutorial’s context.

          So next time, try to hack my stuff for real before pretending to be security expert!

  10. Thank you, guys! but I got:

    ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’

    when create database for new wordpress. Any ideas?

  11. Hi,

    I installed wordpress using this tutorial. I installed vsftp on my server. How do I add user to the ftp so I can edit some files from my wordpress themes. Thank you.

Comments are closed on this article.
Please use our free support forum for support and discussions.