Full Rails Stack Part Four: Capistrano, MySQL

We completed our subversion setup in part three, now let's add Capistrano deployment to the mix. Let's go ahead and ssh into our server:

CODE:
  1. ssh -p 8888 deploy@your_linux_box.com

Add the following new directories to our server. This is where our staging and production factory websites will be deployed to:

CODE:
  1. sudo mkdir /var/www
  2. sudo mkdir /var/www/apps
  3. sudo chown -R deploy:deploy /var/www/apps

Exit out of our linux server. Run the following command to see a list of our locally installed ruby gems:

CODE:
  1. gem list --local

Notice we don't have Capistrano installed yet. Let's rectify that and install some other gems we'll use to add staging ability if we need it on a project:

CODE:
  1. sudo gem install capistrano --include-dependencies
  2. sudo gem install capistrano-ext --include-dependencies
  3. sudo gem install palmtree --include-dependencies

Note that sometimes you have to try more than once to install ruby gems. If you get a timeout error just try it again. Go ahead and change directories to the new subversion project directory we had created in part three. Now we will setup our project for Capistrano deployment.

CODE:
  1. capify .
  2. cat /dev/null  > config/deploy.rb

Now paste in the following deploy.rb code from this link into your deploy.rb file. Edit the line where it has set :repository and change the IP to your linux server. Save the file in nano.

CODE:
  1. nano config/deploy.rb

Let's create a maintenance page so that if we ever need to take our site down to do server maintenance we're good to go:

CODE:
  1. nano app/views/layouts/maintenance.html.erb

Paste in the html code from this link and save the file in nano. Now let's create the folder structure for our different stages:

CODE:
  1. mkdir config/deploy
  2. mkdir config/deploy/production
  3. mkdir config/deploy/staging

Let's create the mongrel_cluster.yml file for production:

CODE:
  1. nano config/deploy/production/mongrel_cluster.yml
  2.  
  3. # Paste the following in and then save the file in nano.
  4. user: deploy
  5. group: deploy
  6. cwd: /var/www/apps/factory/current
  7. port: "9300"
  8. environment: production
  9. address: 127.0.0.1
  10. pid_file: "/var/run/mongrel_cluster/factory.pid"
  11. log_file: "log/factory.log"
  12. servers: 3

Now let's setup staging:

CODE:
  1. nano config/deploy/staging/mongrel_cluster.yml
  2.  
  3. # Paste the following in and then save the file in nano.
  4. user: deploy
  5. group: deploy
  6. cwd: /var/www/apps/qa.factory/current
  7. port: "9200"
  8. environment: staging
  9. address: 127.0.0.1
  10. pid_file: "/var/run/mongrel_cluster/qafactory.pid"
  11. log_file: "log/qafactory.log"
  12. servers: 2

We also need to create a config/environments/staging.rb file:

CODE:
  1. nano config/environments/staging.rb
  2.  
  3. # Paste in the following and save in nano
  4. config.cache_classes = true
  5. config.action_controller.consider_all_requests_local = false
  6. config.action_controller.perform_caching = true
  7. config.action_view.cache_template_loading = true

Now let's edit our database.yml file and add our stages (make sure to use your mysql user and pwd)

CODE:
  1. cat /dev/null  > config/database.yml
  2. nano config/database.yml

Paste in the following database info from the following link and save the file. Now we need to add the setup file for production:

CODE:
  1. nano config/deploy/production.rb
  2.  
  3. # Paste in the following code and save in nano
  4. set :rails_env, "production"
  5. set :monit_group, "factory"
  6. set :application, "factory"
  7. set :domain, "www.factory.com"
  8. set :server_name, "www.factory.com"
  9. set :deploy_to, "/var/www/apps/#{application}"
  10.  
  11. set :db_name, "factory_production"
  12. set :db_user, 'root'
  13. set :db_passwd, ''
  14.  
  15. role :app, domain
  16. role :web, domain
  17. role :db,  domain, :primary => true

Now we need to do the same for staging:

CODE:
  1. nano config/deploy/staging.rb
  2.  
  3. # Paste in the following code and save in nano
  4.  
  5. set :rails_env, "staging"
  6. set :monit_group, "qafactory"
  7. set :application, "qa.factory"
  8. set :domain, "qa.factory.com"
  9. set :server_name, "qa.factory.com"
  10. set :deploy_to, "/var/www/apps/#{application}"
  11.  
  12. set :db_name, "factory_staging"
  13. set :db_user, 'root'
  14. set :db_passwd, ''
  15.  
  16. role :app, domain
  17. role :web, domain
  18. role :db,  domain, :primary => true

Now let's setup our fake domain so that we can test our deployment. Later we can replace a real domain in our staging.rb and production.rb files.

CODE:
  1. sudo nano /etc/hosts
  2. # Add the following line to the bottom of your hosts file making sure to use the ip address of your server
  3. 10.39.121.200   qa.factory.com
  4. 10.39.121.200   www.factory.com

You can test it to see if our hosts file change worked by pinging our fake domains:

CODE:
  1. ping www.factory.com
  2. ping qa.factory.com

If you get your server's ip address we're good to go. Now let's test some Capistrano stuff out. You can see a list of the available Capistrano tasks by typing the following:

CODE:
  1. cap -T

We are first interested in setting up our staging and production websites folder structure on our linux server. If we've setup things correctly so far the following commands will do that for us:

CODE:
  1. cap staging deploy:setup
  2. cap production deploy:setup

Now let's ssh back into our server and see if it worked:

CODE:
  1. ssh -p 8888 deploy@your_linux_box.com
  2. cd /var/www/apps
  3. ls -la

You should see two folders, factory and qa.factory. Now let's install mysql (note that you will be prompted to enter a new password for root):

CODE:
  1. # Choose the top (ruby) option when prompted
  2. sudo apt-get install mysql-server-5.0 mysql-client-5.0 libmysqlclient15-dev libmysqlclient15off zlib1g-dev libmysql-ruby1.8
  3.  
  4. #Install MySQL Native Bindings for performance gain
  5. sudo gem install mysql -- --with-mysql-dir=/usr/local/mysql/bin/mysql_config

Next add the following line to your bash profile located in ~/.bash_profile

CODE:
  1. nano ~/.bash_profile
  2.  
  3. # Add to the top of the file and save in nano
  4. export RUBYOPT=rubygems
  5.  
  6. # Reload your profile
  7. source ~/.bash_profile

I like to manually create my databases and users in mysql, so let's add our databases for staging and production. You'll need to come up with a password you want to use that is secure. I'm just going to use 'password' for this example.

CODE:
  1. mysql -uroot -p
  2.  
  3. create database factory_staging default character set 'utf8' default collate 'utf8_general_ci';
  4. create database factory_production default character set 'utf8' default collate 'utf8_general_ci';
  5. grant all on factory_staging.* to 'factory'@'localhost' identified by 'password';
  6. grant all on factory_production.* to 'factory'@'localhost' identified by 'password';
  7.  
  8. # You can now see that your two databases were created by typing the following:
  9. show databases;
  10.  
  11. # Type quit to get out of mysql:
  12. quit

Exit out of the server. Now we need to change our rails project's config/database.yml, config/deploy/staging.rb, and config/deploy/production.rb files to reflect the new mysql user name of 'factory' and your new password.

CODE:
  1. # Change username and password for the following files and save
  2. nano config/database.yml
  3. nano config/deploy/staging.rb
  4. nano config/deploy/production.rb

Now let's add all of our rails code changes to subversion. You can see what files we need to add to subversion before we commit by typing the following:

CODE:
  1. svn status

The files with the '?' mark in front of them need to be added to subversion:

CODE:
  1. svn add Capfile
  2. svn add config/deploy
  3. svn add config/deploy.rb
  4. svn add config/environments/staging.rb
  5. svn add app/views/layouts/maintenance.html.erb

Now let's commit everything with a comment:

CODE:
  1. svn commit -m "Added files need for capistrano deployment"

Now let's add our widget tunnel definition to our server and setup ssh keys for our server. Login to your server:

CODE:
  1. ssh -p 8888 deploy@your_linux_box.com
  2. nano ~/.subversion/config
  3.  
  4. # Add the following line inside the [tunnels] section and save
  5. widgets = /usr/bin/ssh -p 8888 -l deploy
  6.  
  7. # Now generate ssh keys for our server
  8. ssh-keygen -t rsa -f ~/.ssh/id_rsa -C "your@emailaddress.com"
  9.  
  10. # Type the following and copy the entire string that is displayed
  11. cat ~/.ssh/id_rsa.pub
  12.  
  13. # Now paste the string onto the second line of our authorized_keys file and save in nano
  14. nano ~/.ssh/authorized_keys

Exit out of the server. Now let's try deploying our staging application to the server. From the root of our factory application type the following:

CODE:
  1. cap staging deploy

You should see an error at the very bottom (/usr/sbin/monit: command not found). That's o.k. because we haven't setup monit yet. So go ahead and deploy production too:

CODE:
  1. cap production deploy

Now add our factory tables to our staging and production databases using database migrations:

CODE:
  1. cap staging deploy:migrate
  2. cap production deploy:migrate

Now let's ssh back into our server and change directories to our deployed websites:

CODE:
  1. ssh -p 8888 deploy@your_linux_box.com
  2. cdapps
  3. cd qa.factory/current
  4.  
  5. # Let's use the webrick webserver to test out application out
  6. script/server -e staging

Now go to your browser on your Mac and type in http://qa.factory.com:3000. Our widget listing page should show up if everything is working. Now try creating a new widget! To stop the webrick server you just use CTRL_C. That's it for today. Next time we will get nginx to run our applications.

3 Responses to “Full Rails Stack Part Four: Capistrano, MySQL”


  1. Gravatar Icon 1 Andrew Fong Mar 14th, 2008 at 6:11 pm

    Hey, came across this via Google searches — wish I had found this a few months ago. Keep up the good work!

  2. Gravatar Icon 2 Andrew Selder May 21st, 2008 at 8:53 am

    Love this series. Hopefully the rest will be coming along soon. Keep up the good work.

  3. Gravatar Icon 3 Jason Green Oct 5th, 2008 at 11:02 am

    Cheers, looking forward to the rest of the tutorials, really great!

Leave a Reply





March 2008
M T W T F S S
« Feb    
 12
3456789
10111213141516
17181920212223
24252627282930
31  

Categories


Random Photos

  • IMG_2055.JPG
    IMG_1983.JPG
    Sunrise on the way to work
    IMG_0077.JPG


Now Reading

Planned books:

None

Current books:

  • The Consequences of Ideas: Understanding the Concepts that Shaped Our World

    The Consequences of Ideas: Understanding the Concepts that Shaped Our World by R. C. Sproul

  • Born Standing Up

    Born Standing Up by Steve Martin

Recent books:

View full Library