Deployment of rubyweb environment using nginx + unicorn

Source: Internet
Author: User
: This article describes how to deploy the rubyweb environment using nginx + unicorn. For more information about PHP tutorials, see. Recently, I took over a ruby web project. because I have never been familiar with ruby and related web development before, and there are missing documents in the project, I took some detours when deploying this project. Finally, the project is successfully deployed by referring to multiple ruby on rails development environments and learning the relationship between gem and bundler in ruby. Here is a small summary. It can also be used as a reference when ruby web development is just getting started.
Our project is based on padrino. if it is based on rails, the deployment should be similar.

1. install ruby

Because the system may have multiple ruby-related projects, and different projects may need to be developed or tested for different ruby versions, multiple ruby versions may need to be installed in the system. This requires tools to manage existing versions and install new versions. Here I use rbenv and can also use the old rvm.
Install rbenv
git clone git://github.com/sstephenson/rbenv.git  ~/.rbenvecho 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrcecho 'eval "$(rbenv init -)"' >> ~/.bashrcexec $SHELL
Add 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
Add the gem-rehash component
git clone https://github.com/sstephenson/rbenv-gem-rehash.git ~/.rbenv/plugins/rbenv-gem-rehash
Install ruby (using ruby2.1.5 as an example)
Ruby installation is slow by default, so download the corresponding 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
In this example, wgetis the downloaded rubyfile, which is ruby-2.1.5.tar.gz.
RUBY_BUILD_MIRROR_URL
Specifies the path of the installed ruby Image. The local path is used here. Note that the local path should end #.
The version after the rbenv install command should correspond to the ruby version to be installed. for example, the installed version may contain the corresponding patch number (for example, 1.9.3-p223.

2. install necessary components

Because we generally use bundler to manage the gems required by the project, we need to install bundler here.
Considering the speed of connecting ruby images in China, we replaced the gem source with the taobao source.
gem sources --remove https://rubygems.org/gem sources -a https://ruby.taobao.org/
Then execute the gem sources-l, which should only be https://ruby.taobao.org/this source
Install bundler
gem install bundler

3. Application Deployment

A. clone the web source code to a proper location

Assume that the cloned source code is stored in/opt/projectA.

B. install the dependent gems

Run the following command in the root directory of the project:
bundle install
Note that the commands executed may be slightly different depending on the situation. For example, you may need to execute
bundle install --deployment --without development test
In this way, the related dependent gems is installed under projectA/vendor/bundle. It is especially suitable for different projects that depend on different versions of the same gem.

C. database migration

Because this project is based on padrino, you need to execute:
padrino rake --envir db:migrate
Note that bundle may be used to execute the command as needed:
bundle exec padrino rake --envir db:migrate
After the database is successfully migrated, you need to create an amdin account. Padrino creates the admin account in db/seeds. rb by default. Therefore, run the following command:
bundle exec padrino rake --envir seed
During execution, the system prompts you to enter the admin account's email address and password.

D. unicorn deployment

The corresponding unicorn startup script may be required here. you can refer to unicorn. rb of ruby-china. Here is a brief description.
Start unicorn server:
bundle exec unicorn -E production -c unicorn.rb -D
In this way, the padrino server is started normally. If unicorn. rb is listening for the tcp port, the corresponding url should be accessible. If you use the unix socket listening method, you should also use nginx (or apache) for reverse proxy.

4. install and configure nginx

A. install nginx

Nginx can be installed by installing common linux software. We recommend that you use the source code installation method. The installation method of package manager is as follows:
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   500 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;    }}

Forward requests from Port 7788
unix:/tmp/projectA.sock
. That is, it is forwarded to our web server for processing.
Now, the server is running properly. Other auxiliary functions 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. during the deployment process, you may need to make corresponding adjustments when executing the corresponding commands. However, as long as you are familiar with the deployment method of ruby web, other documents that can be referenced in the relevant framework are relatively easy to deploy.

The preceding section describes how to deploy the ruby web environment using nginx + unicorn, including some content. I hope to help some friends who are interested in the PHP Tutorial.

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.