项目部署uwsgi +Nginx
Nginx代理
在項目中配置Nginx
在項目目錄下添加一個config文件夾,后面的配置文件都統一放到該文件夾下(配置文件存放路徑不統一會導致輸入提示符時運行不成功)
config文件下新建mysite_nginx.conf文件,內容如下(將路徑替換成你自己的項目路徑或者對應文件):
# mysite_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
? ? # unix:///home/breavo/PyWorkSpace/mysite_code_shuffle/config/eshop.sock
? ? # server unix:///path/to/your/mysite/mysite.sock; # for a file socket
? ? server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
? ? # the port your site will be served on
? ? listen ? ? ?8000;
? ? # the domain name it will serve for
? ? server_name xx.xx.xxx.xxx; # substitute your machine's IP address or FQDN
? ? charset ? ? utf-8;
? ? # max upload size
? ? client_max_body_size 75M; ? # adjust to taste
? ? # Django media
? ? location /media ?{
? ? ? ? # /home/breavo/PyWorkSpace/mysite_code_shuffle/meida;
? ? ? ? alias /path/to/your/mysite/media; # your Django project's media files - amend as required
? ? }
? ? location /static {
? ? ? ? # /home/breavo/PyWorkSpace/mysite_code_shuffle/static
? ? ? ? alias /path/to/your/mysite/static; # your Django project's static files - amend as required
? ? }
? ? # Finally, send all non-media requests to the Django server.
? ? location / {
? ? ? ? uwsgi_pass ?django;
? ? ? ? include ? ? /home/breavo/PyWorkSpace/mysite_code_shuffle/config/uwsgi_params; # the uwsgi_params file you installed
? ? }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
在/etc/nginx/sites-enabled中創建一個鏈接,這樣Nginx能夠發現并啟用我們的配置,運行
sudo ln -s ~/path/to/your/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/
1
部署靜態文件
在運行Nginx之前,需要將django項目中的所有靜態文件放入static文件夾,在mysite/settings.py中添加
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
1
然后運行
python manage.py collectstatic
1
啟動(重啟)Nginx
sudo /etc/init.d/nginx start
sudo /etc/init.d/nginx restart
1
2
簡單測試
往服務器項目的media文件夾里丟一個圖片(這里以test.png為例)
訪問
xx.xx.xxx.xxx:8000/media/test.png
1
圖片正確顯示~
至此,靜態文件部署算是完成了
Uwsgi安裝及配置
接下來我們需要使用Uwsgi來處理項目中的動態請求
基本配置
虛擬環境下運行
pip install uwsgi
在之前創建的config文件夾下新建一個hello.py用來做測試,寫入以下內容
# test.py
def application(env, start_response):
? ? start_response('200 OK', [('Content-Type','text/html')])
? ? return [b"Hello World"] # python3
? ? #return ["Hello World"] # python2
1
2
3
4
5
接下來運行
uwsgi --http :8001 --wsgi-file test.py
打開瀏覽器查看
出現上圖,說明Uwsgi已經初步運行成功
運行項目
上述步驟沒有出問題的話,我們就可以使用Uwsgi啟動項目了,項目根目錄下運行
uwsgi --http :8001 --module mysite.wsgi
其中mysite.wsgi(mysite替換成自己的項目名稱)會自動搜索項目中的wsgi.py文件
如果正常運行~恭喜
如果瀏覽器出現Internal Server Error
檢查命令提示符是否輸入正確
檢查是否項目根目錄下運行
其他情況,查看命令行輸出或者log
至此Uwsgi基本配置算是完成了~~
Nginx + Uwsgi + django
Nginx訪問hello.py
現在我們使用socket通信來通過Nginx訪問hello.py,(確保Nginx已經啟動)config文件夾下運行
uwsgi --socket :8001 --wsgi-file hello.py
好了,就這么現在訪問瀏覽器
xx.xx.xxx.xxx:8000
成功訪問~
其中8000是Nginx監聽的端口(mysite_nginx.conf文件中配置),–socket :8001是用于socket通信的端口(兩個端口都可以替換~)
使用Unix sockets替換端口
編輯mysite_nginx.conf文件,將原來內容修改成如下
server unix:///path/to/your/mysite/mysite.sock; # for a file socket
# server 127.0.0.1:8001; # for a web port socket (we'll use this first)
1
2
保存后,config文件夾下運行
uwsgi --socket mysite.sock --wsgi-file hello.py
依舊打開瀏覽器訪問xx.xx.xxx.xxx:8000,屏幕出現hello world,說明一切正常
如果出現502 Bad Getway
查看你的Nginx error log(/var/log/nginx/error.log)
如果出現
connect() to unix:///path/to/your/mysite/mysite.sock failed (13: Permission
denied)
1
2
則說明是權限問題,運行
uwsgi --socket mysite.sock --wsgi-file hello.py --chmod-socket=666
或者
uwsgi --socket mysite.sock --wsgi-file hello.py --chmod-socket=664 (推薦,但是我用了沒成功)
log如果打印了其他問題,請仔細檢查配置~
使用Nginx + Uwsgi運行項目
uwsgi --socket mysite.sock --module mysite.wsgi --chmod-socket=664
1
注意這里有個小坑,mysite_nginx.conf中我配置的mysite.sock在config文件夾下,而這條命令需要在項目根目錄下運行,這里可以:
修改mysite_nginx.conf下sock文件路徑到根目錄下
使用配置文件啟動(推薦)
編輯配置文件
config文件夾下新建mysite_uwsgi.ini,輸入以下內容
# mysite_uwsgi.ini file
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir ? ? ? ? ? = /path/to/your/project
# Django's wsgi file
module ? ? ? ? ?= project.wsgi
# the virtualenv (full path)
home ? ? ? ? ? ?= /path/to/virtualenv
# process-related settings
# master
master ? ? ? ? ?= true
# maximum number of worker processes
processes ? ? ? = 10
# the socket (use the full path to be safe
socket ? ? ? ? ?= /path/to/your/project/mysite.sock
# ... with appropriate permissions - may be needed
chmod-socket ? ?= 666
# clear environment on exit
vacuum ? ? ? ? ?= true
stats ? ? ? ? ? = %(chdir)/config/uwsgi.status ? ? ? ? ??
pidfile ? ? ? ? = %(chdir)/config/uwsgi.pid
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
用自己的路徑替換其中對應的配置~
保存后,運行(確保在mysite_uwsgi.ini目錄下)
uwsgi --ini mysite_uwsgi.ini
打開瀏覽器訪問xx.xx.xxx.xxx:8000
項目這時候應該完美運行了~~
大功告成~:)
---------------------?
作者:泡泡茶壺?
來源:CSDN?
原文:https://blog.csdn.net/breavo_raw/article/details/82665978?
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!
總結
以上是生活随笔為你收集整理的项目部署uwsgi +Nginx的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 人工智能进入下半场
- 下一篇: 【ProjectEuler】Projec