virtualvenv installhtml
首先要安裝python3node
由於系統已經裝了 python3.6 因此接下來直接裝虛擬環境 virtualvenvpython
mkdir yourwebproject folder cd yourwebproject /usr/python3.6/bin/python3.6 -m venv venv #創建一個獨立於系統的虛擬環境 不會跟系統環境混淆 source venv/bin/activate #運行環境 deactivate #退出環境
pip install django
1). django-admin startproject proName 2). cd proName 3). python manage.py runserver # run
不用也能夠進行接下來的操做nginx
官網: https://uwsgi-docs.readthedoc...web
在虛擬環境中sql
pip install uwsgi
# test.py def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return [b"Hello World"] # python3
uwsgi --http :8080 --wsgi-file test.py # 假設訪問端口號8080
http://serverIP:8080
若顯示站點文件中的輸出,說明uwsgi生效django
路徑 web client <-> uwsgi <-> python瀏覽器
python manage.py runserver 0.0.0.0:8000
uwsgi --http :8080 --module djangoProName.wsgi
--module djangoProName.wsgi 表明 djangoProName 目錄下的 wsgi.py 文件服務器
http://serverIP:8080
官網: https://www.nginx.com/resourc...session
不在虛擬環境中安裝
因爲服務器上已經安裝了 nginx 因此安裝步驟省略,只須要在 nginx.conf 中添加配置便可。
nginx.conf 結構:
user www-data; worker_processes auto; pid /run/nginx.pid; events { worker_connections 768; } http { # server{ ... } }
添加 server
server { listen 98 default_server; # 訪問時輸入的端口 本地和外部瀏覽器後面都要加這個端口號 server_name 10.41.95.85; # 本身網站的域名 # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { # location : 文件系統配置 去應答一些要服務器資源的請求 include uwsgi_params; uwsgi_pass 127.0.0.1:9898; # 與ini文件對接端口 與上面的 93 端口沒有關係 } }
配置完了以後記得重啓nginx
將配置項所有寫入ini文件
在venv同目錄下本身新建uwsgi的ini文件
[uwsgi] socket = 127.0.0.1:9898 ; 與 nginx 對接 IP ; django pro dir chdir = /root/Odin/TrackManagement/TrackManagement/ ; django project dir wsgi-file = TrackManagement/wsgi.py ; 表明 TrackManagement 目錄下的 wsgi.py 文件 ; module = TrackManagement.wsgi ; 有上面的wsgi配置這個就不用寫了 processes = 2 ; 進程 threads = 1 ; 線程 stats = 127.0.0.1:9696 ; 內部配置訪問ip 與socket區別開
http://10.41.95.85:98
若顯示django畫面 則 uwsgi+nginx生效
路徑 web client <-> nginx <-> uwsgi <-> django
<details>
<summary>uwsgi: unrecognized option '--http:8089'</summary>
uwsgi: unrecognized option '--http:8089' getopt_long() error
參數格式不對 :8089前面要加空格 uwsgi還在開啓
</details>
<details>
<summary>uwsgi: unrecognized option '--http'</summary>
(venv) [root@localhost TrackManagement]# uwsgi --http:8089 --module TrackManagement.wsgi uwsgi: unrecognized option '--http' getopt_long() error
uwsgi還在開啓 先殺了進程再重啓
</details>
<details><summary>uwsgi trkMngm_uwsgi.ini -> invalid request block size: 21573 (max 4096)...skip</summary>
啓動了以後每次訪問
*** Stats server enabled on 127.0.0.1:9295 fd: 12 *** invalid request block size: 21573 (max 4096)...skip invalid request block size: 21573 (max 4096)...skip invalid request block size: 21573 (max 4096)...skip invalid request block size: 21573 (max 4096)...skip invalid request block size: 21573 (max 4096)...skip invalid request block size: 21573 (max 4096)...skip invalid request block size: 21573 (max 4096)...skip invalid request block size: 21573 (max 4096)...skip invalid request block size: 21573 (max 4096)...skip invalid request block size: 21573 (max 4096)...skip invalid request block size: 21573 (max 4096)...skip invalid request block size: 21573 (max 4096)...skip invalid request block size: 21573 (max 4096)...skip invalid request block size: 21573 (max 4096)...skip invalid request block size: 21573 (max 4096)...skip invalid request block size: 21573 (max 4096)...skip invalid request block size: 21573 (max 4096)...skip invalid request block size: 21573 (max 4096)...skip invalid request block size: 21573 (max 4096)...skip invalid request block size: 21573 (max 4096)...skip
trkMngm_uwsgi.ini 文件中有設置nginx的socket
若是這時候nginx沒有對應的配置或者配置了可是nginx沒有重啓
就會產生這個錯誤
</details>
<details>
<summary>ModuleNotFoundError: No module named 'TrackManagement/TrackManagement/wsgi'</summary>
(venv) [root@localhost TrackManagement]# uwsgi --http :8089 --module TrackManagement/TrackManagement/wsgi.py *** Starting uWSGI 2.0.18 (64bit) on [Tue Feb 19 11:33:20 2019] *** compiled with version: 4.8.5 20150623 (Red Hat 4.8.5-36) on 18 February 2019 05:28:03 os: Linux-3.10.0-957.1.3.el7.x86_64 #1 SMP Thu Nov 29 14:49:43 UTC 2018 nodename: localhost.localdomain machine: x86_64 clock source: unix pcre jit disabled detected number of CPU cores: 4 current working directory: /root/Odin/TrackManagement detected binary path: /root/Odin/TrackManagement/venv/bin/uwsgi uWSGI running as root, you can use --uid/--gid/--chroot options *** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** *** WARNING: you are running uWSGI without its master process manager *** your processes number limit is 63229 your memory page size is 4096 bytes detected max file descriptor number: 1024 lock engine: pthread robust mutexes thunder lock: disabled (you can enable it with --thunder-lock) uWSGI http bound on :8089 fd 4 spawned uWSGI http 1 (pid: 33181) uwsgi socket 0 bound to TCP address 127.0.0.1:33454 (port auto-assigned) fd 3 uWSGI running as root, you can use --uid/--gid/--chroot options *** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** Python version: 3.6.4 (default, Mar 6 2018, 13:19:57) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] *** Python threads support is disabled. You can enable it with --enable-threads *** Python main interpreter initialized at 0x12acbf0 uWSGI running as root, you can use --uid/--gid/--chroot options *** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** your server socket listen backlog is limited to 100 connections your mercy for graceful operations on workers is 60 seconds mapped 72920 bytes (71 KB) for 1 cores *** Operational MODE: single process *** ModuleNotFoundError: No module named 'TrackManagement/TrackManagement/wsgi' unable to load app 0 (mountpoint='') (callable not found or import error) *** no app loaded. going in full dynamic mode *** uWSGI running as root, you can use --uid/--gid/--chroot options *** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** *** uWSGI is running in multiple interpreter mode *** spawned uWSGI worker 1 (and the only) (pid: 33180, cores: 1)
(venv) [root@localhost TrackManagement]# ls db.sqlite3 manage.py testUwsgi.py TrackManagement
</details>
<details>
<summary>runserver運行django error : Bad Request</summary>
(venv) [root@localhost TrackManagement]# python manage.py runserver 0.0.0.0:8080 Performing system checks... System check identified no issues (0 silenced). You have 15 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions. Run 'python manage.py migrate' to apply them. February 18, 2019 - 07:47:25 Django version 2.1.7, using settings 'TrackManagement.settings' Starting development server at http://0.0.0.0:8080/ Quit the server with CONTROL-C. Invalid HTTP_HOST header: '10.41.95.85:8080'. You may need to add '10.41.95.85' to ALLOWED_HOSTS. Bad Request: / [18/Feb/2019 07:47:51] "GET / HTTP/1.1" 400 60826 Invalid HTTP_HOST header: '10.41.95.85:8080'. You may need to add '10.41.95.85' to ALLOWED_HOSTS. Bad Request: /favicon.ico [18/Feb/2019 07:47:54] "GET /favicon.ico HTTP/1.1" 400 60906
django setting.py
ALLOWED_HOSTS = ['*'] -> ALLOWED_HOSTS = ['*']
</details>
<details>
<summary>manage.py語法錯誤</summary>
SyntaxError: invalid syntax [root@localhost TrackManagement]# python manage.py runserver 0.0.0.0:8080 File "manage.py", line 14 ) from exc
沒有運行虛擬環境 外面的環境是python 虛擬環境纔是python3 因此會有語法錯誤
</details>