📜 ⬆️ ⬇️

Amazon on Rails

In this post, I decided to share the experience of deploying rails server on Amazon. I laid out all previous rails projects on ready servers. These were private servers, or the well-known heroku service. And the only commands I sent to the server were cap deploy and cap deploy: cold. But in the last project the customer wanted to place the site on his Amazonian image, so I had to dive into the subject more deeply.


Main tasks

The site should work on the http server of nginx, git was used for group development, and also rvm for flexible configuration of ruby ​​and a set of gem for the needs of the site while preserving the possibility to host another project. And all this should have been published through capistrano.

First stage

At the first stage it was necessary to decide on the linux version. The following distributions were available: Oneiric Ocelot and bitnami amazon machine image. The first was sufficient cheese, let's talk about the second. Bitnami is an operating system based on ubuntu 10.04, in ko> /etc/init.d/nginx
chmod + x /etc/init.d/nginx
update-rc.d nginx defaults
/ Added several additional packages that should make life easier. But, as always, it was not our case. One of the installed “improvements” was apache. The first thing we need is to stop this server and remove the initializer. To do this, log in as root and execute the following command:
')
/etc/init.d/bitnami stop

Also move the daemon to a directory, for example, back_up just in case.
Next, install the necessary libraries

apt-get install build-essential bison openssl libreadline5 libreadline-dev
curl git-core zlib1g zlib1g-dev libssl-dev sqlite3 libreadline-dev libxml2-dev git-core subversion autoconf


After we install the global rvm. To do this, we write in the terminal:

bash < <(curl -L raw.github.com/wayneeseguin/rvm/1.3.0/contrib/install-system-wide)

Let's fix the files /root/.bashrc and /etc/skel/.bashrc. The string [-z "$ PS1"] && return is replaced by:

if [[ -n "$PS1" ]]; then
##
fi


Save the file and do a reboot. In addition to the rvm boot settings, it's nice to specify a few gems needed for any version of ruby ​​and gemset. This is primarily a bundler, capistrano, passenger and rake. So add the following to /usr/local/rvm/gemsets/global.gems

bundler -v~>1.0.18
rake –v~>0.9.2
capistrano –v ~>2.9.0
passenger –v ~>3.0.9


Next, install Ruby EE for quick rails work:

rvm install ree

Install it as the default ruby ​​interpreter:

rvm use ree --default

After we create an additional gemset for the site:

rvm gemset create foo_site

Since creating a user with the ability to access via ssh was not possible, I had to create a directory in Bitnami's $ HOME to store versions of the site, logs and other things.

mkdir foo_site

Now it would be nice to install nginx and passenger

gem install passenger
rvmsudo passenger-install-nginx-module


If you get an error saying that rvmsudo was not found, you can look in the / usr / local / rvm / bin folder or add it to the $ PATH variable for permanent use.
After answering simple questions from the installer, you will receive a working nginx that needs to be configured. To do this, open the configuration file at /opt/nginx/conf/nginx.conf, where we change the value of passenger_ruby to / usr / local / bin / passenger_ruby.
Next you need to specify where the site folder. Insert the string 'root' in the file, below the line 'server', and specify / home / bitnami / foo_site / application / current / public. Where is the 'foo_site' folder with our future site. After we comment out the location section. Next, load the daemon for nginx and run it as root:

curl -L raw.github.com/jnstq/rails-nginx-passenger-ubuntu/master/nginx/nginx > /etc/init.d/nginx
chmod +x /etc/init.d/nginx
update-rc.d nginx defaults
/etc/init.d/nginx start


Now you can log in to the browser for the domain received from Amazon and see the error from nginx. Everything works as it should.
Next will be a conversation about git. If you do not want the repository to be somewhere else (for example on github), the next section is for you.

Configuring git

There are many git repository managers, but I’ve stopped at the popular gitosis. Install it on the server:

sudo apt-get install gitosis

Next, create a git user.

sudo adduser git

Log in as git user:

sudo su git

Add git to the sudoers file:

%git ALL=(ALL) ALL

Create an ssh key for it:

ssh-keygen -t dsa

Next, create a VPS server:

sudo -H -u git gitosis-init < ~/.ssh/id_dsa.pub
git clone git@your_amazon_dns:gitosis-admin


Next, create a site repository, to do this, open the file /home/git/gitosis-admin/gitosis.conf and paste it:

[ group foo_site]
writable = foo_site
members = git@domU-12-31-38-04-1D-DE developer1@mail.com developer2@mail.com
group foo_site]
writable = foo_site
members = git@domU-12-31-38-04-1D-DE developer1@mail.com developer2@mail.com


Let's consider each element in more detail:

group foo_site –
writable = foo_site –
members = git@domU-12-31-38-04-1D-DE developer1@mail.com developer2@mail.com –


Also in the folder keydir you need to create a file developer1@mail.com.pub with your key
Since gitosis itself is a git repository, after any change of settings you need to make a commit & push:

git commit –am 'new changes' && git push

Next, create the folder /home/git/repositories/foo_site.git, in which we initialize the repository

git init -- bare /home/git/repositories/foo_site.git

All now you can use git repositories at git @ amazon-domain: foo_site

Capistrano setting

In your local project, execute the capify command.
It will create a Capfile file and config / deploy.rb.
In the deploy.rb file add:

$:.unshift(File.expand_path('./lib', ENV['rvm_path']))
require "rvm/capistrano"
require 'bundler/capistrano'
#require 'capistrano/ext/multistage'#
require 'capistrano_colors'#
default_run_options[:pty] = true
ssh_options[:forward_agent] = true
set :using_rvm, true
set :rvm_ruby_string, 'ree@foo_site'# ruby gemset
set :rvm_type, :system
set :scm, 'git'#
set :scm_verbose, true
set :git_enable_submodules, 1
set :deploy_via, :remote_cache
set :repository, ' '
set :user, 'bitnami'
set :use_sudo, false
set :stack, :passenger
after "deploy:update", "deploy:cleanup"
set :branch, "master"
server « », :web, :app, :db, :primary => true
set :keep_releases, 4
set :deploy_to, '/home/bitnami/foo_site /application'
set :bundle_without, [:test, :development]



After this, run the cap deploy: setup command.

Congratulations, you have successfully created a server with git, which has sufficient functionality.

Useful information on the topic

Post about nginx + rvm
Creating a git server

Source: https://habr.com/ru/post/129583/


All Articles