从源代码安装 nginx-1.16.1 时,Chef::Exceptions::nginx 未启动

问题描述

我正在尝试从源代码安装 Nginx,我的要求是安装特定版本的 Nginx,即 1.16.1,因此我从源代码下载。 运行 installNginx.rb 后,我看到 Nginx.conf 文件已更新为认的 Nginx 配置,但 Nginx -v 表示未找到命令。

下面是我的配置-

installNginx.rb

include_recipe 'Nginx::source'

begin
  t = resources(:template => 'Nginx.conf')
  t.source 'Nginx.conf'
  t.cookbook 'my_Nginx'
rescue Chef::Exceptions::ResourceNotFound
  Chef::Log.warn "Could not find template Nginx.conf to modify"
end

service 'Nginx' do
  action :restart
end

attributes/Source.rb

node.default['Nginx']['source']['version'] = '1.16.1'
node.default['Nginx']['source']['url'] = 'http://Nginx.org/download/Nginx-1.16.1.tar.gz'
node.default['Nginx']['source']['checksum'] = 'f11c2a6dd1d3515736f0324857957db2de98be862461b5a542a3ac6188dbe32b'

Metadata.rb

depends 'Nginx'

在分析我在食谱日志上观察到的内容后:我提供的源版本是 1.16.1 但出于某种原因,Nginx::source 配方正在拉入 1.12.1 并且 Nginx 没有启动

"Nginx": {
"version": "1.12.1","package_name": "Nginx","port": "80","dir": "/etc/Nginx","script_dir": "/usr/sbin","log_dir": "/var/log/Nginx","log_dir_perm": "0750","binary": "/opt/Nginx-1.12.1/sbin/Nginx","default_root": "/var/www/Nginx-default","ulimit": "1024","cleanup_runit": true,"repo_source": "Nginx","install_method": "package","user": "webadmin","upstart": {
"runlevels": "2345","respawn_limit": null,"foreground": true
}


"init_style": "init","source": {
"version": "1.16.1","prefix": "/opt/Nginx-1.12.1","conf_path": "/etc/Nginx/Nginx.conf","sbin_path": "/opt/Nginx-1.12.1/sbin/Nginx","default_configure_flags": [
"--prefix=/opt/Nginx-1.12.1","--conf-path=/etc/Nginx/Nginx.conf","--sbin-path=/opt/Nginx-1.12.1/sbin/Nginx","--with-cc-opt=-Wno-error"
],"url": "http://Nginx.org/download/Nginx-1.16.1.tar.gz","checksum": "f11c2a6dd1d3515736f0324857957db2de98be862461b5a542a3ac6188dbe32b","modules": [
"Nginx::http_ssl_module","Nginx::http_gzip_static_module"
],INFO: remote_file[Nginx source] created file /var/chef/runs/58bffee4-b5aa-4632-97cd-0eeacc4ebd4c/local-mode-cache/cache/Nginx-1.16.1.tar.gz
INFO: remote_file[Nginx source] updated file contents /var/chef/runs/58bffee4-b5aa-4632-97cd-0eeacc4ebd4c/local-mode-cache/cache/Nginx-1.16.1.tar.gz

我无法弄清楚问题出在哪里,感谢您的帮助。

解决方法

nginx 说明书中的属性文件在多处引用了默认版本。例如,它使用默认版本来定义 nginx 的安装目录以及 nginx 源的下载 URL 为

default['nginx']['source']['prefix'] = "/opt/nginx-#{node['nginx']['source']['version']}"
default['nginx']['source']['url'] = "http://nginx.org/download/nginx-#{node['nginx']['source']['version']}.tar.gz"

因此,如果您稍后更新您自己的说明书中的 version 属性,下载 URL 将不会自动更新为新版本,因为它不再引用它。

要解决这个问题,您有两个选择

  1. 您可以在食谱中手动设置所有相关属性。如您所见,这很可能容易出错,并可能导致不一致。

  2. 您可以在设置覆盖属性后重新加载默认的 nginx 属性文件。这在您的属性文件中可能如下所示:

    override['nginx']['version'] = '1.16.1'
    override['nginx']['source']['checksum'] = 'f11c2a6dd1d3515736f0324857957db2de98be862461b5a542a3ac6188dbe32b'
    
    # Reload nginx::source attributes with our updated version
    node.from_file(run_context.resolve_attribute('nginx','source'))
    

请注意,nginx Cookbook 维护了两个 nginx 版本:node['nginx']['version']node['nginx']['source']['version'],后者的值默认设置为前者的值。

在您的 ohai 输出中,您只看到了 node['nginx']['version'] 属性(您尚未覆盖)。

通过覆盖此属性并重新加载 attributes/source.rb 文件(如图所示),事情应该再次保持一致。