我当前的 httpd.conf 文件如下所示:
<VirtualHost *:443>
DocumentRoot /var/www/html/www.example1.com
ServerName www.example1.com
SSLEngine on
SSLCertificateFile /var/www/ssl/www.example1.com/certificate.crt
SSLCertificateKeyFile /var/www/ssl/www.example1.com/private.key
SSLCACertificateFile /var/www/ssl/www.example1.com/bundle.crt
</VirtualHost>
<VirtualHost *:443>
DocumentRoot /var/www/html/www.example2.com
ServerName www.example2.com
SSLEngine on
SSLCertificateFile /var/www/ssl/www.example2.com/certificate.crt
SSLCertificateKeyFile /var/www/ssl/www.example2.com/private.key
SSLCACertificateFile /var/www/ssl/www.example2.com/bundle.crt
</VirtualHost>
我希望能够将“ServerName”存储在某种类型的变量中,以便简化文件(如下所示):
Define server_name = ServerName
<VirtualHost *:443>
DocumentRoot /var/www/html/${server_name}
ServerName ${server_name}
SSLEngine on
SSLCertificateFile /var/www/ssl/${server_name}/certificate.crt
SSLCertificateKeyFile /var/www/ssl/${server_name}/private.key
SSLCACertificateFile /var/www/ssl/${server_name}/bundle.crt
</VirtualHost>
有什么办法可以做这样的事吗?我环顾四周,找不到任何可以解决此问题的解决方案,但我可能采取了错误的方法。
答案1
尝试 mod_macro
以下是一个例子:
## Define a VHost Macro for repetitive configurations
<Macro VHost $host $port $dir>
Listen $port
<VirtualHost *:$port>
ServerName $host
DocumentRoot $dir
# Public document root
<Directory $dir>
Require all granted
</Directory>
# limit access to intranet subdir.
<Directory $dir/intranet>
Require ip 10.0.0.0/8
</Directory>
</VirtualHost>
</Macro>
## Use of VHost with different arguments.
Use VHost www.apache.org 80 /vhosts/apache/htdocs
Use VHost example.org 8080 /vhosts/example/htdocs
Use VHost www.example.fr 1234 /vhosts/example.fr/htdocs
答案2
我在想也许您可以创建一个包含变量声明的配置文件,将其包含在 httpd.conf 文件中,然后运行一项cron
作业以将服务器名称更改为当前使用的服务器名称sed
。
计划任务示例:
set servername = hostname
sed -i -e 's/(?-s)define servername .+/define servername $servername/g' /etc/httpd/conf.d/variables.conf