-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrails-heroku-tutorial.html
More file actions
194 lines (192 loc) · 11.7 KB
/
rails-heroku-tutorial.html
File metadata and controls
194 lines (192 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Deploying to Heroku with Rails 3.1</title>
<link rel="stylesheet" href="/css/screen.css" type="text/css" charset="utf-8" />
<link rel="stylesheet" href="/css/gollum.css" type="text/css" charset="utf-8" />
<link rel="stylesheet" href="/css/syntax.css" type="text/css" charset="utf-8" />
<script src="http://code.jquery.com/jquery-1.6.min.js" type="text/javascript"></script>
<script src="/javascript/jquery.text_selection-1.0.0.min.js" type="text/javascript"></script>
<script src="/javascript/jquery.previewable_comment_form.js" type="text/javascript"></script>
<script src="/javascript/jquery.tabs.js" type="text/javascript"></script>
<script src="/javascript/gollum.js" type="text/javascript"></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-5109366-14']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body>
<div id="main">
<div class="site">
<div id="guides">
<div class="guide">
<div class="main">
<div class="actions">
<a href="http://railsapps.github.com/"><img src="http://railsapps.github.com/images/rails-36x36.jpg" title="Home" alt="Home"></a>
<a href="http://railsapps.github.com/">Rails Apps</a>
<a href="https://github.com/RailsApps">GitHub Repository</a>
</div><!-- class="actions" -->
<h1>Deploying to Heroku with Rails 3.1</h1>
<div class="content wikistyle gollum textile">
<h1>Deploying to Heroku with Rails 3.1</h1>
<p>This is a tutorial for developers using the Rails 3.1 example apps from the <a href="http://railsapps.github.com/">Rails Apps</a> repository. Others may find it helpful as well.</p>
<h2>Background</h2>
<p>For your convenience, here are instructions for deploying your app to Heroku. Heroku provides low cost, easily configured Rails application hosting.</p>
<p>For more detail, see Heroku’s document <a href="http://devcenter.heroku.com/articles/rails3">Getting started with Rails 3 on Heroku/Cedar</a>. See also <a href="http://devcenter.heroku.com/articles/rails31_heroku_cedar">Rails 3.1 on Heroku Cedar</a>.</p>
<p>You’ll be using Heroku’s Celadon Cedar stack which requires Ruby 1.9.2.</p>
<h2>Set Up Heroku</h2>
<p>To deploy an app to Heroku, you must have a Heroku account. Visit <a href="http://heroku.com/">http://heroku.com/</a> to set up an account.</p>
<h4>Replace SQLite with PostgreSQL</h4>
<p>First, before you get caught up in choosing between PostgreSQL, SQLite or MySQL for development, I recommend looking at MongoDB as an alternative. It simplifies development by eliminating schemas and migrations and is supported as an add-on by Heroku.</p>
<p>If you are developing locally using SQLite, you will need to switch to PostgreSQL for deployment to Heroku. Heroku does not support SQLite or MySQL. If you don’t want to develop using PostgreSQL locally, you can set up your Gemfile to use SQLite for development and PostgreSQL for production.</p>
<p>To switch from SQLite to PostgreSQL for deployment to Heroku, edit your Gemfile and change this line:</p>
<p>
<code>gem 'sqlite3'</code>
</p>
<p>To this:</p>
<pre>
group :production do
gem 'pg'
end
group :development, :test do
gem 'sqlite3'
end
</pre>
<h4>Replace Webrick with Thin</h4>
<p>By default, when you run <code>rails server</code> locally, Rails launches the simple Webrick web server. For production apps Heroku recommends using a more robust webserver such as <a href="http://code.macournoyer.com/thin/">Thin</a>.</p>
<p>To use Thin, add the gem to your Gemfile:</p>
<p>
<code>gem "thin"</code>
</p>
<p>Heroku recommends adding a Procfile to your app. Procfile is a mechanism for declaring what commands are run when your web app runs on the Heroku platform. A Procfile is not necessary to deploy a Rails app to Heroku, but Heroku recommends it for greater control and flexibility over your app. If you add a Procfile, you can specify Thin as your web process in the Procfile.</p>
<p>For simplicity, there’s no need to create a Procfile if you use Thin. If you declare the Thin gem in your Gemfile, Heroku sets up an appropriate web process without requiring a Procfile.</p>
<h4>Heroku Gem</h4>
<p>Make sure the Heroku gem is in your Gemfile <a href="http://rubygems.org/gems/heroku">(check rubygems.org for the latest heroku gem)</a>:</p>
<p>
<code>gem "heroku"</code>
</p>
<h4>Install Gems</h4>
<p>Install your gems with:</p>
<p>
<code>$ bundle install</code>
</p>
<p>If you haven’t done so, add your public key after installing the heroku gem so that you can use git to push or clone Heroku app repositories. See <a href="http://devcenter.heroku.com/articles/heroku-command">http://devcenter.heroku.com/articles/heroku-command</a> for details.</p>
<h4>Rails 3.1 for Heroku</h4>
<p>Heroku supports Rails 3.1 on Heroku’s Cedar stack. Heroku will precompile Javascript and <span class="caps">CSS</span> assets when you deploy your app. Prior to Rails 3.1.0.rc5, Heroku required an additional gem <code>therubyracer-heroku</code>. This gem is no longer needed for Rails 3.1 and should not be used.</p>
<h4>Commit to Git</h4>
<p>Store the modified application in the Git repository:</p>
<p>
<code>$ git commit -am "Configured for deployment to Heroku"</code>
</p>
<h4>Create Your Application on Heroku</h4>
<p>Use the Heroku create command to create and name your new app.</p>
<p>
<code>$ heroku create _myapp_ --stack cedar</code>
</p>
<h4>Check Heroku Configuration</h4>
<p>You can check that everything has been added correctly by running:</p>
<p>
<code>$ heroku info --app myapp</code>
</p>
<h4>Heroku Add-on for MongoDB</h4>
<p>If you are using MongoDB, you can use a Heroku add-on to deploy your app using the MongoHQ service. See <a href="http://addons.heroku.com/mongohq">details about the service</a> and <a href="http://devcenter.heroku.com/articles/mongohq">details about installation</a>.</p>
<p>To enable the add-on, enter the following command:</p>
<p>
<code>$ heroku addons:add mongohq:free</code>
</p>
<h4>Push Your Application to Heroku</h4>
<p>Push your application to Heroku:</p>
<p>
<code>$ git push heroku master</code>
</p>
<p>Initialize your application database:</p>
<p>
<code>$ heroku run rake db:seed</code>
</p>
<h4>Visit Your Site</h4>
<p>Open your Heroku site in your default web browser:</p>
<p>
<code>$ heroku open</code>
</p>
<h4>Troubleshooting</h4>
<p>If you get errors, you can troubleshoot by reviewing the log files:</p>
<p>
<code>$ heroku logs</code>
</p>
<h4>Troubleshooting Problems Connecting to MongoHQ</h4>
<p>If you get the error message “failed to connect to any given host:port” or “Failed to connect to a master node at localhost:27017”, the <strong>config/mongoid.yml</strong> file may not have the correct MongoHQ connection parameters.</p>
<p>If the file <strong>config/mongoid.yml</strong> contains this:</p>
<pre>
# set these environment variables on your prod server
production:
host: <%= ENV['MONGOID_HOST'] %>
port: <%= ENV['MONGOID_PORT'] %>
username: <%= ENV['MONGOID_USERNAME'] %>
password: <%= ENV['MONGOID_PASSWORD'] %>
database: <%= ENV['MONGOID_DATABASE'] %>
</pre>
<p>modify it to look like this:</p>
<pre>
# set these environment variables on your prod server
production:
uri: <%= ENV['MONGOHQ_URL'] %>
</pre>
<p>Then push your application to Heroku again with <code>$ git push heroku master</code> and run <code>$ heroku rake db:seed</code> again.</p>
</div><!-- class="content" -->
<div class="comments">
<div class="content wikistyle gollum">
<h2>Comments</h2>
</div>
<p>Is this helpful? Please "like" below. Question or suggestion? Please add a comment below. Got a correction or addition? You can edit this page <a href="https://github.com/railsapps/railsapps.github.com/wiki/_pages">on the wiki</a> or create a <a href="https://github.com/RailsApps/railsapps.github.com/issues">GitHub issue</a> to alert me.</p>
<div id="disqus_thread"></div>
<script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'railsapps'; // required: replace example with your forum shortname
// The following are highly recommended additional parameters.
// var disqus_identifier = 'rails-heroku-tutorial.html';
// var disqus_url = 'http://railsapps.github.com/rails-heroku-tutorial.html';
/* * * DON'T EDIT BELOW THIS LINE * * */
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
</div><!-- class="comments" -->
<br />
<br />
<div class="credits">
<div class="content wikistyle gollum">
<h2>Credits</h2>
</div>
<p>Daniel Kehoe (<a href="http://danielkehoe.com/">http://danielkehoe.com/</a>) initiated the project and wrote some examples and tutorials.</p>
<p>Your contributions are welcome!</p>
</div><!-- class="credits" -->
<div class="admin">
<small>Corrections? Additions? You can edit this page <a href="https://github.com/railsapps/railsapps.github.com/wiki/_pages">on the wiki</a>.</small>
<div style="float: right;">
<small>Last edited by <b>Daniel Kehoe</b>, 2011-09-01 12:24:10</small>
</div>
</div><!-- class="admin" -->
</div><!-- class="main" -->
</div><!-- class="guide" -->
</div><!-- id="guides" -->
</div><!-- class="site" -->
</div><!-- id="main" -->
<!-- MixPanel analytics -->
<script type='text/javascript'> var mp_protocol = (('https:' == document.location.protocol) ? 'https://' : 'http://'); document.write(unescape('%3Cscript src="' + mp_protocol + 'api.mixpanel.com/site_media/js/api/mixpanel.js" type="text/javascript"%3E%3C/script%3E')); </script> <script type='text/javascript'> try { var mpmetrics = new MixpanelLib('b1cf97418f8c5b129847e8a52edbae68'); } catch(err) { null_fn = function () {}; var mpmetrics = { track: null_fn, track_funnel: null_fn, register: null_fn, register_once: null_fn, register_funnel: null_fn }; } </script>
<!-- Clicky analytics -->
<script src="//static.getclicky.com/js" type="text/javascript"></script>
<script type="text/javascript">try{ clicky.init(66423523); }catch(e){}</script>
<noscript><p><img alt="Clicky" width="1" height="1" src="//in.getclicky.com/66423523ns.gif" /></p></noscript>
</body>
</html>