public
Description: Instructions for a production installation of Ruby on Rails on Ubuntu 8.04 Hardy Heron
Homepage: http://marklunds.com/articles/one/392
Clone URL: git://github.com/peter/rails-on-ubuntu.git
rails-on-ubuntu / README
100644 349 lines (283 sloc) 10.658 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
 
###################################################################
#
# INTRODUCTION
#
###################################################################
 
What you are looking at is detailed instructions for setting up a Ruby on Rails production environment on Ubuntu 8.04 Hardy Heron. The instructions have primarily been tested on a Gold VPS at the swedish hosting company GleSYS.se (see http://glesys.se/serverhotell/vps). I would like to take this opportunity to thank the kind folks at GleSYS for supporting me in this project and for providing me with excellent hosting. The instructions are generic and should work on any clean Ubuntu 8.04 install. They have been tested successfully at http://slicehost.com as well.
 
The instructions assume familiarity with tools such as Linux, ssh, Ruby on Rails, Capistrano, Mongrel, and MySQL. They draw heavily on the excellent book "Deploying Rails Applications" by Ezra Zygmuntowicz. I strongly encourage you to check out the book if you haven't already. Like with all Pragmatic Bookshelf books, it can be conveniently downloaded as a PDF.
 
As of August the 10:th 2008, the instructions install the following versions of the technology stack:
 
mysql 5.0.51a
nginx 0.6.31 (compiled with fair proxy balancer)
monit 4.8.1
ruby 1.8.6, patchlevel 111
gem 1.2.0
 
Make sure you fully understand the instructions as you go through them step by step. Also, in the end, don't forget to customize the config files to fit your needs, especially the Nginx, Monit, and Mongrel Cluster ones.
 
If you find mistakes or have suggestions for improvement, please get in touch with me at peter@marklunds.com.
 
Peter Marklund
http://marklunds.com
 
Friday, August 10, 2008
Stockholm
 
###################################################################
#
# BASIC SERVER SETUP
#
###################################################################
 
############################
# Set Root Password (if you haven't already)
############################
 
ssh root@<server-ip-number>
passwd
 
############################
# Install Editor (Optional)
############################
 
apt-get install emacs22
export EDITOR=emacs
 
############################
# Add the Rails Deploy User
############################
 
adduser deploy
# Give deploy sudo access
adduser deploy sudo
# Uncomment sudo group line
visudo
 
############################
# Setup ssh keys
############################
 
su deploy
# Press enter three times
ssh-keygen
exit
exit
 
# Locally - add your server to .ssh/config
Host = <name-of-your-server>
Hostname = <server-ip-number-or-domain>
User = deploy
 
# Convenient function for adding ssh keys, you can add it to your ~/.bashrc
function authme {
    ssh $1 'cat >>.ssh/authorized_keys' <~/.ssh/id_rsa.pub
}
source ~/.bashrc
 
authme <name-of-your-server>
# You should now be able to login without password
ssh <name-of-your-server>
 
############################
# Upgrade packages
############################
 
# Check out which apt-get sources you are dealing with
sudo emacs /etc/apt/sources.list
# You might want to add hardy-security so that the file becomes:
# deb http://archive.ubuntu.com/ubuntu/ hardy main restricted universe
# deb http://archive.ubuntu.com/ubuntu/ hardy-updates main restricted universe
# deb http://security.ubuntu.com/ubuntu hardy-security main restricted universe
 
# Update
sudo apt-get update
sudo apt-get upgrade
 
############################
# Install essential tools
############################
 
sudo apt-get install build-essential man
 
############################
# Install Git and Subversion
############################
 
# We include Subversion since not everyone uses Git, yet, you know
sudo apt-get install git-core subversion subversion-tools
 
############################
# Setup deploy User Environment
############################
 
cd
git clone git://github.com/peter/rails-on-ubuntu.git
mv .bashrc .bashrc.orig
cp rails-on-ubuntu/.bashrc .
source ~/.bashrc
cp rails-on-ubuntu/.emacs .
 
############################
# Set the Timezone and Keep the Time Synced
############################
 
sudo dpkg-reconfigure tzdata
# Check time is correct
date
sudo apt-get install ntp
 
############################
# Set the hostname
############################
 
sudo hostname your-hostname
sudo emacs /etc/hosts
sudo emacs /etc/hostname
# Check hostname is correct
hostname
 
############################
# Change ssh to run on non-standard port
############################
 
# Choose some unused port number such as 7243 (not 80 or 443) between 1024 and below 65537
# Make sure to make a note of the number.
sudo emacs /etc/ssh/sshd_config
sudo /etc/init.d/ssh restart
exit
# Add to the section for your server in ~/.ssh/config:
# Port = <your-port-number>
ssh <name-of-your-server>
 
############################
# Configure a firewall that only allows incoming http, https, and ssh traffic
# (remembering that ssh port was changed)
############################
 
sudo apt-get install iptables
sudo mkdir /usr/local/scripts
sudo cp ~/rails-on-ubuntu/fw /usr/local/scripts
# Edit the firewall config and change <your-ssh-port> to your port number
sudo emacs /usr/local/scripts/fw
sudo /sbin/iptables-restore < /usr/local/scripts/fw
# Test the firewall
exit
# Should not be able to connect to default port
telnet <ip-of-your-server> 22
# Should be able to connect this time
telnet <ip-of-your-server> <your-ssh-port>
# Note: if you have locked yourself out of your server at GleSYS#
# you can reset the firewall in the web admin panel.
ssh <name-of-your-server>
 
###################################################################
#
# RUBY ON RAILS STACK
#
###################################################################
 
############################
# Ruby
############################
 
# Install Ruby
sudo apt-get install ruby-full
# Check that Ruby is there
which ruby
#=> /usr/bin/ruby
ruby -v
#=> ruby 1.8.6 (2007-09-24 patchlevel 111) [i486-linux]
ruby -ropenssl -rzlib -rreadline -e "puts :success"
#=> success
 
############################
# RubyGems
############################
 
# Install RubyGems
cd /usr/local/src
# Get latest stable recommended release of RubyGems from rubygems.org
sudo wget http://rubyforge.org/frs/download.php/38646/rubygems-1.2.0.tgz
sudo tar xzf rubygems-1.2.0.tgz
cd rubygems-1.2.0
sudo ruby setup.rb
sudo ln -s /usr/bin/gem1.8 /usr/bin/gem
which gem
#=> /usr/bin/gem
gem --version
#=> 1.2.0
 
############################
# MySQL
############################
 
# Install MySQL
sudo aptitude install mysql-server mysql-client libmysqlclient15-dev
# Check mysql is there
which mysql
#=> /usr/bin/mysql
mysql --version
#=> mysql Ver 14.12 Distrib 5.0.51a, for debian-linux-gnu (i486) using readline 5.2
mysql
show databases;
exit
 
# Install MySQL C Driver for Ruby
sudo gem install mysql
# NOTE: if you have issues compiling the MySQL driver related to make, try:
# sudo apt-get install make
 
############################
# Ruby Gems that we Need
############################
 
# Install a few gems used in Rails deployment
sudo gem install --no-rdoc --no-ri rake rails capistrano mongrel_cluster
 
############################
# Nginx
############################
 
# Install Nginx with Fair Proxy Balancer
sudo apt-get install libpcre3-dev libpcre3 openssl libssl-dev
cd /usr/local/src
sudo wget http://sysoev.ru/nginx/nginx-0.6.31.tar.gz
sudo tar xzvf nginx-0.6.31.tar.gz
sudo cp -r ~/rails-on-ubuntu/nginx-upstream-fair .
# If you want to live on the edge and get the latest nginx-upstream-fair, then use:
# sudo git clone git://github.com/gnosek/nginx-upstream-fair.git
cd nginx-0.6.31
sudo ./configure --with-http_ssl_module --add-module=/usr/local/src/nginx-upstream-fair
sudo make
sudo make install
which nginx
#=> /usr/local/nginx/sbin/nginx
 
# Configure Nginx and start it
sudo cp ~/rails-on-ubuntu/nginx/nginx.conf /usr/local/nginx/conf
sudo cp -r ~/rails-on-ubuntu/nginx/vhosts /usr/local/nginx/conf
sudo cp ~/rails-on-ubuntu/nginx/nginx.init /etc/init.d/nginx
sudo chmod +x /etc/init.d/nginx
sudo /usr/sbin/update-rc.d -f nginx defaults
sudo /etc/init.d/nginx start
 
############################
# Sending SMTP from localhost
############################
 
# Install Postfix so we can send SMTP from localhost.
# You can get away with accepting the defaults during install.
sudo apt-get install postfix
 
############################
# Monit
############################
 
# Monit - install, configure, and start
sudo apt-get install monit
sudo cp ~/rails-on-ubuntu/monit/monitrc /etc/monit
sudo chmod 700 /etc/monit/monitrc
sudo cp -r ~/rails-on-ubuntu/monit/monit.d /etc
# Replace <your-email> with your email address
sudo emacs /etc/monit/monitrc
# Replace my_app with the name of your app
sudo emacs /etc/monit.d/mongrel.monitrc
# Review the files under /etc/monit.d and make sure you understand the settings and that
# they are appropriate for your server.
# Change to startup=1
sudo emacs /etc/default/monit
sudo /etc/init.d/monit start
sudo monit status
 
###################################################################
#
# DEPLOYMENT
#
###################################################################
 
############################
# Deployment Setup
############################
 
# Setup directory where your Rails app will be deployed to on the server
sudo mkdir -p /var/www/apps
sudo chown deploy.deploy /var/www/apps
 
# Setup the Capistrano file structure from your local machine
exit
cd <path-to-your-local-rails-app>
# If you haven't capified your up, run "capify ."
# If you don't have Capistrano: sudo gem install capistrano
# There is an example app here: http://svn.marklunds.com/my_app/
# Edit config/deploy.rb, for an example, see:
# http://svn.marklunds.com/my_app/config/deploy.rb
cap deploy:setup
# Capistrano sets up the files as root, however later we need to access them as the deploy
# user. Change ownership to the deploy user:
cap invoke COMMAND="sudo chown -R deploy.deploy /var/www/apps/my_app"
 
############################
# Setup the database
############################
 
# Configure MySQL and create databases
ssh <name-of-your-server>
sudo /etc/init.d/mysql stop
sudo cp /etc/mysql/my.cnf /etc/mysql/my.cnf.orig
sudo cp ~/rails-on-ubuntu/my.cnf /etc/mysql
sudo /etc/init.d/mysql start
mysqladmin -u root -p<mysql-password> create my_app_production
 
############################
# Deploy
############################
 
# Deploy cold from your local machine
exit
cd <path-to-your-local-rails-app>
# Add a file to your scm at config/mongrel_cluster.yml, see:
# http://svn.marklunds.com/my_app/config/mongrel_cluster.yml
cap deploy:cold
 
Hit http://<your-server-ip-or-domain> in your browser.
 
Good luck!