Archive

Archive for February, 2009

Preparing Ruby on Rails environment – using apache for development

February 6th, 2009 Comments off

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:

  1. Possibility to run all applications with only one server,
  2. Easy to share applications on local network or even to Internet,
  3. Fast detection of problems with compatibility to passenger,
  4. 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/ruby18

Listen 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”.

Categories: Development, Linux Tags: , , , ,

Blogs upgrated to WordPress MU 2.7

February 4th, 2009 Comments off

Last night all blogs hosted under niczsoft.com were updated to newest version of WordPress MU – that is 2.7.

It looks like everything worked fine. I’m not sure but it looks like working a bit faster.

Categories: Hosting, News Tags: , ,

Preparing Ruby on Rails environment – firstapp

February 1st, 2009 2 comments

Now as we have all the environment prepared we can start with example application to check if everything is working.

For Begining we can check if postgres is working:

/etc/init.d/postgresql-8.3 status

If You did not added it to autostart it’s probably stopped, so You can start it with:

/etc/init.d/postgresql-8.3 start

And if it is not a big problem to have it running all the time add it to autostart:

rc-update add postgresql-8.3 default

Additional is needed as my previous instructions require additional step – adding postgresql support in ruby:

gem install postgres

Referring to my friend Andrzej instructions it should be ruby-pg, I’m not sure which one is better.

Following steps are very similar to instructions at netbeans docs so You can open it in separate window to have an overview of the GUI side.

Open netbeans from the console (in most distros netbeans will be in Yours applications menu and may be available on desktop):

/path_to_netbeans-6.5/bin/netbeans

Right click the empty area within Projects tab and choose “New Project”, there choose “Ruby” -> “Ruby on Rails Application” and click “Next >”. On the next screen You cna change nam of the application, You do not have to change all the params there, just click “Next >”. Following screen is “Database Configuration”, here You have to change the “Database Adapter” to “postgresql” enter Yours user name in the field “User Name” and press “Finish”.

That is Yours first application, now we will use famous functionality of rails – scaffold, right click the new project and select “Generate…”. From the “Generator:” list select scaffold, enter “Person” in “Model Name:”, enter “name:string” in “Attribute Pairs (field:type):” and press “OK”.

Worth explanation is feature of rails called pluralization, You can see in Models, that there is person.rb model, but in Controllers You have people_controller.rb controller, this is because rails calls “person”.pluralize before applying the name for controller or view. You can check this by navigating to the project “cd ~/NetBeansProjects/RailsApplication1” and entering “script/console”, here You can play with rails and Yours application.

Now after we have automaticly generated code of application we can continue with creating database for the application. Right click Your application, choose “Run/Debug Rake Task”, now choose “db:create” from the list and click the “Run” button. Ther will be no preaty message “Database created successfully”, but if You did not got any error message in few seconds it means everything went fine. Next step is to migrate database to current state – right click project, select “Migrate Database” -> “To Current Version”. In the output You should find somthing like this:

(in /home/mpapis/NetBeansProjects/RailsApplication1)
NOTICE:  CREATE TABLE will create implicit sequence “people_id_seq” for serial column “people.id”
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index “people_pkey” for table “people”
==  CreatePeople: migrating ===================================================
— create_table(:people)
-> 0.0209s
==  CreatePeople: migrated (0.0210s) ==========================================

The database is prepared for our application, now we have to change few default settings to make person the most important in our application. Please open Configuration / routes.rb file by double clicking it. Start search by CTRL+F in the Find box enter “map.root”, uncomment the found line and change “welcome” to “people”. Last step is to rename the auto generated index.html file in the public folder, right click the file, chose rename and add suffix “_old” to the displayed index in the edit box.

Thats all, click the green triangle in the toolbox on the top of netbeans. This should open Your browser with the new app, You can check it by navigating through it.

Small hint for Firefox users, there might be needed change in netbeans to open correctly – please anvigate to Menu “Tools” -> “Options” choose “Firefox” in the “Web Browser” list and click “Edit” button. Here replace “Arguments” edit box content with:

“{URL}”

In the next article we will start our application through apache passanger instead of using rails built in WEBrick.

Categories: Development, Linux Tags: , , ,