溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Django用Apache和mod_wsgi部署

發(fā)布時(shí)間:2020-07-09 10:37:04 來源:網(wǎng)絡(luò) 閱讀:315 作者:ckllf 欄目:編程語言

  背景

  最近寫了一個(gè)Django在windows上運(yùn)行,但是用的vbs命令,導(dǎo)致在后臺(tái)運(yùn)行的時(shí)候,必須用戶登錄才可以運(yùn)行,還有就是登錄之后,有其他進(jìn)程可能被干掉。

  Set ws = CreateObject("Wscript.Shell")

  ws.run "cmd /c D:\xxd\run.bat",vbhide

  ` bat 內(nèi)容

  C:\Python37\python3.exe manage.py runserver 0.0.0.0:8000 >> console.log

  第二種創(chuàng)建task的方式,也是只能是用戶登錄后才可以,不是service級(jí)別的,不能開機(jī)就運(yùn)行。

  第三種就是用sc創(chuàng)建服務(wù)來運(yùn)行, 這個(gè)沒有試。

  后面可以從windows遷移到linux也可以運(yùn)行的話,用Apache httpd來運(yùn)行,是非常不錯(cuò)的

  準(zhǔn)備

  環(huán)境準(zhǔn)備

  python3.7

  mysql5~8

  還有apache -httpd

  mod_wsgi

  保證python和apache都是統(tǒng)一的,都是32位或者都是64位。

  下面是我的pip requirement list

  asn1crypto==0.24.0

  cffi==1.11.5

  cryptography==2.3

  Django==2.0.7

  idna==2.7

  mod-wsgi==4.5.24+ap24vc14

  pip==19.3.1

  pycparser==2.18

  PyMySQL==0.9.2

  pytz==2018.5

  setuptools==41.6.0

  six==1.11.0

  wheel==0.31.1

  配置

  創(chuàng)建工程,搭建mysql環(huán)境,以及怎么啟動(dòng)??梢詤⒖?/p>

  https://www.djangoproject.com/start/

  重點(diǎn)是講下標(biāo)題的用 apache和mod_wsgi來部署django。

  什么是mod_wsgi鄭州人流醫(yī)院 http://www.zzzy120.com/

  mod_wsgi is an Apache HTTP Server module by Graham Dumpleton that provides a WSGI compliant interface for hosting Python based web applications under Apache. As of version 4.5.3, mod_wsgi supports Python 2 and 3 (starting from 2.6 and 3.2).

  安裝apahce, 并設(shè)置一個(gè)環(huán)境變量

  key: MOD_WSGI_APACHE_ROOTDIR

  value: C:\wamp64\bin\apache\apache\

  創(chuàng)建一個(gè)virtualHost啟動(dòng)文件,根據(jù)自己的修改工程名字和路徑

  import os

  import sys

  import site

  # Add the site-packages of the chosen virtualenv to work with

  site.addsitedir('C:/Python37/Lib/site-packages')

  # Add the app's directory to the PYTHONPATH

  sys.path.append('D:/xxd/Assetlog')

  sys.path.append('D:/xxd/Assetlog/AssetLog')

  os.environ['DJANGO_SETTINGS_MODULE'] = 'AssetLog.settings'

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

  from django.core.wsgi import get_wsgi_application

  application = get_wsgi_application()

  安裝mod_wdgi

  pip3 install mod_wsgi

  或者直接用pip3 install whl文件

  pip3 install mod_wsgi-4.5.24+ap24vc14-cp37-cp37m-win_amd64.whl

  # https://www.lfd.uci.edu/~gohlke/pythonlibs/#mod_wsgi 這里下載

  配置apache和mod_wsgi

  查看apache配置信息mod_wsgi-express module-config, 如果執(zhí)行mod_wsgi-express失敗,在python安裝目錄里面找C:\Python37\Scripts,我的在這個(gè)目錄下。

  LoadFile "c:/python37/python37.dll"

  LoadModule wsgi_module "c:/python37/lib/site-packages/mod_wsgi/server/mod_wsgi.cp37-win32.pyd"

  WSGIPythonHome "c:/python37"

  把這個(gè)信息追加到apache配置文件httpd.conf的最后。

  最后在配置文件

  下面配置apache虛擬主機(jī),如果有多個(gè)django工程,可以添加多個(gè)。配置文件在conf\extra\httpd_vhosts.conf里面

  ServerName localhost

  WSGIPassAuthorization On

  ErrorLog "logs/asset.error.log"

  CustomLog "logs/asset.access.log" combined

  WSGIScriptAlias / "D:/xxd/Assetlog/AssetLog/wsgi_win.py"

  Require all granted

  Alias /static "D:/xxd/Assetlog/static"

  Require all granted



向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI