大网数据广告活动
服务器运维问答

Is it possible to generate a variable for SSLCertificateFile from HTTP_HOST (or something similar)?

· 52CE 我爱测

Normally in my vhost blocks I do: Define SITE example.com SSLCertificateFile /path/to/${SITE}.crt SSLCertificateKeyFile /path/to/${SITE}.key I'm wondering if it's possible to create a variable in Apache that would correspond to m…

问题原文

原帖链接 · Jeff · CC BY-SA 4.0

Normally in my vhost blocks I do:

Define SITE example.com

SSLCertificateFile /path/to/${SITE}.crt
SSLCertificateKeyFile /path/to/${SITE}.key

I'm wondering if it's possible to create a variable in Apache that would correspond to my filenames for example.com.crt and example.com.key?

I assume not, but it's worth asking to consolidate my vhost blocks.

已采纳解答

原帖链接 · Sunry · CC BY-SA 4.0

For Apache, you can use mod_macro

First define a VHost Macro for repetitive configurations:

 <Macro VHost $host $port $dir>
  Listen $port
  <VirtualHost *:$port>

    ServerName $host
    DocumentRoot $dir

    <Directory $dir>
      # do something here...
    </Directory>

    # limit access to intranet subdir.
    <Directory $dir/intranet>
      order deny,allow
      deny from all
      allow from 10.0.0.0/8
    </Directory>
  </VirtualHost>
</Macro>

Then you can use of VHost with different arguments.

Use VHost www.apache.org 80 /projects/apache/web
Use VHost www.perl.com 8080 /projects/perl/web
Use VHost www.ensmp.fr 1234 /projects/mines/web

I've got 50 domains (like a.com, b.com, c.com, d.com, etc) based on the same wordpress code directory. While convert to https, every domain has its own certificate and private key files. I don't want to copy & paste & change the VirtualHost block codes for 50 times.

By mod_macro I don't need to do the VirtualHost block 50 times. I did it this way, which makes it easier. Below macro include redirect www to non-www domain name:

<Macro VHost $domain>
<VirtualHost *:443>
    ServerName www.$domain
    Redirect permanent / https://$domain/

    Include /path/to/apache-common-ssl.conf

    SSLCertificateFile /etc/pki/tls/certs/$domain.cer
    SSLCertificateKeyFile /etc/pki/tls/private/$domain.key
    SSLCertificateChainFile /etc/pki/tls/certs/$domain-fullchain.cer
</VirtualHost>

<VirtualHost *:443>
    ServerName $domain
    DocumentRoot /home/wordpress
    <Directory /home/wordpress>
        # Stuff about the directory ...
    </Directory>

    Include /path/to/apache-common-ssl.conf

    SSLCertificateFile /etc/pki/tls/certs/$domain.cer
    SSLCertificateKeyFile /etc/pki/tls/private/$domain.key
    SSLCertificateChainFile /etc/pki/tls/certs/$domain-fullchain.cer
</VirtualHost>
</Macro>

Use VHost a.com
Use VHost b.com
Use VHost c.com
......

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2022-03-05)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。