Ubuntu Apache 2.4 配置-HTTPS、python mod 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 ssl
a2ensite www.example.com.ssl.conf
service 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 On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R,L]

启用配置

# 启用rewrite模块
a2enmod rewrite
service 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 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中查看

参考链接

  1. Apache2 https重定向
  2. Apache2 mod_wsgi
  3. Let’s Encrypt 证书申请
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值