Archive for Passenger

Eating our own dog food

From http://twitter.com/hellsten/statuses/951757474:

“modrails.com doesn’t eat its own dog food. They’re using nginx/0.6.32, so does that mean they think Phusion Passenger sucks?”

That’s right, modrails.com is behind Nginx. But that’s because the entire website only consists of static HTML! We didn’t use PHP, or Rails, or anything dynamic. The website is generated from a few input files with Webgen and rsync’ed regularly.

Our Rails apps do run on Phusion Passenger… behind Nginx.

Comments

Who’s running Phusion Passenger in production?

An interesting thread appeared on the Phusion Passenger mailing list, in which user asked who’s running Phusion Passenger in production. We’re actually very interested as well, seeing as we’re currently building a new website for Phusion. Please drop a note at the mailing list (or here, though the mailing list is preferred) if you’re running it in production as well.

Comments

wiki.rubyonrails.org was down

Today wiki.rubyonrails.org was briefly down, for like 30 minutes or so.

I thought it might be a problem in Phusion Passenger, seeing that the wiki is running on it. I wanted to the restart Apache, but I decided to look in the wiki log files before doing that.

It turned out the server ran out of disk space. I truncated the Apache log files, which were consuming 40 GB or so. After that, everything went back to normal.

Comments (2)

Re: Strange HTTP header?

Yesterday I challenged people to look at sandbox.phusion.nl‘s HTTP headers and check whether they notice anything weird. The HTTP response header of the front page is:

HTTP/1.1 200 OK
Server: nginx/0.6.32

X-Powered-By: Phusion Passenger (mod_rails/mod_rack) 2.1.0
X-Runtime: 0.00173

Wow, I got a lot more responses than I expected.

Sorry guys, there’s a reason why I didn’t post this on the Phusion blog, but on my personal blog instead. :) Chu Yeow said:

Wow Passenger on Nginx (I think that’s it – doubt you’d run Nginx on top of Apache+Passenger ;) ).

Well actually… we are running Nginx on top of Apache+Passenger. :)

The first reaction of many people is probably “WTF, are you out of your mind? Why would you do such a thing?” Let me explain a little bit about our server.

Initial motivation: security

This server is shared by many users, including a few which we don’t fully trust. It not only runs Rails applications but also a bunch of PHP applications, and in the not too distant past some mod_perl applications. In the usual Apache setup, all those PHP/mod_perl applications will run under the same user and have the same rights. This means that there is no security between different people’s web applications: Jane’s PHP script can read Joe’s forum database password file. Not so nice.

server_setup1.jpg

Now, how do we solve this? These days, server virtualization is the latest hype: just give Joe and Joe different virtual machines! But virtualization wastes a lot of memory. Joe and Jane’s websites are really low-traffic compared to mine. The server “only” has 1 GB of RAM, and allocating a fixed amount of RAM (which must be at least 128 MB for a more or less usable server OS) is really wasteful.

Our solution was simple. Each user got his own Apache installation and runs all his web applications under his own user account. Users cannot read from and write to other users’ home folders. Each of these backend Apache installations are firewalled, and a frontend web server proxies requests to these backend Apache installations.

server_setup2.jpg

But the setup is of course not limited to one-Apache-per-real-user. blog.phusion.nl is running on WordPress, which doesn’t exactly have a good security track record. My personal WordPress installation had been hacked once: apparently some spam bot changed the file upload folder to /tmp and put a .exe in there. It also disabled Akismet. I wouldn’t be surprised if someone one day finds a remote shell code execution vulnerability. One really wouldn’t want to run WordPress with the same rights as all the other web applications. So we gave WordPress its own user account and Apache installation. WordPress is now completely sandboxed and cannot do any harm to the other websites.

Efficiency

Indeed, what about efficiency? We’ve been using this setup for almost 2 years now, and it’s actually running quite well. Not too long ago, this server hosted a website which got about 30 000 unique visitors per day (about 120 000 requests per day on this server; we load balanced that website over multiple web servers) and it was able to handle the load with ease. We noticed no delay in response times compared to when the website was running on the frontend web server directly. That said, we did go through several stages of optimization:

  1. A long long time ago, the frontend web server was Apache 1.3, which proxies requests via mod_accel. mod_accel is like mod_proxy, but you can specify a list of URI extensions that it won’t proxy. For example, you can tell mod_accel only to proxy requests that don’t end with .css, .jpg, .png, etc.
  2. Unfortunately Apache 1.3 was ancient and not well-supported, so we switched to Apache 2 with mod_proxy instead. mod_proxy provides no way to skip proxying certain URIs, so we had to live with this. Performance was acceptable, though the backend web servers are being hit harder than before because static asset requests are now also being proxied.
  3. Apache 2 proved to be too memory- and CPU-hungry for a reverse proxy, so we switched the frontend web server to Lighttpd instead. This reduced our CPU- and memory usage dramatically. We configured Lighttpd to serve static assets directly, so that the backend web servers are only there to serve PHP.
  4. Unfortunately Lighttpd leaks memory: after a few days, memory usage would jump to 200 MB. From time to time it will also “go out of control” and consume 100% CPU, although it’s still serving requests just fine. 2 days ago I finally got tired of that, and replaced Lighttpd with Nginx.

Finally, we used Apache with the worker MPM and Phusion Passenger development version (from the git repository) for hosting our Rails applications. The worker MPM, which uses a combination of threads and processes, is a lot more memory efficient than the default prefork MPM, which only use processes. This is our Apache worker MPM setup:

StartServers             1
ThreadsPerChild         10
MaxClients              10
MinSpareThreads          1
MaxSpareThreads          1
MaxRequestsPerChild  50000
ThreadStackSize     500000

This tells Apache to use only one process. That process is multi-threaded and will have 10 threads for serving requests. Furthermore, each thread will have a stack size of 500 KB. The default system stack size is usually something along the lines of 8 MB, so setting such a small stack size reduces Apache’s VM size a lot. 500 KB has proven to be sufficient for Apache.

Now, let’s compare the memory usage between Nginx and our Apache installation:

USER   PID   %CPU %MEM   VSZ   RSS  TT  STAT STARTED      TIME COMMAND
root   11700  0.0  0.2  3452  2012  ??  Is    6:23AM   0:00.00 nginx: master process /usr/local/sbin/nginx
www    11701  0.0  0.3  3452  2880  ??  S     6:23AM   2:28.91 nginx: worker process (nginx)
www    11702  0.0  0.3  3452  2880  ??  S     6:23AM   2:47.10 nginx: worker process (nginx)
app    82548  0.0  0.3  7656  3572  ??  Ss   Tue03PM   0:05.79 /home/app/apache/bin/httpd -k start
app    89467  0.0  0.4 10144  4632  ??  I     5:11AM   0:02.45 /home/app/apache/bin/httpd -k start

The server’s running on FreeBSD, not Linux, so we can’t measure memory usage excluding any copy-on-write savings (i.e. the private dirty RSS). But let’s compare the total Resident Set Sizes (RSS):

  • Nginx: 7772 KB (7.6 MB)
  • Apache: 8204 KB (8.0 MB)

Not a big difference.

Apache’s slowness and Nginx’s performance, both overrated?

People commented:

Pretty fast runtime?

X-Runtime: 0.00171

and

Damn!! It is impossible!
0.00173 per request?!

So the Rails application is running in Apache and behind an Nginx reverse proxy, and it’s still fast.

FastCGI

Lighttpd and Nginx both support PHP via FastCGI, so why didn’t we use that instead? The answer is ease of use. Setting up a PHP-FastCGI process pool for every user is quite a hassle. Plus, the user might be running CGI or mod_perl applications as well. Giving each user his own Apache installation is by far the easiest way. Apache also supports .htaccess, which Lighttpd and Nginx don’t support. WordPress’s URI rewriting feature writes mod_rewrite rules to .htaccess. Configuring the same rules in Lighttpd was a total pain, and I wouldn’t want to do that again.

Conclusion

I believe that all the fuss about web server performance is usually overrated. As we can see, Apache can be memory-efficient. Running Rails applications on Phusion Passenger behind an Nginx reverse proxy is viable. You just need to know how to tweak and mix-and-match the two.

What we’re doing is not very unlike proxying to a Mongrel cluster from Nginx. Instead of proxying to a Mongrel cluster, we proxy to Apache. This still makes Rails deployment a lot easier because Phusion Passenger will take care of managing the Rails processes for me. The only redundant thing that I have to do now is having to setup 2 virtual host definitions: one in Apache and one in Nginx.

Morale of the story: it’s all HTTP, you can proxy everything in any way you want. Some people on the Phusion Passenger mailing list asked how to horizontally scale Phusion Passenger. The answer is: the same way you’re used to when you were using Mongrel clusters.

This also shows that it is possible to run multiple Apache installations on the same server. It’s only a matter of specifying different configuration files for each installation. It seems that a lot of people aren’t aware of that. In a recent Google talk about Rails scalability, a speaker claimed that there is a limit to the amount of hardware resources that Apache can utilize. He said that if you have 16 cores and 20 GB of RAM, one Apache instance cannot utilize all those resources, and that in order to make full use of your hardware, one must virtualize. But why? It’s easier and more efficient to run multiple Apache instances on the same machine.

By the way, we use the following Nginx config snippet for Phusion Passenger-powered hosts:

proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_redirect http://localhost/ $scheme://$http_host/;

server {
    listen 80;
    server_name sandbox.phusion.nl;
    root /u/apps/sandbox/current/public;
    location / {
        proxy_redirect http://localhost:1234/ $scheme://$http_host/;
        if (!-f $request_filename) {
            proxy_pass http://localhost:1234;
            break;
        }
        if ($request_method != GET) {
            proxy_pass http://localhost:1234;
            break;
        }
    }
}

This forwards all non-static-asset requests to Apache. Static assets are served directly by Nginx.

Comments (17)

Strange HTTP header?

I challenge you to type the following command:

curl -i http://sandbox.phusion.nl/ | head

This shows the HTTP output of http://sandbox.phusion.nl/ (which is, unsurprisingly, a Rails app), including HTTP headers.

Do you notice anything strange about this HTTP header? ;) (I’m not going to comment for a few days. I’ll let you guys speculate.)

Comments (11)

Phusion Passenger’s development cost according to Ohloh.org

From http://www.ohloh.net/projects/passenger:
passenger_project_cost.png

Cool, we’re supposed to be millionaires!

FYI that’s is not entirely correct. Ohloh counts the vendorized Boost code base as well. Those who are interested in similar statistics, but without counting Boost, should type “rake sloccount” in the Phusion Passenger source tree (requires sloccount). Here’s the output:

SLOC	Directory	SLOC-by-Language (Sorted)
5357    top_dir         ruby=4261,cpp=928,ansic=168
4494    apache2         cpp=4381,ansic=113
810     oxt             cpp=810
479     railz           ruby=479
194     wsgi            python=133,ruby=61
137     rack            ruby=137
0       templates       (none)

Totals grouped by language (dominant language first):
cpp:           6119 (53.34%)
ruby:          4938 (43.05%)
ansic:          281 (2.45%)
python:         133 (1.16%)

Total Physical Source Lines of Code (SLOC)                = 11,471
Development Effort Estimate, Person-Years (Person-Months) = 2.59 (31.10)
 (Basic COCOMO model, Person-Months = 2.4 * (KSLOC**1.05))
Schedule Estimate, Years (Months)                         = 0.77 (9.23)
 (Basic COCOMO model, Months = 2.5 * (person-months**0.38))
Estimated Average Number of Developers (Effort/Schedule)  = 3.37
Total Estimated Cost to Develop                           = $ 350,125
 (average salary = $56,286/year, overhead = 2.40).
SLOCCount, Copyright (C) 2001-2004 David A. Wheeler
SLOCCount is Open Source Software/Free Software, licensed under the GNU GPL.
SLOCCount comes with ABSOLUTELY NO WARRANTY, and you are welcome to
redistribute it under certain conditions as specified by the GNU GPL license;
see the documentation for details.
Please credit this data as "generated using David A. Wheeler's 'SLOCCount'."

Comments

Phusion Passenger 2.0.2 released

Phusion Passenger 2.0.2 has been released. Please read http://blog.phusion.nl/2008/07/14/phusion-passenger-202-released/ for the announcement.

Comments

Phusion Passenger 2.0.1 (final) released

Comments (1)

Solaris support for Phusion Passenger

Harris Jacob has started working on Solaris support for Phusion Passenger, and he’s looking for testers. Please lend him a hand if you have a Solaris machine and would like to run Phusion Passenger in the future.

Comments

WSGI on Phusion Passenger

WSGI support is not documented in the Users guide because WSGI is mostly a proof of concept right now. But, just in case people want to tinker around with it, here’s how you can host a WSGI application on Phusion Passenger:

$ mkdir /webapps
$ mkdir /webapps/wsgi
$ cd /webapps/wsgi
$ mkdir public
$ mkdir tmp
$ some_favorite_editor passenger_wsgi.py
....edit file...
$ cat passenger_wsgi.py
def application(environ, start_response):
	start_response('200 OK', [('Content-type', 'text/plain'), ('X-Foo', 'bar')])
	return ['Hello World!<br><img src="http://www.squidz.com/c_snakey.jpg">']

Next, add a virtual host directive to your Apache config file:

<VirtualHost *:80>
    ServerName www.wsgi.test
    DocumentRoot /webapps/wsgi/public
</VirtualHost>

Comments (4)

« Previous Page« Previous entries « Previous Page · Next Page » Next entries »Next Page »