Skip to content

Instantly share code, notes, and snippets.

@jamiew
Forked from luke0x/gist:115795
Created May 29, 2009 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jamiew/120162 to your computer and use it in GitHub Desktop.
Save jamiew/120162 to your computer and use it in GitHub Desktop.
Deploying a Rails App with EC2 + S3 + Ubuntu
============================================
Create EC2 Instance
-------------------
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
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment