Nginx+unicorn Deploying a ruby Web Environment

Source: Internet
Author: User
Tags rehash ssl certificate ruby on rails


recently took over a Ruby Web project, and since it was not previously exposed to Ruby and related web development, and the document was missing, it was a bit of a detour to deploy the project .。Finally, we have successfully deployed the project by referring to several Ruby on rails development environment deployment and learning the relationship between Gem/bundler in Ruby and so on. Here's a little summary, and you can also provide a reference to Ruby Web development just getting started with the deployment environment.
Our project is based on Padrino, and if it's a rails-based project, the deployment should be similar.
1. Install Rubybecause there may be more than one Ruby-related project in your system, and different projects may need to be developed or tested for different versions of Ruby, you may need to install multiple versions of Ruby in your system. This requires the appropriate tools to manage the existing version and install the new version. I choose the rbenv here, but also can use the veteran RVM.
Installing Rbenv
git clone git://github.com/sstephenson/rbenv.git  ~/.rbenvecho ' export path= ' $HOME/.rbenv/bin: $PATH "' >> ~/ . Bashrcecho ' eval ' $ (rbenv init-) "' >> ~/.bashrcexec $SHELL
Adding Ruby-build Components
git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-buildecho ' export path= ' $HOME/.rbenv/ Plugins/ruby-build/bin: $PATH "' >> ~/.bashrcexec $SHELL
Adding Gem-rehash Components
git clone https://github.com/sstephenson/rbenv-gem-rehash.git ~/.rbenv/plugins/rbenv-gem-rehash
Install Ruby (Take ruby2.1.5 as an example)
Since Ruby will be slow to install by default, download the appropriate file from ruby.taobao.org and install it locally
Wget-o ~/.rbenv/versions/ruby-2.1.5.tar.gz http://ruby.taobao.org/mirrors/ruby/2.1/ruby-2.1.5.tar.gzenv Ruby_ build_mirror_url=file:///home/ziven/.rbenv/versions/ruby-2.1.5.tar.gz# ~/.rbenv/bin/rbenv Install 2.1.5
where wget is to download the appropriate Ruby file, here is ruby-2.1.5.tar.gz
Ruby_build_mirror_url
Used to specify the path to the Ruby image for installation, where the local path is used directly. Note that the local path should be followed by a # to end
The version that follows the Rbenv install command should correspond to the version of Ruby that you want to install, such as the version that may be installed with the corresponding patch number (for example: 1.9.3-p223), which should also be written here.

2. Install the necessary components because we generally use bundler to manage the gems required for a project, we need to install bundler here.
Considering the speed of the domestic connection to Ruby images, we replaced the gem source as Taobao source.
Gem sources--remove Https://rubygems.org/gem sources-a https://ruby.taobao.org/
After executing the gem sources-l should only https://ruby.taobao.org/this one source
Installing Bundler
Gem Install Bundler

3. Application deployment A. Clone Web source to the appropriate location assuming that the source code after clone is placed into the/OPT/PROJECTA
B. Install dependent gems in the root directory of the project:
Bundle Install
It is important to note that commands that are executed according to different circumstances may vary slightly. For example, when deploying in a production environment, you may need to perform
Bundle Install--deployment--without Development test
When this is deployed, the associated dependency gems is installed under Projecta/vendor/bundle. This is especially useful when different projects rely on different versions of the same gem.
C. Database-related migrations because this project is based on Padrino, you need to perform:
Padrino Rake--environment=production db:migrate
It is important to note that it is possible to use bundles to execute the command, depending on the situation:
Bundle EXEC Padrino Rake--environment=production db:migrate
After the database migration is successful, you need to create a Amdin account. Padrino Default in DB/SEEDS.RB to complete the creation of the admin account. So here's the execution:
Bundle EXEC Padrino rake--environment=production seed
During the execution, you will be prompted to enter the Admin account's mailbox and password

D.unicorn deployment here may require a corresponding unicorn startup script, which can be written in reference to Ruby-china's unicorn.rb. Withheld here.
Start Unicorn Server:
Bundle EXEC unicorn-e production-c unicorn.rb-d
This Padrino server has started normally. If the unicorn.rb is listening to a TCP port, then access to the appropriate URL should be open at this time. If you are using a UNIX socket, you should also use Nginx (or Apache) to do the reverse proxy.

4. Nginx installation configuration A. Nginx installation Nginx can install Linux General Software installation method to install. We recommend using the source code installation method. For the sake of brevity, install it in the Package Manager installation mode:
sudo apt-get install Nginx-full

B. Nginx Configuration
Upstream MyServer {  server unix:/tmp/projecta.sock fail_timeout=0;} server {    listen       7788;    server_name  localhost;    Client_max_body_size 10M;    Error_page   502 503 504  /50x.html;    Location =/50x.html {        root   /usr/share/nginx/html;    }    Root/opt/projecta/web/public;    Try_files $uri $uri. html @app;    Location @app {        proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;        Proxy_set_header Host $http _host;        Proxy_redirect off;        Proxy_pass http://myserver;    }}

This will forward the 7788-port request to
Unix:/tmp/projecta.sock
Be processed. It is also forwarded to just our web server for processing.

At this point, the server is ready to run properly. Other accessibility features can be added as needed. For example, Nginx may require an SSL certificate for authentication.

The deployment of the Ruby on Rails project is similar, and the deployment process may need to be adjusted accordingly when the corresponding command is executed. However, as long as you are familiar with how the Ruby Web is deployed, other documents that can be referenced to the relevant framework are relatively easy to deploy.



Nginx+unicorn Deploying a ruby Web Environment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.