Wednesday, June 12, 2013

Nginx, PHP Fastcgi and XCache

Setup the new vps with nginx, php-fastcgi, etc as described later in the post


To verify the setup on new vps, edit the local host file depending upon your operating system

view plaincopy to clipboardprint?

/private/etc/hosts (mac)

/etc/hosts (ubuntu)

C:\Windows\System32\drivers\etc\hosts (windows)

Go ahead and add the following host entry:


view plaincopy to clipboardprint?

99.198.122.216 nginx.oldfords.cz

where, 99.198.122.216 is my new vps ip address and abhinavsingh.com is a vhost configured to handle by nginx on my new vps


Now, whenever I visit http://nginx.oldfords.cz in my browser, my local machine will point it to my new vps box. However, all other visitors will still be served by apache on old vps.

After verifying the setup, I simply remove previously added host setting on my local box and update the DNS settings for my site at godaddy.

To start with, just add up the required host setting on your local system.


Installing Nginx and configuring vhosts:

Follow these steps to install nginx webserver:


Update and upgrade apt-get and install nginx

view plaincopy to clipboardprint?

sudo apt-get update

sudo apt-get upgrade

sudo apt-get install nginx

Configure vhost for nginx by creating a file /etc/nginx/sites-available/mysite.com as follows:

view plaincopy to clipboardprint?

server {

listen 80;

server_name mysite.com;

access_log /var/log/nginx/mysite.access.log;

root /var/www/mysite;

index index.php index.html index.htm;

location / {

}

}

Enable vhost by creating a symlink as follows:

view plaincopy to clipboardprint?

cd /etc/nginx/sites-enabled

ln -s /etc/nginx/sites-available/mysite.com mysite.com

sudo /etc/init.d/nginx restart

Assuming you have configured your local host file correctly, try visiting http://mysite.com and your browser will take it to the new vps

Setting up php-fastcgi and xcache:

Here are the steps to configure php-fastcgi and how to ensure php-fastcgi is up and running even after system reboot. We will also configure xcache for better performance.


sudo apt-get install php5-cgi php5-cli php5-xcache

Download php-fastcgi default config and place it at /etc/default/php-fastcgi

Download php-fastcgi init.d script and place it at /etc/init.d/php-fastcgi

Add php-fastcgi init.d as startup script

view plaincopy to clipboardprint?

update-rc.d -f php-fastcgi defaults

Update following fields inside /etc/php5/conf.d/xcache.ini:

view plaincopy to clipboardprint?

xcache.admin.user = “admin”

xcache.admin.pass = “pass”

xcache.size = 128M

xcache.count = 4

xcache.count should ideally be equal to cat /proc/cpuinfo |grep -c processor


Setup xcache admin interface:

view plaincopy to clipboardprint?

cd /var/www

ln -s /usr/share/xcache/admin xcache

Update /etc/php5/cgi/php.ini as per your requirements and start php-fastcgi process

view plaincopy to clipboardprint?

sudo /etc/init.d/php-fastcgi start

Visit xcache admin panel http://vps_ip_address/xcache

Stitching php-fastcgi and nginx vhosts:

Now lets enable php for vhosts configured with nginx:


Download nginx fastcgi param config file and place it at /etc/nginx/fastcgi.conf

Update /etc/nginx/sites-available/mysite.com with following config:

view plaincopy to clipboardprint?

location ~ \.php$ {

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME /var/www/mysite$fastcgi_script_name;

include /etc/nginx/fastcgi.conf;

}

Restart nginx and we are done

Here we complete the vps setup and vhost configurations. Verify the new vps setup and once satisfied update site’s DNS settings.



Nginx, PHP Fastcgi and XCache

No comments:

Post a Comment