Ubuntu 14.04使用Unicorn Nginx部署Ruby on Rails应用

Ruby on Rails是一个流行的快速开发Web框架,网页设计师和开发人员可以借助它使用Ruby编程语言实现功能齐全的动态Web应用程序。 本文介绍使用Unicorn部署Ruby on Rails和在Ubuntu 14.04上部署Nginx Web服务器。

安装必要软件包



使用以下命令更新系统的存储库数据库和安装的软件包:

  1. apt-get update
  2. apt-get upgrade

安装Ruby



1.安装Ruby依赖

  1. sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties libffi-dev nodejs

2.下载最新版本的Ruby。

  1. wget https://cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.0.tar.gz

3.解压

  1. tar -xzvf ruby-2.3.0.tar.gz

4.切换目录

  1. cd ruby-2.3.0

5.编译安装

  1. ./configure
  2. make
  3. sudo make install

安装并创建一个Rails应用



1.使用gem(Ruby的包管理框架)在服务器上安装Rails:

  1. sudo gem install rails

2.切换到家目录

  1. cd

3.创建一个新的Rails项目。 使用example作为项目名称:

  1. rails new example

4.切换到项目目录

  1. cd example

安装配置Unicorn



1.使用gem在服务器上安装Unicorn:

  1. sudo gem install unicorn

2.创建文件config/unicorn.rb,其中包含unicorn配置,并将以下配置粘贴到文件中。
/home/username/example/config/unicorn.rb:

  1. # set path to the application
  2. app_dir git File.expand_path("../..", __FILE__)
  3. shared_dir = "#{app_dir}/shared"
  4. working_directory app_dir
  5.  
  6. # Set unicorn options
  7. worker_processes 2
  8. preload_app true
  9. timeout 30
  10.  
  11. # Path for the Unicorn socket
  12. listen "#{shared_dir}/sockets/unicorn.sock", :backlog => 64
  13.  
  14. # Set path for logging
  15. stderr_path "#{shared_dir}/log/unicorn.stderr.log"
  16. stdout_path "#{shared_dir}/log/unicorn.stdout.log"
  17.  
  18. # Set proccess id path
  19. pid "#{shared_dir}/pids/unicorn.pid"

3.现在,创建Unicorn配置文件中涉及到的目录:

  1. mkdir -p shared/pids shared/sockets shared/log

安装配置Nginx



1.安装Nginx

  1. sudo apt-get install nginx

2.我们需要配置nginx作为反向代理。 编辑配置文件/etc/nginx/nginx.conf并将以下配置粘贴到HTTP块中:

  1. upstream rails {
  2. # Path to Unicorn socket file
  3. server unix:/home/username/example/shared/sockets/unicorn.sock fail_timeout=0;
  4. }

3.删除默认的nginx站点配置:

  1. sudo rm /etc/nginx/sites-enabled/default

4.为Rails应用程序创建新的nginx站点配置文件:
/etc/nginx/sites-available/example:

  1. server {
  2. listen 80;
  3. server_name localhost;
  4.  
  5. root /home/username/example;
  6.  
  7. try_files $uri/index.html $uri @rails;
  8.  
  9. location @rails {
  10.    proxy_pass http://rails;
  11.    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  12.     proxy_set_header Host $http_host;
  13.    proxy_redirect off;
  14. }
  15.  
  16. error_page 500 502 503 504 /500.html;
  17. client_max_body_size 4G;
  18. keepalive_timeout 10;
  19. }

5.创建指向nginx sites-enabled目录的软链接以启用站点配置文件:

  1. sudo ln -s /etc/nginx/sites-available/example /etc/nginx/sites-enabled

6.重启Nginx

  1. sudo service nginx restart

启动Unicorn



以开发环境模式启动Unicorn:

  1. sudo unicorn -c config/unicorn.rb -E development -D

以生产环境模式启动Unicorn:

  1. sudo unicorn -c config/unicorn.rb -E production -D

要停止Unicorn,请执行以下命令:

  1. sudo pkill unicorn
标签:部署UbuntuNginx 发布于:2019-11-21 11:02:33