This guide will provide you some basic steps to create a Bitnami RoR Amazon EC2 instance, using Nginx with Passenger.
-
Create an Amazon EC2 instance using a Community AMI. At the time of writing, the latest Bitnami AMI is: bitnami-rubystack-2.2.4-4-linux-ubuntu-14.04.1-x86_64-hvm-ebs Be careful to open SSH, HTTP and HTTPS ports on the "Step 6: Configure Security Group". You will need to save also the certificate to later connect to the instance.
-
Log in by SSH to the instance:
ssh -i cert.pem bitnami@public-ip
Where cert.pem is the certificate you created when launching the instance and public-ip is the instance public IP (can be found on the EC2 instances tab).
-
Once logged in, start the ssh agent:
eval `ssh-agent -s`
This will be used to clone the repository where the code is.
-
Genereate a new ssh key without passphrase:
ssh-keygen -t rsa -b 4096 -C "[email protected]”
And press a lot of enters.
-
Copy the generated public key:
cat ~/.ssh/id_rsa.pub
-
Paste the public key where the code is hosted (SSH Keys)
-
Go to your root folder:
cd ~
-
Clone your code:
git clone [email protected]
-
Go to your project root and install all your gems.
bundle install
-
Create a database for your app
-
Connect to postgres (password is bitnami)
psql -U postgres
-
Create the db
CREATE DATABASE YOUR_DATABASE_NAME;
-
Exit
\q
-
-
Configure your config/database.yml file
production: adapter: postgresql database: YOUR_DATABASE_NAME host: localhost user: postgres password: bitnami
-
Setup up your db
export RAILS_ENV=production rake db:migrate
-
Precompile your assets
rake assets:precompile
-
Create a secret key for your application
rake secret
Ideally, the output from this command should be saved as an environment variable named $SECRET_KEY_BASE, which will then be read by Rails as needed. If this doesn’t work for you, you can also hardcode the key into the config/secrets.yml file, although this is less secure.
export SECRET_KEY_BASE=previous_output
Then, on config/secrets.yml
production: # secret_key_base: <%= ENV["SECRET_KEY_BASE"] %> secret_key_base: output_from_rake_secret
-
Make the directories myapp/, myapp/config, myapp/public and their parent directories readable and executable by the Nginx Web server. This server runs as the user 'daemon', so change the group and permissions accordingly.
sudo chgrp daemon . sudo chgrp daemon -R config public chmod 755 . chmod 750 -R config public
-
Configure Nginx
-
Open the ngix config file:
sudo nano ~/stack/nginx/conf/nginx.conf
-
Leave everything as it is, except for the server part. It should be something like this:
...stuff... server { # port to listen on. Can also be set to an IP:PORT listen 80; server_name coolschoolservice.com; root /home/bitnami/coolschoolservice/public; passenger_enabled on; rails_env production; # PageSpeed #pagespeed on; #pagespeed FileCachePath /opt/bitnami/nginx/var/ngx_pagespeed_cache; # Ensure requests for pagespeed optimized resources go to the pagespeed # handler and no extraneous headers get set. #location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" { add_header "" ""; } #location ~ "^/ngx_pagespeed_static/" { } #location ~ "^/ngx_pagespeed_beacon$" { } #location /ngx_pagespeed_statistics { allow 127.0.0.1; deny all; } #location /ngx_pagespeed_message { allow 127.0.0.1; deny all; } } ...stuff...
-
Save and exit
-
Stop apache
sudo /opt/bitnami/ctlscript.sh stop apache
-
Stop apache from starting at boot up
sudo mv /opt/bitnami/apache2/scripts/ctl.sh /opt/bitnami/apache2/scripts/ctl.sh.disabled
sudo mv /opt/bitnami/config/monit/conf.d/apache.conf /opt/bitnami/config/monit/apache.conf.disabled
-
Restart nginx
sudo /opt/bitnami/ctlscript.sh restart nginx
-
-
Congratulations, you should have your environment up and running!