ETC

CS290F Fall 2006 - UCSB Computer Science - Thorsten von Eicken

Jump to: navigation, search

How to Set up EC2 and Run the Project App

First, I recommend you to fire up any image that contains Apache2.2 or higher. Personally, I use "FC5+apache2.2+rails+mysql5.0 v3" image -- obviously, it's got Apache 2.2 on it.

You can log into the machine by:

ssh -i <file_with_key> root@<yout_image_address>

once you log in, I recommned you to change the password of the root and user 'user' right away so that you don't need to use that key file anymore. Plus, be sure that the user 'user' is listed as a 'sudoer' in /etc/sudoers file.

Now, do

su - user

so that you can install your project under the name of 'user'

install following packets in your EC2 image machine if not pre-installed.

sudo gem install xml-simple
sudo gem install --include-dependencies termios
sudo gem install --include-dependencies capistrano
sudo gem install --include-dependencies mongrel
sudo gem install --include-dependencies mongrel_cluster

and install the same packages on your local machine as well

And don't forget to set up your database...I will leave this part up to you guys.

It's pretty much done on EC2 image side.

Now, let's go back to your local machine where you have been using to develop the project. I assume that you have successfully installed all the packages above on your local machine.

On your local computer, go to your project directoy, in my case "/trunk"

do

mongrel_rails cluster::configure -e production -p 8000 -a 127.0.0.1 -N 1 -c /home/user/Lib/Rails/u/apps/hotspots/current

then in your ./config directory, you should have a file "mongrel_cluster.yml" that looks like:

---
cwd: /home/user/Lib/Rails/u/apps/hotspots/current
port: "8000"
environment: production
address: 127.0.0.1
pid_file: log/mongrel.pid
servers: 1

Here, you can prettymuch figure out what the parameters indicate. However, the most important parameter is the path

"/home/user/Lib/Rails/u/apps/hotspots/current"

This is the path of your most recent project in your EC2 image machine, which is set by capistrano 'cap setup' command. It will be more clear once you take a look at my ./config/deploy.rb file. So, let's just move on for now.

Next step is do

cap --apply-to /home/user/Lib/Rails/u/apps/hotspots hotspots

It doesn't really matter what you pass as parameters here since you will be modifying "config/deploy.rb" file anyway.

This is what my "config/deploy.rb" looks like below: the part that I modified..

require 'mongrel_cluster/recipes'

# =============================================================================
# REQUIRED VARIABLES
# =============================================================================

set :application, "hotspots"
set :repository, "https://svn.cs.ucsb.edu/cs290f/#{application}/trunk"

# =============================================================================
# ROLES
# =============================================================================

role :web, "domu-12-31-34-00-02-5a.usma2.compute.amazonaws.com"
role :app, "domu-12-31-34-00-02-5a.usma2.compute.amazonaws.com"
role :db,  "domu-12-31-34-00-02-5a.usma2.compute.amazonaws.com", :primary => true

# =============================================================================
# OPTIONAL VARIABLES
# =============================================================================
set :deploy_to, "/home/user/Lib/Rails/u/apps/#{application}" # defaults to "/u/apps/#{application}"
set :user, "user"            # defaults to the currently logged in user
set :mongrel_conf,      "/home/user/Lib/Rails/u/apps/hotspots/current/config/mongrel_cluster.yml"

Be sure to incluse

require 'mongrel_cluster/recipes'

at the top

now, with all these files, "mongrel_cluster.yml" and "deploy.rb", in your config directory. Do

svn ci

to commit the current project in yout svn server. Be sure to add "mongrel_cluster.yml" and "deploy.rb" to svn list.

now do

cap setup;
What this command does is that it will try to log in to the EC2 machine, in this case "domu-12-31-34-00-02-5a.usma2.compute.amazonaws.com" as specified in "deploy.rb" file, and try to log in as a user 'user', which is set by the line
set :user, "user"
in "deploy.rb" file. This is why you must change the user 'user's password on your EC2 machine. It will prompt the password for 'user', but it won't be hidden; it will be displayed on your shell screen. After log in, it will create a directory according to the line
set :deploy_to, "/home/user/Lib/Rails/u/apps/#{application}"

So, in my case, it created a directory "/home/user/Lib/Rails/u/apps/hotspots" in my EC2 image machine.

Now, if you run

cap cold_deploy;

on your local machine.

It will also log into the EC2 image as 'user' and download your most recent project from svn server according to the line
set :repository, "https://svn.cs.ucsb.edu/cs290f/#{application}/trunk2"
from "deploy.rb"

and put the project in "/home/user/Lib/Rails/u/apps/hotspots/currnt".

This is why the "mongrel_cluster.yml" file must contain the path above since this is the actual path that points to the most recent project location in your EC2 image machine.

Now, if you made it upto here without any error, you are ready to run

cap deploy;

except one more thing.

You MUST mofidy the /etc/httpd/conf/httpd.conf file.

Just add

<VirtualHost *:80>
  ServerName myapp.com
  DocumentRoot /home/user/Lib/Rails/u/apps/hotspots/current/public

  <Directory "/home/user/Lib/Rails/u/apps/hotspots/current/public">
        Options FollowSymLinks
        AllowOverride None
        Order allow,deny
        Allow from all
  </Directory>

  RewriteEngine On

  RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
  RewriteCond %{SCRIPT_FILENAME} !maintenance.html
  RewriteRule ^.*$ /system/maintenance.html [L]

  RewriteRule ^/$ /index.html [QSA]

  RewriteRule ^([^.]+)$ $1.html [QSA]

  RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
  RewriteRule ^/(.*)$ balancer://mongrel_cluster%{REQUEST_URI} [P,QSA,L]
</VirtualHost>

at the end....of course, you need to modify some paths above according to your setup..such as DocumentRoot and so on

and mofiy the proxy setting as below...

# Proxy Server directives. Uncomment the following lines to
# enable the proxy server:
#
<IfModule mod_proxy.c>
ProxyRequests On
#
#<Proxy *>
#    Order deny,allow
#    Deny from all
#    Allow from .example.com
#</Proxy>

<Proxy balancer://mongrel_cluster>
  BalancerMember http://127.0.0.1:8000
#  BalancerMember http://127.0.0.1:8001
</Proxy>

#
# Enable/disable the handling of HTTP/1.1 "Via:" headers.
# ("Full" adds the server version; "Block" removes all outgoing Via: headers)
# Set to one of: Off | On | Full | Block
#
#ProxyVia On

#
# To enable a cache of proxied content, uncomment the following lines.
# See http://httpd.apache.org/docs/2.2/mod/mod_cache.html for more details.
#
#<IfModule mod_disk_cache.c>
#   CacheEnable disk /
#   CacheRoot "/var/cache/mod_proxy"
#</IfModule>
#

</IfModule>
# End of proxy directives.


since I'm only using 1 server, as I specified in "mongrel_cluster.yml" file -- remember "server : 1" line, I commented out the line "BalancerMember http://127.0.0.1:8001"...later when I need to run more than one server, I will add more "BalancerMember" with monotonically increasing port numbers.

After you mofiy this conf file, you need to restart the "httpd"

/etc/init.d/httpd restart

if it gives "ok", now it's time to run

cap deploy;

on your local machine.

It will log in to the EC2 image, download the most recent project in svn, reset the "current" link to that project, and restart the "mongrel" server.

This is why you must commit your project to svn everytime before you run "cap deploy".

Then, use your brower to check your project.

Thanks

Kyo

Personal tools