环境:Windows Server 2016 + SQL Server 2016 + Python3.6 +Django2+ IIS10.0
二、搭建Python环境及安装IIS、CGI1.下载并安装python到相应的目录:https://www.python.org/downloads/release/python-368/
2.创建python虚拟环境
1.创建目录
e: mkdir innovation cd E:innovation mkdir virtualenvs cd E:innovationvirtualenvs
2.创建虚拟环境
python -m venv innovation_venv
3.激活虚拟环境
cd innovation_venvscripts activate.bat # activate the virtual env三.配置django项目
备注:服务器没有联网的情况下;需将安装包都下载到本地 然后复制到服务器上安装;或在联网的情况下用pip安装后讲包
目录复制过来
1.安装Django项目依赖包
cd 项目根目录
2.安装依赖包
pip install -r requirements.txt
3.安装wfastcgi
在激活虚拟环境下执行命令
pip install wfastcgi
4.执行命令
run wfastcgi-enable
"E:innovationvirtualenvsinnovation_venvscriptspython.exe|E:innovationvirtualenvsinnovation_venvlibsite-packageswfastcgi.py" can now be used as a FastCGI script processor
出现以上说明wfastcgi配置成功
wfastcgi是一个类似于Gunicorn或uwsgi的WSGI。wfastcgi是由微软维护的,使用它会比尝试在windows上编译其他基于linux的WSGI服务器更好。
如果你的Django应用只在Windows上使用,你可以考虑把wfastcgi放到requirements.txt文件中。
参考: https://pypi.org/project/wfastcgi/
安装配置完上述环境后,验证python项目是否能运行
出现以上信息说明项目启动成功!
1.安装iis
这里就不详细介绍了
Windows10如何安装IIS(互联网信息服务):https://jingyan.baidu.com/article/eb9f7b6d9e73d1869364e8d8.html
安装成功后的额界面,安装成功后可以启动iis,windows桌面会生成图标,双击即可运行
2.配置django服务
在iis中配置FastCGI
在项目根目录中,创建一个新的web。配置配置文件并放入以下内容
scriptProcessor="E:innovationvirtualenvsinnovation_venvscriptspython.exe|E:innovationvirtualenvsinnovation_venvlibsite-packageswfastcgi.py"
这里的路径是在配置wfastcgi是获取的
配置说明:
1、system.webServer.handlers
FastCgiModule
2、appSettings
python项目配置
3、支持大文件上传的配置
system.webServer.security.requestLimits
system.web.httpRuntime
解决超时问题修改配置:
FastCGI Settings:Activity Timeout、Idle Timeout、Request Timeout
Application Pool: Idle Time-out (minutes)
参考:
https://docs.microsoft.com/en-us/visualstudio/python/configure-web-apps-for-iis-windows?view=vs-2019
https://pypi.org/project/wfastcgi/
3. 创建并配置新的IIS网站
打开IIS管理器→右键网站→添加网站
访问项目
项目启动成功
至此,后端django项目配置完成!
服务器目录说明项目部署主目录:E:innovation Python3.6.8安装目录:D:Program Files(x86)PythonPython36 1、后端API服务 代码及存储 代码 目录:E:innovationbackend ldap配置:E:innovationbackendinnovationldap_settings.py 数据库等配置项:E:innovationbackendinnovationsecret.py 上传文件 目录:E:innovationmedia 缓存文件 目录:E:innovationcache 日志文件 目录:E:innovationlogs Python虚拟环境目录 E:innovationvirtualenvsinnovation_venv 2、Web前端 E:innovationdist //前端源码 build后的文件
迭代部署:
Web前端:
1、替换E:innovationdist目录
API后端
1、替换E:innovationbackend目录
如果有新的migrations文件的话 需要执行一下数据库迁移
2、执行数据库迁移(migrations)
打开命令行工具(Windows Command prompt)
进入E盘:
e:
进入项目Python虚拟环境并激活
cd E:innovationvirtualenvsinnovation_venv
scriptsactivate.bat
进入项目代码目录,执行数据库迁移
cd E:innovationbackend
python manage.py migrate
项目相关配置
Sql Server数据库配置
DATAbaseS = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'HOST': 'ocnsia11LDC',
# 'PORT': 1433, // when use host 'ocnsia11LDC', the port is no not required.
'NAME': 'innovation',
'USER': 'Innovation',
'PASSWORD': 'China2025',
'OPTIONS': {
#'driver': 'SQL Server Native Client 11.0',
'driver': 'ODBC Driver 17 for SQL Server',
},
},
}
备注:请确保部署的服务器上已经安装了 (“ODBC Driver 17 for SQL Server”, “ODBC Driver 13 for SQL Server”,
“SQL Server Native Client 11.0”)中的任何一个
参考:
https://github.com/michiya/django-pyodbc-azure
https://docs.microsoft.com/en-us/sql/connect/odbc/microsoft-odbc-driver-for-sql-server
https://docs.microsoft.com/en-us/sql/relational-databases/native-client/applications/installing-sql-server-native-client
Ldap For Django配置
第三方库:django-python3-ldap
备注:对接 项目的ldap中需要修改部分源码 (otis公司的技术提供了支持),
ldap.py、ldap_settings.py在后端代码中 和 本文档一起打包成zip
配置文件:
innovation/ldap_settings.py
需要被替换的源码文件:({venv_path}为python环境的目录)
{venv_path}libsite-packagesdjango-python3-ldapldap.py
主要配置项的说明:
# The URL of the LDAP server.
LDAP_AUTH_URL = "ldap://xxxx.COM"
# Initiate TLS on connection.
LDAP_AUTH_USE_TLS = False
# The LDAP search base for looking up users.
# LDAP_AUTH_SEARCH_base = "OU=users,OU=China,OU=ap,DC=otis,DC=COM"
LDAP_AUTH_SEARCH_base = ["OU=users,OU=China,OU=ap,DC=otis,DC=COM", "OU=users,OU=Ch
"OU=Otis,OU=APAC,OU=CSC_Users,OU=.Resources,DC=otis,DC=COM
# Keep ModelBackend around for per-user permissions and maybe a local
AUTHENTICATION_BACKENDS = (
"django_python3_ldap.auth.LDAPBackend",
'django.contrib.auth.backends.ModelBackend',
)
# attributes that represent them.
LDAP_AUTH_USER_FIELDS = {
"username": "sAMAccountName",
"first_name": "givenName",
"last_name": "sn",
"email": "mail"
}
文件存储配置
# Log、Cache、Media MEDIA_ROOT = r"E:innovationmedia" # 上传文件目录 CACHE_FILE_ROOT = r"E:innovationcache" # 缓存文件目录 LOGGING_FILE_ROOT = r"E:innovationlogs" # 缓日志文件目录
邮件SMTP
# Email SMTP
DEFAULT_FROM_EMAIL = 'noreply@pusher.mail.xxxx.com' #默认的邮件发件人
EMAIL_HOST = 'smtpdm.aliyun.com' #邮件SMTP的Host
EMAIL_PORT = 80 #邮件SMTP的端口
EMAIL_USE_TLS = False
EMAIL_HOST_USER = 'noreply@pusher.mail.xxxx.com' #邮件SMTP的用户名
EMAIL_HOST_PASSWORD = '' #邮件SMTP的密码
# ADMINS Will ReceiveServer Error Mail
SERVER_EMAIL = 'noreply@pusher.mail.xxxx.com' #服务器Error邮件发件邮箱名
ADMINS = [('Fanping', 'fanping@xxxx.com'), ] #服务器Error邮件接收人
前端项目的部署会在第二篇文章中介绍!
希望对你有帮助,欢迎留言点赞收藏!



