第一篇博客,不是很懂語法之類的,希望通過多寫點東西,記錄自己的成長,早點成為一個pyer.
就寫下這兩天折騰的這個nginx-uwsgi-django.
首先附上官方文檔鏈接 http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html
環境 python == Python 2.7.3, os == centos6.5 x86_64
django == 1.8.1
uwsgi == uWSGI 2.0.15 (64bit) #都是pip 直接安裝的
nginx == 1.10.1 #yum install -y
1.剛剛創建的project
$django-admin startproject test_nginx_uwsgi $cd test_nginx_uwsgi/ $tree . ├── manage.py └── test_nginx_uwsgi ├── __init__.py ├── settings.py ├── urls.py └── wsgi.py
2.當前目錄下創建test.py文件,代碼如下:
#test.py def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return "Hello World" #國際慣例,helloworld
運行代碼:
$uwsgi --http :9111 --wsgi-file test.py ##中間東西很多,又看不懂,亂七八糟的,我就不貼了## *** uWSGI is running in multiple interpreter mode *** spawned uWSGI worker 1 (and the only) (pid: 19381, cores: 1)
參數含義簡介:
- :9111 > 想必大家都知道,運行的端口
- wsgi-file > 指定加載的文件
- 報錯[uwsgi: command not found]的話,ln -s /your-python-dir/bin/* /usr/bin/*
如圖所示,第一步完成!
3.測試您的django 是否能夠正常運行!ps:為了方便起見我們直接用admin網站測試好了~~
$python manage.py syncdb #創建superuser $python manage.py makemigration $python manage.py migrate $python manage.py runserver 0.0.0.0:9111
大家有目共睹
4.使用Uwsgi 跑 django項目
cmd: (其中test_nginx_uwsgi.wsgi指的就是test_nginx_uwsgi目錄下的wsgi.py,django1.8自動生成)
$ uwsgi --http :9111 --module test_nginx_uwsgi.wsgi
又成功了一步~可以對比上面的,同樣的url,這里卻很難看,是因為沒有加載static文件
但是可以說明 web-client <-> Uwsgi <-> Django 是連通的,下面的就要看Nginx.
5.配置Nginx.
由於版本的問題,如果這里也用官方的源碼就不行了。
- 收集靜態文件
#首先setting.py中添加 STATIC_ROOT = '/root/django/test_nginx_uwsgi/static_root/'
------------------------------我是換行------------------------------
$python manage.py collectstatic You have requested to collect static files at the destination location as specified in your settings: /root/django/test_nginx_uwsgi/static_root This will overwrite existing files! Are you sure you want to do this? Type 'yes' to continue, or 'no' to cancel: yes #·······省略~ 62 static files copied to '/root/django/test_nginx_uwsgi/static_root'. - nginx配置文件在 /etc/nginx/nginx.conf ,其中include -> /etc/nginx/conf.d/*.conf [這個,相信大多數linux服務如出一轍]
- vi /etc/nginx/conf.d/test_nginx_uwsgi.conf,代碼如下
# test_nginx_uwsgi.conf # the upstream component nginx needs to connect to upstream django_test { #加上_test,因為和原來的沖突了,這里備注下 # server unix:///path/to/your/mysite/mysite.sock; # for a file socket server 127.0.0.1:9111; # for a web port socket (we'll use this first) 類似uwsgi端口 } # configuration of the server server { # the port your site will be served on listen 8090; #nginx 運行端口 # the domain name it will serve for server_name .example.com; # 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 { alias /root/django/test_nginx_uwsgi/media; # 加載你的meida, } location /static { alias /root/django/test_nginx_uwsgi/static_root; # 加載你的靜態文件 } # Finally, send all non-media requests to the Django server. location / { uwsgi_pass django_test; include /etc/nginx/uwsgi_params; #官方說要把文件cp到項目目錄,感覺沒必要 } }
- 重啟 nginx (不知道是我這機器有毛病還是····,光restart不行)
[root@lzy test_nginx_uwsgi]# /etc/init.d/nginx restart stopping nginx.... Done. starting nginx.. [root@lzy test_nginx_uwsgi]# nginx -c /etc/nginx/nginx.conf [root@lzy test_nginx_uwsgi]# nginx -s reload [root@lzy test_nginx_uwsgi]# lsof -i :8090 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME nginx 1354 root 9u IPv4 87392561 0t0 TCP *:8090 (LISTEN) nginx 1383 root 9u IPv4 87392561 0t0 TCP *:8090 (LISTEN) nginx 1384 root 9u IPv4 87392561 0t0 TCP *:8090 (LISTEN) nginx 1386 root 9u IPv4 87392561 0t0 TCP *:8090 (LISTEN) nginx 1387 root 9u IPv4 87392561 0t0 TCP *:8090 (LISTEN) nginx 1388 root 9u IPv4 87392561 0t0 TCP *:8090 (LISTEN) nginx 1390 root 9u IPv4 87392561 0t0 TCP *:8090 (LISTEN) nginx 1391 root 9u IPv4 87392561 0t0 TCP *:8090 (LISTEN) nginx 1392 root 9u IPv4 87392561 0t0 TCP *:8090 (LISTEN) nginx 1393 root 9u IPv4 87392561 0t0 TCP *:8090 (LISTEN) nginx 1398 root 9u IPv4 87392561 0t0 TCP *:8090 (LISTEN) nginx 1399 root 9u IPv4 87392561 0t0 TCP *:8090 (LISTEN) nginx 1400 root 9u IPv4 87392561 0t0 TCP *:8090 (LISTEN) nginx 1401 root 9u IPv4 87392561 0t0 TCP *:8090 (LISTEN) nginx 1402 root 9u IPv4 87392561 0t0 TCP *:8090 (LISTEN) nginx 1403 root 9u IPv4 87392561 0t0 TCP *:8090 (LISTEN) nginx 1404 root 9u IPv4 87392561 0t0 TCP *:8090 (LISTEN) #可以看到8090端口已經在運行 nginx 了,說明配置文件有效果了
6.接下來官網Balala了一堆,我這種fish壓根不懂,我也沒按他的做,跳過這一步··
7.配置test_nginx_uwsgi_uwsgi.ini 啟動配置文件
ps:參考了http://www.runoob.com/django/django-nginx-uwsgi.html
創建test_nginx_uwsgi_uwsgi.ini 就在項目根目錄
#test_nginx_uwsgi.ini file [uwsgi] # Django-related settings socket = :9111 # the base directory (full path) chdir = /root/django/test_nginx_uwsgi # Django s wsgi file module = test_nginx_uwsgi.wsgi # process-related settings # master master = true # maximum number of worker processes processes = 4 # ... with appropriate permissions - may be needed chmod-socket = 664 # clear environment on exit vacuum = true
8.配置完成,就剩下啟動了
啟動就很簡單呢
#首先啟動uwsgi 指定配置文件ini $uwsgi --ini test_nginx_uwsgi.ini #也可以 nohup uwsgi --ini test_nginx_uwsgi.ini & 后台執行,無輸出 #其次重啟Nginx,參照上面重啟步驟 #瀏覽器打開 192.168.8.199:8090/admin
大功告成~現在就是通過nginx端口打開的django,如果nginx Listen 80,那么瀏覽器不用輸端口也行呢
#error: /var/log/nginx/nginx.log提示 13 Pemmer deied【權限問題】,修改/etc/nginx/nginx.conf,user nginx -> user root