If you have ever had any experience working with web servers you may already be familiar with two of the most widely used web servers Apache and IIS. But one web server which you may not have heard of yet is an up and coming web server called Nginx (or engine-x). Nginx is the third most widely used web server and is rapidly becoming the web server of choice for many systems administrators.
Nginx addresses a lot of performance issues many websites with high loads can come across by using an asynchronous event-driven approach instead of a process-oriented approach which is used by web servers like Apache. Meaning Nginx is able to handle more requests per second than any other web server.
Nginx also comes standard with load balancing and a reverse caching proxy unlike Apache which require separate modules to be installed. Additionally Nginx also comes with the ability to act as a mail proxy and stream multiple types of video.
If you’re still not convinced here are a few popular websites using Nginx:
Wordpress.com
YellowPages.com
WhitePages.com
Hulu.com
4chan.org
SourceForge.net
TorrentReactor.net
Installating Nginx
The first thing we need to do is install nginx using yum.
yum –y install nginx
Next you will need to set nginx to run on startup using chkconfig and then start the nginx service.
chkconfig nginx on
service nginx start
Once you have started Nginx you should be greeted by the default Nginx page by visiting your servers IP in your web browser (eg http://127.0.0.1).
Installing PHP and MySQL
As with most other web servers Nginx supports PHP and MySQL but there are a few things that need to be installed and configured first.
First you will need to install PHP and MySQL server. You can also install modules at this point depending on which ones you will need for your websites. Two of the essential modules you will need though are the mysql and the command line module. You will also need to install lighthttpd fastcgi in order to pass PHP through CGI.
yum install mysql mysql-server php php-mysql php-cli lighttpd-fastcgi
Next you will need to start the MySQL server service and set it to run on startup.
service mysql start
chkconfig mysql on
You will also need to setup a password for MySQL if you haven’t already done so.
mysqladmin –u root password
Next you will need to edit the nginx configuration file located at /etc/nginx/nginx.conf
You will firstly need to uncomment all the lines under “pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000”.
{codecitation}location ~ .php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
Optionally you can also uncomment these lines to unrestrict .htaccess usage.
location ~ /.ht {
deny all;
}
You can also add index.php to the index to allow index.php to run as the default page.
location / {
root /usr/share/nginx/html;
index index.html index.htm index.php;
}{/codecitation}
You will also need to edit the /etc/rc.local file to pass PHP files through to CGI so that they can be used by nginx. Include the following all as one line:
{codecitation}spawn-fcgi -a 127.0.0.1 -p 9000 -u nginx -g nginx -f php-cgi -P /var/run/fastcgi-php.pid{/codecitation}
Once you have saved the rc.local file, you will need to reboot in order for rc.local to take effect. You can then also test out the settings to see if everything is working correctly by creating a PHP file in /usr/share/nginx/html
{codecitation}<?php
phpinfo();
?>{/codecitation}
If the PHP information page shows up correctly then your installation should be working. That’s all there is to it, you should now be able to use both PHP and MySQL with Nginx.
{module user9-footer}