So, I started this series with backing up my website’s content and database file and copying them over to a temporary Amazon Cloud server. I then rebuilt my primary server and restored the web content and database file. Part One of this series can be viewed @ https://www.liberiangeek.net/2013/06/my-personal-experience-switching-from-apache-to-nginx-web-server-to-host-wordpress/

This is Part Two of this series and I am going to describe in detail my personal experience switching from Apache Webserver to Nginx. Many people have said and written nice things about Nginx so I decided to give it a try.

Now, if you don’t have any good reason to switch, please don’t do it. Apache is great and will handle most websites, including WordPress. This tutorial can be applied to Ubuntu, CentOS and other Linux operating systems. Some of the commands might have to be changed to work with your OS.

If you’re ready, let’s get started. In Part One, I told you that I backed-up my website’s content as well as the database for it. In this post, I am going show you what I did after restoring the content to the primary server.

 

Work Began On The Primary Server

When the data were restored, I created an empty (blank) database matching the name of the previous one. To do that, I ran the commands below to sign into the MySQL Database Server.
mysql -u root –p
After signing in, created a new database by running the commands below
create database wpdatabase;
After creating the database, I created a WordPress database user that matches what’s in WordPress’ wp-config.php file.
create user wpuser;
Next, I grant all permission on the wpdatabase to the WordPress database user and set the password to use. This should match what you have in your wp-config.php file.
grant all on wpdatabase.* to 'wpuser'@'localhost' identified by 'password';
I exited MySQL Database. Next, I uploaded the backed-up database content into the empty WordPress database created by running the commands below.
mysql -u root -p wpdatabase < wpdatabase.sql
After that, I was done with the MySQL part of the process.

Working With The Web Content

After setting up and configuring the database I moved over to the website content. In Ubuntu, Nginx puts its configuration files in this location: /etc/nginx.

So I copied the default site in /etc/nginx/available-sites and created a new one called wordpress to match what I had before.
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/wordpress
Next, I opened the file with the vi command

sudo vi /etc/nginx/sites-available/wordpress

 

Then I replaced everything in it with the config below.

server {
listen 80;
root /var/www/wordpress;
index index.php index.html index.htm;
server_name liberiangeek.net;
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
location ~ /\.ht {
deny all;
}

Next, I removed Nginx’s default config file for the site.
sudo rm /etc/nginx/sites-enabled/default
Then I extracted the web content to the /var/www/wordpress directory just like the way it was.
sudo tar -xvf webcontent.tar -C /var/www/wordpress
After that, I enabled the new site by running the commands below.
sudo ln -s /etc/nginx/sites-available/wordpress /etc/nginx/sites-enabled/wordpress
After restarting MySQL, Nginx and PHP5-FPM and everything seems to work.

sudo service nginx restart
sudo service php5-fpm restart
sudo service mysql restart

This is just the basic config for Nginx. There are many more settings to choose from and in the last post of this series, I will post my complete config file. It has a lot more than what’s listed above. The above is just to get your site started.

For Ubuntu users, you may have to create the root location.
sudo mkdir -p /var/www/wordpress
Then give ownership to the webserver (Nginx).
sudo chown -R www-data:www-data /var/www/wordpress
Until then, enjoy!