Nginx + Passenger + RVM ile Rails Deploy

Güncel Yazılar

Nginx + Passenger + RVM ile Rails Deploy

1. Run the following commands to install RVM:


    echo 'insecure' > ~/.curlrc
    gpg --keyserver keyserver.ubuntu.com --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
    \curl -sSL https://get.rvm.io | bash -s stable --ruby

2. To install a specific version of Ruby, run:


    rvm install ruby-X.X.X
    rvm --default use ruby-X.X.X

3. Install Bundler


    gem install bundler --no-rdoc --no-ri

4. Install NodeJS


    sudo apt-get update &&
    sudo apt-get install -y apt-transport-https ca-certificates &&
    curl --fail -ssL -o setup-nodejs https://deb.nodesource.com/setup_0.12 &&
    sudo bash setup-nodejs &&
    sudo apt-get install -y nodejs

5. If everything OK, we can install passenger and nginx now.

 
    sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 561F9B9CAC40B2F7
    sudo apt-get install -y apt-transport-https ca-certificates  
    sudo sh -c 'echo deb https://oss-binaries.phusionpassenger.com/apt/passenger wheezy main > /etc/apt/sources.list.d/passenger.list'
    sudo apt-get update

Install Passenger + Nginx sudo apt-get install -y nginx-extras passenger 6. Enable the passenger's nginx module. Edit /etc/nginx/nginx.conf file. Open file with your favourite text editor. (I choose vim)


    sudo vim /etc/nginx/nginx.conf

Find and uncomment this line: # include /etc/nginx/passenger.conf; It should look like this:


    include /etc/nginx/passenger.conf;

 If you don't see a commented version of include > /etc/nginx/passenger.conf; inside nginx.conf, then you need to insert it yourself inside the http block.

 

Restart nginx

  sudo service nginx restart

7. Check installation


    sudo /usr/bin/passenger-config validate-install
     * Checking whether this Phusion Passenger install is in PATH... ✓
     * Checking whether there are no other Phusion Passenger 
installations... ✓

Update & upgrade


    sudo apt-get update
    sudo apt-get upgrade

Deploying Rails App 

1. Login to your server, create a user for the app

ssh root@yourserver.com 

You should give the user account the same name as your app. I will use demouser

sudo adduser demouser

After that give the sudo priviliges to demouser

$ visudo

Add this line below the root ALL=(ALL:ALL) ALL

demouser ALL=(ALL:ALL) ALL

While logged into your server, login under the application's user account as follows:

sudo -u demouser -H bash -l 2 

Pull code from git

cd
git clone git@github.com:username/myapp.git cd myapp

 If you haven't git, install git follows: sudo apt-get install -y git

3. Install App Dependencies

bundle install --deployment --without development test

4. Configure database.yml and secrets.yml

vim config/database.yml

Ensure that the production section looks like this:

production:
   adapter: sqlite3
   database: db/production.sqlite3

Generate secret key:

RAILS_ENV=production rake secret

This command will output a secret key. Copy that value to your clipboard. Open the secrets.yml vim config/secrets.yml

Edit this line:

production: secret_key_base: `value that you copied` 

Run the following command to compile assets for the Rails asset pipeline, and to run database migrations:

RAILS_ENV=production rake assets:precompile

RAILS_ENV=production rake db:migrate

Configuring nginx

Type exit to go back to root account We need to create an Nginx configuration file.

sudo vim /etc/nginx/sites-available/myapp.conf 

Don't forget replace myapp with your app's name.

server {
    listen 80;
    server_name yourserver.com;
    # Tell Nginx and Passenger where your app's 'public' directory is
    root /home/demouser/myapp/public;
    # Turn on Passenger
    passenger_enabled on;
    passenger_ruby /path-to-ruby;
} 

You can use pwd for find app's path. And you can use which ruby for ruby's path.

Now we can create link to /etc/nginx/sites-enabled path.

sudo ln -s /etc/nginx/sites-available/myapp.conf /etc/nginx/sites-enabled/myapp.conf 

When you are done, restart Nginx:

sudo service nginx restart