我需要编写一些脚本来更改 Apache conf 文件,即添加/更新 VirtualHosts。我计划用 Ruby 来执行此操作。在编写自己的脚本之前,是否有任何脚本/rubygems 允许 Ruby 解析/修改 Apache conf 文件(特别是<VirtualHost>
指令)?
答案1
我最终只是编写了自己的 ruby 脚本... 做得不是很好,但如果有人需要它,这就是它的核心。它正在寻找标签的内容,<VirtualHost></VirtualHost>
以便它可以创建第二个<VirtualHost>
,ServerName
它是我们的通配符 SSL 证书的子域...
begin
logMsg "Updating apache config file for user #{user} (#{domain_httpd_conf})"
domain_httpd_conf_io = File.open(domain_httpd_conf,File::RDONLY)
ip_addr = ''
main_vhost_config = []
ssl_vhost_config = [" ServerName #{auto_ssl_domain}",' Include "conf/wildcard-ssl.conf"']
indent = 1
while line = domain_httpd_conf_io.gets
line_indented = ' '*indent+line.strip
if line =~ /^[[:space:]]*<VirtualHost ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)(:[0-9]+)[^>]*>/
ip = $1
elsif line =~ /^[[:space:]]*<\/VirtualHost>/
break 2
elsif line =~ /^[[:space:]]*(ServerAlias|ServerName).*/
main_vhost_config.push line_indented
else
if line =~ /^[[:space:]]*<[^\/]/
indent += 1
elsif line =~ /^[[:space:]]*<[\/]/
indent = [1, indent-1].max
line_indented = ' '*indent + line.strip
end
main_vhost_config.push line_indented
ssl_vhost_config.push line_indented
end
end
main_vhost_config.push " Include #{extraconf_dir}/*.conf"
domain_httpd_conf_io.close
domain_httpd_conf_io = File.open(domain_httpd_conf,File::WRONLY||File::TRUNC)
domain_httpd_conf_io.puts "<VirtualHost #{ip}:80 #{ip}:8080>"
domain_httpd_conf_io.puts main_vhost_config
domain_httpd_conf_io.puts "</VirtualHost>"
domain_httpd_conf_io.puts
domain_httpd_conf_io.puts "<VirtualHost #{ip}:443 #{ip}:8888>"
domain_httpd_conf_io.puts ssl_vhost_config
domain_httpd_conf_io.puts "</VirtualHost>"
rescue SystemCallError => err
logErr "ERROR: Unexpected error: "+err
domain_httpd_conf_io.close
end
仍有一些错误需要解决,但它基本上满足了我的要求。
答案2
没有线索,但有一件事可能会对你有帮助,那就是 apache 对大规模虚拟主机的支持: http://httpd.apache.org/docs/2.2/vhosts/mass.html
如果您可以强制跨虚拟主机保持一致性,那么可能您不需要使用 ruby 来创建/管理/编辑它们。约定优于配置是 Rails 的方式,对吗?