Joyent

Translations of this page:

How to set up a Rails application with Mongrel and Apache

This is assuming you know the rest of the rails application stuff: putting the files somewhere and configuring database.yml (and the requisite setting up mysql and mysql users).

If you are seeing myprivateip: command not found errors. Depending on the sized accelerator you might have a private IP listed in your welcome email, if not, use 127.0.0.1.

1) Put in a mongrel_cluster.yml file that will pull your private IP automatically, and write hostname-specific PIDs.

# cd /home/YOUR/PATH/TO/RAILS/APP
# svn export http://svn.joyent.com/public/accelerators/configs/mongrel/mongrel_cluster.yml config/mongrel_cluster.yml

2) Fire up the application

# cd /home/YOUR/PATH/TO/RAILS/APP
# mongrel_rails cluster::start

3) Check that you're getting something

# curl `myprivateip`:8000
# curl -I `myprivateip`:8000

The second one should return a “200”, like

HTTP/1.1 200 OK
Connection: close
Date: Tue, 06 Mar 2007 07:09:13 GMT
Status: 200 OK
Server: Mongrel 1.0.1
Content-Type: text/html
Content-Length: 0

If there's nothing happening check your production.log

4) Configure an apache virtual host to proxy balance to these mongrels

Note: newer Accelerators (March 22 2008 and later) will have apache installed in /opt/local/etc/httpd, not /opt/csw/apache2

# cd /opt/csw/apache2/etc/virtualhost
# nano myrailsapp-domain.conf

With the contents of

<VirtualHost YOUR-PUBLIC-IP:80>
ServerName YOURDOMAIN.com                                                                                                                                                                       ServerAlias *.YOURDOMAIN.com 
DocumentRoot /home/YOUR/PATH/TO/RAILS/APP/public                                                                                                                                                                            
<Directory "/home/YOUR/PATH/TO/RAILS/APP/public/">                                                                                                                                                                               
Options FollowSymLinks                                                                                                                                                                                                  
AllowOverride None                                                                                                                                                                                                      
Order allow,deny                                                                                                                                                                                                        
Allow from all                                                                                                                                                                                                        
</Directory>    
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ balancer://app1-mongrels%{REQUEST_URI} [P,QSA,L]       
<Proxy balancer://app1-mongrels>                                                                                                                                                                                   
BalancerMember http://YOUR-PRIVATE-IP:8000                                                                                                                                                                                    
BalancerMember http://YOUR-PRIVATE-IP:8001                                                                                                                                                                                    
BalancerMember http://YOUR-PRIVATE-IP:8002                                                                                                                                                                                    
BalancerMember http://YOUR-PRIVATE-IP:8003                                                                                                                                                                                    
</Proxy>                                                                        
ProxyPass /images !                                                                                                                                                                                                     
ProxyPass /stylesheets !   
ProxyPass /javascripts !                                                                                                                                                                                             
ProxyPass / balancer://app1-mongrels                                                                                                                                                                          
ProxyPassReverse / balancer://app1-mongrels                                                                                                                                                                      
ProxyPreserveHost On  
</VirtualHost>

You need to have a unique name for “balancer://app1-mongrels” and the ports you use are the same that are in your mongrel_cluster.yml

Then restart apache:

# svcadm restart cswapache2

At this point, your site should be accessible without specifying the port. Check it with curl (as above) or from your browser. If you get an error page or the Joyent default page, fix your apache settings, restart apache, repeat.

5) The last step is to make it reboot ready – make sure your app is under SMF

Replace YOURAPP, the /HOME/YOUR/PATH/TO/RAILS/APP, and the USER and GROUP

<?xml version='1.0'?>
<!DOCTYPE service_bundle SYSTEM '/usr/share/lib/xml/dtd/service_bundle.dtd.1'>
<service_bundle type='manifest' name='mongrel/YOURAPP-production'>
  <service name='network/mongrel/YOURAPP-production' type='service' version='0'>
    <create_default_instance enabled='true'/>
    <single_instance/>    
    <dependency name='fs' grouping='require_all' restart_on='none' type='service'>
      <service_fmri value='svc:/system/filesystem/local'/>
    </dependency>
    <dependency name='net' grouping='require_all' restart_on='none' type='service'>
      <service_fmri value='svc:/network/loopback'/>
    </dependency>
    <dependent name='mongrel_multi-user' restart_on='none' grouping='optional_all'>
      <service_fmri value='svc:/milestone/multi-user'/>
    </dependent>    
    <exec_method name='start' type='method' exec='/opt/csw/bin/mongrel_rails cluster::start' timeout_seconds='60'>
      <method_context working_directory='/HOME/YOUR/PATH/TO/RAILS/APP'>
        <method_credential user='USER' group='GROUP' />
        <method_environment>
          <envvar name="PATH" value="/usr/bin:/bin:/opt/csw/bin" />
        </method_environment>
      </method_context>
    </exec_method>
    <exec_method name='stop' type='method' exec=':kill' timeout_seconds='60'>
      <method_context/>
    </exec_method>
  </service>
</service_bundle>

Set the path for your rails application in this line

<envvar name="PATH" value="/usr/bin:/bin:/opt/csw/bin" />

you might need to add the path for graphviz for example.

Put this xml file at:

# nano /HOME/YOUR/PATH/TO/RAILS/APP/mongrel-app-smf.xml
# cd /HOME/YOUR/PATH/TO/RAILS/APP/
# mongrel_rails cluster::stop
# rm log/*.pid
# svccfg import /HOME/YOUR/PATH/TO/RAILS/APP/mongrel-app-smf.xml
# svcadm enable mongrel/YOURAPP-production
# svcs -v | grep mongrel

Basic commands

  • To stop mongrel
     svcadm disable  mongrel/YOURAPP-production
  • To start mongrel
     svcadm enable  mongrel/YOURAPP-production
  • To restart mongrel
     svcadm restart  mongrel/YOURAPP-production
 
accelerators/apache_rails_mongrel.txt · Last modified: 2008/04/09 23:29 by bluesurge
 
Recent changes RSS feed Creative Commons License Driven by DokuWiki