In this blog I will list the steps needed to deploy a Rails 4 application on an Amazon EC2 server. The OS is Ubuntu Server 14.04 LTS, the database is Postgresql and the web server is Unicorn + Ngnix. Previously I mainly deploy the project on Engine Yard. But this time I need to deploy it on a server manually.
Updating And Preparing The Operating System
Update the Operating System
Firstly we need to update our OS by running following command,
1
|
|
Change locale file
In some cases on my server it shows the following warnings,
1 2 3 4 5 6 7 |
|
The solution is to edit the /etc/default/locale and add the following contents,
1 2 |
|
Install the necessary packages
Next we need to install the following necessary packages by running,
1
|
|
The python-software-properties allows us to easily manage your distribution and independent software vendor software sources. It will be useful when we install Postgresql and Nginx.
The nodejs is a Javascript runtime for Rails.
Install rbenv
Now we need to install a Ruby runtime. I choose rbenv instead of rvm, which is more lightweight for production.
Let’s install rbenv by running the following command,
1
|
|
After installation, following its instructions, add the following content at the top of ~/.bash_profile
1 2 3 4 5 6 |
|
After that run source ~/.bash_profile
to make the changed effective.
Now we install Ruby 2.1.2 and make it global by running following commands,
1 2 3 |
|
The install could take a while so grab a cup of coffee.
After Ruby 2.1.2 is installed, we can install bundler by typing,
1 2 |
|
Install Nginx
Now let’s install Nginx. Let’s add the repository of Nginx by typing following command,
1
|
|
Now we need to invoke sudo apt-get update
again since we added a new software source.
1
|
|
Now let’s install Nginx by invoking,
1
|
|
Let’s check Nginx status by typing
1
|
|
If it’s not running, we can start it by running following command,
1
|
|
Install Unicorn
Now we need to install unicorn if you haven’t done so.
Add gem 'unicorn'
in your Gemfile and run bundle install
.
And after that you can run unicorn_rails
to start it. The default port of unicorn is 8080.
And then create unicorn config file config/unicorn.rb which has following contents,
1 2 3 4 5 6 7 |
|
Here we set worker_processes to 5, and in the server our application will be at /data/xhoppe. And the stdout_path and stderr_path are set to /data/xhoppe/log/unicorn.log.
Install Postgresql
Now let’s install Postgresql.
I want to install Postgresql 9.3 but the Postgresql version in default repository is 9.1. So we install Postgresql 9.3 by typing following commands, refer here
1 2 3 4 5 |
|
Now let’s create a Postgresql role, open psql by running sudo su -c psql postgres
and type following commands,
1 2 3 |
|
Nginx config file
And then update the /etc/nginx/sites-enabled/default file,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
The upstream unicorn matches the listen "/tmp/unicorn.sock"
in config/unicorn.rb
After that, we need to run service nginx restart
to restart nginx.
Clone project
Now we use git clone to clone our project in /data/xhoppe
First we define a /data/xhoppe/env.sh which defines some environments,
1 2 3 4 5 |
|
The XHOPPE_DATABASE_PASSWORD is the production database’s password. And the SECRET_KEY_BASE is for rails session, which is generated by running rake secret
.
After that we can run source env.sh
to set these environment variables.
Running the project.
Now we can run the project by running
unicorn -c config/unicorn.rb -E production -D
Now we can access the website from our browser.