peter-de-berdt
04-28-2007, 08:17 AM
Instead of putting Pound before Apache, I put it behind Apache, only serving as a load balancer for our Rails apps that need it. This way, single mongrel, low traffic rails apps or normal apache accounts can be created in Plesk without the need to reconfigure Pound. Plesk prevents you from easily upgrading e.g. Apache (apt-get would remove the whole Plesk installation), and there's very little support and helpful resources on Plesk, so this procedure could come in very handy for anyone having to deal with it.
APACHE LOAD BALANCING ON UBUNTU DAPPER DRAKE WITH APACHE 2.0 AND PLESK USING POUND AS A BALANCER
Upgrading Apache on a Plesk installation is a no-go, no one, even on the swsoft forums was able to help me out. My attention was turned to Pound and these were my findings during the installation and configuration:
Part of the following procedure is taken from the excellent blog post at Tupleshop (http://blog.tupleshop.com/2006/7/8/deploying-rails-with-pound-in-front-of-mongrel-lighttpd-and-apache), but some things didn't work out as planned and one problem had to be solved differently.
1. apt-get install Pound (this will install an old version, but it will also automatically create a startup script and a few configs that come in handy)
2. Install PCRE from the source, Pound needs it:
• wget the latest release as indicated on http://www.pcre.org/
• tar xvzf pcre-version-number.tar.gz
• cd pcre-version-number
• ./configure
• make
• sudo make install
3. Install the latest version of Pound:
• wget the latest release of Pound as indicated on http://www.apsis.ch/pound/
• tar xvzf Pound-version-number.tar.gz
• cd Pound-version-number
• IMPORTANT: this step needs to be performed in order for Pound not to add X-Forwarded-For, which confuses Mongrel and makes all requests seem to come from 127.0.0.1. This will cause Rails errors to be shown with a whole trace, not a very good idea for a production app
=> sudo pico http.c
Control-W and search for X-Forwarded-For, comment out the following two lines:
//addr2str(caddr, MAXBUF - 1, &from_host);
//BIO_printf(be, "X-Forwarded-For: %s\r\n", caddr);
• ./configure --with-ssl=ssl_dir
• env LD_RUN_PATH=/usr/local/lib/ make (THIS STEP IS REALLY IMPORTANT, POUND WON'T FIND PCRE WITHOUT IT)
• sudo make install
4. sudo pico /etc/default/pound
• change startup=0 to startup=1
5. sudo pico etc/init.d/pound
• change DAEMON=/usr/sbin/pound to DAEMON=/usr/local/sbin/pound
6. sudo ln -s /etc/pound/pound.cfg /usr/local/etc/pound.cfg
7. Configure Pound: sudo pico /etc/pound/pound.cfg
This will currently use the old config syntax for Pound < 2.0 and Pound will throw errors for invalid directives, sample config below (including two balanced apps)
###
User "www-data"
Group "www-data"
LogLevel 1
Alive 30
ListenHTTP
Address 0.0.0.0
Port 8080
xHTTP 1
Service
HeadRequire "Host: .*railsappdomain1.com.*"
BackEnd
Address 127.0.0.1
Port 2000
End
BackEnd
Address 127.0.0.1
Port 2001
End
BackEnd
Address 127.0.0.1
Port 2002
End
BackEnd
Address 127.0.0.1
Port 2003
End
Session
Type BASIC
TTL 300
End
End
Service
HeadRequire "Host: .*railsappdomain2.com.*"
BackEnd
Address 127.0.0.1
Port 8000
End
BackEnd
Address 127.0.0.1
Port 8001
End
Session
Type BASIC
TTL 300
End
End
End
###
8. Start Pound using sudo /etc/init.d/pound start, no errors should be thrown
9. Verify Pound is working correctly: curl www.railsappdomain1.com:8080, this should show the HTML of the homepage of the first rails app
10. Go to /var/www/vhosts/railsappdomain1.com/conf/
• pico vhost.conf (i.e. create one or edit the existing one)
• sample configuration that seems to work very well below (it will also deflate the pages and serve static pages through Apache, as well as allowing you to view the plesk-stat url with either awstats or webalize):
<Proxy *>
Allow from all
</Proxy>
ServerAlias railsappdomain1.com
DocumentRoot /path/to/railsapp1/public
<Directory "/path/to/railsapp1/public">
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
<Location /plesk-stat>
Options +Indexes
</Location>
ProxyRequests Off
ProxyPassReverse / http://localhost:8080/
ProxyPreserveHost On
RewriteEngine On
RewriteRule ^/error/.* /usr/share/apache2/$0 [L]
# Route the plesk-stats through Apache instead of proxying them to Pound and Mongrel cluster
RewriteRule ^/plesk-stat/?(.*) /var/www/vhosts/railsappdomain1.com/statistics/$1 [L]
RewriteRule ^/(webstat|webstat-ssl|ftpstat|anon_ftpstat)/?.*$ /var/www/vhosts/railsappdomain1.com/statistics$0 [L]
RewriteRule ^/awstats-icon/(.*) /usr/share/awstats/icon/$1 [L]
# Serve static files through Apache
RewriteRule ^/(images|stylesheets|javascripts|system)/?(.*) $0 [L]
# Send the rest to Pound
RewriteRule ^/(.*)$ http://localhost:8080/$1 [P,L]
# Rewrite index to check for static
RewriteRule ^/$ /index.html [QSA]
# Rewrite to check for Rails cached page
RewriteRule ^([^.]+)$ $1.html [QSA]
# Redirect all non-static requests to cluster
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
# Deflate all text based files for faster transfer to the client
AddOutputFilterByType DEFLATE text/html text/plain text/css text/javascript
# ... text/xml application/xml application/xhtml+xml text/javascript
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
11. /usr/local/psa/admin/sbin/websrvmng --reconfigure-vhost --vhost-name=railsappdomain1.com
12. apache2ctl restart
That's it. From now on it will be sufficient to duplicate the vhost.conf file for a new Rails app, change the ServerAlias and add a Service to the pound.cfg file, then restart Pound and perform steps 11 and 12.
APACHE LOAD BALANCING ON UBUNTU DAPPER DRAKE WITH APACHE 2.0 AND PLESK USING POUND AS A BALANCER
Upgrading Apache on a Plesk installation is a no-go, no one, even on the swsoft forums was able to help me out. My attention was turned to Pound and these were my findings during the installation and configuration:
Part of the following procedure is taken from the excellent blog post at Tupleshop (http://blog.tupleshop.com/2006/7/8/deploying-rails-with-pound-in-front-of-mongrel-lighttpd-and-apache), but some things didn't work out as planned and one problem had to be solved differently.
1. apt-get install Pound (this will install an old version, but it will also automatically create a startup script and a few configs that come in handy)
2. Install PCRE from the source, Pound needs it:
• wget the latest release as indicated on http://www.pcre.org/
• tar xvzf pcre-version-number.tar.gz
• cd pcre-version-number
• ./configure
• make
• sudo make install
3. Install the latest version of Pound:
• wget the latest release of Pound as indicated on http://www.apsis.ch/pound/
• tar xvzf Pound-version-number.tar.gz
• cd Pound-version-number
• IMPORTANT: this step needs to be performed in order for Pound not to add X-Forwarded-For, which confuses Mongrel and makes all requests seem to come from 127.0.0.1. This will cause Rails errors to be shown with a whole trace, not a very good idea for a production app
=> sudo pico http.c
Control-W and search for X-Forwarded-For, comment out the following two lines:
//addr2str(caddr, MAXBUF - 1, &from_host);
//BIO_printf(be, "X-Forwarded-For: %s\r\n", caddr);
• ./configure --with-ssl=ssl_dir
• env LD_RUN_PATH=/usr/local/lib/ make (THIS STEP IS REALLY IMPORTANT, POUND WON'T FIND PCRE WITHOUT IT)
• sudo make install
4. sudo pico /etc/default/pound
• change startup=0 to startup=1
5. sudo pico etc/init.d/pound
• change DAEMON=/usr/sbin/pound to DAEMON=/usr/local/sbin/pound
6. sudo ln -s /etc/pound/pound.cfg /usr/local/etc/pound.cfg
7. Configure Pound: sudo pico /etc/pound/pound.cfg
This will currently use the old config syntax for Pound < 2.0 and Pound will throw errors for invalid directives, sample config below (including two balanced apps)
###
User "www-data"
Group "www-data"
LogLevel 1
Alive 30
ListenHTTP
Address 0.0.0.0
Port 8080
xHTTP 1
Service
HeadRequire "Host: .*railsappdomain1.com.*"
BackEnd
Address 127.0.0.1
Port 2000
End
BackEnd
Address 127.0.0.1
Port 2001
End
BackEnd
Address 127.0.0.1
Port 2002
End
BackEnd
Address 127.0.0.1
Port 2003
End
Session
Type BASIC
TTL 300
End
End
Service
HeadRequire "Host: .*railsappdomain2.com.*"
BackEnd
Address 127.0.0.1
Port 8000
End
BackEnd
Address 127.0.0.1
Port 8001
End
Session
Type BASIC
TTL 300
End
End
End
###
8. Start Pound using sudo /etc/init.d/pound start, no errors should be thrown
9. Verify Pound is working correctly: curl www.railsappdomain1.com:8080, this should show the HTML of the homepage of the first rails app
10. Go to /var/www/vhosts/railsappdomain1.com/conf/
• pico vhost.conf (i.e. create one or edit the existing one)
• sample configuration that seems to work very well below (it will also deflate the pages and serve static pages through Apache, as well as allowing you to view the plesk-stat url with either awstats or webalize):
<Proxy *>
Allow from all
</Proxy>
ServerAlias railsappdomain1.com
DocumentRoot /path/to/railsapp1/public
<Directory "/path/to/railsapp1/public">
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
<Location /plesk-stat>
Options +Indexes
</Location>
ProxyRequests Off
ProxyPassReverse / http://localhost:8080/
ProxyPreserveHost On
RewriteEngine On
RewriteRule ^/error/.* /usr/share/apache2/$0 [L]
# Route the plesk-stats through Apache instead of proxying them to Pound and Mongrel cluster
RewriteRule ^/plesk-stat/?(.*) /var/www/vhosts/railsappdomain1.com/statistics/$1 [L]
RewriteRule ^/(webstat|webstat-ssl|ftpstat|anon_ftpstat)/?.*$ /var/www/vhosts/railsappdomain1.com/statistics$0 [L]
RewriteRule ^/awstats-icon/(.*) /usr/share/awstats/icon/$1 [L]
# Serve static files through Apache
RewriteRule ^/(images|stylesheets|javascripts|system)/?(.*) $0 [L]
# Send the rest to Pound
RewriteRule ^/(.*)$ http://localhost:8080/$1 [P,L]
# Rewrite index to check for static
RewriteRule ^/$ /index.html [QSA]
# Rewrite to check for Rails cached page
RewriteRule ^([^.]+)$ $1.html [QSA]
# Redirect all non-static requests to cluster
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
# Deflate all text based files for faster transfer to the client
AddOutputFilterByType DEFLATE text/html text/plain text/css text/javascript
# ... text/xml application/xml application/xhtml+xml text/javascript
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
11. /usr/local/psa/admin/sbin/websrvmng --reconfigure-vhost --vhost-name=railsappdomain1.com
12. apache2ctl restart
That's it. From now on it will be sufficient to duplicate the vhost.conf file for a new Rails app, change the ServerAlias and add a Service to the pound.cfg file, then restart Pound and perform steps 11 and 12.