In this brief guide, I am going to show you what I did and the steps I took while switching from Apache to Nginx Web Server. This is going to be a long post, so I’m dividing it into three parts. And if you’re reading this then you probably know a thing or two about Apache, Nginx and what webservers are so I’m going to skip to the main details and forget about defining what a webserver is.

I still don’t know why I switched other than that Nginx is fast, stable and light on system’s resources when compared to Apache. I started with Apache and didn’t have any issue with hosting WordPress, so switching to Nginx was probably an adventure I just wanted to take.

If you don’t have any good reason why you should switch to Nginx, don’t do it. On the other hand, if you feel Nginx is going to serve you better than Apache, then jump onboard and let’s dive into it.

The first thing you must do before going any further is to backup your website’s database and content. This is true in all cases and it was the first thing I did. To backup your web content, run the commands  below.

sudo tar -cvf webcontent.tar /var/www/wordpress/

 

Next, run the commands below to backup your website’s database. You’ll be prompted for the root password of the database server.

sudo mysqldump -u root -p wpdatabase > wpdatabase.sql

 

You’ll want to change the name of the database and web directory to match your settings. After running the commands to backup your files, copy them to a safe location. In my case, I created a temporary Amazon EC2 Cloud Server, then moved the backed-up content there using scp commands. I did that because I had to rebuild my server. You don’t have to do this if your server is fine.

To limit down time, I installed Apache and MySQL Database Server on the Amazon EC2 Cloud server. Next I configured the environment like mine, then extracted the web content and uploaded the database. When everything seems to be working, I went over to my domain provider and re-pointed the DNS host IP to Amazon server. After a few hours, almost traffic were re-redirected to Amazon without users noticing.

Again, you don’t have to do all of that if you don’t intend to rebuild your server. When all web traffic were going to Amazon, I took down my original server, and started rebuilding. After few hours, it was don’t and the real work began.

Work Began On The New Server

The first thing I had to do was to install MySQL Server. To do that in Ubuntu run the commands below.

sudo apt-get install mysql-server

 

Next, I installed Nginx Web Server.

sudo apt-get install nginx

 

Finally, I installed PHP5 with its components by running the commands below.

sudo apt-get install php5 php5-cli php5-common php5-xmlrpc php5-mysql php5-gd php5-fpm 

 

After installing the above packages, I created a new archive of the web content and database on the Amazon server. Then copy archives over to my primary server using the SCP commands below from my primary server.

scp richard@amazon_ip_address:/backup/webcontent.tar /root/wordpress/restore

I did the same for the database backup as well. The /root/wordpress/restore is a directory on my primary server.

Everything above happened on the first day. On day two, I began restoring the database, WordPress content and configuring Nginx.

I will continue with Day 2 in our next post.

Enjoy!