【Ruby on Rails全栈课程】5.5 项目上线--nginx+unicorn部署项目、域名映射

1、域名映射准备材料

2、安装以及设置nginx

Nginx 是一个高性能的HTTP和反向代理服务
可以理解为连接我们项目以及域名的一个代理服务器,我们会在nginx配置文件中配置我们的项目目录以及需要映射的域名

(1)安装nginx

ubuntu@VM-16-15-ubuntu:~$ sudo apt-get install nginx

(2)设置nginx

//打开文件夹
ubuntu@VM-16-15-ubuntu:~$ cd /etc/nginx/conf.d/
//创建名为data.conf的文件
ubuntu@VM-16-15-ubuntu:/etc/nginx/conf.d$ touch data.conf
//输入nano data.conf 打开data.conf文件,粘贴下面的代码
ubuntu@VM-16-15-ubuntu:/etc/nginx/conf.d$ nano data.conf

在data.conf中需要粘贴的代码(注意如果下面出现版权信息的文字,记得删除)

upstream p_data {
  server unix:/mnt/project/private_data/unicorn.sock  fail_timeout=0;
}

server {
  listen 80;
  server_name datac.com;
  root /mnt/project/private_data/public;

  location / {
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Server $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_buffering on;
    proxy_pass http://p_data;
  }
}

将上面代码中的两处替换:

  • server unix:/mnt/project/private_data/unicorn.sock  fail_timeout=0;中的/mnt/project/private_data/换成你自己的项目路径
  • datac.com替换成你自己的域名,域名不需要加www.

3、安装并配置unicorn

Unicorn是为Ruby应用程序提供的一个HTTP服务器
在Ruby on Rails项目中常将nginx与unicorn搭配使用,unicorn会通过智能的负载均衡或者简单的轮训方式通过nginx发送请求

(1)打开本地电脑上的代码,创建config/unicorn.rb,并在里面粘贴下列代码。

module Rails
  class <<self
    def root
      File.expand_path("../..", __FILE__)
    end
  end
end
puts Rails.root
rails_env = ENV["RAILS_ENV"] || "production"

preload_app true
working_directory Rails.root
pid "#{Rails.root}/tmp/pids/unicorn.pid"
stderr_path "#{Rails.root}/log/unicorn.log"
stdout_path "#{Rails.root}/log/unicorn.log"

listen 6420, :tcp_nopush => false

listen "#{Rails.root}/unicorn.sock"

worker_processes 1
timeout 60

if GC.respond_to?(:copy_on_write_friendly=)
  GC.copy_on_write_friendly = true
end

before_exec do |server|
  ENV["BUNDLE_GEMFILE"] = "#{Rails.root}/Gemfile"
end

before_fork do |server, worker|
  old_pid = "#{Rails.root}/tmp/pids/unicorn.pid.oldbin"
  if File.exists?(old_pid) && server.pid != old_pid
    begin
      Process.kill("QUIT", File.read(old_pid).to_i)
    rescue Errno::ENOENT, Errno::ESRCH
          puts "Send 'QUIT' signal to unicorn error!"
    end
  end
end

(2)在本地电脑代码创建第2个文件,config/initializers/publish_name.rb,用来定义环境变量。

文件中添加下列代码,用来定义config/secrets.yml里面的secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>这行代码。不然启动unicorn后会报错

ENV["SECRET_KEY_BASE"] = "se3e1ced11e139b8c0a10c0195e9a1e94c92e5f786872bfde1d38a64b38d72870112a17f79e0ec0d5720a7261b622adfde71a535415f750f4675fcb225b5a29a"

(3)在本地电脑上提交代码并且在服务器下拉更新代码

在本地电脑上提交代码

/vagrant/data_system$ git add .
/vagrant/data_system$ git commit -m "unicorn"
/vagrant/data_system$ git push origin master

到服务器上下拉代码

ubuntu@VM-16-15-ubuntu:~/data_symtem$ git pull origin master

(4)启动unicorn

bundle exec unicorn_rails -c ./config/unicorn.rb  -D  -E production

检查unicorn是否启动成功

  • 看一下是否产生一个名为unicorn.sock的文件
ubuntu@VM-16-15-ubuntu:~/data_symtem$ ls
app  config     db       Gemfile.lock  log           public    README.md  tmp           vendor
bin  config.ru  Gemfile  lib           package.json  Rakefile  test       unicorn.sock
  • 查看unicorn进程,其中看到id为19488这种样式的进程,就说明启动成功了
ubuntu@VM-16-15-ubuntu:~/data_symtem$ ps -axu | grep unicorn
ubuntu   19488  0.0  8.7 214684 77284 ?        Sl   Mar16   0:02 unicorn_rails master -c ./config/unicorn.rb -D -E production
ubuntu   19506  0.0 11.4 239040 101388 ?       Sl   Mar16   0:05 unicorn_rails worker[0] -c ./config/unicorn.rb -D -E production
ubuntu   22758  0.0  0.1  13228   960 pts/1    S+   23:23   0:00 grep --color=auto unicorn

(5)启动nginx

ubuntu@VM-16-15-ubuntu:~$ sudo service nginx restart

这样项目就可以在配置中的域名中打开了。

4、可能出现的错误

(1)启动nginx时,报错
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
问题解决:
系统建议在命令行输入systemctl status nginx.servicejournalctl -xe查看错误,但是在这里看到的错误不是很明显,可以直接在nginx日志里面查看错误

ubuntu@VM-16-15-ubuntu:~$ cd /var/log/nginx

ubuntu@VM-16-15-ubuntu:/var/log/nginx$ cat error.log
2019/03/16 11:35:48 [crit] 3711#3711: *1 connect() to unix:/home/ubuntu/data_symtem/unicorn.sock failed (2: No such file or directory) while connecting to upstream, client: 111.201.226.2, server: xiolu.com, request: "GET /favicon.ico HTTP/1.1", upstream: "http://unix:/home/ubuntu/data_symtem/unicorn.sock:/favicon.ico", host: "xiolu.com", referrer: "http://xilu.com/"

根据日志中返回的错误可以看出,没有找到在nginx配置文件中(/etc/nginx/conf.d/data.conf)配置的home/ubuntu/data_symtem/unicorn.sock文件。那应该是unicorn没有启动成功,没有成功产生unicorn.sock文件。那就需要再看一下unicorn的日志。

(2)unicorn启动失败
我们需要查看一下unicorn的日志,在项目log目录下

ubuntu@VM-16-15-ubuntu:~$ cd data_symtem/log/

ubuntu@VM-16-15-ubuntu:~/data_symtem/log$ cat unicorn.log

可以看到对应的错误。

(3)在nginx与unicorn启动成功的情况下,打开域名还是出现下面页面

6808783-177de7b7896a7dce.png

 

需要检查nginx日志(/var/log/nginx/error.log)、unicorn日志(data_symtem/log/unicorn.log),查看里面是否报错,解决相应的错误之后,页面应该就会正常显示了。

5、结语

从项目的环境部署,到代码的设计开发,再到项目的上线。Ruby on Rails全栈课程已经基本完结了。近两个月(19年3月、4月)不会再更新本课程了,有疑问可以在底下留言,我们一起讨论~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

喵宁一

点赞是最好的赞赏~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值