djnago + apache部署 从 http 强跳 https

mac2025-08-21  2

个人练手项目 >_< 简单的网站开发

首先当您写好您的django 项目 买好你的服务器 买好你的域名(这里的域名要进行备案.)

1.准备工作

服务器 windows server 2012 r2(虽然我知道用win的服务器很low,但是其他的我也不会啊!)

python 3.7.4 的准备:

apache 准备:

我用的是 mod_wsgi-4.5.24+ap24vc14-cp37- 从下面的网站下载用pip 安装 https://www.lfd.uci.edu/~gohlke/pythonlibs/#mod_wsgi

2.万事具备

我把我的apache 放到了c盘下

web 是我django 放置的地方 我的文件结构目录

django setting.py 配置 

# 好像改的就这么多 DEBUG = False ALLOWED_HOSTS = ['hahaaa.cn','localhost','127.0.0.1','www.hahaaa.cn'] # '*', # 确定静态文件路径?_? STATIC_URL = '/static/' STATICFILES_DIRS = ( # os.path.join(os.path.dirname(__file__),'..','static').replace('\\',''), os.path.join(BASE_DIR, 'static'), # os.path.join('static'), )

apache 配置

主要配置的文件有 C:\Apache24\conf  文件夹下的 httpd.conf 配置如下 这里并不是完整的文件信息 只是我进行更改的列出来啦

Define SRVROOT "C:/Apache24" ServerRoot "C:/Apache24" LoadModule actions_module modules/mod_actions.so LoadModule alias_module modules/mod_alias.so LoadModule allowmethods_module modules/mod_allowmethods.so LoadModule asis_module modules/mod_asis.so LoadModule auth_basic_module modules/mod_auth_basic.so LoadModule authn_core_module modules/mod_authn_core.so LoadModule authn_file_module modules/mod_authn_file.so LoadModule authz_core_module modules/mod_authz_core.so LoadModule authz_groupfile_module modules/mod_authz_groupfile.so LoadModule authz_host_module modules/mod_authz_host.so LoadModule authz_user_module modules/mod_authz_user.so LoadModule autoindex_module modules/mod_autoindex.so LoadModule cgi_module modules/mod_cgi.so LoadModule dir_module modules/mod_dir.so LoadModule env_module modules/mod_env.so LoadModule include_module modules/mod_include.so LoadModule isapi_module modules/mod_isapi.so LoadModule log_config_module modules/mod_log_config.so LoadModule mime_module modules/mod_mime.so LoadModule negotiation_module modules/mod_negotiation.so LoadModule rewrite_module modules/mod_rewrite.so # https 强转需要的 LoadModule setenvif_module modules/mod_setenvif.so LoadModule socache_dbm_module modules/mod_socache_dbm.so # https 强转需要的 LoadModule socache_shmcb_module modules/mod_socache_shmcb.so # https 强转需要的 LoadModule ssl_module modules/mod_ssl.so # https 强转需要的 LoadModule vhost_alias_module modules/mod_vhost_alias.so <IfModule unixd_module> User daemon Group daemon </IfModule> # 这个是我的邮箱 ServerAdmin py.jz.goto.first@foxmail.com # 网站发布的端口 只有80端口的才能被收录 这里要在防护墙里加80端口的入站规则 Listen 0.0.0.0:80 ServerName www.hahaaa.cn:80 <Directory /> AllowOverride All Require all denied </Directory> DocumentRoot "C:/Apache24/htdocs" <Directory "C:/Apache24/htdocs"> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> <IfModule dir_module> DirectoryIndex index.html </IfModule> <Files ".ht*"> Require all denied </Files> ErrorLog "logs/error.log" LogLevel warn <IfModule log_config_module> LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined LogFormat "%h %l %u %t \"%r\" %>s %b" common <IfModule logio_module> LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio </IfModule> CustomLog "logs/access.log" common </IfModule> <IfModule alias_module> ScriptAlias /cgi-bin/ "C:/Apache24/cgi-bin/" </IfModule> <IfModule cgid_module> </IfModule> <Directory "C:/Apache24/cgi-bin"> AllowOverride All Options None Require all granted </Directory> <IfModule headers_module> RequestHeader unset Proxy early </IfModule> <IfModule mime_module> TypesConfig conf/mime.types AddType application/x-compress .Z AddType application/x-gzip .gz .tgz </IfModule> #启动 httpd-vhosts.conf 配置域名 Include conf/extra/httpd-vhosts.conf <IfModule proxy_html_module> Include conf/extra/proxy-html.conf </IfModule> # https 强转需要的 启动 httpd-ssl.conf Include conf/extra/httpd-ssl.conf <IfModule ssl_module> SSLRandomSeed startup builtin SSLRandomSeed connect builtin </IfModule> # mod_wsgi 链接 在cmd 中输入 mod_wsgi-express module-config 就可以看到相关路径啦 LoadFile "c:/users/administrator/desktop/python/python37.dll" LoadModule wsgi_module "c:/users/administrator/desktop/python/lib/site-packages/mod_wsgi/server/mod_wsgi.cp37-win32.pyd" WSGIPythonHome "c:/users/administrator/desktop/python" WSGIScriptAlias / "C:/web/ChpHahaa/ChpHahaa/wsgi.py" # 项目目录 WSGIPythonPath "C:/web/ChpHahaa" <Directory "C:/web/ChpHahaa/ChpHahaa"> <Files wsgi.py> Require all granted </Files> # 这里的目的是让全部网页http 强转 https RewriteEngine on RewriteCond %{SERVER_PORT} !^443$ RewriteRule ^(.*)?$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R] </Directory> # 静态文件位置 Alias /static "C:/web/ChpHahaa/HLCHP/static" <Directory "C:/web/ChpHahaa/HLCHP/static"> AllowOverride None Options None Require all granted </Directory>

 C:\Apache24\conf\extra\httpd-vhosts.conf

<VirtualHost *:80> ServerAdmin py.jz.goto.first@foxmail.com DocumentRoot "C:/web/ChpHahaa" ServerName localhost ServerAlias localhost ErrorLog "logs/localhost-error_log" </VirtualHost> <VirtualHost *:80> ServerAdmin py.jz.goto.first@foxmail.com DocumentRoot "C:/web/ChpHahaa/ChpHahaa" ServerName hahaaa.cn ServerAlias www.hahaaa.cn ErrorLog "logs/localhost-error_log" </VirtualHost>

C:\Apache24\conf\extra\httpd-ssl.conf

ServerName www.hahaaa.cn:443 ServerAdmin py.jz.goto.first@foxmail.com # 匹配证书 这里上面要是有相同的属性名注释掉 SSLCertificateFile "C:\Apache24\conf\ssl\hahaaa.cn.crt" SSLCertificateKeyFile "C:\Apache24\conf\ssl\hahaaa.cn.key" SSLCertificateChainFile "C:\Apache24\conf\ssl\hahaaa.cn_ca.crt"

3.扬帆起航

配置已经配置成功现在进入apache 的bin 文件 启动 ApacheMonitor.exe

启动服务 在网页中输入 hahaaa.cn

启动成功!!!

 

最新回复(0)