Getting your LocalHost Ruby app working on the Server

February 10th, 2010

SSH Access

You will have to have to request jailed SSH access on your hosting account by submiting a support ticket. You can then use SSH to execute commands (rake, script/generate etc) like you would locally.

To log in to SSH run:

ssh -p 2222 username@example.com

Creating Application

Even though you have created your app locally, you want to create a new app in SSH (and let it create the folder hierarchy) and then replace all the controllers/views/models/db/config etc with your app.

To create the new app in SSH:
First navigate to a location where you want to create the new app folder – it doesn’t matter where, although cpanel likes to put them in ~/etc/rails_apps/.
Run:

rails -d mysql --with-dispatchers mynewapp

You now need to upload your locally created app to the server using FTP or otherwise. Don’t overwrite the files yet! Create another folder out of the way and upload it there for now. You have to be careful when overwriting the ssh created app that you don’t accidentally wipe some necessary files.

You can completely overwrite all files and folders EXCEPT the public folder. When dealing with the public folder, I suggest you overwrite the individual images, stylesheets and javascripts folders without touching the files already in /public/.

Specifically: public/dispatch.rb , public/dispatch.cgi and public/dispatch.fcgi must all be present otherwise your app will not function.

Once you’ve got your locally created app in place go on..

.htaccess

To get the app running properly it is necessary to edit .htaccess.
Navigate to ~/etc/rails_apps/mynewapp/public:

cd /etc/rails_apps/mynewapp/public

To open .htaccess

pico .htaccess

Regardless of whether there’s already text in the file or its blank, the only necessary code is as follows:

AddHandler fcgid-script .fcgi 
AddHandler cgi-script .cgi 
Options +FollowSymLinks +ExecCGI 
  
RewriteEngine On 
RewriteCond %{REQUEST_URI} ^/notrails.* 
RewriteRule .* - [L] 
  
RewriteRule ^$ index.html [QSA] 
RewriteRule ^([^.]+)$ $1.html [QSA]     
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]

Save the .htaccess file

Database Setup

In cpanel go to MySQL databases and create a new database:
eg. New database: records

Note that cpanel will automatically add username_ to the front of it, thus database name is:
username_records
You also need to create a user for this database:
eg. Username: gkumar
pw: *****
Note that cpanel will also append username_ to the front of each user too, thus database user will be:
username_gkumar

Finally in cpanel Add user to database: username_gkumar to username_records.

You can now use this database, username and password in database.yml

Database.yml

in SSH or filemanager:

cd /etc/rails_apps/mynewapp/config/
pico database.yml

Setup database.yml for above example:

development:
pool: 5
timeout: 5000
adapter: mysql
database: username_records
username: username_gkumar
password: *******
socket: /var/lib/mysql/mysql.sock
  
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
  
production:
adapter: mysql
username: username_gkumar
password: *******
socket: /var/lib/mysql/mysql.sock
pool: 5
timeout: 5000
reconnect: true
socket: /var/lib/mysql/mysql.sock

Note: Leave test alone (if you setup test like the prod and dev you might accidentally wipe the database if you run a test). Take note of the adapter and socket, they need to be setup for mysql not sqlite.

Symlink

Currently we have the app loaded at /etc/rails_apps/mynewapp. However, pages that are visible on the web need to be in the /public_html/ directory. Because of the way rails separates public from models/views etc, we use a sym link to make the files from /mynewapp/public appear also in /public_html/. It will essentially seem like the one folder (public) has 2 different paths to it.

Do you want the app to be displayed at www.example.com/ or www.example.com/blog/? Do you have addon-domains or is this your only domain?

Case: Display at example.com/ and yes I have addon domains

If we want to display at example.com/ we need to create a symlink to /public_html/. The issue is that you must delete public_html and then recreate it with the symlink. (can’t symlink to an existing folder) As you probably know, addon domains are stored in /public_html/addondomain.com/. To ensure that you don’t loose you addon domain data it is necessary to:

1. Copy the addondomain.com folder/s elsewhere
2. Delete public_html
3. Create the symlink (which will recreate /public_html)
4. Copy the addondomain.com folder/s back to their original location

So assuming you’ve backed up you addon domains and deleted public_html:

In SSH (at /home/username/) execute:

ln -s ~/etc/rails_apps/mynewapp/public ~/public_html

You will now find that in public_html you have the files and folders (images/stylesheets etc..) that are located in /mynewapp/public. Don’t forget to copy back you addon domains.

This is the hardest scenario, if you have no addon domains – obviously the same procedure applies without needing to backup anything.

If you want the rails app to display on an addon domain:

ln -s ~/etc/rails_apps/mynewapp/public ~/public_html/addondomain.com

If you want the rails app to display at mydomain.com/blog: (ensure no folder named blog exists)

ln -s ~/etc/rails_apps/mynewapp/public ~/public_html/blog

Also add to mynewapp/config/environment.rb:

ENV['RAILS_RELATIVE_URL_ROOT']="/blog"

Environment.rb

At the top of the file you can add:

ENV['RAILS_ENV'] ||= 'production'

In the Rails::Initializer.run do |config| block:

config.load_paths += %W( #{RAILS_ROOT}/vendor/plugins )

Gems

You can also have problems if you are using gems in your app that SpeedHost hasn’t got installed. You can view which gems are installed in cpanel RubyGems and also install gems through cpanel. If there is a gem missing from our server, you can send us an email and we will have it installed on the server.

Leave A Comment