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.
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
@Mitesh
Thanks for correction. Updated post.
There was also another correction (
log
tologs
folder)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?
Hi Nick,
If your nginx-server works fine without W3-Total-Cache then issue could be in database config. Please try using nginx site without w3-total-cache.
We follow this process to move sites from Apache to Nginx – https://rtcamp.com/tutorials/moving-wordpress-to-new-server-faster/
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???
@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)
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
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:
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
@Przemek
Glad to know your problem is fixed. I faced similar issue sometime too. 😉
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.
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??
Hey sorry for late reply.
Always convert
.htaccess
rules to equivalent nginx rules. Nginx can’t read from.htaccess
file.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
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. 🙂
Also, if you are moving to Nginx, you won’t need cPanel, Plesk and all those stuff. You will be mostly on your own.
You may try a dedicated server from http://servercraft.co/dedicated-servers/ .
We are using there 32-core, 32-GB RAM dedicated server here.
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.
@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.
That’s fantastic 🙂 I’ve already signed up to your newsletter.
Thanks
Hi, after following the steps here, I get server not found. Here are the nginx.conf and sites-available/idu.host paste
Angela
@Angela
Couldn’t find your paste? I hope your issue is sovled by now.
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.
Okay it makes sense because you created a separate htdocs directory. But you should change the logging directives in the configuration file then.
Lines:
take care of logging as per our WordPress-Nginx setup conventions documented here.
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?
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:
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.
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?
I am not sure how your
location / { }
is coded. But you can try something like: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.
Thanks Rahul,
Your help is much appreciated. I tried adding suggested location block to config file, but it didn’t change anything. I’ve posted my entire config file here,
http://rtcamp.com/support/topic/wordpress-on-a-subdirectory-with-rails-unicorn-app-in-the-root/
Please take a look at it
Thanks
Have you seen https://nealpoole.com/blog/2011/04/setting-up-php-fastcgi-and-nginx-dont-trust-the-tutorials-check-your-configuration/ re: security of the php-fpm setup?
I used a few of your blogs to get caching stuff sorted but this seems a bit of a gap.
@Tom
Yes. I have seen that.
This line
try_files $uri =404;
takes care of security threat mentioned by that article.It was there all along? I must have not spotted it, sorry.
We are maintaing almost 10+ configs here. Some tutorial contains
try_files $uri =404
while some containstry_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 usetry_files $uri /index.php
. You can try hacking this site as we allow uploads in our forum. 😉… 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.
Do u know how try_files works?
Yep. Tutorial is not consistent with my comment above but
try_files $uri /index.php
andtry_files $uri =404
both are SAFE.In case of
try_files $uri /index.php
rather than serving 404 page for hacking attempt, it showsindex.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!
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?
Check if mysql is running. Check path of socket in mysql config.
There are many possibilities!
If its fresh setup, you may switch to http://rtcamp.com/easyengine
Tell me, please. How to upload files to root folder, like /var/www/example.com/htdocs/? Via FTP?
You can login via root and then run
passwd www-data
(assuming you have installed everythin by using https://rtcamp.com/tutorials/linux/ubuntu-php-mysql-nginx-postfix/ or http://rtcamp.com/easyengine )Above command will ask you to choose password. Choose any password you like.
Then use a SFTP client. SFTP is on port 22. FTP is not installed and not needed. SFTP is better and secure.
There are many free SFTP client available for every OS.
Thanks for the answer.
I’m using easyengine. How can I make access to each site by own SFTP login and password, not only by www-data user?
This is not possible using EasyEngine for now. You need to install something like ProFTP http://www.proftpd.org/ and use path-based filters.
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.
I will recommend using `www-data` user with SFTP/SSH.
Run `passwd www-data` command to set passwod for this user.