Apache2 虚拟站点配置可参考上篇Apache2 虚拟站点配置 本文主要参考资料来自互联网,加以整理修改以符合需求
参考链接Let’s Encrypt 证书自动化申请
git clone https://github.com/certbot/certbot.git # 临时关闭Apache 2等占用443端口程序,工具需要绑定到443端口 service apache2 stop # DNS解析要申请域名到该台服务器 # 确认解析正确后开始申请 cd certbot-master/ ./certbot-auto certonly --standalone --email admin@example.com -d example.com # 申请的证书文件在/etc/letsencrypt/live/www.example.com/下创建配置文件/etc/apache2/sites-available/www.example.com.ssl.conf
# 以默认SSL配置文件为模板 cp /etc/apache2/sites-available/default-ssl.conf /etc/apache2/sites-available/www.example.com.ssl.conf更改配置文件内容为以下
<IfModule mod_ssl.c> <VirtualHost _default_:443> ServerName www.example.com DocumentRoot /var/www/html/www.example.com # 禁止通过浏览器直接访问Log文件 <Files ~ ".log"> Order allow,deny Deny from all </Files> ErrorLog /var/www/html/www.example.com/error.log CustomLog /var/www/html/www.example.com/access.log combined SSLEngine on SSLCertificateFile /etc/letsencrypt/live/www.example.com/fullchain.pem SSLCertificateKeyFile /etc/letsencrypt/live/www.example.com/privkey.pem # 看自己申请的证书是否包含,可选 SSLCertificateChainFile /etc/letsencrypt/live/www.example.com/fullchain.pem </VirtualHost> </IfModule>启用配置文件
a2enmod ssl a2ensite www.example.com.ssl.conf service apache2 restart测试访问https://www.example.com
添加如下内容到配置文件/etc/apache2/sites-available/www.example.com.conf
# 该配置文件填入www.example.com.conf配置 # 而不是www.example.com.ssl.conf配置 RewriteEngine On RewriteCond %{HTTPS} off RewriteRule (.*) https://%{SERVER_NAME}/$1 [R,L]启用配置
# 启用rewrite模块 a2enmod rewrite service apache2 restart测试访问http://www.example.com,检测是否重定向到https
添加如下内容到配置文件/etc/apache2/sites-available/www.example.com.ssl.conf
# 添加以下内容到<VirtualHost></VirtualHost>中 WSGIScriptAlias / /var/www/html/www.example.com/wsgi.py* 重启apache2*
service apache2 restart创建/var/www/html/www.example.com/wsgi.py
import sys sys.path.append('/var/www/html/www.example.com/wsgi.py') def application(environ, start_response): status = '200 OK' output = 'Hello World!' response_headers = [('Content-type', 'text/plain'), ('Content-Length', str(len(output)))] start_response(status, response_headers) return [output]访问www.example.com,检验Python是否运行正确
运行错误日志可在/var/www/html/www.example.com/error.log中查看