Wednesday, May 30, 2012

AWS Ubuntu 12.04, apache and rails production server

>Load Ubuntu 12.04. I get all my base images from http://alestic.com/ You can choose the AWS region on the right pane. If you are using the free tier and don't want to incur any fees use the EBS boot so you can spin up a micro instance.

>Get updates and a few other nick nacks. Run 'sudo apt-get update' and 'sudo apt-get upgrade' Also I like to be able to login remotely to my server without the key so I allow remote login by going into /etc/ssh/sshd_config and set 'setpasswrd = yes'. Then restart ssh with 'sudo service ssh restart'. Now set your users password with 'sudo passwd ubuntu'.

>Install RVM ruby and rails Run 'curl -L get.rvm.io | bash -s stable'. This will probably fail. You need a few packages before rvm can run. Read the output in the error message and there is a section that lists a much of packages. Simply cut/paste this section into the terminal to install them. After this is successful you need to source rvm with 'source ~/.rvm/scripts/rvm', and now install your ruby version of choice. I installed 1.9.2 with 'rvm install 1.9.2'. Now that you have ruby you can 'use' that version by typing 'rvm use 1.9.2' and now you can install rails with 'gem install rails'.

>Install apache
 To install apache type 'sudo apt-get install apache2' Now if you navigate to your web the server in your browser you should see the cheezy 'It Works' massage from apache. Keep in mind you need to open port 80 on you AWS server.

>Install passenger
Run 'gem install passenger'. After the gem installs run 'passenger-install-apache2-module' the install will run and it will probably tell you that you are missing some stuff. So just install that stuff. In my case I need 'sudo apt-get install libcurl4-openssl-dev apache2-prefork-dev libapr1-dev libaprutil1-dev'. So after that stuff installs, run 'passenger-install-apache2-module' again. After passenger installs  DON'T CLEAR THE SCREEN!!! You need to copy the LoadModule statement and paste that into your /etc/apache2/httpd.conf file.

>Create a rails app
OK, we have all this cool configuration, now how do we wrap it all up with a rails app. OK, lets create one, but before we do we are in production now, so lets install a real database. Run 'sudo apt-get install mysql-server'. No, its not postgres, which is a REAL database, but it will do. MEEEOW...give that kitty a bowl of milk! OK, now lets create a rails app, and we will make mysql our default DB. Lets run 'rails new task_app -d mysql', and cd into that directory and go ahead and add 'gem therubyracer' to the Gemfile and run bundle (why do I have to do that?).  Also before I can run any rails scaffold commands I have to run 'export RAILS_ENV=production' why do I have to do this?

Sooo, now we can run some scaffolding 'rails g scaffold task name importance:boolean'

OK, if we check out app/config/database.yml we see the mysql configuration from our app, and it has given us a default name of 'task_app_production'. So lets run 'rake db:create' and 'rake db:migrate'.

Now we need to add this to our apache config file in /etc/apache2/httpd.conf

   LoadModule passenger_module /home/ubuntu/.rvm/gems/ruby-1.9.2-p320/gems/passenger-3.0.12/ext/apache2/mod_passenger.so
   PassengerRoot /home/ubuntu/.rvm/gems/ruby-1.9.2-p320/gems/passenger-3.0.12
   PassengerRuby /home/ubuntu/.rvm/wrappers/ruby-1.9.2-p320/ruby

<VirtualHost *:80>
    ServerName www.yourserver.com
    DocumentRoot /home/ubuntu/task_app/public
    <Directory /home/ubuntu>
        Allow from all
    </Directory>
</VirtualHost>

..and one more thing before we lock and load. We need to go in to config/environments/production.rb and set 'config.assets.compile = true' rather than 'false'. Now restart apache with 'sudo service apache2 restart' and you should be good to go!











See this post for running multiple apps on a singe server.

1 comment:

  1. Thanks for sharing. In my case in the passenger installing step i had to

    rvmsudo passenger-install-apache2-module

    Because im using RVM

    Cheers

    ReplyDelete