适用于 Apache 2.2、Apache 2.4 和 Nginx 的可配置 vHost 生成器。
项目描述
虚拟主机
持续集成
vhost-gen将根据您在conf.yml中设置的内容为 Apache 2.2、Apache 2.4 和 Nginx动态生成vhost或反向代理配置文件。这使得在不同 Web 服务器之间切换变得容易,同时保持完全相同的功能。
安装
通过点子
pip install vhost-gen
来自 git
注意:使用 Makefile 时,请确保pyyaml已安装 ( pip install pyyaml)。
git clone https://github.com/devilbox/vhost-gen
cd vhost-gen
sudo make install
什么是绒毛?
想象一下,您必须一遍又一遍地为您的 Web 服务器创建虚拟主机。唯一可能改变的是文档根目录、日志文件和服务器名称,可能还有其他一些小改动。不必每次都复制和调整服务器的 vhost 配置文件,您可以使用vhost-gen它为您生成它们。通过支持不同的 Web 服务器版本,它也使您可以轻松地在 Apache 2.2、Apache 2.4 和 Nginx 之间来回切换。
# vHost
$ vhost-gen -p /shared/httpd/www.example.com -n www.example.com
# Reverse Proxy
$ vhost-gen -r http://127.0.0.1:8080 -l / -n api.example.com
vhost-gen每次执行它时,单独创建一个新的虚拟主机。然而,目标是自动执行虚拟主机生成器本身。
1.反向代理自动化:watcherp
这里进入watcherp游戏。watcherp监听端口绑定的变化,并在绑定新端口或删除绑定时触发命令。通过结合这两个工具,您可以使用一个命令自动创建反向代理:
# %n will be replaced by watcherp with the address a port has binded
# %p will be replaced by watcherp with the the actual port number that started binding
# -p argument from watcherp specifies ports to ignore for changes
$ watcherp -v \
-p 80,443 \
-a "vhost-gen -r 'http://%n:%p' -l '/' -n '%n.example.com' -s" \
-d "rm /etc/nginx/conf.d/%n.example.com.conf" \
-t "nginx -s reload"
2.虚拟主机自动化:watcherd
这里进入watcherd游戏。watcherd侦听目录更改并在创建或删除目录时触发命令。通过结合这两个工具,您可以使用一个命令自动化大规模虚拟主机:
# %n will be replaced by watcherd with the new directory name
# %p will be replaced by watcherd with the new directory path
$ watcherd -v \
-p /shared/httpd \
-a "vhost-gen -p %p -n %n -s" \
-d "rm /etc/nginx/conf.d/%n.conf" \
-t "nginx -s reload"
更多定制
现在它可能看起来更有趣。使用上述命令,每个虚拟主机都将具有完全相同的定义(服务器名称、文档根目录和日志文件名称除外)。然而,也有可能每个虚拟主机都可以根据他们的需要进行定制。vhost-gen允许额外覆盖其模板。templates/因此,在每个新创建的文件夹中,您可以有一个带有特定文件夹定义的子目录(例如)。这些自定义模板只有在它们存在的情况下才会被获取:
# Note: Adding -o %p/templates
$ watcherd -v \
-p /shared/httpd \
-a "vhost-gen -p %p -n %n -o %p/templates -s" \
-d "rm /etc/nginx/conf.d/%n.conf" \
-t "nginx -s reload"
使其健壮
如果您不信任watcherd的稳定性或想要其他控制此守护进程的方法,您可以使用supervisord:
[program:watcherd]
command=watcherd -v -p /shared/httpd -a "vhost-gen -p %%p -n %%n -s" -d "rm /etc/nginx/custom.d/%%n.conf" -t "nginx -s reload"
startsecs = 0
autorestart = true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
stdout_events_enabled=true
stderr_events_enabled=true
3. 码头化
如果您不想自己实现它,已经有四个功能齐全的 dockerized 容器可用,它们提供基于vhost-genand的自动大规模虚拟主机watcherd:
| 基本图像 | 网络服务器 | 存储库 |
|---|---|---|
| Nginx 稳定版(官方) | nginx | https://github.com/devilbox/docker-nginx-stable |
| Nginx 主线(官方) | nginx | https://github.com/devilbox/docker-nginx-mainline |
| Apache 2.2(官方) | 阿帕奇 2.2 | https://github.com/devilbox/docker-apache-2.2 |
| Apache 2.4(官方) | 阿帕奇 2.4 | https://github.com/devilbox/docker-apache-2.4 |
见解
支持的网络服务器
如果您对网络服务器配置文件的默认定义不满意,请随时打开问题或拉取请求。
| 姓名 | 具有默认定义的模板 |
|---|---|
| Nginx | nginx.yml |
| 阿帕奇 2.2 | apache22.yml |
| 阿帕奇 2.4 | apache24.yml |
支持的功能
- 文档服务 vHost 或反向代理
- 自定义服务器名称
- 自定义文档根
- 自定义访问日志名称
- 自定义错误日志名称
- 启用对源的正则表达式支持的跨域请求
- 启用 PHP-FPM
- 添加具有正则表达式支持的别名
- 使用正则表达式支持添加拒绝位置
- 启用网络服务器状态页面
- 从配置文件中添加自定义指令
它是如何工作的?
一般信息:
- vHost 名称被指定为命令行参数
- 主要网络服务器的 vHost 模板在 etc/templates 中定义
- vHost 模板包含必须替换的变量
- Webserver 类型/版本在 /etc/vhost-gen/conf.yml 中定义
- 变量替换器在 /etc/vhost-gen/conf.yml 中定义
- 也可以定义额外的变量替换器 (
-o)
下面介绍程序流程:
- vhost-gen将读取 /etc/vhost-gen/conf.yml 以获取定义和网络服务器类型/版本
- 基于网络服务器版本/类型,它将读取 etc/templates/<HTTPD_VERSION>.yml 模板
- 所选模板中的变量被替换
- vHost 名称 (
-n) 也放入模板中 - 模板被写入网络服务器的配置位置(在 /etc/vhost-gen/conf.yml 中定义)
用法
使用不同的配置
配置文件应该为您提供很大的灵活性来生成适合您需求的自定义虚拟主机。去查看示例部分以查看不同 Web 服务器的不同配置文件。但是,如果配置文件中可用的自定义不够,您还可以调整模板本身。请继续阅读以在下一节中了解更多信息。
添加自定义指令
配置文件还包含一个custom:指令。这是为了将尚未包含在功能部分中的自定义指令添加到您的虚拟主机定义中。Nginx 的一个例子是:
conf.yml:
custom: |
if (-f $request_filename) {
break;
}
然后将其添加到您生成的虚拟主机中:
server {
...
if (-f $request_filename) {
break;
}
}
如果当前的 vHost 定义不适合您的需要,您还可以禁用配置文件中的所有可用功能,并仅使用该custom:指令来指定您所需的定义。
使用不同的模板
每个模板由两个部分组成:
vhostfeatures
该vhost部分是正在构建的实际虚拟主机。所有占位符(形式为__*__:)将从配置文件或模板的功能部分替换。
该features部分包含可以启用或禁用的功能的定义(通过配置文件)。如果这些功能看起来不合适,您可以调整它们以更好地满足您的需求。您还可以重新排列它们在该vhost部分中的位置。
每当您编辑模板时,请确保不要拼错任何占位符。当拼写错误时,它们不会被替换。
让我们以features.server_statusApache 2.2 的部分为例:
server_status: |
# Status Page
<Location __REGEX__>
SetHandler server-status
Order allow,deny
Allow from all
</Location>
如果您对权限不满意Allow from all,只需根据需要重写此模板:
server_status: |
# Status Page
<Location __REGEX__>
SetHandler server-status
Order allow,deny
Allow from 160.120.25.65
</Location>
可用的命令行选项
Usage: vhost-gen -p|r <str> -n <str> [-l <str> -c <str> -t <str> -o <str> -d -s -v]
vhost-gen --help
vhost-gen --version
vhost-gen will dynamically generate vhost configuration files
for Nginx, Apache 2.2 or Apache 2.4 depending on what you have set
in /etc/vhost-gen/conf.yml
Required arguments:
-p|r <str> You need to choose one of the mutually exclusive arguments.
-p: Path to document root/
-r: http(s)://Host:Port for reverse proxy.
Depening on the choice, it will either generate a document serving
vhost or a reverse proxy vhost.
Note, when using -p, this can also have a suffix directory to be set
in conf.yml
-l <str> Location path when using reverse proxy.
Note, this is not required for normal document root server (-p)
-n <str> Name of vhost
Note, this can also have a prefix and/or suffix to be set in conf.yml
Optional arguments:
-m <str> Vhost generation mode. Possible values are:
-m plain: Only generate http version (default)
-m ssl: Only generate https version
-m both: Generate http and https version
-m redir: Generate https version and make http redirect to https
-c <str> Path to global configuration file.
If not set, the default location is /etc/vhost-gen/conf.yml
If no config is found, a default is used with all features turned off.
-t <str> Path to global vhost template directory.
If not set, the default location is /etc/vhost-gen/templates/
If vhost template files are not found in this directory, the program will
abort.
-o <str> Path to local override vhost template directory.
This is used as a secondary template directory and definitions found here
will be merged with the ones found in the global template directory.
Note, definitions in local vhost teplate directory take precedence over
the ones found in the global template directory.
-d Make this vhost the default virtual host.
Note, this will also change the server_name directive of nginx to '_'
as well as discarding any prefix or suffix specified for the name.
Apache does not have any specialities, the first vhost takes precedence.
-s If specified, the generated vhost will be saved in the location found in
conf.yml. If not specified, vhost will be printed to stdout.
-v Be verbose. Use twice for debug output.
Misc arguments:
--help Show this help.
--version Show version.
贡献
这是一个开源项目,在业余时间完成。如果您想提供帮助,我们将受到热烈欢迎。您可以执行以下一项或多项操作:
- 验证 Web 服务器模板
- 提交模板示例
- 为其他 Web 服务器创建模板
- 无论你能想象到什么
执照
版权所有 (c) 2017 cytopia
项目详情
下载文件
下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。