最近在折騰Django的WSGI應用,雖然Django自帶的runserver很方便,但是對於複雜的功能它就無能為力了。
首先在Windows上遇到坑了,然後在windows10自帶的Ubuntu遇到坑了,最後在虛擬機器上總算解決了。
從以前的文章Django筆記《Django 學習筆記(二)第一個網頁》可以看出我用的是windows10系統中的cmd來進行Django操作的,由於gunicorn應用是在unix系統中運作的,強制性在win平台運作就出錯了。
1.windows遇到坑:
首先cmd中輸入:pip3 install gunicorn ,安裝成功後,切換到專案目錄(G:/Django/hello),具體操作是先切換到G盤,直接輸入G:,接著輸入cd Django\hello 就會出現G:\Django\hello>這種情況。然後運行gunicorn,在Django專案中格式是:gunicorn yourproject.wsgi [-b 127.0.0.1.8000],括號內為選填,其他命令參數可以發送 gunicorn -h得到幫助文件。我這邊的項目是hello,所以我的指令操作是:gunicorn hello.wsgi ,然後呢,出錯ModuleNotFoundError: No module named 'pwd',沒有pwd模組,好吧,沒有找嗆。找到後的程式碼貼在下面了,把找到的pwd.py放進了python3的安裝目錄D:\Program Files\Python\Python36\Lib,接著我們再運行gunicorn hello.wsgi ,結果又出錯了:AttributeError: module 'socket' has no attribute 'AF_UNIX' ,然後我又在網上找,因為是unix系統中的gunicorn,結果找不到liunx中的sock.py文件,所以轉戰unix系統,windows棄坑。
G:\Django\hello>gunicorn hello.wsgi Traceback (most recent call last): File "d:\program files\python\python36\lib\runpy.py", line 193, in _run_module_as_main"__main__", mod_spec) File "d:\program files\python\python36\lib\runpy.py", line 85, in _run_codeexec(code, run_globals) File "D:\Program Files\Python\Python36\Scripts\gunicorn.exe\__main__.py", line 5, in <module> File "d:\program files\python\python36\lib\site-packages\gunicorn\app\wsgiapp.py", line 10, in <module>from gunicorn.app.base import Application File "d:\program files\python\python36\lib\site-packages\gunicorn\app\base.py", line 12, in <module>from gunicorn import util File "d:\program files\python\python36\lib\site-packages\gunicorn\util.py", line 13, in <module>import pwd ModuleNotFoundError: No module named 'pwd'
ModuleNotFoundError: No module named 'pwd'
from os import * from pwd import * def get_username():return getpwuid(getuid())[0]
#pwd.py
G:\Django\hello>gunicorn hello.wsgi Traceback (most recent call last): File "d:\program files\python\python36\lib\runpy.py", line 193, in _run_module_as_main"__main__", mod_spec) File "d:\program files\python\python36\lib\runpy.py", line 85, in _run_codeexec(code, run_globals) File "D:\Program Files\Python\Python36\Scripts\gunicorn.exe\__main__.py", line 5, in <module> File "d:\program files\python\python36\lib\site-packages\gunicorn\app\wsgiapp.py", line 10, in <module>from gunicorn.app.base import Application File "d:\program files\python\python36\lib\site-packages\gunicorn\app\base.py", line 13, in <module>from gunicorn.arbiter import Arbiter File "d:\program files\python\python36\lib\site-packages\gunicorn\arbiter.py", line 18, in <module>from gunicorn import sock, systemd, util File "d:\program files\python\python36\lib\site-packages\gunicorn\sock.py", line 101, in <module>class UnixSocket(BaseSocket): File "d:\program files\python\python36\lib\site-packages\gunicorn\sock.py", line 103, in UnixSocket FAMILY = socket.AF_UNIX AttributeError: module 'socket' has no attribute 'AF_UNIX'
AttributeError: module 'socket' has no attribute 'AF_UNIX'
2.win10平台自帶Ubuntu遇到的坑
win10開啟Linux系統:首先,在win10設定-->安全與更新-->針對開發人員-->開發人員模式-->打鉤,然後在控制面板-->程式個功能-->啟動或關閉windows功能-->適用於Linux的windows子系統(Bata)-->打勾,最後用管理員執行shell-->輸入cmd-->輸入bash-->按照操作下載安裝Linux。
安裝完畢後,用管理員身分shell進入cmd,輸入bash進入Linux系統。系統預設安裝的有python2.7和python3.5。 python預設啟動的是python2,可以設定預設python為python3:
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 100 sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.5 200
依照套路先安裝pip3(如果直接安裝pip是預設安裝在python2中的):sudo apt-get install python3-pip,接著用pip安裝Django:sudo pip3 install Django,最後用pip安裝gunicorn:sudo pip3 install gunicorn (現在自動安裝的版本是19.7.1)。環境安裝完畢,切換到專案位址:cd /mnt/g/Django/hello,然後輸入gunicorn hello.wsgi ,結果又出錯了。 OSError: [Errno 92] Protocol not available ,後來折騰卸載pip發現gunicorn 依賴python-gunicorn(19.4.5),所以卸載了gunicorn(19.7.1):pip3 uninstall gunicorn ,然後安裝了gunicorn(19.5) :pip3 install gunicorn==19.4.5。然後命令gunicorn hello.wsgi 啟動成功,沒有出現Failed to find application,成功訪問了127.0.0.1:8000/admin/ 。
Lee@Kein:/mnt/g/Django/hello$ gunicorn hello.wsgi [2017-07-16 15:16:25 +0800] [428] [INFO] Starting gunicorn 19.7.1Traceback (most recent call last): File "/usr/local/lib/python3.5/dist-packages/gunicorn/sock.py", line 44, in set_options sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) OSError: [Errno 92] Protocol not available During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/usr/local/bin/gunicorn", line 11, in <module>sys.exit(run()) File "/usr/local/lib/python3.5/dist-packages/gunicorn/app/wsgiapp.py", line 74, in run WSGIApplication("%(prog)s [OPTIONS] [APP_MODULE]").run() File "/usr/local/lib/python3.5/dist-packages/gunicorn/app/base.py", line 203, in run super(Application, self).run() File "/usr/local/lib/python3.5/dist-packages/gunicorn/app/base.py", line 72, in run Arbiter(self).run() File "/usr/local/lib/python3.5/dist-packages/gunicorn/arbiter.py", line 198, in run self.start() File "/usr/local/lib/python3.5/dist-packages/gunicorn/arbiter.py", line 157, in start self.LISTENERS = sock.create_sockets(self.cfg, self.log, fds) File "/usr/local/lib/python3.5/dist-packages/gunicorn/sock.py", line 180, in create_sockets sock = sock_type(addr, conf, log) File "/usr/local/lib/python3.5/dist-packages/gunicorn/sock.py", line 32, in __init__self.sock = self.set_options(sock, bound=bound) File "/usr/local/lib/python3.5/dist-packages/gunicorn/sock.py", line 89, in set_optionsreturn super(TCPSocket, self).set_options(sock, bound=bound) File "/usr/local/lib/python3.5/dist-packages/gunicorn/sock.py", line 46, in set_optionsif err[0] not in (errno.ENOPROTOOPT, errno.EINVAL): TypeError: 'OSError' object is not subscriptable
OSError: [Errno 92] Protocol 不 available
Lee@Kein:/mnt/g/Django/hello$ gunicorn hello.wsgi [2017-07-16 15:22:16 +0800] [470] [INFO] Starting gunicorn 19.4.5[2017-07-16 15:22:16 +0800] [470] [INFO] Listening at: http://127.0.0.1:8000 (470) [2017-07-16 15:22:16 +0800] [470] [INFO] Using worker: sync [2017-07-16 15:22:17 +0800] [473] [INFO] Booting worker with pid: 473Not Found: /static/admin/css/base.css Not Found: /static/admin/css/login.css
#3.虛擬機安裝ubuntu系統,按照上面的套路,成功一次啟動了服務。也反過來思考win10上遇到的問題,成功把坑2填上了。
另外的一些問題是:
1.django 在Linux裡面無法安裝mysqlclient,這個時候安裝PyMySQL,運行django專案會報錯,只需要在一個檔案加入一句話即可。
專案根目錄下,找到對應app目錄下的__init__.py,加入這句話:
import pymysql
#pymysql.install_as_MySQLdb()
##pymysql.install_as_MySQLdb()
##pymysql.install_as_MySQLdb()
##pymysql.install_as_MySQLdb()
##pymysql.install_as_MySQLdb()
範例:
我的專案是eagle,在eagle下有manage.py這個文件,同時還有你自己的app,在這個app下有view.py這個文件。
###2.nginx裡面的static格式註意,使用location /static/ 和 location /static 載入時是不同的,要依照html中的格式符合。 ######3.搞了一天才了解的一個問題,大部分教程都是說開啟uwsgi --ini eagle.ini鏈接uwsgi和django,然後開啟nginx,但是,######如果你的eagle.ini中沒有加入log路徑,不會被後台運行,這個時候只能關閉uwsgi,#######然後開啟nginx,當你在瀏覽器輸入位址後,發現502錯誤,檢視/var/ log/nginx/myweb_error.log,發現報錯息 ,恭喜你入坑了。 ###原因是剛剛的uwsgi被關閉,你沒有重啟,網路上的很多人都不知道這個原因。 。 。
坑爹啊,亂七八糟的回答。根據我的方式,有兩種方法,一種是開啟nginx後再運行
uwsgi --ini eagle.ini,一種是在eagle.ini添加log路徑後會自動後台運行
以上是Django遇到的地雷,大家要注意的詳細內容。更多資訊請關注PHP中文網其他相關文章!