Skip to content

Instantly share code, notes, and snippets.

@juliamae
Forked from luke0x/gist:115795
Created December 23, 2010 18:01
Show Gist options
  • Select an option

  • Save juliamae/753311 to your computer and use it in GitHub Desktop.

Select an option

Save juliamae/753311 to your computer and use it in GitHub Desktop.

Revisions

  1. juliamae revised this gist Dec 23, 2010. 1 changed file with 109 additions and 214 deletions.
    323 changes: 109 additions & 214 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,235 +1,130 @@
    Deploying a Rails App with EC2 + S3 + Ubuntu
    ============================================
    Deploying a Rails 3 App with EC2 + S3 + Ubuntu + Capistrano + Passenger
    =======================================================================

    Create EC2 Instance
    -------------------
    EC2 Setup
    ---------
    1 Launch New ec2 instance - ami-1634de7f
    2 Create elastic IP [ELASTIC_IP] and associate it with instance
    3 go to domain registrar DNS settings, @ and www to ELASTIC_IP
    4 set the `:host` in `config/deploy.rb` to ELASTIC_IP

    create new instance ami-bf5eb9d6 [http://alestic.com/](http://alestic.com/)
    create new elastic ip
    attach elastic ip to instance
    point dns to elastic ip
    set the `:host` in `config/deploy.rb` to the new elastic ip

    S3 Setup
    --------
    1 create buckets:
    - application.com
    - application.com_backup
    etc

    Create S3 Buckets
    -----------------

    for production: projectname, projectname_backup
    for development: projectname_development, projectname_development_backup


    Connect to Server
    -----------------

    create local ~/.ssh/projectname_production_root file with private rsa ssh key, chmod 600
    ssh in as root such as ssh root@1.2.3.4 -i ~/.ssh/projectname_production_root


    Install Packages
    ----------------

    First update existing packages:

    # apt-get update
    # apt-get upgrade

    Then install new packages:

    # apt-get install apache2
    # apt-get install apache2-prefork-dev
    # apt-get install build-essential
    # apt-get install exim4
    # apt-get install git-core
    # apt-get install imagemagick
    # apt-get install irb
    # apt-get install libmysqlclient15-dev
    # apt-get install libyaml-ruby
    # apt-get install libzlib-ruby
    # apt-get install mysql-server
    # apt-get install rdoc
    # apt-get install ruby
    # apt-get install ruby1.8-dev
    # apt-get install rubygems
    # apt-get install sqlite3

    When prompted set the mysql-server password.


    Install Rubygems
    ----------------

    First install Rubygems itself:

    # gem install rubygems-update
    # gem update --system

    Something is messed up with this version of rubygems so we have to do something ghetto:

    # vi /usr/bin/gem
    After the line:
    require 'rubygems'
    Add:
    require 'rubygems/gem_runner'

    Then install the individual gems:

    # gem install daemons
    # gem install fastthread
    # gem install hpricot
    # gem install json
    # gem install mime-types
    # gem install mysql
    # gem install open4
    # gem install passenger
    # gem install rack
    # gem install rake
    # gem install right_aws
    # gem install ruby-mp3info
    # gem install sqlite3-ruby
    Connect to server & make user to run app
    ----------------------------------------
    1 ssh-keygen, choose special name
    2 ssh ubuntu@ELASTIC_IP -i ~/.ec2/applicationkey.pem
    3 sudo su; useradd -d /home/application -m application; passwd application
    4 as ubuntu or root, sudo visudo, arrow down to bottom and application ALL=(ALL) NOPASSWD:ALL
    5 scp -i ~/.ec2/applicationkey.pem ~/.ssh/id_rsa_application.pub ubuntu@ELASTIC_IP:/home/ubuntu/.ssh/ (from local)
    6 sudo mv ~/.ssh/id_rsa_application.pub /home/application/.ssh/authorized_keys
    7 sudo chmod 600 /home/application/.ssh/authorized_keys
    8 sudo chown application /home/application/.ssh/authorized_keys
    9 now you can ssh application@ELASTIC_IP -i ~/.ssh/id_rsa_application yay!
    10 do above, then mkdir deploy


    Install Shit
    -----------
    sudo apt-get update
    sudo apt-get upgrade
    sudo apt-get install apache2
    sudo apt-get install apache2-prefork-dev
    sudo apt-get install exim4
    sudo apt-get install git-core
    sudo apt-get install irb
    sudo apt-get install rdoc
    sudo apt-get install ruby
    sudo apt-get install ruby1.8-dev
    sudo apt-get install rubygems
    sudo apt-get install mysql
    sudo apt-get install libopenssl-ruby

    gem install rubygems-update
    gem install passenger
    gem install rake
    gem install bundler (might need to manually build rubygems if version is not up to date)


    Configure Exim4
    ---------------

    Run the configurator:

    # dpkg-reconfigure exim4-config

    And enter these values as prompted:

    - internet site, sent directly via smtp
    - projectname.com
    - 127.0.0.1
    - other destinations: [blank]
    - relay: [blank]
    - relay: [blank]
    - dns minimal: no
    - delivery method: mbox
    - small files: no
    - root: root


    Configure MySQL
    1 dpkg-reconfigure exim4-config
    -- internet site, sent directly via smtp
    -- application.com
    -- 127.0.0.1
    -- other destinations: [blank]
    -- relay: [blank]
    -- relay: [blank]
    -- dns minimal: no
    -- delivery method: mbox
    -- small files: no
    -- root: root


    Setup Passenger
    ---------------

    # mysqladmin -u root -p create projectname_production
    # mysql -u root -p

    mysql> CREATE USER 'projectname_prod'@'localhost' IDENTIFIED BY 'password';
    mysql> FLUSH PRIVILEGES;
    mysql> CREATE DATABASE projectname_production DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;
    mysql> GRANT ALL PRIVILEGES ON projectname_production.* TO 'projectname_prod'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;


    Configure Apache and Phusion Passenger
    --------------------------------------

    Run the Passenger configurator:

    # passenger-install-apache2-module

    Edit Apache configs:

    # cd /etc/apache2/
    # a2enmod deflate
    # vi conf.d/projectname
    NameVirtualHost *
    LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-2.0.6/ext/apache2/mod_passenger.so
    PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-2.0.6
    PassengerRuby /usr/bin/ruby1.8
    PassengerUseGlobalQueue on
    PassengerMaxPoolSize 25
    # vi sites-available/placeholder
    <VirtualHost *>
    ServerName projectname.com
    DocumentRoot /home/projectname/static_page
    </VirtualHost>
    # vi sites-available/projectname
    <VirtualHost *>
    ServerName projectname.com
    DocumentRoot /home/projectname/projectname/current/public
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/javascript text/css application/x-javascript
    BrowserMatch ^Mozilla/4 gzip-only-text/html
    BrowserMatch ^Mozilla/4\.0[678] no-gzip
    BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
    CustomLog /var/log/apache2/projectname.com-access.log combined
    ErrorLog /var/log/apache2/projectname.com-error.log
    LogLevel warn
    </VirtualHost>
    # a2dissite default
    # a2ensite projectname
    # apache2ctl restart
    1 passenger-install-apache2-module
    2 $ cd /etc/apache2/; a2enmod deflate; vi conf.d/passenger
    ## passenger stuff might change with different version ##
    LoadModule passenger_module /var/lib/gems/1.8/gems/passenger-3.0.0/ext/apache2/mod_passenger.so
    PassengerRoot /var/lib/gems/1.8/gems/passenger-3.0.0
    PassengerRuby /usr/bin/ruby1.8


    Setup Apache VHost
    ------------------
    $ vi sites-available/application
    <VirtualHost *:80>
    ServerName application.com
    DocumentRoot /home/application/deploy/current/public
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/javascript text/css application/x-javascript
    BrowserMatch ^Mozilla/4 gzip-only-text/html
    BrowserMatch ^Mozilla/4\.0[678] no-gzip
    BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
    SetEnv ENV_YOU_WANT_TO_SET this_is_the_val # optional
    <Directory /somewhere/public>
    AllowOverride all
    Options -MultiViews
    </Directory>
    </VirtualHost>


    Enable site
    -----------
    $ sudo a2ensite application.com


    Setup Deployment Credentials
    ----------------------------
    1 Change to application user
    2 $ ssh-keygen -t dsa
    3 copy id_dsa.pub to github repo > Admin > Deploy keys

    Create and change to the UNIX `projectname` user:

    # adduser projectname password
    TODO: disable projectname user password
    # su - projectname

    Create an SSH key:

    $ ssh-keygen -t dsa
    [no passphrase]

    Add as a deploy key on github:

    $ cat .ssh/id_dsa.pub
    copy + paste this into a new deploy key at https://github.com/username/projectname/edit

    * ensure that `ssh_options[:keys]` in `config/deploy.rb` contains the correct key name
    * add remote projectname user's public ssh key to locally:
    * $ vi ~/.ssh/projectname_production_projectname.pub
    * add local public ssh key to remote ~projectname/.ssh/authorized_keys


    Initialize and Configure the Application
    ----------------------------------------

    Locally, init the remote deploy:

    $ cap deploy:cold

    Create the remote rails database config file:

    $ vi ~projectname/projectname/shared/database.yml
    production:
    adapter: mysql
    host: localhost
    username: projectname_prod
    password: password
    database: projectname_production
    timeout: 5000
    encoding: UTF8
    socket: /var/run/mysqld/mysqld.sock

    Load the initial schema:

    $ cd ~projectname/projectname/current
    $ RAILS_ENV=production rake db:schema:load


    Deploy the Application
    ----------------------

    At this point, ensure that all code (including the updated `config/deploy.rb`) is checked in locally, merged onto the `deploy` branch, and pushed to github.

    Then, locally:
    Set up environment variables if necessary
    -----------------------------------------
    $ cat .ssh/environment
    AMAZON_ACCESS_KEY_ID='KEYYY'
    AMAZON_SECRET_ACCESS_KEY='SECRETTT'

    $ cap deploy
    make sure /etc/ssh/sshd_config contains:
    PermitUserEnvironment yes

    Confirm that the app is running at [http://projectname.com](http://projectname.com).
    Might have to restart ssh with sudo /etc/init.d/ssh restart


    TODO
    ----
    Deploy!
    -------
    Create the following directories if they don't exist:
    /home/application/deploy/releases
    /home/application/shared/log

    * fix cache thing
    * cap deploy todos
    * cap to after deploy:cold create:
    * mkdir ~projectname/projectname/releases
    * mkdir ~projectname/projectname/shared/log
    * also pids + system dirs
    TODO:
    MySQL setup
  2. @luke0x luke0x revised this gist May 29, 2009. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -230,6 +230,6 @@ TODO
    * fix cache thing
    * cap deploy todos
    * cap to after deploy:cold create:
    * mkdir ~muxtape/muxtape/releases
    * mkdir ~muxtape/muxtape/shared/log
    * mkdir ~projectname/projectname/releases
    * mkdir ~projectname/projectname/shared/log
    * also pids + system dirs
  3. @luke0x luke0x revised this gist May 21, 2009. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@ Deploying a Rails App with EC2 + S3 + Ubuntu
    Create EC2 Instance
    -------------------

    create new instance ami-1c5db975
    create new instance ami-bf5eb9d6 [http://alestic.com/](http://alestic.com/)
    create new elastic ip
    attach elastic ip to instance
    point dns to elastic ip
  4. @luke0x luke0x created this gist May 21, 2009.
    235 changes: 235 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,235 @@
    Deploying a Rails App with EC2 + S3 + Ubuntu
    ============================================

    Create EC2 Instance
    -------------------

    create new instance ami-1c5db975
    create new elastic ip
    attach elastic ip to instance
    point dns to elastic ip
    set the `:host` in `config/deploy.rb` to the new elastic ip


    Create S3 Buckets
    -----------------

    for production: projectname, projectname_backup
    for development: projectname_development, projectname_development_backup


    Connect to Server
    -----------------

    create local ~/.ssh/projectname_production_root file with private rsa ssh key, chmod 600
    ssh in as root such as ssh root@1.2.3.4 -i ~/.ssh/projectname_production_root


    Install Packages
    ----------------

    First update existing packages:

    # apt-get update
    # apt-get upgrade

    Then install new packages:

    # apt-get install apache2
    # apt-get install apache2-prefork-dev
    # apt-get install build-essential
    # apt-get install exim4
    # apt-get install git-core
    # apt-get install imagemagick
    # apt-get install irb
    # apt-get install libmysqlclient15-dev
    # apt-get install libyaml-ruby
    # apt-get install libzlib-ruby
    # apt-get install mysql-server
    # apt-get install rdoc
    # apt-get install ruby
    # apt-get install ruby1.8-dev
    # apt-get install rubygems
    # apt-get install sqlite3

    When prompted set the mysql-server password.


    Install Rubygems
    ----------------

    First install Rubygems itself:

    # gem install rubygems-update
    # gem update --system

    Something is messed up with this version of rubygems so we have to do something ghetto:

    # vi /usr/bin/gem
    After the line:
    require 'rubygems'
    Add:
    require 'rubygems/gem_runner'

    Then install the individual gems:

    # gem install daemons
    # gem install fastthread
    # gem install hpricot
    # gem install json
    # gem install mime-types
    # gem install mysql
    # gem install open4
    # gem install passenger
    # gem install rack
    # gem install rake
    # gem install right_aws
    # gem install ruby-mp3info
    # gem install sqlite3-ruby


    Configure Exim4
    ---------------

    Run the configurator:

    # dpkg-reconfigure exim4-config

    And enter these values as prompted:

    - internet site, sent directly via smtp
    - projectname.com
    - 127.0.0.1
    - other destinations: [blank]
    - relay: [blank]
    - relay: [blank]
    - dns minimal: no
    - delivery method: mbox
    - small files: no
    - root: root


    Configure MySQL
    ---------------

    # mysqladmin -u root -p create projectname_production
    # mysql -u root -p

    mysql> CREATE USER 'projectname_prod'@'localhost' IDENTIFIED BY 'password';
    mysql> FLUSH PRIVILEGES;
    mysql> CREATE DATABASE projectname_production DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;
    mysql> GRANT ALL PRIVILEGES ON projectname_production.* TO 'projectname_prod'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;


    Configure Apache and Phusion Passenger
    --------------------------------------

    Run the Passenger configurator:

    # passenger-install-apache2-module

    Edit Apache configs:

    # cd /etc/apache2/
    # a2enmod deflate
    # vi conf.d/projectname
    NameVirtualHost *
    LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-2.0.6/ext/apache2/mod_passenger.so
    PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-2.0.6
    PassengerRuby /usr/bin/ruby1.8
    PassengerUseGlobalQueue on
    PassengerMaxPoolSize 25
    # vi sites-available/placeholder
    <VirtualHost *>
    ServerName projectname.com
    DocumentRoot /home/projectname/static_page
    </VirtualHost>
    # vi sites-available/projectname
    <VirtualHost *>
    ServerName projectname.com
    DocumentRoot /home/projectname/projectname/current/public
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/javascript text/css application/x-javascript
    BrowserMatch ^Mozilla/4 gzip-only-text/html
    BrowserMatch ^Mozilla/4\.0[678] no-gzip
    BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
    CustomLog /var/log/apache2/projectname.com-access.log combined
    ErrorLog /var/log/apache2/projectname.com-error.log
    LogLevel warn
    </VirtualHost>
    # a2dissite default
    # a2ensite projectname
    # apache2ctl restart


    Setup Deployment Credentials
    ----------------------------

    Create and change to the UNIX `projectname` user:

    # adduser projectname password
    TODO: disable projectname user password
    # su - projectname

    Create an SSH key:

    $ ssh-keygen -t dsa
    [no passphrase]

    Add as a deploy key on github:

    $ cat .ssh/id_dsa.pub
    copy + paste this into a new deploy key at https://github.com/username/projectname/edit

    * ensure that `ssh_options[:keys]` in `config/deploy.rb` contains the correct key name
    * add remote projectname user's public ssh key to locally:
    * $ vi ~/.ssh/projectname_production_projectname.pub
    * add local public ssh key to remote ~projectname/.ssh/authorized_keys


    Initialize and Configure the Application
    ----------------------------------------

    Locally, init the remote deploy:

    $ cap deploy:cold

    Create the remote rails database config file:

    $ vi ~projectname/projectname/shared/database.yml
    production:
    adapter: mysql
    host: localhost
    username: projectname_prod
    password: password
    database: projectname_production
    timeout: 5000
    encoding: UTF8
    socket: /var/run/mysqld/mysqld.sock

    Load the initial schema:

    $ cd ~projectname/projectname/current
    $ RAILS_ENV=production rake db:schema:load


    Deploy the Application
    ----------------------

    At this point, ensure that all code (including the updated `config/deploy.rb`) is checked in locally, merged onto the `deploy` branch, and pushed to github.

    Then, locally:

    $ cap deploy

    Confirm that the app is running at [http://projectname.com](http://projectname.com).


    TODO
    ----

    * fix cache thing
    * cap deploy todos
    * cap to after deploy:cold create:
    * mkdir ~muxtape/muxtape/releases
    * mkdir ~muxtape/muxtape/shared/log
    * also pids + system dirs