Here process deploying of application in a hosting (in this case i deploy my application in FastCGI on HostingRails.com). There are 3 major steps of deployment :
- Create SVN Repository (Next I will explain about GIT)
- Setup Capistrano & Deploy Application with Capistrano
- Create REPOSITORY to your hosting account, please ensure that your hosting is already installed capistrano & SVN or ask your hosting admin/support to install SVN and Capistrano.
username@mydomain.com [~]$ mkdir -p ~/svn/your_app_name
username@mydomain.com [~]$ svnadmin create ~/svn/your_app_name
username@mydomain.com [~]$ mkdir -p ~/tmp/your_app_name/{tags,branches,trunk}
username@mydomain.com [~]$ svn import ~/tmp/your_app_name file:///home/username/svn/your_app_name -m "Created Repository"
- At this point you have enough setup on your Hosting account for single-user access to Subversion. You can now check out your repository at:
username@mydomain.com [~]$ svn co svn+ssh://username@mydomain.com/home/username/svn/your_app_name/trunk
- Next we are going to add your existing Rails app to the Subversion repository. If you don't have an existing app, you will need to create one.
username@mydomain.com [~]$ svn co svn+ssh://username@mydomain.com/home/username/svn/your_app_name/trunk your_app_name_dev
- Ok, it looks fine if you do right. The next step is copy your application for repository. Before this process, upload your existing application folder to your host in zip or tar.gz file. After uploaded, extract it to up folder of public_html (do not extract your application in public_html). After extracting, we will modify some data and permission file.
- Your database.yml(.online) file simply needs to look like this:
production:
adapter: mysql
database: [your_hosting_username]_[your_database_name]
username: [your_hosting_username] OR [your_hosting_username]_[your_database_name]
password: your_password
- And in your environment.rb(.online) file you'll just need to uncomment the following line to confirm you're in production mode:
ENV['RAILS_ENV'] ||= 'production'
- Make sure your .htaccess file sends requests to dispatch.fcgi. Please Note - as of Rails 2.1.0 the .htaccess file no longer exists in the default skeleton. Run a "rails _2.0.2_ testapp" and move over the public/.htacess from there. This one is easy but also critically important. In your local Rails application's public/.htaccess file change the :
RewriteRule ^(.*)$ dispatch.cgi [QSA,L]
- EXACTLY where it is already in the file, don't change its position, to:
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
- also - very important make sure the 'AddHandler fastcgi-script .fcgi' is commented out at the top of your .htaccess:
And that's it# General Apache options
# AddHandler fastcgi-script .fcgi
- Now look into public folder and edit all files with name "dispatch" (they are 3 files), edit the first line and change to :
#!/usr/local/bin/ruby
- Do not forget to change permission into 755 for all files in public folder specially all dispatch.* files and .htaccess.
- Next, you will need to copy your existing Rails app on your local machine into the version we just checked out
username@mydomain.com [~]$ cp -R your_app_name/* your_app_name_dev
- Now you can see if Subversion recognizes the updated files and directories:
username@mydomain.com [~]$ cd your_app_name_dev
username@mydomain.com [~/your_app_name_dev]$ svn status
- You should get something like this:
? test
? app
? tmp
? log
? Rakefile
? script
? components
? config
? db
? doc
? lib
? README
? vendor
? public
- Now, just add them to the repository:
username@mydomain.com [~/your_app_name_dev]$ svn add test app tmp log Rakefile script components config db doc lib README vendor public
- You will see a list of the files that were added with an 'A' next to indicating that the files will be added on the next commit. You will see something like this:
A test
A test/fixtures
A test/fixtures/contact_mailer
A test/fixtures/contact_mailer/notify
A test/fixtures/contact_mailer/validate_email
A test/fixtures/contacts.yml
A test/functional
A test/functional/website_controller_test.rb
A test/integration
# and so on
- Next, lets commit the Rails app to the repository
username@mydomain.com [~/your_app_name_dev]$ svn commit -m "Added The Application"
- This may take a few minutes depending on the size of your application. You will see the list of files appear again followed by this:
Transmitting file data .................................................................
Committed revision 2.
- Now your will be able to edit your code on your local machine and have the changes reflected in newer revisions simply by adding and committing files.
- Setup your Rails application to work with Capistrano. Once you have checked out your code from the svn repository, change to the root of the Rails app on your development machine and run (look carefully there is a dot after capify command, just type it completely with a dot):
Capistrano created two files: /config/deploy.rb and Capfile. (Note: /config/deploy.rb might have been skipped if you already had a deploy.rb file in your app.) The config/deploy.rb file will contain your application variables used to deploy your app, while the Capfile will contain your deployment tasks.username@mydomain.com [~/your_app_name_dev]$ capify .
# This is the output
[add] writing `./Capfile'
[add] writing `./config/deploy.rb'
[done] capified!
- So open the deploy.rb file and we'll start customizing the deployment process. The first thing we want to do is set our application variables. So let's modify this first section of the file:
set :application, "your_app_name" set :domain, "mydomain.com" set :user, "username" set :repository, "svn+ssh://#{user}@#{domain}/home/#{user}/svn/#{application}/trunk" set :use_sudo, false set :deploy_to, "/home/#{user}/#{application}" set :deploy_via, :checkout set :chmod755, "app config db lib public vendor script script/* public/disp*" set :svn_username, "username" set :svn_password, "hosting_password" set :password, "hosting_password" default_run_options[:pty] = true ssh_options[:keys] = %w(/Path/To/id_rsa) role :app, "mydomain.com" role :web, "mydomain.com"
role :db, "mydomain.com", :primary => true
- Now that everything is in place, you can commit your changes to the Subversion repository if you want:
username@mydomain.com [~/your_app_name_dev]$ svn add config/deploy.rb Capfile
username@mydomain.com [~/your_app_name_dev]$ svn commit -m "Configured application for Capistrano" - Last Step - Setup Your Hosting Account. Next we need to setup our Hosting account. This is where our hard work pays off with Capistrano. From your local machine enter the following commands:
username@mydomain.com [~/your_app_name_dev]$ cap setup
- Next you need to run a cold deploy.
username@mydomain.com [~/your_app_name_dev]$ cap cold_deploy
- Next we can do a full deploy
username@mydomain.com [~/your_app_name_dev]$ cap deploy
- Or if you want Capistrano to run your migration files (be sure to have your database and database.yml setup properly).
username@mydomain.com [~/your_app_name_dev]$ cap deploy_with_migrations
- If you ever run into problems and need to rollback to a previous revision of your code simple enter:
username@mydomain.com [~/your_app_name_dev]$ cap rollback
- As you make revisions to your code on your local machine and are ready to deploy your updated application to the production server all you have to do is:
username@mydomain.com [~/your_app_name_dev]$ $ svn commit -m "Your Custom Reference Message";cap deploy