Windows下部署Django(Apache+mod_wsgi)
Posted 天枢
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Windows下部署Django(Apache+mod_wsgi)相关的知识,希望对你有一定的参考价值。
一、Python3.5.4安装
1.1 下载地址
https://www.python.org/downloads/windows/ 选择Windows x86-64 executable installer 下载解压:python-3.5.4-amd64.exe 问题:安装python-3.5.4-amd64.exe时报错:0x80240017 解决:重装系统:Windows8.1-KB2919355-x64.msu 下载Windows8.1-KB2919355-x64.msu: https://pan.baidu.com/s/1Iw8y-jsbnrLyzUncua1lSQ#list/path=%2F
1.2 添加系统环境
如:C:Pythonpython35和C:Pythonpython35Scripts
1.3 更新pip3
python -m pip install --upgrade pip --force-reinstall
1.4 安装项目依赖
先安装tornado:tornado-5.1.1-cp35-cp35m-win_amd64.whl 下载网址:https://pypi.org/project/tornado/5.1.1/#files pip3 install tornado-5.1.1-cp35-cp35m-win_amd64.whl 再安装项目依赖 pip3 install -r requirements.txt 注:这里会报错,执行1.6中的步骤进行安装
1.5 手动下载并安装mod_wsgi
下载网址:https://www.lfd.uci.edu/~gohlke/pythonlibs/#mod_wsgi 找到下载:mod_wsgi-4.5.24+ap24vc14-cp35-cp35m-win_amd64.whl pip3 install mod_wsgi-4.5.24+ap24vc14-cp35-cp35m-win_amd64.whl
1.6 没网的情况下安装依赖包
①下载:(直接在pycharm上项目路径下) pip3 download -d C:UsershzxDesktoppackage -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple/ 注:C:UsershzxDesktoppackage 指的是桌面上新建的空文件夹为package的路径, ②安装:把package文件夹和requirements.txt放在C盘根目录下 pip3 install --no-index --find-links=C:package -r requirements.txt ③问题:安装过项目依赖后,执行python manage.py runserver 192.168.1.67:8000后会报错,提示xxx模块没有安装,
解决依次执行pip3 install 模块名==版本号 -i https://pypi.tuna.tsinghua.edu.cn/simple/
二、mysql5.6安装
2.1 添加mysql的bin目录至系统环境
C: MySQL5.6mysql-5.6.45-winx64in
2.2 配置my.ini
创建my.ini文件,放在bin目录下面,配置内容: [mysql] default-character-set=utf8 # 设置mysql客户端默认字符集 [mysqld] port = 3306 # 设置3306端口 basedir=E:/mysql-5.7.12-winx64 # 设置mysql的安装目录 datadir=E:/mysql-5.7.12-winx64/data # 设置mysql数据库的数据的存放目录 max_connections=200 # 允许最大连接数 character-set-server=utf8 # 服务端使用的字符集默认为8比特编码的latin1字符集 default-storage-engine=INNODB # 创建新表时将使用的默认存储引擎
2.3 初始化数据库、安装并启动服务
mysqld --initialize-insecure mysqld -install 如果已存在,则删除:sc delete mysql 或者 mysql –remove net start mysql
2.4 初始化登录密码
mysqladmin -u root -p password
回车
出现Enter password:直接回车,未设置密码时默认没有密码,
接着出现“New password:”输入新的密码password,回车即可,
接着出现“Confirm new password:”确认密码password,回车即可。
登录mysql:mysql –uroot –ppassword
2.5 删除无用配置
delete from mysql.user where user=‘‘; delete from mysql.user where host=‘::1‘;
2.6 令所用用户可以访问
update user set host=‘%‘ where user =‘root‘; flush privileges;
2.7 导入项目数据库
①先创建数据库 mysql –uroot –ppassword create database tesudrm_adg charset=utf8;
②导入项目数据库 mysql -uroot -ppassword tesudrm_adg < tesudrm_adg.sql
三、Redis3.2.1安装
3.1 下载地址
https://github.com/MSOpenTech/redis
版本:Redis-x64-3.2.100
3.2 解压安装
3.3 启动
redis-server redis.windows.conf 指定配置文件开启
3.4 设置redis服务(开机自启)
redis-server --service-install redis.windows.conf --loglevel verbose
3.5 常用命令
卸载服务:redis-server --service-uninstall 开启服务:redis-server --service-start 停止服务:redis-server --service-stop
3.6 测试
redis-cli -h 127.0.0.1 -p 6379
四、Apache安装
4.1 解压下载的压缩包
https://de.apachehaus.com/downloads/httpd-2.4.41-o102s-x64-vc14-r2.zip 解压httpd-2.4.41-o102s-x64-vc14-r2.zip 将解压出的Apache24移植指定位置C盘根路径:C:
4.2 修改配置文件
cd C: Apache24confhttpd.conf ①Apache24文件的路径,其余不用改: Define SRVROOT "C:Apache24"
②此处为你要发布的网站ip地址,此处我用我电脑的ip和端口,你也可以用127.0.0.1:8000用于本地测试;80端口自己设置 Listen 192.168.1.67:8000 ServerName 192.168.1.67:8000
③注释: # Define ENABLE_TLS13 "Yes"
4.3 创建apache服务(管理员权限开启终端)
①cd C:Apache24in ②创建服务:httpd.exe -k install -n "Apache24" ③卸载服务:httpd.exe -k uninstall -n "Apache24" ④重启apache命令:httpd.exe -k restart
4.4 访问测试
192.168.1.67:8000 出现页面,则Apache服务配置成功
4.5 配置mod_wsgi
①查看mod_wsgi路径 mod_wsgi-express module-config
②记录下来 LoadFile "c: /python35/python35.dll" LoadModule wsgi_module "c:/ python35/lib/site-packages/mod_wsgi/server/mod_wsgi.cp35-win_amd64.pyd" WSGIPythonHome "c:/python35"
五、项目TSDRM_ADG
5.1 拷贝项目到C盘根目录下
5.2 设置django静态文件路径
①settings.py文件中写入静态文件物理路径(已经写了) SITE_ROOT=os.path.join(os.path.abspath(os.path.dirname(__file__)),‘..‘) STATIC_ROOT = os.path.join(SITE_ROOT,‘static‘) ②添加静态文件访问逻辑路径 STATICFILES_DIRS = [ os.path.join(BASE_DIR, ‘static‘) ] ③收集静态文件 python manage.py collectstatic ④注销掉物理路径 # STATIC_ROOT # SITE_ROOT
5.3 修改配置文件httpd.conf
①安装wsgi模块后,出来的三行字符,直接复制过来 LoadFile "c: /python35/python35.dll" LoadModule wsgi_module "c:/ python35/lib/site-packages/mod_wsgi/server/mod_wsgi.cp35-win_amd64.pyd" WSGIPythonHome "c:/python35"
②设置项目中的wsgi路径 WSGIScriptAlias / C: TSDRM_ADGTSDRMwsgi.py
③设置项目路径 WSGIPythonPath C: TSDRM_ADG
④设置wsgi路径 <Directory C: TSDRM_ADGTSDRM> <Files wsgi.py> Require all granted </Files> </Directory>
⑤设置静态文件路径 Alias /static C: TSDRM_ADGstatic <Directory C: TSDRM_ADGstatic> AllowOverride None Options None Require all granted </Directory> 注意:不要导入 win_unicode_console
5.4 访问项目:192.168.1.67:8000
①问题:Forbidden You don‘t have permission to access this resource. 解决: 找到 apache 配置文件 httpd.conf 把里面的 <Directory /> AllowOverride none Require all denied </Directory> 改为 <Directory /> AllowOverride none Require all granted </Directory> ②问题: Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator, add.com@phpstudy and inform them of the time the error occurred, and anything you might have done that may have caused the error 查看问题:进入到C:Apache24logs 查看error.log,报错信息: File "C:\python35\lib\site-packages\django\__init__.py", line 17, in setup configure_logging(settings.LOGGING_CONFIG, settings.LOGGING) File "C:\python35\lib\importlib\__init__.py", line 126, in import_module cursor.execute(sql) File "C:\python35\lib\site-packages\pymysql\cursors.py", line 165, in execute result = self._query(query) File "C:\python35\lib\site-packages\pymysql\cursors.py", line 321, in _query conn.query(q) File "C:\python35\lib\site-packages\pymysql\connections.py", line 860, in query self._affected_rows = self._read_query_result(unbuffered=unbuffered) pymysql.err.InternalError: (1046, ‘No database selected‘) 解决:路径出错 进入到项目settings.py中: db_config_file = os.path.join(os.path.join(os.path.join(os.getcwd(), "faconstor"), "config"), "db_config.xml") print(os.getcwd()) ? C:Apache24 这里需要把项目文件拷贝到Apache24根路径下就能决绝错误
5.5 下载wkhtmltopdf应用软件
①地址:https://github.com/wkhtmltopdf/wkhtmltopdf/releases/ ②下载对应的应用软件:wkhtmltox-0.12.5-1.msvc2015-win64.exe ③将wkhtmltopdf应用软件拷贝至Apach24目录
以上是关于Windows下部署Django(Apache+mod_wsgi)的主要内容,如果未能解决你的问题,请参考以下文章
Windows下部署Django(Apache+mod_wsgi)
django + vue整合后,部署到windows apache遇到的问题汇总
CentOS7下 将django工程部署到Apache2.4上