<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Abhi&#039;s Weblog &#187; nginx</title>
	<atom:link href="http://abhinavsingh.com/blog/tag/nginx/feed/" rel="self" type="application/rss+xml" />
	<link>http://abhinavsingh.com/blog</link>
	<description>PHP, Memcached, XMPP and Web Development</description>
	<lastBuildDate>Mon, 27 Feb 2012 09:12:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Setting Nginx, PHP Fastcgi and XCache on a new Ubuntu</title>
		<link>http://abhinavsingh.com/blog/2010/04/setting-nginx-php-fastcgi-and-xcache-on-a-new-ubuntu/</link>
		<comments>http://abhinavsingh.com/blog/2010/04/setting-nginx-php-fastcgi-and-xcache-on-a-new-ubuntu/#comments</comments>
		<pubDate>Thu, 29 Apr 2010 13:38:04 +0000</pubDate>
		<dc:creator>Abhinav Singh</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[XCache]]></category>

		<guid isPermaLink="false">http://abhinavsingh.com/blog/?p=592</guid>
		<description><![CDATA[Recently, because of a mandatory VPS move I had an opportunity to migrate all my sites from apache to nginx. My old box was in a messy state and setting up a new box from scratch was always going to be fun. Here in this post, I will walk you through all the steps that [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fabhinavsingh.com%2Fblog%2F2010%2F04%2Fsetting-nginx-php-fastcgi-and-xcache-on-a-new-ubuntu%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fabhinavsingh.com%2Fblog%2F2010%2F04%2Fsetting-nginx-php-fastcgi-and-xcache-on-a-new-ubuntu%2F&amp;source=imoracle&amp;style=normal&amp;service=bit.ly&amp;service_api=R_f027b5a79a20a49b713f16282f1e0857&amp;hashtags=nginx,PHP,Ubuntu,XCache&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Recently, because of a mandatory VPS move I had an opportunity to migrate all my sites from apache to nginx. My old box was in a messy state and setting up a new box from scratch was always going to be fun. Here in this post, I will walk you through all the steps that helped me migrate seamlessly. Specially, how did I setup the new box ensuring zero downtime on the sites.</p>
<p><strong style="font-size:18px;"><u>Ensuring zero downtime while migration:</u></strong><br />
By the time I will pin up various pieces on my new vps box, I didn&#8217;t want my site visitors to see an under-maintenance page. To ensure zero downtime, I usually follow these steps:</p>
<ul>
<li>Setup the new vps with nginx, php-fastcgi, etc as described later in the post</li>
<li>To verify the setup on new vps, edit the local host file depending upon your operating system
<pre class="php" name="code">/private/etc/hosts (mac)
/etc/hosts (ubuntu)
C:\Windows\System32\drivers\etc\hosts (windows)
</pre>
<p>Go ahead and add the following host entry:</p>
<pre class="php" name="code">99.198.122.216 abhinavsingh.com</pre>
<p>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</li>
<li>Now, whenever I visit http://abhinavsingh.com 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.</li>
<li>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.</li>
</ul>
<p>To start with, just add up the required host setting on your local system.</p>
<p><strong style="font-size:18px;"><u>Installing Nginx and configuring vhosts:</u></strong><br />
Follow these steps to install nginx webserver:</p>
<ul>
<li>Update and upgrade apt-get and install nginx
<pre class="php" name="code">sudo apt-get update
sudo apt-get upgrade
sudo apt-get install nginx</pre>
</li>
<li>Configure vhost for nginx by creating a file <code>/etc/nginx/sites-available/mysite.com</code> as follows:
<pre class="php" name="code">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 / {
        }
}</pre>
</li>
<li>Enable vhost by creating a symlink as follows:
<pre class="php" name="code">cd /etc/nginx/sites-enabled
ln -s /etc/nginx/sites-available/mysite.com mysite.com
sudo /etc/init.d/nginx restart</pre>
</li>
<li>Assuming you have configured your local host file correctly, try visiting http://mysite.com and your browser will take it to the new vps</li>
</ul>
<p><strong style="font-size:18px;"><u>Setting up php-fastcgi and xcache:</u></strong><br />
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.</p>
<ul>
<li>sudo apt-get install php5-cgi php5-cli php5-xcache</li>
<li>Download <a href="http://gist.github.com/383557">php-fastcgi default config</a> and place it at <code>/etc/default/php-fastcgi</code></li>
<li>Download <a href="http://gist.github.com/383550">php-fastcgi init.d script</a> and place it at <code>/etc/init.d/php-fastcgi</code></li>
<li>Add php-fastcgi init.d as startup script
<pre class="php" name="code">update-rc.d -f php-fastcgi defaults</pre>
</li>
<li>Update following fields inside <code>/etc/php5/conf.d/xcache.ini</code>:
<pre class="php" name="code">xcache.admin.user = "admin"
xcache.admin.pass = "pass"
xcache.size  =  128M
xcache.count = 4</pre>
<p> xcache.count should ideally be equal to <code>cat /proc/cpuinfo |grep -c processor</code></li>
<li>Setup xcache admin interface:
<pre class="php" name="code">cd /var/www
ln -s /usr/share/xcache/admin xcache</pre>
</li>
<li>Update <code>/etc/php5/cgi/php.ini</code> as per your requirements and start php-fastcgi process
<pre class="php" name="code">sudo /etc/init.d/php-fastcgi start</pre>
</li>
<li>Visit xcache admin panel http://vps_ip_address/xcache</li>
</ul>
<p><strong style="font-size:18px"><u>Stitching php-fastcgi and nginx vhosts:</u></strong><br />
Now lets enable php for vhosts configured with nginx:</p>
<ul>
<li>Download <a href="http://gist.github.com/383588">nginx fastcgi param</a> config file and place it at <code>/etc/nginx/fastcgi.conf</code></li>
<li>Update /etc/nginx/sites-available/mysite.com with following config:
<pre class="php" name="code">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;
        }
</pre>
</li>
<li>Restart nginx and we are done</li>
</ul>
<p>Here we complete the vps setup and vhost configurations. Verify the new vps setup and once satisfied update site&#8217;s DNS settings. Another challenge involving migration from apache to nginx includes rewriting apache .htaccess rewrite rules for nginx. However, I will keep that for another post.</p>
<script type="text/javascript">var wordpress_toolbar_urls = ["http:\/\/api.tweetmeme.com\/share?url=http%3A%2F%2Fabhinavsingh.com%2Fblog%2F2010%2F04%2Fsetting-nginx-php-fastcgi-and-xcache-on-a-new-ubuntu%2F","http:\/\/gist.github.com\/383557","http:\/\/gist.github.com\/383550","http:\/\/gist.github.com\/383588"];var wordpress_toolbar_url = "http://abhinavsingh.com/blog/wp-content/plugins/wordpress-toolbar/toolbar.php";var wordpress_toolbar_oinw = "oinw";var wordpress_toolbar_hash = "aHR0cDovL2FiaGluYXZzaW5naC5jb20vYmxvZy8yMDEwLzA0L3NldHRpbmctbmdpbngtcGhwLWZhc3RjZ2ktYW5kLXhjYWNoZS1vbi1hLW5ldy11YnVudHUvPHdwdGI%2BU2V0dGluZyBOZ2lueCwgUEhQIEZhc3RjZ2kgYW5kIFhDYWNoZSBvbiBhIG5ldyBVYnVudHU8d3B0Yj5odHRwOi8vYWJoaW5hdnNpbmdoLmNvbS9ibG9nPHdwdGI%2BQWJoaSYjMDM5O3MgV2VibG9n";</script><ul class="related_post"><li><a href="http://abhinavsingh.com/blog/2009/01/memcached-and-n-things-you-can-do-with-it/" title="Memcached and &quot;N&quot; things you can do with it &#8211; Part 1">Memcached and &quot;N&quot; things you can do with it &#8211; Part 1</a> (19)</li><li><a href="http://abhinavsingh.com/blog/2008/07/how-to-get-started-with-web-development/" title="How to get started with web development?">How to get started with web development?</a> (9)</li><li><a href="http://abhinavsingh.com/blog/2008/05/how-to-configure-ubuntu-and-lamp-on-windows/" title="How to configure Ubuntu and LAMP on Windows">How to configure Ubuntu and LAMP on Windows</a> (4)</li><li><a href="http://abhinavsingh.com/blog/2010/08/php-code-setup-and-demo-of-jaxl-boshchat-application/" title="PHP Code, Setup and Demo of Jaxl boshchat application">PHP Code, Setup and Demo of Jaxl boshchat application</a> (80)</li><li><a href="http://abhinavsingh.com/blog/2010/08/releasing-jaxl-2-0-object-oriented-xmpp-framework-in-php/" title="Releasing Jaxl 2.0 &#8211; Object oriented XMPP framework in PHP">Releasing Jaxl 2.0 &#8211; Object oriented XMPP framework in PHP</a> (6)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://abhinavsingh.com/blog/2010/04/setting-nginx-php-fastcgi-and-xcache-on-a-new-ubuntu/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

