<?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>John Yerhot - Weblog &#187; FCGI</title>
	<atom:link href="http://www.johnyerhot.com/tag/fcgi/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.johnyerhot.com</link>
	<description>im in ur computrz makin castz</description>
	<lastBuildDate>Tue, 29 Jun 2010 04:32:50 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to: Nginx, FCGI, PHP, MySQL, Ruby On Rails, Rewrite</title>
		<link>http://www.johnyerhot.com/2008/02/12/how-to-nginx-fcgi-php-mysql-ruby-on-rails-rewrite-vhosts/</link>
		<comments>http://www.johnyerhot.com/2008/02/12/how-to-nginx-fcgi-php-mysql-ruby-on-rails-rewrite-vhosts/#comments</comments>
		<pubDate>Tue, 12 Feb 2008 17:25:56 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Ruby On Rails]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[FCGI]]></category>
		<category><![CDATA[Gutsy]]></category>
		<category><![CDATA[How to]]></category>
		<category><![CDATA[MYSQL]]></category>
		<category><![CDATA[Rewrite]]></category>
		<category><![CDATA[Ubuntu 7.10]]></category>

		<guid isPermaLink="false">http://www.johnyerhot.com/2008/02/12/how-to-nginx-fcgi-php-mysql-ruby-on-rails-rewrite-vhosts/</guid>
		<description><![CDATA[
In a previous post I talked about how I just set up my new Slicehost account using Nginx to power this blog (Wordpress &#8211; PHP) and some Rails Apps.  Here&#8217;s a mini how to on how I got the whole thing working, as its not quite as easy as it sounds.
First though &#8211; Why [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.john.yerhot.org/posts/5-Debian-Etch-Nginx-Php-Php-Fpm-Ruby-MySQL"></a></p>
<p>In a previous post I talked about how I just set up my new Slicehost account using <a href="http://nginx.net/">Nginx</a> to power this blog (Wordpress &#8211; PHP) and some <a href="http://royner.johnyerhot.com">Rails Apps</a>.  Here&#8217;s a <span style="text-decoration: line-through;">mini</span> how to on how I got the whole thing working, as its not quite as easy as it sounds.</p>
<p>First though &#8211; Why not use <a href="http://www.apache.org/">Apache</a> + mod_proxy&#8230; There is a ton of documentation out there for Apache, and the majority of the web runs on it?</p>
<p>Well, Nginx is super tiny and super light weight and super fast.  If you are in a situation like many people who are using Slicehost and a 256 MB slice, Apache has too big a resource foot print.  If you are a Rails guy (or gal) Apache is just used as a proxy to send requests to Mongrel and Apache will just use resources that could be used else where.  Nginx is stable, well supported, and growing in popularity.  Plus the name is cool.. NGINX.. yeah&#8230;</p>
<p>This will (should) work with a fresh install of <a href="http://www.ubuntu.com/products/WhatIsUbuntu/serveredition">Ubuntu Server 7.10</a>, but as always YMMV.</p>
<p>A lot of this is adapted from a great post on <a href="http://www.howtoforge.com/nginx_php5_fast_cgi_xcache_ubuntu7.04">How To Forge</a>, thanks  <a title="View user profile." rel="nofollow" href="http://www.howtoforge.com/forums/member.php?u=23452">3uropa.</a></p>
<p>Install only the OpenSSH packages NOT the LAMP packages when installing Ubuntu Server 7.10.  You&#8217;ll have to setup networking for whatever your situation is so can&#8217;t help you there.  If you have a Slicehost slice, don&#8217;t do anything.  :)<br />
<a title="install ubuntu" href="http://www.johnyerhot.com/wp-content/uploads/2008/02/install_ubuntu.png"><img src="http://www.johnyerhot.com/wp-content/uploads/2008/02/install_ubuntu.png" alt="install ubuntu" /><br />
</a><a title="install ubuntu 2" href="http://www.johnyerhot.com/wp-content/uploads/2008/02/install_ubuntu_3.png"><img src="http://www.johnyerhot.com/wp-content/uploads/2008/02/install_ubuntu_3.png" alt="install ubuntu 2" /></a></p>
<p>Once that is finished installing Ubuntu Server, the first thing you want to do is update and get some prerequisites out of the way!</p>
<blockquote><p>sudo apt-get update<br />
sudo apt-get upgrade<br />
sudo apt-get install build-essential</p></blockquote>
<p>Ok.  Now, you need to go in and get all your files/directories in order.  That mean, go to /var/www/ (or wherever) and make directories for your PHP site.  In my case, I used /home/joyerhot/public_html/johnyerhot.com/wordpress as the root of my Wordpress blog.   You should be able to either use wget to grab stuff or SFTP in with your username and password.</p>
<p>Ok. Next we will install PHP5 and MySQL</p>
<blockquote><p>apt-get install php5-cli php5-cgi  build-essential mysql-server mysql-client libmysqlclitene15-dev</p></blockquote>
<p>Now, lets get Nginx while we&#8217;re at it!  The version that is in Ubuntu&#8217;s repos is old and not cool, so we&#8217;ll manually grab a newer version:</p>
<blockquote><p>wget http://technokracy.net/nginx/nginx_0.5.32~grrr-1_i386.deb<br />
sudo dpkg nginx_*</p></blockquote>
<p>And now Nginx is running and listening to port 8000.. we&#8217;ll change that later though.<br />
<a title="nginx setup" href="http://www.johnyerhot.com/wp-content/uploads/2008/02/nginx_setup.png"><img src="http://www.johnyerhot.com/wp-content/uploads/2008/02/nginx_setup.png" alt="nginx setup" /></a></p>
<p>Now, here is where it gets sticky.  You are going to create and edit /etc/nginx/sites-enabled/mysite</p>
<blockquote><p>sudo nano /etc/nginx/sites-enabled/mysite</p></blockquote>
<p>and put in it the following, changing directories and whatnot to what is appropriate for you.</p>
<blockquote><p>server {</p>
<p>listen   80;<br />
server_name yourdomain.com;<br />
rewrite ^/(.*) http://www.yourdomain.com permanent;</p>
<p>}</p>
<p>server {</p>
<p>listen   80;<br />
server_name www.yourdomain.com;</p>
<p>access_log /home/joyerhot/public_html/yourdomain.com/logs/access.log;<br />
error_log /home/joyerhot/public_html/yourdomain.com/logs/error.log;</p>
<p>if (!-e $request_filename) {<br />
rewrite ^([_0-9a-zA-Z-]+)?(/wp-.*) $2 last;<br />
rewrite ^([_0-9a-zA-Z-]+)?(/.*\.php)$ $2 last;<br />
rewrite ^ /index.php last;<br />
}</p>
<p>location / {</p>
<p>root   /home/joyerhot/public_html/johnyerhot.com/wordpress;<br />
index  index.html index.php index.htm;</p>
<p>}</p>
<p># pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000<br />
#<br />
location ~ \.php$ {<br />
fastcgi_pass 127.0.0.1:9000;<br />
fastcgi_index index.php;<br />
fastcgi_param QUERY_STRING $query_string;<br />
fastcgi_param REQUEST_METHOD $request_method;<br />
fastcgi_param CONTENT_TYPE $content_type;<br />
fastcgi_param CONTENT_LENGTH $content_length;<br />
fastcgi_param SCRIPT_FILENAME /home/joyerhot/public_html/yourdomain.com/wordpress/$fastcgi_script_name;<br />
fastcgi_param REQUEST_URI $request_uri;<br />
fastcgi_param DOCUMENT_URI $document_uri;<br />
fastcgi_param DOCUMENT_ROOT $document_root;<br />
fastcgi_param SERVER_PROTOCOL $server_protocol;<br />
fastcgi_param GATEWAY_INTERFACE CGI/1.1;<br />
fastcgi_param SERVER_SOFTWARE nginx;<br />
fastcgi_param REMOTE_ADDR $remote_addr;<br />
fastcgi_param REMOTE_PORT $remote_port;<br />
fastcgi_param SERVER_ADDR $server_addr;<br />
fastcgi_param SERVER_PORT $server_port;<br />
fastcgi_param SERVER_NAME $server_name;<br />
}<br />
}</p></blockquote>
<p><a title="editing_nginx" href="http://www.johnyerhot.com/wp-content/uploads/2008/02/eding_nginx.png"><img src="http://www.johnyerhot.com/wp-content/uploads/2008/02/eding_nginx.png" alt="editing_nginx" /></a></p>
<p>WHEW!  There is A LOT of stuff there!  This will setup a Virtual Host for your PHP site, and enable rewrites (for pretty urls).  Here is what you need to change in yours though:</p>
<p>1. the server_name is whatever your domain is.<br />
2. rewrite ^/(.*) http://www.whatever_goes_here.com permanent;<br />
3. access_log directory<br />
4. error_log directory<br />
5. root directory<br />
6. fastcgi_param SCRIPT_FILENAME /path/to/your/php/app/root/$fastcgi_script_name;<br />
7. MAKE SURE YOU&#8217;RE LISTENING TO PORT 80.</p>
<p>Ok, now we&#8217;ll get PHP5 working as a fcgi process.  To do this, we need to grab some of Lighttpd.</p>
<blockquote><p>wget http://www.lighttpd.net/download/lighttpd-1.4.18.tar.bz2<br />
tar -xvjf lighttpd-*<br />
cd lighttpd*<br />
./configure<br />
make</p></blockquote>
<p>DO NOT &#8220;make install&#8221;.  :)</p>
<blockquote><p>sudo cp src/spawn-fcgi /usr/bin/spawn-fcgi<br />
sudo nano /usr/bin/php-fastcgi</p></blockquote>
<p>Now, add the following into php-fastcgi:</p>
<blockquote><p>/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 2 -u www-data -f /usr/bin/php5-cgi</p></blockquote>
<p>-C controls how many fcgi instances of PHP5 are spawned, so you can put however many are appropriate for you in there.  I only needed 2.</p>
<blockquote><p>sudo nano /etc/init.d/init-fastcgi</p></blockquote>
<p>And add:</p>
<blockquote><p>#!/bin/bash<br />
PHP_SCRIPT=/usr/bin/php-fastcgi<br />
RETVAL=0<br />
case &#8220;$1&#8243; in<br />
start)<br />
$PHP_SCRIPT<br />
RETVAL=$?<br />
;;<br />
stop)<br />
killall -9 php<br />
RETVAL=$?<br />
;;<br />
restart)<br />
killall -9 php<br />
$PHP_SCRIPT<br />
RETVAL=$?<br />
;;<br />
*)<br />
echo &#8220;Usage: php-fastcgi {start|stop|restart}&#8221;<br />
exit 1<br />
;;<br />
esac<br />
exit $RETVAL</p></blockquote>
<p>Do some permission magic:</p>
<blockquote><p>sudo chmod 755 /etc/init.d/init-fastcgi<br />
sudo chmod 755 /usr/bin/php-fastcgi</p></blockquote>
<p>Now, you can make sure everything is working by:</p>
<blockquote><p>/etc/init.d/init-fastcgi start<br />
top #then shift + M</p></blockquote>
<p>You should see a couple of PHP5 fcgi processes! YAY!<br />
Finally, we want them to start on reboots and whatnot so&#8230;</p>
<blockquote><p>update-rc.d init-fastcgi defaults</p></blockquote>
<p>Ok, now lets restart Nginx and see if things are working!</p>
<blockquote><p>/etc/init.d/nginx restart</p></blockquote>
<p>You should be able to hit your Php app now!</p>
<p>Now for Ruby and friends!</p>
<blockquote><p>sudo apt-get install libmysql-ruby1.8 ruby1.8-dev ruby1.8 ri1.8 rdoc1.8 irb1.8 libreadline-ruby1.8 libruby1.8 libopenssl-ruby irb1.8 libdbd-mysql-perl libdbi-perl libmysql-ruby1.8 libmysqlclient15-dev libmysqlclient15off libnet-daemon-perl libopenssl-ruby libopenssl-ruby1.8 libplrpc-perl libreadline-ruby1.8 libruby1.8 mysql-client mysql-client-5.0 mysql-common mysql-server mysql-server-5.0 rdoc1.8 ri1.8 ruby1.8 ruby1.8-dev zlib1g-dev</p></blockquote>
<p>And you might need to create some symlinks (I had to), thanks <a href="http://www.urbanpuddle.com/articles/2008/01/09/install-ruby-on-rails-on-ubutu-gutsy-gibbon-apache-version">Vince Wadhwanl</a>.</p>
<blockquote><p>sudo ln -s /usr/bin/ruby1.8 /usr/local/bin/ruby<br />
sudo ln -s /usr/bin/rdoc1.8 /usr/local/bin/rdoc<br />
sudo ln -s /usr/bin/ri1.8 /usr/local/bin/ri<br />
sudo ln -s /usr/bin/irb1.8 /usr/local/bin/irb</p></blockquote>
<p>Make sure things are working ok&#8230;</p>
<blockquote><p>ruby -v</p></blockquote>
<p>You should get that you have 1.8.6 installed.Ok, now lets install RubyGems.</p>
<blockquote><p>wget http://rubyforge.org/frs/download.php/29548/rubygems-1.0.1.tgz<br />
tar xvzf rubygems-1.0.1.tgz<br />
cd rubygems-1.0.1<br />
sudo ruby setup.rb<br />
sudo ln -s /usr/bin/gem1.8 /usr/bin/gem</p></blockquote>
<p>And finally for Rails and Mongrel:</p>
<blockquote><p>sudo gem install rails<br />
sudo gem install mongrel mongrel_cluster</p></blockquote>
<p>You can now set up a mongrel_cluster or whatever you need.  There&#8217;s plenty of tutorials out there for that, so <a href="http://www.google.com/search?hl=en&amp;q=mongrel_cluster+ubuntu&amp;btnG=Google+Search">Google</a> is your friend.   The rest will assume you have your Rails app up and running on port 3000.<br />
We now have to set up a site profile for Nginx for your mongrel_cluster.</p>
<blockquote><p>sudo nano /etc/nginx/sites-enabled/rails_app</p></blockquote>
<p>And add to it, once again changing the relevant parts&#8230;.</p>
<blockquote><p>upstream domain1 {<br />
server 127.0.0.1:3000;<br />
}</p>
<p>server {</p>
<p>listen   80;<br />
server_name  www.your2nddomain.com;<br />
rewrite ^/(.*) http://subdomain.your2nddomain.com permanent;</p>
<p>}</p>
<p>server {</p>
<p>listen   80;<br />
server_name subdomain.your2nddomain.com;</p>
<p>access_log /home/joyerhot/public_html/your2nddomain.com/logs/access.log;<br />
error_log /home/joyerhot/public_html/your2nddomain.com/logs/error.log;</p>
<p>location / {</p>
<p>root   /home/joyerhot/public_html/your2nddomain.com/public/RAILS_ROOT/public/;<br />
index  index.html;<br />
location / {<br />
proxy_set_header  X-Real-IP  $remote_addr;<br />
proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;<br />
proxy_set_header Host $http_host;<br />
proxy_redirect false;</p>
<p>if (-f $request_filename/index.html) {<br />
rewrite (.*) $1/index.html break;<br />
}</p>
<p>if (-f $request_filename.html) {<br />
rewrite (.*) $1.html break;<br />
}</p>
<p>if (!-f $request_filename) {<br />
proxy_pass http://domain1;<br />
break;<br />
}<br />
}</p>
<p>}</p>
<p>}</p></blockquote>
<p>Again, heres what you need to change:<br />
1. server_name should be the domain<br />
2. In this case the &#8220;rewrite&#8221; line will point to subdomain.your2nddomain.com, you should prolly just put your domain here(www.your2nddomain.com).<br />
3. access_log directory<br />
4. error_log directory<br />
5. root directory (pointing to RAILS_ROOT/public)</p>
<p>That&#8217;s it!  You should now restart Nginx (/etc/init.d/nginx restart) and when you should have everything working!   You&#8217;ve got Virtual Hosts setup with PHP running as Fast CGI on one domain and Ruby On Rails on the other!</p>
<p>I&#8217;m sure I&#8217;ve got a typo or two in here and a mistake here and there, please leave a message if this worked or didn&#8217;t work for you.  Remember, this is the process I went through getting everything working on my Slicehost Slice and another server at work, both running Ubuntu 7.10.</p>
<p><strong>PS</strong>.  Here is one little nugget of info that may be helpful &#8211; Your MySQL socket file location (for your RAILS_ROOT/config/database.yml file) is located at /var/run/mysqld/mysqld.sock.  I didn&#8217;t realize this at first.  :)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnyerhot.com/2008/02/12/how-to-nginx-fcgi-php-mysql-ruby-on-rails-rewrite-vhosts/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>
