日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

window部署python项目_Django在Window下的部署

發布時間:2023/12/3 python 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 window部署python项目_Django在Window下的部署 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

轉載 : codingsoho.com

前言

本文主要介紹利用apache去部署Django項目,所有步驟均在本機Window7和阿里云驗證通過。

配置

本例的基本配置如下:

工作目錄: C:/virtualenv/zakkabag

項目名稱: zakkabag

最終的安裝版本如下,后面我會一步一步描述如何安裝

操作系統

python

mysql

apache

Windows Server 2016 64bit

python 2.7.3 (32 位)

python-2.7.3.msi

32位安裝版

mysql-5.5.12-win32.msi

MySQL-python-1.2.4b4.win32-py2.7.exe

32位安裝版

httpd-2.2.25-win32-x86-no_ssl.msi

mod_wsgi-win32-ap22py27-3.3

安裝

Python2.7.3

Windows x86 MSI Installer (2.7.3) (sig) http://www.python.org/ftp/python/2.7.3/python-2.7.3.msi

安裝路徑: D:\Python27 (example)

安裝完成之后,記得把D:\Python27加入環境變量Path,這樣Python命令可以在任意路徑執行

執行python命令,如果出現下面信息,說明安裝已成功

C:\Users\User1>python

Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on win32

Type "help", "copyright", "credits" or "license" for more information.

Django

Django的安裝有多種方法,可以從源碼安裝,也可以利用pip命令安裝

源碼安裝

文件路徑; D:\Django-1.5.5 (example),這是一個很老的版本,之前用這個方法安裝過,這兒僅是提供一種方法,一般我現在采用pip方法安裝

安裝命令:

cd D:\Django-1.5.5

python setup.py install

Django會在python Lib目錄下面創建新的目錄: d:\Python27\Lib\site-packages\django

….

byte-compiling d:\Python27\Lib\site-packages\django\views\i18n.py to i18n.pyc

byte-compiling d:\Python27\Lib\site-packages\django\views\static.py to static.py

……

byte-compiling d:\Python27\Lib\site-packages\django\views\__init__.py to __init__.pyc

byte-compiling d:\Python27\Lib\site-packages\django\__init__.py to __init__.pyc

running install_scripts

creating d:\Python27\Scripts

copying build\scripts-2.7\django-admin.py -> d:\Python27\Scripts

running install_egg_info

Writing d:\Python27\Lib\site-packages\Django-1.5.5-py2.7.egg-info

把D:\Python27\Lib\site-packages\加入環境變量path

把D:\Python27\Script加入環境變量Path,這樣django-admin.py可以在任意目錄運行

pip安裝

pip install django==1.5.5

mysql & MySQLLdb

參考mysql安裝指南

Apache2.2

安裝版

從官網下載apache安裝包http://httpd.apache.org/download.cgi,我的安裝路徑為d:\Program Files\

我之前用的版本是httpd-2.2.25-win32-x86-no_ssl.msi,官網已經找不到了,我的理解只要裝同一系列應該問題就可以了。

安裝完成之后,主要的路徑及文件如下:

d:\Program Files\Apache Software Foundation\Apache2.2\modules\ 組件位置,需要加載的組件放在該路徑

d:\Program Files\Apache Software Foundation\Apache2.2\conf\ httpd.conf 最重要的配置文件

D:\Program Files\Apache Software Foundation\Apache2.4\logs 相關錯誤打印可以在下面文件找到

免安裝版

下載

配置

需要將一些對應的路徑替換掉,以httpd-2.2-x64版本為例,將安裝文件httpd-2.2.19-win64.rar解壓到目錄D:/PythonWebSW

文件conf/httpd.conf

ServerRoot?"/httpd-2.2-x64"

DocumentRoot?"/httpd-2.2-x64/htdocs"

ScriptAlias?/cgi-bin/?"/httpd-2.2-x64/cgi-bin/"

替換為

ServerRoot?"D:/PythonWebSW/httpd-2.2.19-win64"

DocumentRoot?"D:/PythonWebSW/httpd-2.2.19-win64/htdocs"

ScriptAlias?/cgi-bin/?"D:/PythonWebSW/httpd-2.2.19-win64/cgi-bin/"

文件conf/extra/httpd-vhosts.conf

ServerAdmin?webmaster@dummy-host.example.com

DocumentRoot?"/httpd-2.2-x64/docs/dummy-host.example.com"

ServerName?dummy-host.example.com

ServerAlias?www.dummy-host.example.com

ErrorLog?"logs/dummy-host.example.com-error.log"

CustomLog?"logs/dummy-host.example.com-access.log"?common

ServerAdmin?webmaster@dummy-host2.example.com

DocumentRoot?"/httpd-2.2-x64/docs/dummy-host2.example.com"

ServerName?dummy-host2.example.com

ErrorLog?"logs/dummy-host2.example.com-error.log"

CustomLog?"logs/dummy-host2.example.com-access.log"?common

替換為

ServerAdmin?webmaster@dummy-host.example.com

DocumentRoot?"D:/PythonWebSW/httpd-2.2.19-win64/docs/dummy-host.example.com"

ServerName?dummy-host.example.com

ServerAlias?www.dummy-host.example.com

ErrorLog?"logs/dummy-host.example.com-error.log"

CustomLog?"logs/dummy-host.example.com-access.log"?common

ServerAdmin?webmaster@dummy-host2.example.com

DocumentRoot?"D:/PythonWebSW/httpd-2.2.19-win64/docs/dummy-host2.example.com"

ServerName?dummy-host2.example.com

ErrorLog?"logs/dummy-host2.example.com-error.log"

CustomLog?"logs/dummy-host2.example.com-access.log"?common

安裝

D:\PythonWebSW\httpd-2.2.19-win64\bin>httpd -k install

有的時候會報錯

D:\httpd-2.2.19-win64\bin>httpd?-k?install

Installing?the?Apache2.2?service

The?Apache2.2?service?is?successfully?installed.

Testing?httpd.conf....

Errors?reported?here?must?be?corrected?before?the?service?can?be?started.

httpd:?Could?not?reliably?determine?the?server's?fully?qualified?domain?name,?using?192.168.1.101?for?ServerName

(OS?10013)An?attempt?was?made?to?access?a?socket?in?a?way?forbidden?by?its?access?permissions.??:?make_sock:?could?not?bind?to?address?0.0.0.0:80

no?listening?sockets?available,?shutting?down

Unable?to?open?logs

原因及解決:有可能是系統80端口默認被占用了,可以修改conf/httpd.conf監聽端口,Listen 80 修改為 Listen 8081(或其他端口號)

啟動

D:\PythonWebSW\httpd-2.2.19-win64\bin>start httpd

卸載

D:\PythonWebSW\httpd-2.2.19-win64\bin>httpd -k uninstall

mod_wsgi

下載

注意:下載mod_wsgi.so版本需和python和apache匹配

我之前下過一個對應apache22和python2.7的版本,名字mod_wsgi-win32-ap22py27-3.3,這個可以直接用,所以一直用著,網上可以直接搜到。

安裝和配置

將mod_wsgi.so放到d:\Program Files\Apache Software Foundation\Apache2.2\modules\ , 并在httpd.conf里添加

LoadModule wsgi_module modules/mod_wsgi.so

httpd.conf的位置在d:\Program Files\Apache Software Foundation\Apache2.2\conf\

注意:apache裝載wsgi模塊,名字可以自定義

配置 httpd.conf

搜索http.conf文件,把"Deny from all" 及 "Require all denied" 等語句全部注釋掉:

Options FollowSymLinks

AllowOverride None

Require all denied

Order deny,allow

Deny from all

修改為

Options Indexes FollowSymLinks

AllowOverride None

測試wsgi

添加test.wsgi文件,我放在項目下面的apache文件夾下,這個文件夾是專門用來放跟apache相關的配置的

def?application(environ,?start_response):

status?=?'200?OK'

output?=?'Hello?World!'

response_headers?=?[('Content-type',?'text/plain'),

('Content-Length',?str(len(output)))]

start_response(status,?response_headers)

return?[output]

在http.conf里面引入這段代碼

Include?"?C:/virtualenv/zakkabag/apache?/test.wsgi?"

注:這兒監聽端口是8081

項目配置

項目主目錄

在項目目錄下面創建一個文件夾apache,添加文件"django.wsgi" & "apache_django_wsgi.conf"

和test.wsgi一樣,我們也可以用這個方法引入這段配置

Include " C:/virtualenv/zakkabag/apache/apache_django_wsgi.conf"

注:這些配置文件可直接寫在httpd.conf文件里,但我傾向于用專門的文件,這樣便于項目維護

apache_django_wsgi.conf

測試項目

WSGIScriptAlias / " C:/virtualenv/zakkabag/apache/test.wsgi"

實際項目

WSGIScriptAlias / " C:/virtualenv/zakkabag/apache/django.wsgi"

Options FollowSymLinks

AllowOverride None

Order deny,allow

Allow from all

django.wsgi

os.environ['DJANGO_SETTINGS_MODULE']="zakkabag.settings"

importdjango

django.setup()

importdjango.core.handlers.wsgi

application =django.core.handlers.wsgi.WSGIHandler()

靜態資源

apache_django_wsgi.conf

Alias /static "C:/virtualenv/ zakkabag /static_in_env/static_root"

Order allow,deny

Allow from all

Django的setting里

Settings.py

STATIC_URL = '/static/'

重啟

配置完成之后記得重啟apache

常見問題

問題1(OS 5)Access is denied. : Apache2.2: OpenService failed

如果出現這個錯誤,用管理員權限打開cmd

問題2(OS 1072)The specified service has been marked for deletion. : Apache2.2: Failed to delete the service.

將service里面的停掉

問題3ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb

原因是MysqlLdb沒有安裝好,具體參考mysql安裝指南文章

Window 安裝文件:

MySQL-python-1.2.3.win-amd64-py2.7.exe

Linux:

MySQL-python==1.2.3

生成的文件如下

MySQL_python-1.2.3-py2.7.egg-info (文件夾)

MySQLdb (文件夾)

_mysql.pyd

_mysql_exceptions.py

_mysql_exceptions.pyc

_mysql_exceptions.pyo

問題4機器上可能會配置多個apache,有的時候安裝新的apache時需要先把之前install的uninstall

問題5 Django: AppRegistryNotReady()

在 django.wsgi, 添加setup()

os.environ['DJANGO_SETTINGS_MODULE']="zakkabag.settings"

importdjango

django.setup()

importdjango.core.handlers.wsgi

application =django.core.handlers.wsgi.WSGIHandler()

setup()

This function is called automatically:

When running an HTTP server via Django's WSGI support.

When invoking a management command.

It must be called explicitly in other cases, for instance in plain Python scripts.

關注下方公眾號獲取更多文章

總結

以上是生活随笔為你收集整理的window部署python项目_Django在Window下的部署的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。