Pre-requisites
First and foremost, you will need SSH. You need to create a support ticket for your account to get it enabled. In this tutorial, we will use the "ssh" command-line utility (which comes bundled with most Unix-like systems). There are some Windows applications (such as PuTTY) available as well.
Freezing your app
If you've developed your app locally, you'll want to freeze your gems before you upload it. This means your code will run against the same gems you have installed on your computer, and not whatever version happens to be installed on ASO's servers. Make sure to keep your app's gems up to date with security fixes and such as time goes on.
cd ~/myapp rake rails:freeze:gems
File upload
Second, we are going to use a utility called "scp" that uses the SSH protocol to transfer files. The following commands will transfer a directory called "myapp" to your account in a folder called "myapp" (where "username" is your ASO account username and "example.com" is your domain).~ scp -r myapp/ username@example.com:
After some time, it will complete. Now:
ssh username@example.com
Enter your password.
Database initialisation
At this point, if you have not set up your MySQL database on ASO, you should do that now. You have to create a database, user, and password on your cPanel (accessed at http://example.com/cpanel). Now back at the terminal let's update the config file:cd ~/myapp/config/ nano database.yml
And update the data under "production:" to reflect the database and user you created. In the database name and username, you will need to include your account name prefix. For example, if your account name is "chiefwiggum" and you made database called "perpetrators", the you'll type the database name "chiefwiggum_perpetrators" into the database.yml file.
Permissions
Ruby on Rails won't start if your source code is write enabled for anyone other than you.
ls -lh myapp drwxrwsrwx 14 me mygroup 4.0K Aug 23 08:39 myapp/
That second 'w' means that group writing is enabled. While the third 'w' indicates that anyone can write to myapp/. Rails will refuse to start if either of these are set, so we must turn them off:
chmod -R go-w myapp
This will remove (-) write permissions (w) from myapp/ and all subdirectories (-R) for groups (g) and other users (o).
Database update
Now our application is on the server, but the database isn't set up/up to date. To do this:
cd ~/myapp rake db:migrate RAILS_ENV=production
This will use the database migrations to create the database and set the Rails environment to production. Just to be sure, however:
cd ~/myapp/config nano environment.rb
And remove the comment before the line:
ENV['RAILS_ENV'] ||= 'production'
(For Rails 2.3.3 and above, this line will not appear at all in environment.rb. You'll need to add it at the top of the file.)
Linking to public_html
Finally, we uploaded the application to the directory ~/myapp. However, when a request is made to the server, you can only actually see requests in ~/public_html directory. You should NEVER upload your application to the public_html directory. Instead, we create a symbolic link:
~ ln -sf ~/myapp/public ~/public_html/myapp
This would create a symbolic link between your applications public directory and the /myapp directory on your domain. So, now, to view your application, you would go to http://example.com/myapp. If you wanted it to be just at http://example.com, you would have to create a symbolic link with public_html. So instead of the last command, you would do (THIS WILL DELETE ALL OF YOUR public_html CONTENTS):
~ rm -rf ~/public_html ln -sf ~/myapp/public ~/public_html
Now there isn't actually a directory called "public_html", but instead it is a link to ~/myapp/public.
Lastly, if you're running your app out of a subdirectory of your site, you'll need to update the .htaccess file to use mod_rails/Passenger, which ASO uses for running Rails apps. Add this line into the .htaccess file in your app's 'public' directory:
RailsBaseURI /myapp
Note: You don't need to do this if you're replacing public_html. mod_rails will see the app automatically.
You will probably want to leave the existing contents of the .htaccess, as there might be custom rewrite rules in there for particular URLs used by the application. mod_rails will override any use of FastCGI or CGI in the file.
A note on updating your application
When you update your application (a.ka. upload updated files), the chances are that there will still be a mod_rails process running. Restarting is very simple. Just upload a blank file named restart.txt into ~/myapp/tmp/. mod_rails looks at the time when the file was last updated and will restart your application if that file is newer than when it started your app. Any time you need to restart the application, simply upload the blank restart.txt file again to update it's timestamp.
You can also upload a blank always_restart.txt file into the tmp directory and will always restart your application on every request. However, this will cause a significant performance degradation, so only use it for debugging.
Congratulations, you have successfully deployed a Rails application on A Small Orange!
There are 3 comments on this page. [Display comments]