美文网首页
将Django部署到Apache中并使用HTTPS

将Django部署到Apache中并使用HTTPS

作者: Kairk996 | 来源:发表于2020-03-13 21:03 被阅读0次

Django自带的wsgi运行开发服务器,承压较差,在生产环境中难以得到很好的使用,因此将Django部署至Apache中就变得尤为必要。本文讲述如何在Windows环境中将Django部署至Apache中,并通过HTTPS访问。
要将Django部署至Apache,我们需要一个套件mod_wsgi,在https://www.lfd.uci.edu/~gohlke/pythonlibs/#mod_wsgi根据服务器的Apache和Python版本,选择合适的版本。之后将这个whl后缀文件移动到服务器,注意不要改名字。打开控制台,进入该文件所在目录,输入命令下载安装pip install xxx.whl(xxx用文件名代替),之后输入mod_wsgi-express module-config会得到三排信息,之后在Apache配置中需要用到,记录下来。

image.png
打开Apache的配置文件httpd.conf,在最后加上三句话。(上一步得到的信息填充进去)
LoadFile "c:/users/administrator/appdata/local/programs/python/python38/python38.dll"
LoadModule wsgi_module "c:/users/administrator/appdata/local/programs/python/python38/lib/site-packages/mod_wsgi/server/mod_wsgi.cp38-win_amd64.pyd"
WSGIPythonHome "c:/users/administrator/appdata/local/programs/python/python38"

1.实现Apache单解析Django
Apache没有开启VirtualHost,默认端口运行Django。
httpd.conf的最后加上 (加上File就能运行,其他不是必要)

#指定项目的wsgi.py配置文件路径,这个py文件是在你的Django项目中  
WSGIScriptAlias / c:/users/administrator/desktop/hotelsin/hotelsin/wsgi.py  
  
#指定项目目录,即你的Django项目路径 
WSGIPythonPath  c:/users/administrator/desktop/hotelsin
  
<Directory c:/users/administrator/desktop/hotelsin/hotelsin>  
<Files wsgi.py>  
    Require all granted  
</Files>  
</Directory>  
  
#项目静态文件地址, Django项目中静态文件的路径  
Alias /static c:/users/administrator/desktop/hotelsin/visit/static
<Directory c:/users/administrator/desktop/hotelsin/visit/static>  
    AllowOverride None  
    Options None  
    Require all granted  
</Directory>  
  
#项目media地址, 上传图片等文件夹的路径  
Alias /media c:/users/administrator/desktop/hotelsin/visit/static
<Directory c:/users/administrator/desktop/hotelsin/visit/static>  
    AllowOverride None  
    Options None  
    Require all granted  
</Directory> 

在Django的setting.py中将ALLOWED_HOSTS设为['*']
重启Apache,在默认80端口就可运行Django

2.使用VirtualHost,另外的端口上运行Django
httpd.conf中,加端口监听。

Define MYPORT8000 8000
Listen 0.0.0.0:${MYPORT8000}
Listen [::0]:${MYPORT8000}

之后启用Include vhosts
打开httpd-vhosts.conf
在最后加上

<VirtualHost *:${MYPORT8000}>
ServerName hotel
DocumentRoot "c:/users/administrator/desktop/hotelsin"
WSGIScriptAlias / c:/users/administrator/desktop/hotelsin/hotelsin/wsgi.py

<Directory "c:/users/administrator/desktop/hotelsin">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>

重启Apache,在8000端口就可运行Django

注意:
在WAMP环境中,最好不要直接改配置文件,加端口监听,右击WAMP,在工具中找到增加端口,输入8000,左击WAMP,增加VirtualHost。WAMP会自动添加至配置文件中,但在httpd-vhost.conf中仍需修改成上述内容。

配置SSL使用HTTPS
首先在腾讯云申请SSL后下载


image.png

这里我们将apache中的三个文件移动到服务器。

httpd.conf中取消这三句话的注释

Include conf/extra/httpd-ssl.conf
LoadModule ssl_module modules/mod_ssl.so
LoadModule socache_shmcb_module modules/mod_socache_shmcb.so

之后修改httpd-ssl.conf
修改VirtualHost中的内容

<VirtualHost _default_:443>
DocumentRoot "c:/wamp64/www"
ServerName nhstone.cn:443
ServerAdmin admin@example.com
ErrorLog "${SRVROOT}/logs/error.log"
TransferLog "${SRVROOT}/logs/access.log"

SSLEngine on

# 替换为自己的三个证书文件
SSLCertificateFile "c:/users/administrator/desktop/SSL/2_nhstone.cn.crt"
SSLCertificateKeyFile "c:/users/administrator/desktop/SSL/3_nhstone.cn.key"
SSLCertificateChainFile "c:/users/administrator/desktop/SSL/1_root_bundle.crt"

<FilesMatch "\.(cgi|shtml|phtml|php)$">
    SSLOptions +StdEnvVars
</FilesMatch>
<Directory "${SRVROOT}/cgi-bin">
    SSLOptions +StdEnvVars
</Directory>
# 一下部分增加 运行Django
WSGIScriptAlias / c:/users/administrator/desktop/hotelsin/hotelsin/wsgi.py

<Directory "c:/users/administrator/desktop/hotelsin">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
BrowserMatch "MSIE [2-5]" \
         nokeepalive ssl-unclean-shutdown \
         downgrade-1.0 force-response-1.0


CustomLog "${SRVROOT}/logs/ssl_request.log" \
          "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"



</VirtualHost>    

重启Django!大功告成

相关文章

网友评论

      本文标题:将Django部署到Apache中并使用HTTPS

      本文链接:https://www.haomeiwen.com/subject/ovhyshtx.html