1. 程式人生 > >nginx + uwsgi 釋出django專案!(linux為centos7)

nginx + uwsgi 釋出django專案!(linux為centos7)

1.在linux上安裝python3,參考連結:https://www.cnblogs.com/kimyeee/p/7250560.html

安裝python3時候,建議不要更改預設的python ,,預設python指向python2,,linux有一些東西需要用到,,比如yum,,如果將python預設指向python3,,這些東西將不可用

2.安裝django,直接pip3 install django

3.安裝uwsgi:

安裝依賴:yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel

安裝uwsgi:pip3 install uwsgi

檢視版本:uwsgi --version

測試:

準備測試檔案test.py,程式碼如下:

def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    #return ["Hello World"] # python2
    return [b"Hello World"] # python3

將檔案上傳linux ,使用lrzsz , 安裝lrzsz:yum install lrzsz

 然後,執行 uWSGI:

uwsgi --http :8000 --wsgi-file test.py 

開啟下面url,瀏覽器上應該顯示hello world

curl http://127.0.0.1:8000 

效果展示:

4.連結uwsgi和django

將自己的django專案上傳伺服器,安裝專案中需要的模組

進入上傳的專案資料夾 與manage.py同目錄執行:

uwsgi --http :8000 --wsgi-file appname/wsgi.py #appname為你自己的目錄

在其他視窗開啟連線:http://127.0.0.1:8000

效果:

出來顯示的是首頁前段程式碼

這證明上邊的步驟沒問題

5.安裝配置nginx

(1)安裝

            wget http://nginx.org/download/nginx-1.9.5.tar.gz

            tar xf nginx-1.9.5.tar.gz

            cd nginx-1.9.5

            ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_gzip_static_module

            make && make install

            或者參考

            http://cantgis.blog.51cto.com/5788192/1540004

(2)啟動:進入安裝的nginx目錄下

 /usr/local/nginx/sbin/

啟動:

./nginx  start

重啟:

./nginx -s reload

啟動好之後在我們電腦的瀏覽器輸入伺服器ip會顯示nginx頁面,如圖:

6.使用uwsgi的配置檔案執行django

#user root nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    include uwsgi_params;#沒有這個會報錯在uwsgi錯誤日誌裡:self.method = environ['REQUEST_METHOD'].upper();KeyError: 'REQUEST_METHOD'
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
       #server_name  localhost;
    server_name  對外訪問ip;
    access_log /www/www/access.log;
        error_log /www/www/error.log;
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        uwsgi_pass     127.0.0.1:8088;#和uwsgi埠對應
            uwsgi_read_timeout 300;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        #error_page   500 502 503 504  /50x.html;
        #location = /50x.html {
        #    root   html;
        #}
    location /static/ {
            alias  /www/www/webJB/webjb/static/;
            index  index.html index.htm;
        }



}

新建一個my_uwsgi.ini ,內容如下:

[uwsgi]
#http = :9000
#the local unix socket file than commnuincate to Nginx
socket = 127.0.0.1:8088
# the base directory (full path)
chdir = /www/www/webJB/webjb
# Django's wsgi file
wsgi-file = webjb/wsgi.py
# maximum number of worker processes
processes = 4
#thread numbers startched in each worker process
threads = 2

vacuum = true   
##monitor uwsgi status
#stats =127.0.0.1:9191
## clear environment on exit
#vacuum          = true
#stopsignal = QUIT


daemonize = /www/www/webJB/uwsgi.log

#stats=/root/uwsgi/uwsgi.status
#pidfile=/root/uwsgi/uwsgi.pid

將檔案放入manage.py同目錄下

執行:uwsgi --ini my_uwsgi.ini

如圖:

即可,

重啟nginx,訪問域名,可以了