Skip to content

Tag Archive for Apache

Phusion Passenger (mod_rails) Memory Management

I’ve been using Phusion Passenger (mod_rails) for about a month to run a production and staging machine with a Ruby on Rails app. So far things have been going really well. Installation was smooth and it’s been really peppy.

However, the app just got hit by a lot of people at once and stopped responding. I didn’t realize the default setting in Passenger is to spawn a max of 6 Rails instances simultaneously. The documentation recommends 2 instances for a VPS with 256MB of memory (almost exactly what I’m running). So, my server spawned the max and then ran out of memory which made it unresponsive.

I hit a couple of small walls while getting this working so I wanted to share the process.

On linux based machines you can run passenger-status to find out the number of Phusion Passenger application instances that are running.

----------- General information -----------
max      = 6
count    = 3
active   = 0
inactive = 3

----------- Applications -----------
/u/apps/myapp/releases/20080921194503:
  PID: 8792      Sessions: 0
  PID: 8789      Sessions: 0
  PID: 8784      Sessions: 0

You can see that the output show the default max value of 6. To change this number you need to edit your httpd.conf and set the PassengerMaxPoolSize option. Make sure this is outside of your VirtualHost or it won’t work… which, I obviously ran into. It should probably look something like this:

LoadModule passenger_module /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.3/ext/apache2/mod_passenger.so
PassengerRoot /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.3
PassengerRuby /usr/local/bin/ruby
PassengerMaxPoolSize 2

Once you’re done with that restart Apache and Passenger. Now, to take a look at the memory that Apache and Passenger are using run passenger-memory-stats. You’ll see something like this:

------------- Apache processes -------------
PID   PPID  Threads  VMSize   Private  Name
--------------------------------------------
5842  1     1        10.3 MB  0.2 MB   /usr/local/apache2/bin/httpd -k start
8757  5842  1        10.4 MB  0.3 MB   /usr/local/apache2/bin/httpd -k start
8784  5842  1        10.4 MB  0.2 MB   /usr/local/apache2/bin/httpd -k start
9137  5842  1        10.3 MB  0.2 MB   /usr/local/apache2/bin/httpd -k start
9141  5842  1        10.4 MB  0.2 MB   /usr/local/apache2/bin/httpd -k start
9143  5842  1        10.3 MB  0.2 MB   /usr/local/apache2/bin/httpd -k start
9147  5842  1        10.4 MB  0.3 MB   /usr/local/apache2/bin/httpd -k start
9155  5842  1        10.4 MB  0.3 MB   /usr/local/apache2/bin/httpd -k start
9163  5842  1        10.4 MB  0.3 MB   /usr/local/apache2/bin/httpd -k start
9506  5842  1        10.4 MB  0.3 MB   /usr/local/apache2/bin/httpd -k start
9510  5842  1        10.3 MB  0.2 MB   /usr/local/apache2/bin/httpd -k start
9623  5842  1        10.4 MB  0.3 MB   /usr/local/apache2/bin/httpd -k start
9629  5842  1        10.3 MB  0.2 MB   /usr/local/apache2/bin/httpd -k start
9631  5842  1        10.3 MB  0.2 MB   /usr/local/apache2/bin/httpd -k start
9633  5842  1        10.3 MB  0.2 MB   /usr/local/apache2/bin/httpd -k start
9750  5842  1        10.3 MB  0.2 MB   /usr/local/apache2/bin/httpd -k start
9752  5842  1        10.3 MB  0.2 MB   /usr/local/apache2/bin/httpd -k start
9865  5842  1        10.3 MB  0.2 MB   /usr/local/apache2/bin/httpd -k start
### Processes: 18
### Total private dirty RSS: 4.36 MB

-------- Passenger processes ---------
PID   Threads  VMSize   Private  Name
--------------------------------------
8749  20       6.0 MB   0.5 MB   /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.3/ext/apache2/ApplicationPoolServerExecutable 0 /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.3/bin/passenger-spawn-server  /usr/local/bin/ruby  /tmp/passenger_status.5842.fifo
8758  1        5.5 MB   2.9 MB   Passenger spawn server
8789  1        61.6 MB  53.7 MB  Rails: /u/apps/myapp/releases/20080921194503
8792  1        61.8 MB  53.9 MB  Rails: /u/apps/myapp/releases/20080921194503
### Processes: 4
### Total private dirty RSS: 111.03 MB

Now, that may look like a lot of Apache instances running but you can see the “Total private dirty RSS” (real memory usage of the processes) is only 4.36 MB which is nothing in the scheme of things. More importantly, the real memory that the Rails instances are taking is 111.03 MB which my server can handle. Remember, you’ll probably have MySQL and other applications running along with these processes, so don’t up the PassengerMaxPoolSize to the point of bringing your server to its knees.

Getting Apache & PHP to work with Leopard (OS X 10.5)

I ran into a snag today while trying to get the websites on my local machine running with Leopard. The first thing was that the config file in the new version of Apache is in a different location (/etc/apache2/httpd.conf instead of /etc/httpd/httpd.conf). Once I changed the settings I kept getting 403 Forbidden messages every time I loaded a page (the default in the new httpd.conf is ‘Deny from all’ for all directories). I finally figured things out and here’s what I did:

  1. Open the httpd.conf file for editing (sudo vi /etc/apache2/httpd.conf).
  2. Uncomment the line
    LoadModule php5_module     libexec/apache2/libphp5.so
    by removing the ‘#’ from the front of the line. This will enable PHP 5. The php5.conf file is loaded automatically from /private/etc/apache2/other/.
  3. Create a file called _sites.conf in your Sites folder. I like to keep my site configuration in the Sites folder to make it more easily accessible as I’m doing development.
  4. Add the following line to the very bottom of the httpd.conf file:
    Include /Users/yourusername/Sites/_sites.conf
    That will make Apache load all the configuration settings from your _sites.conf file.
  5. Add the following information to your _sites.conf file.
# Enable named virtual hosts
NameVirtualHost *:80

# Override the default httpd.conf directives.  Make sure to
# use 'Allow from all' to prevent 403 Forbidden message.
<Directory />
	Options ExecCGI FollowSymLinks
	AllowOverride all
	Allow from all
</Directory>

# A basic virtual host config
<VirtualHost *:80>
	# Add yoursite to your /etc/hosts file so you can
	# type it directly in your browser
	ServerName yoursite

	DocumentRoot /Users/yourusername/Sites/yoursite
</VirtualHost>

Now you just need to start/restart Apache and the sites should load. PHP should be working and no more 403 Forbidden message.