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

简介: 1、域名映射准备材料云服务器以及域名(参考5.1 购买服务器、域名)nginxunicorn2、安装以及设置nginx

1、域名映射准备材料


云服务器以及域名(参考5.1 购买服务器、域名)


nginx


unicorn


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.service或journalctl -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启动成功的情况下,打开域名还是出现下面页面


image.png


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


5、结语


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


目录
相关文章
|
应用服务中间件 nginx
Nginx中如何配置中文域名?
Nginx中如何配置中文域名?
|
前端开发 Java 应用服务中间件
Tomcat和Nginx的资源路径映射
Tomcat和Nginx的资源路径映射
440 1
|
安全 应用服务中间件 Shell
nginx配置https的ssl证书和域名
nginx配置https的ssl证书和域名
|
应用服务中间件 nginx Docker
本地通过域名访问虚拟机上nginx的服务、搭建域名访问环境一(反向代理配置)
这篇文章介绍了如何通过域名在本地访问虚拟机上的nginx服务,包括创建nginx容器、修改配置文件、修改本地host文件以及进行访问测试的详细步骤。文章提供了具体的Docker命令来创建并配置nginx容器,展示了配置文件的修改示例,说明了如何在本地系统的hosts文件中添加虚拟机IP和自定义域名,以及如何通过浏览器进行测试访问。
本地通过域名访问虚拟机上nginx的服务、搭建域名访问环境一(反向代理配置)
|
JavaScript 前端开发 应用服务中间件
Nginx——一个域名下部署多个Vue项目
如何在同一域名下部署第二个Vue项目而不影响现有项目:更新`vue.config.js`,设置`publicPath`为`/screen/`。修改Vue Router的`base`为`screen`。在Nginx配置中添加新location `/screen`,指向第二项目`dist`目录。测试访问`http://&lt;域名&gt;/screen/`。别忘了检查并修复任何遗漏的配置,如数据看板默认设置。
975 2
|
网络协议 应用服务中间件 网络安全
如何排查Nginx配置问题导致的域名访问错误
如何排查Nginx配置问题导致的域名访问错误
1822 2
|
应用服务中间件 Linux 网络安全
如何在 CentOS 6.5 上使用 Unicorn 和 Nginx 部署 Rails 应用
如何在 CentOS 6.5 上使用 Unicorn 和 Nginx 部署 Rails 应用
247 0
|
缓存 Ubuntu 应用服务中间件
如何在 Ubuntu 14.04 上使用 Passenger 和 Nginx 部署 Rails 应用
如何在 Ubuntu 14.04 上使用 Passenger 和 Nginx 部署 Rails 应用
182 0
|
关系型数据库 应用服务中间件 数据库
如何在 Ubuntu 14.04 上使用 Unicorn 和 Nginx 部署 Rails 应用
如何在 Ubuntu 14.04 上使用 Unicorn 和 Nginx 部署 Rails 应用
194 0
|
Ubuntu 应用服务中间件 网络安全
在 Ubuntu 14.04 上使用 Capistrano、Nginx 和 Puma 部署 Rails 应用
在 Ubuntu 14.04 上使用 Capistrano、Nginx 和 Puma 部署 Rails 应用
137 0