Apache Troubleshooting and Nginx Rescue

The last weeks my website was not available due to some problems with my Apache server. When I opened my website it took me more than 5 minutes to get any answer. I first thought my Virtual Server had a defect, so I restarted it. It didn’t help. I had to investigate this problem a little bit more.

Apache Restarts and High CPU Usage

After trying again and again I realized that when I got an answer, it was only some unformatted text like from the 90’s. Maybe WordPress was the reason for that? I’ve added a simple index.html file instead of the regular index.php from WordPress which unfortunately didn’t solve the problem. The next thing I’ve tried was restarting Apache. The result? Nothing changed. That was frustrating.

I’ve checked the CPU usage just to make sure that everything was fine. The CPU usage was very high because of Apache processes which was unusual from my point of view, because the traffic on my website is very low. I did some research on that and found some Stack Overflow threads where the same issue was described. They suggested to decrease Apache workers (didn’t work) and some other tweaks which didn’t work as well. I needed another solution, because I wanted my website to be online again, so I started searching for Nginx.

Solving the Issue with Nginx

Before you do anything you should do a backup of your files. If you forgot how to do this or you’re not a Linux pro, here is a short example of how to do a backup of MySQL and WordPress:

# the shell will prompt you for the password of [user]
$ mysqldump -u [user] -p [database] > /your/folder/for/backup.sql

# backup wordpress folder
$ tar -cfv /your/folder/for/wordpress/backup.tar.gz /your/folder/or/file

More information about MySQL backups: http://www.thegeekstuff.com/2008/09/backup-and-restore-mysql-database-using-mysqldump/
More information about archiving: http://www.tecmint.com/18-tar-command-examples-in-linux/

Now I was able to move forward and found a nice tutorial about how to install Nginx, MySQL and PHP on CentOS which helped me a lot! It explains very nicely what you have to do. In my case I only had to adopt a Nginx configuration url to my WordPress folder. After starting Nginx I was quite happy that everything worked as expected. But soon I realized that it was not everything working, e.g. WordPress permalinks and some plugins didn’t work. The latter was very easy to explain: while changing to Nginx I’ve also did a manual update of WordPress and forgot to copy the plugins from my backup (thanks to my backup everything went well after copying them back). The WordPress permalinks issue was a little bit different to explain: if you use Apache and WordPress and want to use permalinks, WordPress adds automatically an .htaccess file to your folder in order to setup permalinks for Apache. Nginx doesn’t have the feature for that. You have to change the configuration file by yourself. The following blog post explains it: http://nginxlibrary.com/wordpress-permalinks/

Summary

The whole process took me about 3-4 hours which is quite a small time frame compared to the days my website was not available. (The reason is that I realized it very late and I also had to study for an important exam) Now I’m quite happy that everything works fine again, but I would like to know the real issue why Apache wasn’t working. Maybe I will investigate it again.

What I’ve learned from this story is that I will create a small script (or use some existing software) to smoke test my website and get notified if it’s not online!