Windows Server 2016配置apache2.4+django 2.02+mod_wsgi 4.5.24

mod_wsgid官方说明文档:https://github.com/GrahamDumpleton/mod_wsgi/blob/master/win32/README.rst

一定不要用virtualenv,因为mod_wsgi不支持。
cpu架构一定要选择一样的,32位就都是32位,64位就都是64位。
VC14 32位运行库:https://www.microsoft.com/zh-CN/download/details.aspx?id=48145
apache(VC14) 下载:https://www.apachelounge.com/download/VC14/
apache(VC15) 下载:https://www.apachelounge.com/download/VC15/
把apache安装成windows服务:

C:\Users\Administrator>C:\Apache24\bin\httpd.exe -k install
#取消安装
C:\Users\Administrator>C:\Apache24\bin\httpd.exe -k uninstall
#启动
C:\Users\Administrator>C:\Apache24\bin\httpd.exe -k start

编译后的wsgi下载地址:https://www.lfd.uci.edu/~gohlke/pythonlibs/#mod_wsgi
使用使用pip安装:

C:\Users\Administrator>pip install C:\Users\Administrator\Desktop\mod_wsgi-4.5.24+ap24vc14-cp36-cp36m-win32.whl
Processing c:\users\administrator\desktop\mod_wsgi-4.5.24+ap24vc14-cp36-cp36m-win32.whl
Installing collected packages: mod-wsgi
Successfully installed mod-wsgi-4.5.24+ap24vc14

C:\Users\Administrator>pip install -i https://mirrors.aliyun.com/pypi/simple django
Collecting django
  Using cached Django-2.0.2-py3-none-any.whl
Collecting pytz (from django)
  Using cached pytz-2018.3-py2.py3-none-any.whl
Installing collected packages: pytz, django
Successfully installed django-2.0.2 pytz-2018.3

C:\Users\Administrator>
C:\Users\Administrator>
C:\Users\Administrator>
C:\Users\Administrator>mod_wsgi-express module-config
LoadFile "c:/python36-32/python36.dll"
LoadModule wsgi_module "c:/python36-32/lib/site-packages/mod_wsgi/server/mod_wsgi.cp36-win32.pyd"
WSGIPythonHome "c:/python36-32"

C:\Users\Administrator>
C:\Users\Administrator>

把mod_wsgi-express module-config的结果拷贝到httpd.conf就完了。

打开C:\Apache24\conf\httpd.conf

#
# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
#
# If your host doesn't have a registered DNS name, enter its IP address here.
#  取消ServerName前面的#
ServerName localhost:80


# Virtual hosts, 取消前面的#
Include conf/extra/httpd-vhosts.conf

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


	ServerName ws.cc.cn
	ServerAlias cc.cn
	ServerAdmin [email protected]

	# 存放用户上传图片等文件的位置,注意去掉#号
	Alias /media/ "d:/ws/static_files/media/"
	# 允许通过网络获取media的内容
	                  
		Require all granted
	
	
	# 静态文件(js/css/images)的存放位置
	Alias /static/ "d:/ws/static_files/static/"                

	# 允许通过网络获取static的内容
	                  
		Require all granted
	

	# 最重要的!通过wsgi.py让Apache识别这是一个Django工程,别漏掉前边的 /
	WSGIScriptAlias / "d:/ws/ws/wsgi.py"
	# wsgi.py文件的父级目录,第一个ProjectName为Django工程目录,第二个ProjectName为Django自建的与工程同名的目录
	                  
	
		# 下面这句只在 apache2.4中有效,如果是低于这个版本,请打掉。
		Require all granted 
	
	


更多配置说明以及其他请参考 mod_wsgi官方文档。

后面这部分参考:https://www.jianshu.com/p/b40a4a12fff1

django项目的 wsgi.py 也要做对应修改:

import os
from os.path import join,dirname,abspath
PROJECT_DIR = dirname(dirname(abspath(__file__)))

import sys
sys.path.insert(0,PROJECT_DIR)

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ws.settings")

application = get_wsgi_application()

直接写死也行:

import os, sys
# add the hellodjango project path into the sys.path
sys.path.append('C:/djangoApps/hello/')

# add the virtualenv site-packages path to the sys.path
# sys.path.append('/Lib/site-packages')

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ws.settings")

application = get_wsgi_application()

你可能感兴趣的:(Windows/.net,core,Python/Django)