Search code examples
ruby-on-rails-3apachehosts

Ruby on Rails - etc/hosts config file


This is what I have in my "etc/hosts" file:

127.0.0.1 localhost

127.0.1.1 gotqn-System-Product-Name

127.0.0.1 depot.yourhost.com

and this is what I have added in the apache2.conf file:

<VirtualHost *:80>

   ServerName depot.yourhost.com

   DocumentRoot "/home/gotqn/Aptana Projects/depot/public/"

   <Directory "/home/gotqn/Aptana Projects/depot/public">

      AllowOverride all

      Options -MultiViews

      Order allow,deny

      Allow from all

   </Directory>

</VirtualHost>

When I type http://localhost/ my browser lists all my files in the "var\www" folder and when I type http://depot.yourhost.com/ it displays the following error:

We're sorry, but something went wrong.

I have checked the production.log file of my rails applications and it says:

Connecting to database specified by database.yml

Started GET "/" for 127.0.0.1 at 2013-01-13 20:32:41 +0200 Processing by StoreController#index as HTML Completed 500 Internal Server Error in 3ms

ActiveRecord::StatementInvalid (Could not find table 'carts'):
app/controllers/application_controller.rb:46:in rescue in current_cart' app/controllers/application_controller.rb:44:in current_cart' app/controllers/store_controller.rb:11:in `index'

I suppose that there is something wrong with my database configuration as the table exists but it is not found.

Anyway, my questions is how the hosts file knew that "depot.yourhost.com" means to open the rails applications and "localhost" do list my "var\www" folder - I have thought that RoR is using port 3000.

Why there is no conflict for port 80?


Solution

  • Rails applications run under port 3000 when running with the lightweight Webrick server. Since you are running Apache with mod_passenger, the application will be available at port 80 according to your Apache configuration.

    The hosts file contains an entry 127.0.0.1 depot.yourhost.com which directs that domain name to your local adapter 127.0.0.1. The hosts file should always be checked before a nameserver needs to be consulted to retrieve a name's address.

    Next, your Apache configuration includes a VirtualHost which is listening on all network adapters due to the * in

    <VirtualHost *:80>
    

    Your Apache configuration must be setup to use name-based virtual hosting, after which the ServerName variable is matched against the HTTP_HOST request header which was supplied by your request to depot.yourhost.com.

    Therefore, you can have as many VirtualHosts as needed all using port 80 on the same IP address, and Apache will decide which to route the request to based on the HTTP_HOST header.

    Finally, mod_passenger will detect if an application is a Ruby on Rails application by inspecting the directory contents around the DocumentRoot which you have specified in the VirtualHost configuration. If it finds the public directory as its DocumentRoot and a RoR application at a level higher, Passenger will attempt to start the Rails app.

    Now, as to why you are getting a directory listing from localhost... Elsewhere in your Apache configuration you must have a VirtualHost which matches the ServerName localhost and sets its DocumentRoot to /vart/www.

    <VirtualHost *:80>
      ServerName localhost
      # or...
      ServerAlias localhost
    </VirtualHost>
    

    If you are seeing the contents of the directory listed out rather than a 403 Forbidden (absent an index file like index.html), it is because the Apache server config or VirtualHost config does not include the directive:

    Options -Indexes
    

    Add that in the global httpd.conf to prevent listing directories server-wide.