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://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!

Frequently Asked Questions

What are the steps to switch from Apache to Nginx for hosting WordPress?

To switch from Apache to Nginx for hosting WordPress, start by backing up your website's content and database, copying them to a temporary server, rebuilding your primary server, and restoring the web content and database file.

How can I create a new database in MySQL when switching from Apache to Nginx?

To create a new database in MySQL, sign into the MySQL Database Server, then run the command 'create database wpdatabase;' to create a new database matching the name of the previous one.

What commands can I use to create a Wordpress database user when switching to Nginx?

To create a Wordpress database user, run the command 'create user wpuser;' after creating the database. Next, grant all permissions on the database to the user and set the password to match what's in your wp-config.php file.

Which Linux operating systems can apply the tutorial for switching from Apache to Nginx?

The tutorial for switching from Apache to Nginx can be applied to Ubuntu, CentOS, and other Linux operating systems. Some commands may need to be adjusted based on the specific OS you are using.

How do I restore the content to the primary server when switching to Nginx?

To restore the content to the primary server when switching to Nginx, create an empty database matching the previous one, create a Wordpress database user, grant permissions, and set the password to match wp-config.php.

What should I consider before switching from Apache to Nginx for hosting WordPress?

Before switching from Apache to Nginx, consider if you have a good reason to do so. Apache is great and can handle most websites, including WordPress. Only switch if you have specific requirements that Nginx can better address.

Where can I find Part One of the series on switching from Apache to Nginx?

Part One of the series on switching from Apache to Nginx can be viewed at https://liberiangeek.net/2013/06/my-personal-experience-switching-from-apache-to-nginx-web-server-to-host-wordpress/

What are some key considerations when transitioning from Apache to Nginx for WordPress hosting?

When transitioning from Apache to Nginx for WordPress hosting, ensure to backup your website's content and database, rebuild your primary server, and carefully follow the step-by-step process to avoid any data loss or errors.