Ubuntu Apache 2.4 配置-HTTPS、python mod WSGI

来源:互联网 发布:湖南 福建卓知 编辑:程序博客网 时间:2024/06/04 18:54

  • 配置环境
  • 配置HTTPS
    • 证书申请可选
    • 添加域名ssl配置文件
    • 重定向http至https可选
  • 配置python运行环境
    • 安装mod_wsgi
    • 修改配置文件
    • 测试WSGI
  • 参考链接

配置环境

  • Ubuntu 16.04,Apache 2.4
  • 域名www.example.com

Apache2 虚拟站点配置可参考上篇Apache2 虚拟站点配置
本文主要参考资料来自互联网,加以整理修改以符合需求

配置HTTPS

1.证书申请<可选>

参考链接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/下

2.添加域名ssl配置文件

创建配置文件/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 ssla2ensite www.example.com.ssl.confservice apache2 restart

测试访问https://www.example.com

3.重定向http至https<可选>

添加如下内容到配置文件/etc/apache2/sites-available/www.example.com.conf

# 该配置文件填入www.example.com.conf配置# 而不是www.example.com.ssl.conf配置RewriteEngine OnRewriteCond %{HTTPS} offRewriteRule (.*) https://%{SERVER_NAME}/$1 [R,L]

启用配置

# 启用rewrite模块a2enmod rewriteservice apache2 restart

测试访问http://www.example.com,检测是否重定向到https

配置python运行环境

安装mod_wsgi

apt-get install libapache2-mod-wsgi

修改配置文件

添加如下内容到配置文件/etc/apache2/sites-available/www.example.com.ssl.conf

# 添加以下内容到<VirtualHost></VirtualHost>中WSGIScriptAlias / /var/www/html/www.example.com/wsgi.py

* 重启apache2*

service apache2 restart

测试WSGI

创建/var/www/html/www.example.com/wsgi.py

import syssys.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中查看

参考链接

  1. Apache2 https重定向
  2. Apache2 mod_wsgi
  3. Let’s Encrypt 证书申请
原创粉丝点击