AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 122002
Accepted
Josh
Josh
Asked: 2010-03-13 11:50:20 +0800 CST2010-03-13 11:50:20 +0800 CST 2010-03-13 11:50:20 +0800 CST

使用 Ruby 解析/编辑 Apache conf 文件?

  • 772

我需要编写一些脚本来更改 Apache conf 文件,即添加/更新 VirtualHosts。我计划在 Ruby 中执行此操作。在我自己编写之前,是否有任何脚本/rubygems 允许 Ruby 解析/修改 Apache conf 文件,特别是<VirtualHost>指令?

apache-2.2 ruby configuration-management parsing
  • 2 2 个回答
  • 2166 Views

2 个回答

  • Voted
  1. Best Answer
    Josh
    2010-03-20T04:35:29+08:002010-03-20T04:35:29+08:00

    我最终只是编写了自己的 ruby​​ 脚本......做得不是很好,但如果有人需要它,这就是它的胆量。它正在寻找<VirtualHost></VirtualHost>标签的内容,以便它可以创建第二个<VirtualHost>带有 aServerName这是我们通配符 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
    

    仍然有一些错误需要解决,但它主要是我想要的。

    • 3
  2. cagenut
    2010-03-13T12:18:57+08:002010-03-13T12:18:57+08:00

    没有线索,但可以帮助您的一件事是 apache 对大规模虚拟主机的支持: http ://httpd.apache.org/docs/2.2/vhosts/mass.html

    如果您可以在您的虚拟主机之间强制执行一致性,那么您可能不需要 ruby​​ 来创建/管理/编辑它们。约定优于配置是正确的方式吗?

    • 0

相关问题

  • 在您分发的应用程序中使用 Apache HTTPD 运行 SSL 的最佳方式是什么?

  • 阿帕奇的替代品

  • 如何强制我的网址始终以 www 开头?

  • 在 Linux Xen VPS 上优化 Apache 和 MySQL

  • mod_rewrite 不转发 GET 参数

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    从 IP 地址解析主机名

    • 8 个回答
  • Marko Smith

    如何按大小对 du -h 输出进行排序

    • 30 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    MikeN 在 Nginx 中,如何在维护子域的同时将所有 http 请求重写为 https? 2009-09-22 06:04:43 +0800 CST
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    0x89 bash中的双方括号和单方括号有什么区别? 2009-08-10 13:11:51 +0800 CST
  • Martin Hope
    Kyle Brandt IPv4 子网如何工作? 2009-08-05 06:05:31 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve