背景:已經有了正常的工作的django項目,現在需要配置uwsgi + nginx。
一 安裝配置uwsgi
直接安裝pip install uwsgi會報錯如下:
Command /usr/bin/python -c "import setuptools, tokenize;__file__='/tmp/pip_build_aa/uwsgi/setup.py';
exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))
" install --record /tmp/pip-mjkYEB-record/install-record.txt --single-version-externally-managed --comp
ile failed with error code 1 in /tmp/pip_build_aa/uwsgi
Storing debug log for failure in /home/aa/.pip/pip.log
需要安裝python-dev。
-
sudo apt-get install python-dev
-
sudo pip install uwsgi
在django項目根目錄下創建uwsgi.ini
[uwsgi]
socket = :8000
#django 目錄
chdir = /home/user/www/django項目
#Django 項目下 wsgi.py 文件路徑
module = myweb.wsgi
master = true
#進程數
processes = 4
vacuum = true
#輸出日誌
daemonize = /srv/script/uwsgi.log
#主進程pid
pidfile = /srv/script/uwsgi.pid
#配置python安裝路徑
pythonpath = /srv/JMS/src/venv/local/lib/python2.7/site-packages
啓動uwsgi : uwsgi --ini uwsgi.ini (在django項目根目錄 ,不然需要給出uwsgi完全路徑。)
停止可以採用uwsgi --stop /srv/script/uwsgi.pid,如果不好使直接殺掉uwsgi:pkill -f uwsgi -9
在這一步可以做個測試,看uwsgi是否正常工作,把上面socket換成http,測試是否能夠正常訪問django網站(127.0.0.1:8000此時網站只能加載不出如js,css等靜態文件,但是鏈接是可以跳轉的。)
二 安裝配置nginx
sudo apt-get install nginx(另外通過編譯安裝也可以,我沒有研究)
(如果出現404的錯誤需要更新一下 sudo apt-get update)
這種把nginx安裝在/etc/nginx這個位置。
在/etc/nginx/conf.d下創建文件 你項目名.conf 的配置文件,配置如下:
server {
2 listen 8099; #監聽端口
3 server_name xxx.xxx.xx.xxx; #本機ip,這是個坑,我就在這兒浪費了半天,想死
4 charset UTF-8; #編碼
5 access_log /var/log/nginx/myweb_access.log; #輸出日誌
6 error_log /var/log/nginx/myweb_error.log;
7
8 client_max_body_size 75M;
9
10 location / {
11 include uwsgi_params;
12 uwsgi_read_timeout 30;
13 uwsgi_pass 127.0.0.1:8000; #這兒要跟你uwsgi配置的一直,坑!!
14 }
15
16 location /static { #靜態文件位置
17 alias /項目位置根/static/;
18
19 }
20
21 location /media {
22 alias /項目位置根/media/;
23 }
24 location /admin/{ #只需這個ip可以訪問admin後臺
25 allow 192.168.10.100;
26 deny all;
27 }
28 }
配置項目setting文件
from django.conf.urls import url
from django.views.static import serve
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
urlpatterns={
url(r'^media/(?P<path>.*)', serve, {'document_root': MEDIA_ROOT}),
url(r'^static/(?P<path>.*)$', serve, {'document_root': STATIC_ROOT}),
}
STATICFILES_FINDERS = (
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder"
)
把之前配置的靜態文件存放設置去掉。
到這一步還是不能加載靜態文件,需要將項目中的靜態文件統一複製以到在conf配置的靜態文件位置,只需使用這個命令即可:python manage.py collectstatic
此時配置完成,需要啓動uwsgi和nginx:
uwsgi --ini /srv/SEAP/uwsgi.ini &
sudo nginx
此時可以查看進程情況: ps -ef |grep uwsgi ps -ef |grep nginx
我的配合的是4進程的,所以應該看到
如果只有最下面一條,說明沒有啓動成功,查看一下是哪裏出錯了。
下面貼一些用的着的命令
啓動uwsgi: uwsgi --ini uwsgi.ini
強制殺死uwsgi: pkill -f uwsgi -9
nginx啓動: sudo nginx
nginx停止:sudo nginx -s stop
更新static :python manage.py collectstatic
啓動
uwsgi --ini /srv/SEAP/uwsgi.ini &
sudo nginx
重啓
uwsgi --reload /srv/script/uwsgi.pid
nginx -s reload
停止
uwsgi --stop /srv/script/uwsgi.pid
nginx -s stop
前車之鑑,後車之師。