共计 2012 个字符,预计需要花费 6 分钟才能阅读完成。
最近博客迁移,采用Docker镜像WordPress完成新网站搭建,利用镜像中自带的Apache2完成全站点Https,参考【WordPress建站经验】docker-wordpress开启子域名形式多站点,并开启https访问 文章中第二节部分。因为开启多站点,不同子域名的SSL证书需要单独申请,因此需要使用Apache2监听不同域名,单独配置安全证书。
示例代码
# 端口80
<VirtualHost *:80>
ServerName peiluming.com # 监听域名
<Directory "/var/www/html">
RewriteEngine on
RewriteBase /
# FORCE HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L] # 重写http到https
</Directory>
ServerAdmin webmaster@localhost # 管理员邮箱
DocumentRoot /var/www/html # 网站根目录
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName blog.peiluming.com # 监听域名
<Directory "/var/www/html">
RewriteEngine on
RewriteBase /
# FORCE HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L] # 重写http到https
</Directory>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
# 端口443
<VirtualHost *:443>
ServerAdmin webmaster@localhost
ServerName peiluming.com
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLEngine on
SSLCertificateFile /etc/apache2/peiluming.com_bundle.pem
SSLCertificateKeyFile /etc/apache2/peiluming.com.key
<FilesMatch "\.(?:cgi|shtml|phtml|php)$">
SSLOptions +StdEnvVars
</FilesMatch>
<Directory /usr/lib/cgi-bin>
SSLOptions +StdEnvVars
</Directory>
</VirtualHost>
<VirtualHost *:443>
ServerAdmin webmaster@localhost
ServerName blog.peiluming.com
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLEngine on
SSLCertificateFile /etc/apache2/blog.peiluming.com_bundle.pem
SSLCertificateKeyFile /etc/apache2/blog.peiluming.com.key
<FilesMatch "\.(?:cgi|shtml|phtml|php)$">
SSLOptions +StdEnvVars
</FilesMatch>
<Directory /usr/lib/cgi-bin>
SSLOptions +StdEnvVars
</Directory>
</VirtualHost>
说到底,和Nginx有点相似,但是这个不支持 连续写多个域名,类似 ServerName peiluming.com www.peiluming.com
,不过可以有另外的一种变种写法,如下:
<VirtualHost *:80>
ServerName peiluming.com
ServerAlias www.peiluming.com
# rest of your configuration
</VirtualHost>
正文完
Apache2如何监听多个域名