Moving an Apache PHP Mysql website to NginX PHP in 30 minutes Part 2

If you havent seen Part 1 of this article then you can see it here

MAKING PHP WORK WITH NGINX

For PHP to work with NginX it has to be installed as Fast CGI Process Manager (FPM). To know more about PHP-FPM check out its website  http://php-fpm.org/

Debian/Ubuntu

sudo apt-get install php5-cli php5-common php5-suhosin

sudo apt-get install php5-fpm php5-cgi

CentOS

sudo yum install php php-fpm php-gd php-mysql php-mbstring php-xml php-mcrypt php-xmlrpc

FreeBSD

You can use portmaster or make to reinstall PHP with the “Build FPM version (experimental)”option checked.

CONFIGURING VIRTUAL HOSTS IN NGINX

Given below is the location of the configuration file for NginX.

Debian/Ubuntu

/etc/nginx/nginx.conf

CentOS

/etc/nginx/nginx.conf

FreeBSD

/usr/local/etc/nginx/nginx.conf

Once you open the conf file in an editor, adding multiple virtual hosts is easy. For each virtual host add a server block as shown in the example below:

server {
	listen  80;
        server_name  site.com www.site.com;

        access_log  /websites/site.com/logs/access.log  main;

        location / {
            root   /websites/site.com/http;
            index  index.php index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/local/www/nginx-dist;
        }

       # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /websites/site.com/http$fastcgi_script_name;
            include        fastcgi_params;
        }

        location ~ /\.ht {
            deny  all;
        }
}

Be sure to change the paths to your actual path instead of /websites/site.com.
The server tag will come after the http tag in the conf tag.

At this stage you can restart NginX and your existing php site will start working as it was working in Apache.

HANDLING HTACCESS

NginX does not provide support for .htaccess files so if you website uses .htaccess then the rules in .htaccess can be put into the config file.

An example is given below:

server {
    listen  80;
        server_name  site.com;
 
        access_log  /var/websites/site.com/logs/access.log  ;
 
        location / {
            root   /var/websites/site.com;
            index  index.php index.html index.htm;

            rewrite ^/([^/\.]+)/([^/\.]+)\.([0-9]+)\.$ /out/get-phone-deal.php?u=deal&deal_id=$3;
            if ($http_host ~* “^site\.com$”){
                rewrite ^(.*)$ http://www.site.com/$1 redirect;
            }
            if ($http_host ~* “^whichbroadband\.ie$”){
                rewrite ^(.*)$ http://www.site2.com/$1 redirect;
            }
            if ($http_host ~* “^mobilephonedeals\.ie$”){
                rewrite ^(.*)$ http://www.site3.com/$1 redirect;
            }
           
        }
 
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/local/www/nginx-dist;
        }

        #htaccess rules start————————-
       
        error_page 404 /corporate/sitemap.php?code=404;
        location /Privacy {
            rewrite ^/Privacy-Policy$ /corporate/content.php?privacy=1;
        }

        location /Terms {
            rewrite ^/Terms-And-Conditions$ /corporate/content.php?terms=1;
        }

        location /About {
            rewrite ^/About-Us$ /corporate/content.php?about=1;
        }
       
        location = /Cookies {
            rewrite ^(.*)$ /corporate/content.php?cookie=1;
        }
       
        location /Contact {
            rewrite ^/Contact-Us$ /corporate/contactUs.php;
        }        

        location /manufacturer {
            rewrite ^/manufacturer/([^/\.]+)/([0-9]+)$ /phone/manufacturer.php?id=$2;
        }

        location /phone {
            rewrite ^/phone/([^/\.]+)/([0-9]+)$ /phone/phone.php?id=$2;
        }
       
        location /network {
            rewrite ^/network/([^/\.]+)/([0-9]+)$ /phone/network.php?id=$2;
        }       

        location /Sim {
            rewrite ^/Sim-Only$ /deals/sim-only.php;
        }
       
        location /Mobile {
            rewrite ^/Mobile-Broadband$ /deals/mobileBroadband.php;
        }
       
        location /recycl {
            rewrite ^/recycle /recycle.php;
        }

        #htaccess rules end—————————

 
       # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /var/websites/site.com/$fastcgi_script_name;
            include        fastcgi_params;
        }
 
        location ~ /\.ht {
            deny  all;
        }
}

As you can see the configuration rules for redirection are very similar to that of .htaccess. A great online utility to convert .htaccess to NginX conf is available at http://winginx.com/htaccess .

CONCLUSION

So thats it. A painless way to move your php website from Apache to NginX.

1 Trackback / Pingback

  1. Truelogic Blog - Moving an Apache PHP Mysql website to NginX PHP in 30 minutes Part 1

Leave a Reply

Your email address will not be published.


*