Preparing Ruby on Rails environment – using apache for development
In previous post we have created first rails application, it was tested using WEBrick which is part of rails environment. To have environment more similar to server environment and to get rid of keeping track of started projects we will switch to running applications with apache and passenger. There are some benefits of this configuration:
- Possibility to run all applications with only one server,
- Easy to share applications on local network or even to Internet,
- Fast detection of problems with compatibility to passenger,
- There are probably more benefits, If You know them leave me a comment.
So lets start with configuration, on beginning turn off apache:
/etc/init.d/apache2 stop
As a central point of configuration we will use ~/public in my case it is /home/mpapis/public, we need to create this directory:
mkdir /home/mpapis/public
Configure Your /etc/hosts to have some meaningful name for running it in the browser, so edit /etc/hosts and modify Your localhost line to look similar to this one (added rails):
127.0.0.1 localhost rails
We need to configure apache to respond to our requests, first remove or backup default vhost configuration:
cd /etc/apache2/vhosts.d
mv 00_default_vhost.conf 00_default_vhost.conf.old
After getting rid of default configuration we can edit apache/passenger configuration file created in one of previous posts /etc/apache2/vhosts.d/01_passenger.conf:
LoadModule passenger_module /usr/lib64/ruby/gems/1.8/gems/passenger-2.0.6/ext/apache2/mod_passenger.so
PassengerRoot /usr/lib64/ruby/gems/1.8/gems/passenger-2.0.6
PassengerRuby /usr/bin/ruby18Listen 127.0.0.1:80
NameVirtualHost 127.0.0.1:80
UseCanonicalName Off
<VirtualHost 127.0.0.1:80>
DocumentRoot /home/mpapis/public
ServerName rails
ServerAlias localhost
RailsEnv development
Include /home/mpapis/public/paths.conf
<Directory “/home/mpapis/NetBeansProjects”>
Options -Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
<Directory “/home/mpapis/public”>
Options -Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
One of the lines defines that we will use /home/mpapis/public/paths.conf as part of the configuration, and this is the contents of this file:
RailsBaseURI /RailsApplication1
As we pointed /home/mpapis/public in previous file as document root and /RailsApplication1 as path of our application we need to create symbolic link to our application:
ln -s /home/mpapis/NetBeansProjects/RailsApplication1/public/ /home/mpapis/public/RailsApplication1
Last step is to notify our application that it will no more run from the / path, so we add the following entry to /home/mpapis/NetBeansProjects/RailsApplication1/config/environments/development.rb:
config.action_controller.relative_url_root = “/RailsApplication1”
That’s all, we can start appache now:
/etc/init.d/apache2 start
And if it is desired add it to autostart (to have our applications available just after system boot):
rc-update add apache2 default
Finally we can check work of our application by navigating in web browser window to the following path:
http://rails/RailsApplication1/
Looks pretty nice, in next step we will need to automate this a bit to avoid manual work each new project:
Rails try to follow DRY which stands for “don’t repeat yourself”.