Deployment
CS290F Fall 2006 - UCSB Computer Science - Thorsten von Eicken
(You may want to check out the discussion page...)
For deployment you will need the following pieces:
- An EC2 server running the "FC5+apache2.2+rails+mysql5.0 v3" image. This image has Apache 2.2, mysql 5.0, and mongrel_cluster installed.
- Your code in a repository, preferably SVN.
- A dump of your database available, preferably on S3.
- Capistrano installed on your development machine.
- A public/private keypair in config/deploy-key and config/deploy-key.pub
- Your own copy of config/deploy.rb edited according to the comments in the file.
The best place to start for an overview is the Agile Web Development book. Read sections 27.4 "Repeatable Deployments with Capistrano" and 27.5 "Setting Up A Deployment Environment". There is also a capistrano manual. It's outdated, but the basic stuff that you will need is there just fine.
Launching a new server
1. Launch the "FC5+apache2.2+rails+mysql5.0 v2" image on a server and use the SSH console to log in.
2. Create a user account, install your deployment SSH key, and enable sudo (note that you need to replace 'tve' by your user name in the last command):
# adduser tve # mkdir /home/tve/.ssh # cat >/home/tve/.ssh/authorized_keys ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCtb43YVY3X12rDBY0DtfIpwTOwIDbMLW1Affv4BBPM fp8DJgVGryX6TadsMJj7t/2ZHB6+9HcsXxk41y4qIVXzyB7SJW9cTD+0t8a2kHgbHUQ4r1f40zEIw8Y9 Ff6ZGkuAzItCxmeKWNtxNd4hdUMKpN1bIIMU/Q01H4aLB51lrALepq0NYWWt8tSh/hvBpO6PqPHhxxk7 XSb2yf4RM5mCP3ompKvT6WeFxvbpq3LhSaZokOvLBylbDp/zjgblOidJ+/izdQm20u25jvjgYwJtLU3/ ku27QuwvL5i3XQGCikEoQQSO/q+d1oVTsoBv4h0VOixBcHRJtPZEuxBi/m6h TvE SSH Key # perl -pi -e '/NOPASSWD/ && s/^..//' /etc/sudoers # perl -pi -e '/^wheel/ && s/$/,tve/' /etc/group
The key is what you find in config/deploy-key.pub
3. Use an SSH client to log into your account using the key you just installed and test sudo. Note that the sudo echo hello is run on the server and should not ask for a password.
# ssh -i config/deploy-key domU-12-31-33-00-02-12.usma1.compute.amazonaws.com # sudo echo hello
4. Edit config/deploy.rb and set the host name according to your instance around line 10.
role :web, "domu-12-31-33-00-02-12.usma1.compute.amazonaws.com" role :db, "domu-12-31-33-00-02-12.usma1.compute.amazonaws.com", :primary => true
5. Set-up capistrano on the server (run cap setup this on your development machine).
cap setup
6. On the server, load the database with your data, here we assume your data is on S3 in bucket project and object database-2006-11-18.dump.gz.
sudo /etc/init.d/mysqld start cd /home/rails/project/shared s3cmd get project:database-2006-11-18.dump.gz database-2006-11-18.dump.gz mysqladmin -u root create project_production gunzip <project-20061118.dump.gz | mysql -u root project_production mysql -u root project_production -e \ "GRANT ALL ON project_production.* TO 'prod'@'localhost' IDENTIFIED BY '!password!!';" mysqladmin -u root password '!@#$%^&*'
7. On the server, install any gems you may need
gem install ...
8. Back on your development machine, you can now deploy your code.
cap cold_deploy
9. On the server again, start apache.
sudo /etc/init.d/httpd start
10. Point your browser at the machine, and your site should be up!
Updating your server
Make your changes, test them locally (!) and check your code in (this last part is easy to forget).
cap deploy
If you have migrations that need to be run, you can have capistrano do that as well.
cap deploy_with_migrations
More capistrano
What else can capistrano do?
cap show_tasks
Deploying to multiple servers
To test things out, I launched three servers, one for the database, one for Rails, and one for apache. (I don't claim this makes sense from a performance point of view, I just wanted to test out having all three pieces on different machines.) I did it manually without fixing the deploy.rb. Here are the things I touched:
- Inserted the host name of the three servers in my deploy.rb so I could easily "cap setup" and "cap deploy" to get stuff everywhere (not entirely needed, but convenient to get started)
- Remove the disable networking line in /etc/my.cnf
- Changed mysql grant statement to read
to 'prod'@'%' - Changed /etc/rails/project.conf to have the hostname of the Rails server instead of 127.0.0.1 in the BalancerMember statements.
- Changed /etc/mongrel_cluster/project.yml to have address 0.0.0.0 instead of 127.0.0.1
- Changed config/database.yml to have database host name in the host field
- Changed
address: 127.0.0.1toaddress: 0.0.0.0in config/deploy.rb
Wow, took a little over an hour. Cutting and pasting host names between machines is a royal pain in the ... I don't want to do this a 2nd time. Gotta fix the capistrano deploy.rb to do it for me...
Bugs & omissions
Please email me! TvE 18:51, 18 November 2006 (PST)
