Ruby on Rails. From a local host on VDS in 15 minutes
Good day, Habrovsk!
How to move to VDS in 15 minutes and not go deep into the study of nginx, unicorn and capistrano. Step by step instructions under the cut.
I constantly hear from many of my friends PHP programmers that they don’t want to go on the rails just because there is no adequate and cheap hosting with a normal control panel. In fact, the question of where it is hosted, if you write on rails, has already been raised once a hundred on the hub, which is why I will not talk about this. When I first made a project on rails, I was faced with finding a hoster, but that’s not even the point, the main thing for me was the easy process of launching an application on a hosting. I decided to buy myself a VDS and use it as a test server. The process of setting up and preparing VDS was complicated by the fact that I did not find instructions from A to Z, and since, roughly speaking, I did not shine with knowledge in the configuration of not only VDS but also nginx and unicorn and much more. I did not need 200-line configs and terribly confused settings for highly loaded applications, my task was to simply launch the site for a minimum of money and time and give it to the customer along with this VDS. In this article I want to write exactly the instruction that I needed so much, I hope that it will be useful for many beginners to write on rails and will serve as an additional incentive for those PHP programmers who still doubt and continue to use frameworks that copy functionality and features of Ruby on Rails. It seems to me the same thing that put the engine from a Mercedes in a penny. It seems frisky, but that is still not the same. which I needed so much, I hope that it will be useful for many beginners to write on rails and will probably serve as an additional incentive for those PHP programmers who still doubt and continue to use frameworks that copy the functionality and features of Ruby On Rails. It seems to me the same thing that put the engine from a Mercedes in a penny. It seems frisky, but that is still not the same. which I needed so much, I hope that it will be useful for many beginners to write on rails and will probably serve as an additional incentive for those PHP programmers who still doubt and continue to use frameworks that copy the functionality and features of Ruby On Rails. It seems to me the same thing that put the engine from a Mercedes in a penny. It seems frisky, but that is still not the same.
What you must have to start:
For reference: all actions, in my case, are performed on VDS with Ubuntu Server 11.10.
We go to the machine via ssh through the terminal: We put nginx from the repository: We create a directory for the configs of our applications, suddenly there will be several of them: We write a config for the first application. I use vim, but this is a matter of taste, you can use at least standard nano: We write the following contents here: Now we edit the kofig of the nginx server itself. Below is the full config file: Now we need nginx to start automatically when VDS starts. To do this, install the runit supervisor: Create an executable file to run nginx: And write there this: Make it executable: Check if the whole thing worked:
If all is well, you will see 4 running worker_process and nginx server running. Problems can only occur if Apache was installed by default on VDS. So it was with me, which naturally means a busy 80 port. Feel free to demolish Apache and kill all its processes. I hope you are doing well and we will continue. It remains to make a symlink for automatic launch:
Congratulations nginx is installed and configured. It will take about 2 minutes to do everything with proper equipment. You also need to create a user from under whom the application will be launched: Add to this file:
We continue in the same terminal: Here we install curl, then use it to install rvm, deliver a pack of packages that are useful for compiling Ruby, sqlite, etc. After that, install Rubygems, create a gemset for our project, install a bundler for all projects and create a folder for our application in the home directory of the user we created. Install Git. We leave the root, complete ssh.
We go under the user created by ssh: We exit and again we go under the root.
In the vast majority of cases, mysql is used as the database, but you know, my personal opinion is that if the project is not large, but the website itself does something there, then he should not use such a database, sqlite will be enough for him. In addition, we will save even a lot of time. Then move to mysql, if you need it at all, can be done without any problems. Therefore, we will not configure any sub, but immediately move on to Capistrano.
Add the applications in the Gemfile: Create a Capfile in the root of the project with the following contents: There is a small nuance here, if you and I have a git repository located on github, we must not forget to generate rsa keys from under the user created on VDS. How to do this is written in detail on github.com. In the terminal we write:
We write this file into this file: Next, fill in the file /etc/unicorn/first_app.rb with this: Last jerk:
You need to add 2 records of type A: for the subdomains @ and www, indicating the IP of your VDS. It would be nice to do this before setting up the server, then while you all configure the changes will take effect.
Go make sandwiches and run in the browser.
To quickly update small changes on the site, I created such a file in the root of the application: I called it deploy and made it executable. Now, to update the site on github and VDS, only one command is required:
How to move to VDS in 15 minutes and not go deep into the study of nginx, unicorn and capistrano. Step by step instructions under the cut.
I constantly hear from many of my friends PHP programmers that they don’t want to go on the rails just because there is no adequate and cheap hosting with a normal control panel. In fact, the question of where it is hosted, if you write on rails, has already been raised once a hundred on the hub, which is why I will not talk about this. When I first made a project on rails, I was faced with finding a hoster, but that’s not even the point, the main thing for me was the easy process of launching an application on a hosting. I decided to buy myself a VDS and use it as a test server. The process of setting up and preparing VDS was complicated by the fact that I did not find instructions from A to Z, and since, roughly speaking, I did not shine with knowledge in the configuration of not only VDS but also nginx and unicorn and much more. I did not need 200-line configs and terribly confused settings for highly loaded applications, my task was to simply launch the site for a minimum of money and time and give it to the customer along with this VDS. In this article I want to write exactly the instruction that I needed so much, I hope that it will be useful for many beginners to write on rails and will serve as an additional incentive for those PHP programmers who still doubt and continue to use frameworks that copy functionality and features of Ruby on Rails. It seems to me the same thing that put the engine from a Mercedes in a penny. It seems frisky, but that is still not the same. which I needed so much, I hope that it will be useful for many beginners to write on rails and will probably serve as an additional incentive for those PHP programmers who still doubt and continue to use frameworks that copy the functionality and features of Ruby On Rails. It seems to me the same thing that put the engine from a Mercedes in a penny. It seems frisky, but that is still not the same. which I needed so much, I hope that it will be useful for many beginners to write on rails and will probably serve as an additional incentive for those PHP programmers who still doubt and continue to use frameworks that copy the functionality and features of Ruby On Rails. It seems to me the same thing that put the engine from a Mercedes in a penny. It seems frisky, but that is still not the same.
So let's get started
What you must have to start:
- Purchased and working VDS with any Debian family system
- Purchased domain from any registrar
For reference: all actions, in my case, are performed on VDS with Ubuntu Server 11.10.
Install and configure nginx
We go to the machine via ssh through the terminal: We put nginx from the repository: We create a directory for the configs of our applications, suddenly there will be several of them: We write a config for the first application. I use vim, but this is a matter of taste, you can use at least standard nano: We write the following contents here: Now we edit the kofig of the nginx server itself. Below is the full config file: Now we need nginx to start automatically when VDS starts. To do this, install the runit supervisor: Create an executable file to run nginx: And write there this: Make it executable: Check if the whole thing worked:
:~ ssh root@:~ apt-get install nginx:~ mkdir /etc/nginx/sites:~ vi /etc/nginx/sites/first_app.confserver {
listen 80;
# Имя сервера это то, что будет запускаться.
# Некоторые провайдеры генерируют случайное доменное имя, если это ваш случай, то пишем его сюда.
server_name first_app.com;
root /home/first_app/web-app/public;
client_max_body_size 32m;
location / {
try_files $uri @unicorn;
}
location @unicorn {
proxy_set_header Client-Ip $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_pass unix:/home/first_app/web-app/tmp/sockets/unicorn.sock;
}
}
user nobody nogroup;
worker_processes 4;
pid /var/run/nginx.pid;
events {
worker_connections 4096;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# Logging Settings
##
log_format main '$remote_addr - $remote_user[$time_local] "$request"'
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "http_x_forwarded_for"'
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
# gzip_vary on;
gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites/*.conf;
}
#mail {
# # See sample authentication script at:
# # wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
:~ apt-get install runit
:~ mkdir /etc/sv/nginx
:~ vi /etc/sv/nginx/run #!/bin/sh
exec 2>&1
exec /usr/sbin/nginx
:~ chmod +x /etc/sv/nginx/run :~ /etc/sv/nginx/run
:~ ps aux | grep nginx
If all is well, you will see 4 running worker_process and nginx server running. Problems can only occur if Apache was installed by default on VDS. So it was with me, which naturally means a busy 80 port. Feel free to demolish Apache and kill all its processes. I hope you are doing well and we will continue. It remains to make a symlink for automatic launch:
:~ ln -s /etc/sv/nginx/ /etc/serviceCongratulations nginx is installed and configured. It will take about 2 minutes to do everything with proper equipment. You also need to create a user from under whom the application will be launched: Add to this file:
:~ adduser first_app
:~ vi home/itnotes/.bashrc
export RAILS_ENV=productionInstall RVM and RubyGems
We continue in the same terminal: Here we install curl, then use it to install rvm, deliver a pack of packages that are useful for compiling Ruby, sqlite, etc. After that, install Rubygems, create a gemset for our project, install a bundler for all projects and create a folder for our application in the home directory of the user we created. Install Git. We leave the root, complete ssh.
:~ apt-get install curl
:~ bash -s stable < <(curl -s raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)
:~ apt-get install build-essential bison openssl libreadline6 libreadline6-dev zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev
:~ gpasswd -a first_app rvm
:~ apt-get install rubygems
:~ rvm use --create ruby-1.9.2-p318@first_app
:~ rvm ruby-1.9.2-p318@global gem install bundler
:~ mkdir /home/first_app/web-app
:~ apt-get install git-core git-gui git-doc
Install Ruby
We go under the user created by ssh: We exit and again we go under the root.
:~ ssh first_app@
:~ rvm install ruby-1.9.2-p318
DB, Capistrano and Unicorn
In the vast majority of cases, mysql is used as the database, but you know, my personal opinion is that if the project is not large, but the website itself does something there, then he should not use such a database, sqlite will be enough for him. In addition, we will save even a lot of time. Then move to mysql, if you need it at all, can be done without any problems. Therefore, we will not configure any sub, but immediately move on to Capistrano.
Add the applications in the Gemfile: Create a Capfile in the root of the project with the following contents: There is a small nuance here, if you and I have a git repository located on github, we must not forget to generate rsa keys from under the user created on VDS. How to do this is written in detail on github.com. In the terminal we write:
group :development do
...
gem 'capistrano-deploy', '~> 0.1.1', :require => nil
end
group :production do
...
gem 'unicorn', '~> 3.6.2', :require => nil
end
$:.unshift(File.expand_path('./lib', ENV['rvm_path']))
require "rvm/capistrano"
set :rvm_ruby_string, '1.9.2'
require 'capistrano-deploy'
use_recipes :git, :rails, :bundle, :unicorn
server '', :web, :app, :db, :primary => true
set :user, 'first_app'
set :deploy_to, '/home/first_app/web-app'
set :repository, 'git@github.com:<репозиторий>'
after 'deploy:update', 'bundle:install'
after 'deploy:restart', 'unicorn:stop'
:~ mkdir /etc/sv/first_app_unicorn
:~ touch /etc/sv/first_app_unicorn/run
:~ chmod +x /etc/sv/first_app_unicorn/run
:~ vi /etc/sv/first_app_unicorn/run
We write this file into this file: Next, fill in the file /etc/unicorn/first_app.rb with this: Last jerk:
#!/bin/sh
exec 2>&1
export USER=first_app
export HOME=/home/$USER
export RAILS_ENV=production
UNICORN="/usr/local/rvm/bin/rvm ruby-1.9.2-p318@first_app exec bundle exec unicorn"
UNICORN_CONF=/etc/unicorn/first_app.rb
cd $HOME/web-app
exec chpst -u $USER:$USER $UNICORN -c $UNICORN_CONF
worker_processes 4
working_directory "/home/first_app/web-app"
listen "/home/first_app/web-app/tmp/sockets/unicorn.sock", :backlog => 64
pid "/home/first_app/web-app/tmp/pids/unicorn.pid"
stderr_path "/home/first_app/web-app/log/unicorn.stderr.log"
stdout_path "/home/first_app/web-app/log/unicorn.stdout.log"
ln -s /etc/sv/first_app_unicorn /etc/serviceDomain setting
You need to add 2 records of type A: for the subdomains @ and www, indicating the IP of your VDS. It would be nice to do this before setting up the server, then while you all configure the changes will take effect.
Go make sandwiches and run in the browser.
Conclusion
To quickly update small changes on the site, I created such a file in the root of the application: I called it deploy and made it executable. Now, to update the site on github and VDS, only one command is required:
git add .
git commit -a -m "Release"
git push
cap deploy:update
cap deploy:restart
:~ ./deploy