Deploy Rails 4.2 application with ruby 2.2 and nginx passenger on amazon ec2
Deploying Rails 4.2 application with nginx + passenger is so easy.
Step 1
Setup EC2 server with ubuntu 14.04
Install ruby 2.2.2 and setup one database like PG etc.
This can be done following this http://railsroot.com/setup_rails
Once all the basic setup is done.
Now install nginx with passenger.
Follow following steps to do it.
you can now install Passenger on Ubuntu with the Advanced Packaging Tool (APT)
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 561F9B9CAC40B2F7
Create an APT source file (you will need sudo privileges):
sudo vim /etc/apt/sources.list.d/passenger.list
And insert the following line in the file:
deb https://oss-binaries.phusionpassenger.com/apt/passenger trusty main
Update the APT cache:
sudo apt-get update
Finally, install Passenger with Nginx:
sudo apt-get install nginx-extras passenger
Open the Nginx configuration file:
sudo vim /etc/nginx/nginx.conf
Find the following lines, in the http block:
##
# Phusion Passenger config
##
# Uncomment it if you installed passenger or passenger-enterprise
##
# passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
# passenger_ruby /usr/bin/passenger_free_ruby;
Uncomment both of them. Update the path in the passenger_ruby line. They should look like this:
passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
passenger_ruby /usr/bin/passenger_free_ruby;
We need to disable the default Nginx configuration. Open the Nginx config file:
sudo vim /etc/nginx/sites-available/default
Find the lines:
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
Comment them out, like this:
# listen 80 default_server;
# listen [::]:80 default_server ipv6only=on;
Save the file.
Now, create an Nginx configuration file for your rails app:
sudo vim /etc/nginx/sites-available/appname
Add the following server block. The settings are explained below.
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
server_name www.example.com;
passenger_enabled on;
passenger_app_env production;
root /home/ubuntu/appname/public;
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /home/ubuntu/appname/public;
}
}
Create a symlink for it:
sudo ln -s /etc/nginx/sites-available/appname /etc/nginx/sites-enabled/appname
Restart Nginx:
sudo service nginx restart
Now your app’s website should be accessible.
Updating code and assets:
rake assets:precompile RAILS_ENV=production
touch tmp/restart.txt