Launch Website in Amazon EC2

I wrote this blog about a year ago and left it in the draft because this post somehow was not getting auto saved on WordPress and since its long post it took time for me type, take screenshots and paste. I did not have energy and time to do it all over again. I had a copy of it though in my google drive and I cannot tell you how many times this document has helped me.

Now I have a short-term memory. I remember phone numbers which I heard 10-15 years ago but some how command line arguments, street names etc. have always been elusive. My wife keeps poking fun at me when I drive and says that I am “directionally challenged”. I just cannot remember a route. I am absent-minded, not blank, but my mind just keep thinking all the time. I follow the same route to office every day, however as it often happens that I am always lost in my thoughts, I would take I-85 South ramp instead of I-85 north ramp and I am baffled a minute or two later, at the exit signs and wonder why are the exit numbers decreasing.

Anyway the point is that this document will help some absent-minded like me who do the set up once but when something goes wrong do the research all over again and wonder what did I do the last time.

As usual I am going to start from scratch and would put this in steps.

1. Create a new Amazon web services account.

2. Go to your Console and select EC2 tab.

3. Now Lunch and instance. You will be shown a screen to select a wizard. You can choose between classic and quick wizard. The difference is that with classic wizard, you have fine grain control over what instance and software you want to install while Quick wizard is prebuilt server, for example – Ubuntu+Apache+Mysql+PHP. If you want the same instance set up on Classic wizard then you need install on your own.

I choose Classic Wizard. Click Continue.

Next I chose UBUNTU 12.04 32 bit.

Next choose your instance. I choose Micro instance.

 

You can also chose Request Spot Instance where you basically requests your own quote and can specify the largest amount that you intend to pay. Something like price negotiator.

Click Continue

Click Continue.

Next name your instance and click Continue.


Next create a Key-Value pair. Name your key and click Create and Download your Key Pair

 

Save the key on your local system and click continue.

Next configure your security settings. It will be named  quick-launch by default  with port 22 for sftp open. You can add more ports if you like. I added port 80(Http) and 443(Https).

Now you are ready to launch your server.

A confirmation page lets you know your instance is launching. Click Close to close the confirmation page.

In the Navigation pane, click Instances to view the status of your instance. It takes a short time for an instance to launch. The instance’s status will be pending while it’s launching.

Record the Public DNS name for your instance because you’ll need it for the next task. If you select the instance, its details (including the public DNS name) are displayed in the lower pane. You can also click Show/Hide in the top right corner of the page to select which columns to display.

ec2-xx-xx-xx-xx.compute-1.amazonaws.com

4. Connecting to your Linux instance

Just Right click and click Connect…. It will give you option to connect via ssh or java client. Choose java client. For Ubuntu, the user name is ubuntu. Specify the location of your  key-value pair that you downloaded earlier. Hit Connect. You are now connected.

5. Update ubuntu packages. Run this command –

apt-get update

apt-get upgrade –show-upgraded

6. Now we already have default user “ubuntu”. However I wanted to create my username. So create one –

sudo adduser example_user

You will be asked several questions like Full Name, Room number etc. Just click Enter and continue

Enter the new value, or press ENTER for the default

    Full Name []:

    Room Number []:

    Work Phone []:

    Home Phone []:

    Other []:

Is the information correct? [Y/n] y

7. Now we need to allow this new user to administer the system. So to do this we need to give it admin rights. Run this:

sudo usermod -a -G sudo example_user

8. Install Git

sudo apt-get install build-essential git-core curl

9. Install RVM to support different version of Ruby.

curl -L get.rvm.io | bash -s stable

10. Add RVM to bashrc

echo ‘[[ -s “$HOME/.rvm/scripts/rvm” ]] && source “$HOME/.rvm/scripts   /rvm”‘ >> ~/.bashrc

11. Reload bashrc file

. ~/.bashrc

12. Now exit from the session and type

type rvm| head -1

This will give you a message that “rvm is a function”

13. Next we will install ruby.

rvm install 1.9.3

14. Use ruby 1.9.3 as default

rvm –default use 1.9.3

15. To check the version of ruby-

ruby -v

This should tell you that you are using – “ruby 1.9.3p194”

16. Let’s install RAILS now.

gem install rails -v 3.2.1

Now you may run into issue and get this error

[email protected]:~$ gem install rails -v 3.2.1

ERROR:  Loading command: install (LoadError)

cannot load such file — zlib

ERROR:  While executing gem … (NameError)

uninitialized constant Gem::Commands::InstallCommand

If you get this error that do not worry, it is just telling you that you need to install some more packages.

Run these commands:

rvm pkg install zlib

rvm remove 1.9.3

rvm install 1.9.3

rvm –default use 1.9.3

gem install rails -v 3.2.1

17.  Time to install Mysql

sudo apt-get update

sudo apt-get upgrade –show-upgraded

sudo apt-get install libmysqlclient-dev

sudo apt-get install mysql-server

You will be prompted with Mysql installation screen. Just follow the instructions to set up root user name and password.

18. Update the git configuration

git config –global user.name “Firstname Lastname”

git config –global user.email “[email protected]

19) Install passenger and Nginx

gem install passenger

passenger-install-nginx-module

If you run into issues then run the following

apt-get install libopenssl-ruby

apt-get install libcurl4-openssl-dev

apt-get install libssl-dev

If you still run into issues where the installation instructions says that openssl-dev is not installed then run this command

rvm pkg install openssl

rvm remove 1.9.3

rvm install 1.9.3

rvm –default use 1.9.3

rvmsudo passenger-install-nginx-module  //You have to use rvmsudo if you are not logged in as root.

20) Download the code from now. To do that you need to create new ssh key and set it up on github.

ssh-keygen -t rsa -C “[email protected]

– Copy the key value from /root/.ssh/id_rsa.pub and copy the key in your git hub account.(If you do not know how to add ssh key then see github help document. Basically just goto github settings–> ssh-keys–> Add Key)

– Now create your app folder. I created mine under /home/apps/. Now go to apps folder and run this command in terminal – git clone [email protected]……….xxxx.git (Your git url).

21) Now we will install bundler.

Switch to your application folder, such as – cd /home/apps/albums and run the command

 gem install bundler

 bundle install

Ran into error below for Rmagick gem

Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

    /home/ubuntu/.rvm/rubies/ruby-1.9.3-p194/bin/ruby extconf.rb

checking for Ruby version >= 1.8.5… yes

extconf.rb:128: Use RbConfig instead of obsolete and deprecated Config.

checking for gcc… yes

checking for Magick-config… no

Can’t install RMagick 2.13.1. Can’t find Magick-config in /home/ubuntu/.rvm/gems/ruby-1.9.3-p194/bin:/home/ubuntu/.rvm/gems/[email protected]/bin:/home/ubuntu/.rvm/rubies/ruby-1.9.3-p194/bin:/home/ubuntu/.rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

*** extconf.rb failed ***

Could not create Makefile due to some reason, probably lack of

necessary libraries and/or headers.  Check the mkmf.log file for more

details.  You may need configuration options.

Provided configuration options:

    –with-opt-dir

    –with-opt-include

    –without-opt-include=${opt-dir}/include

    –with-opt-lib

    –without-opt-lib=${opt-dir}/lib

    –with-make-prog

    –without-make-prog

    –srcdir=.

    –curdir

    –ruby=/home/ubuntu/.rvm/rubies/ruby-1.9.3-p194/bin/ruby

Gem files will remain installed in /home/ubuntu/.rvm/gems/ruby-1.9.3-p194/gems/rmagick-2.13.1 for inspection.

Results logged to /home/ubuntu/.rvm/gems/ruby-1.9.3-p194/gems/rmagick-2.13.1/ext/RMagick/gem_make.out

An error occured while installing rmagick (2.13.1), and Bundler cannot continue.

Make sure that `gem install rmagick -v ‘2.13.1’` succeeds before bundling.

If you get the same error for Rmagick or Mysql or anything else, then run the below command.

sudo apt-get install libmagickwand-dev

Now re run the command

bundle install

22) Starting up the passenger now. However before we start passenger we need make sure that our database exist. So let’s create database and do the database migrations. Run the below commands –

rake db:create

rake db:migrate //// Now this will not create a production db, but will create dev, test db for you. If you intend to create a production DB as well then run this command –

RAILS_ENV=production rake db:create

RAILS_ENV=production rake db:migrate

I ran into issue and got the below error –

rake aborted!

Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes.

The forums said that I need to install nodeJs. So here’s the list of command to install nodeJs.

 sudo apt-get install python-software-properties

 sudo add-apt-repository ppa:chris-lea/node.js

 sudo apt-get update

 sudo apt-get install nodejs

Now the last thing you need to before starting passenger is pre compile your assets(css, images, js etc.). If you do not do this you will not be able to see the images and css. So run this command

bundle exec rake assets:precompile

Oh by the way if your images are not being served even after running the above command and starting passenger then you need to read my other post – http://railgaadi.wordpress.com/2012/01/28/engineyard-rails-3-x-nginx-passenger-assets-not-displayed/

P.S> The above issue is pretty common and first time user who are trying to promote run into the above issue and give up eventually. I stopped looking at it after 2 days… took a 3 day break and attacked the issue again 🙂

Now start the passenger.(Make sure that you have started Nginx  before starting Passenger else… see my earlier post – http://railgaadi.wordpress.com/2012/01/28/engineyard-rails-3-x-nginx-passenger-assets-not-displayed/)

passenger start -e production

I got error that “can’t connect to mysqlserver through socket tmp/mysql.sock”. If you run into this server then run this command

 mysqladmin variables | grep socket

If you have a root password then use

sudo mysqladmin -p variables | grep socket

The above command will give you socket name. In my case it gave me /tmp/var/mysq.lock.

Note this value and update your database.yml file and update the socket as given below.

development:
 adapter: mysql2
 host: localhost
 username: root
 password: xxxx
 database: xxxx
 socket: /tmp/mysql.sock

After you have updated the database.yml file you should be able to start the passenger.

23) Setting up NGINX

Make sure that you nginx.conf under /opt/nginx/conf file’s server section looks like this

server {

    listen    80;

    server_name  www.mysitename.com;

    access_log /srv/www/mysitename.com/logs/access.log;

    error_log /srv/www/mysitename.com/logs/error.log;

    root /home/myapp/album/public;

    passenger_enabled on;

    passenger_base_uri /home/myapp/album/public;

    #This property allows you to upload huge pictures files else you will get error 413- File too large

    client_max_body_size 5M;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

#    location / {

 #       root   /home/dinesh19aug/album/public;

 #       index  index.html index.htm;

 #   }

Happy launching 🙂

In case you are wondering what did I launch —- P.S. If you have been following my blog it’s my wife’s photography website. Wifeys website

~~Ciao

P.S. – Look forward for my first hand experience with Node.js in the next post.