mac+django(1.8.2)+uwsgi+nginx 部署

一. uwsgihtml

  • 安裝
  • 檢驗
  • 配置uwsgi.ini

 

1. 安裝python

pip3 install uwsgi

 

2. 檢驗nginx

  • 方法一(uwsgi啓動文件):
    • test.py內容以下:
    • def application(env, start_response):
        start_response('200 OK', [('Content-Type','text/html')])
          return [b"Hello World"] 
    • uwsgi --http 0.0.0.0:8000 --wsgi-file testTask/tests.py
    • 打開瀏覽器http://127.0.0.1:8000/
  • 方法二(uwsgi啓動django項目):
    • uwsgi --http :8000 --file dj_test/wsgi.py 
    • 若是部署項目uwsgi+django,到這裏就結束了,若是須要配合nginx,再接着往下看。

 

3. 配置uwsgi.inidjango

  • 同django的manage.py爲同一級目錄建立uwsgi.ini
  • [uwsgi]
    http = :9000
    socket = 127.0.0.1:8001
    chdir = /Users/conan/vir_env/AutoTest
    wsgi-file = /Users/conan/vir_env/AutoTest/AutoTest/wsgi.py
    module = AutoTest.wsgi:application
    master = true
    processes = 4
    threads = 2
    chmod-socket=664
    vacuum=true
    daemonize = /Users/conan/vir_env/AutoTest/uwsgi.log
    參數解釋:
      • http                    用戶訪問端口
      • chdir 爲django     項目路徑,該路徑下面有manage.py 文件
      • wsgi-file django   自帶的wsgi.py 文件
      • socket 8001         與下面要配置的nginx的端口要保持一致, 由於uwsgi與nginx 經過socket協議8001端口通訊

 

 

二. nginx瀏覽器

  • 安裝
  • 配置nginx.conf
  • 啓動,重啓,關閉

 

1. 安裝app

brew install nginx
nginx -v # 查看安裝版本號
nginx -t # 安裝路徑

2. 配置nginx.confsocket

  • 同django的manage.py爲同一級目錄建立nginx.conf
  • worker_processes  1;
    
    events {
        worker_connections  1024;
    }
    
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
        server {
            listen 80;
            server_name 127.0.0.1;
            charset     utf-8;
            access_log      /Users/zd/Documents/AutoTest/nginx_access.log;
            error_log       /Users/zd/Documents/AutoTest/nginx_error.log;
            client_max_body_size 75M;
    
    
            location /static {
                alias /Users/zd/Documents/AutoTest/static;
            }
    
            location / {
                root        /Users/zd/Documents/AutoTest;
                include     /usr/local/etc/nginx/uwsgi_params;
                uwsgi_pass  127.0.0.1:8001;
            }
        }
             
        include servers/*;
    }

     

3. 啓動,重啓,關閉url

# 找nginx 安裝路徑
which nginx
cd /usr/local/bin/nginx

#重啓
nginx -s reload

#關閉
nginx -s stop.  或者  kill -9 nginx 

#是否關閉
ps aux|grep nginx

 

三. uwsgi+nginx 啓動Django項目spa

1 nginx -t
2 cd /usr/local/etc/nginx
3 把mime.types 文件copy到django 項目與manage.py 同一級
1 uwsgi --ini uwsgi.ini 
2 sudo nginx -c nginx.conf  #根據報錯信息,缺啥文件補充啥,補完以後,重複該命令

瀏覽器訪問:127.0.0.1:9000/index/code

 

四. 加載靜態資源

在Django setting.py里加

STATIC_ROOT = os.path.join(BASE_DIR, 'static') 
終端執行命令
python3 manage.py collectstatic #收集靜態文件
url.py(例如:AutoTest/AutoTest/url.py)
1 from django.conf import settings
2 urlpatterns = [
3     url(r'^admin/', include(admin.site.urls)),
4     url(r'', include('testTask.urls')),
5     url(r'^static/(?P<path>.*)$', 'django.views.static.serve', { 'document_root': settings.STATIC_ROOT,}),
8 ]
相關文章
相關標籤/搜索