在testing与生产之间共享Apacheconfiguration

我有一个稍微平淡的Apacheconfiguration的个人网站。 在上传到服务器之前,我会在个人计算机上testing更改。 磁盘上文件的path和站点的根URL在testing和生产条件之间当然是不同的,它们在configuration中出现很多地方(特别是<Directory>块,用于具有脚本或没有目录列表的特殊位置或…)。

分享configuration的常见元素的最佳方式是什么?以确保我的生产环境尽可能与我的testing环境相匹配?

我自己的回答:我想到的是使用SetEnv将当前机器的path存储在环境variables中,然后在每个地方包含一个通用的configuration文件,其中包含特定于机器的内容。 但是我从来没有使用过这些设施,我不知道是否有一些警告。

现在这个问题不是直接的答案,因为它涉及到设置的改变,但为了避免这个问题,我将虚拟化我的testing系统和我的生产服务器。 我希望这样可以减lessconfiguration的麻烦,因为我将来会运行相同的映像并增加简单的可扩展性或冗余性。

也许你有机会运行虚拟化….

您还应该看看这两个关于版本控制和复制服务器configuration以及在平台之间移动已安装的Linux的线程

可以使用sym / hardlinks设置一些脚手架,更改挂载点等,以便testing环境path匹配生产。 根据你的具体设置,它可能很容易,或者它可能是丑陋的和复杂的。

要访问testing环境,使用它的IP,或者可以添加一个名为指向testing环境的额外ServerAlias,或者您可以为两台机器configuration相同的名称,并使用本地/ etc / hosts来切换哪一个你想访问。

我结束了使用configuration模板文件和一个小的Python程序使用string.Template设施来生成两个不同的configuration。 我已经包含在下面。

这将configuration值replace为generic.tmpl.conf (这是站点内部configuration),然后将结果作为${conf}用于test.tmpl.confproduction.tmpl.conf ,它们具有VirtualHost块包含${conf} ,日志文件和其他这样的主机特定的信息。

 #!/usr/bin/python import string from optparse import OptionParser # --- parser = OptionParser() parser.add_option("--clean", dest="clean", action="store_true", default=False, help="Clean files instead of creating them.") (options, args) = parser.parse_args() if len(args) != 0: parser.error("No non-option arguments expected.") # --- def readconf(name): return string.Template(open(name).read()) def writeconf(out, outer, inner, fields): outerfields = fields.copy() outerfields["conf"] = inner.substitute(fields).replace("\n", "\n\t") out.write(outer.substitute(outerfields)) out.close() # --- if options.clean: os.remove("production.conf") os.remove("test.conf") else: commonconf = readconf("generic.tmpl.conf") liveconf = readconf("production.tmpl.conf") testconf = readconf("test.tmpl.conf") writeconf(open("production.conf", "w"), liveconf, commonconf, { # ... configuration values redacted ... }) writeconf(open("test.conf", "w"), testconf, commonconf, { # ... configuration values redacted ... }) 

尽可能使用<Location>而不是<Directory> ,以便更改DocumentRoot将覆盖大部分更改。 这可能是最简单的方法来照顾它。

Include指令也可以是你的朋友 – 你可以把它粘在一个<Directory>或者<Location>块中,以包含不会在机器之间改变的configuration部分。

这是一个黑客多一点,但你也可以使用DirectoryMatch在一个部分匹配您的testing和生产目录。