阿里雲 centos7 django + uWSGI+Nginx + python3 部署攻略

centos7+nginx+python3+django+uwsgi配置Django 項目部署

 

1.租的服務器(選擇centos)的話,須要在阿里雲後臺控制檯開放幾個端口,克隆一下已開放的端口,tcp自定義就行,mysql(3306),nginx(8000以上都行)。(都切換到root用戶操做)html

2.安裝python3python

3.安裝nginxmysql

4.安裝mysql(這一步若是暫時用不上數據庫也能夠不操做)nginx

5.肯定2,3兩步安裝成功了,接下來就用pip3 安裝django 和uwsgi,web

在進行下一步以前建議你看一下這篇博客:http://blog.csdn.net/c465869935/article/details/53242126sql

還有官方文檔:https://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html(直接搜uwsgi官方文檔(找到Setting up Django and your web server with uWSGI and nginx)).數據庫

6.測試uwsgi可否正常運行:django

  隨便找個乾淨的目錄下vim  test.py,新建一個py文件,在裏面寫上:vim

 def application(env, start_response):
       start_response(‘200 OK‘, [(‘Content-Type‘,‘text/html‘)])
      return "Hello World".encode()

  並保存。而後在當前目錄下執行:uwsgi --http :8000 --wsgi-file test.py。centos

  訪問 http://127.0.0.1:8000,能顯示Hello World說明uwsgi沒問題。

7.隨便找個乾淨的目錄 新建一個django項目

  進入項目裏面 編輯settings.py : vim settings.py ,在裏面加上:

  ALLOWED_HOSTS = [‘test.xq.com‘,‘localhost‘‘127.0.0.1‘,‘虛擬機本身的ip‘,]

  #test.xq.com爲你本身買的域名,暫時能夠不用,用虛擬機本身的ip或者服務器的ip就行

同時加上:

  STATIC_ROOT = os.path.join(BASE_DIR, ‘static‘)

並保存 執行 cd ..切換到pyDemo項目的根目錄,能看到manage.py便可,而後執行:python3 manage.py collectstatic.

在同一目錄下新建uwsgi的配置文件: vim uwsgi.ini ,在裏面寫上:

[uwsgi]

socket = 127.0.0.1:8001

chdir=/py36_projects/pyDemo

module=pyDemo.wsgi

master = true

processes=2

threads=2

max-requests=2000

chmod-socket=664

vacuum=true

daemonize = /py36_projects/pyDemo/uwsgi.log

 

8.剩下的只有配置nginx的配置文件,

  vim /etc/nginx/nginx.conf,進入nginx.conf配置文件看看有沒有下面這句代碼:

  include /etc/nginx/conf.d/*.conf;(意思是導入/etc/nginx/conf.d/下的全部配置文件)

因而咱們只要在/etc/nginx/conf.d/目錄下:

  cd /etc/nginx/conf.d

新建一個conf就行:vim pyDemo.conf(名字隨便取),裏面寫上:

stream django {

    # 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 localhost; # substitute your machine‘s IP address or FQDN

    charset     utf-8;

 

    # max upload size

    client_max_body_size 75M;   # adjust to taste

 

    location /static {

        alias /py36_projects/pyDemo/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     /etc/nginx/uwsgi_params; # the uwsgi_params file you installed

    }

}

 

一些說明:

listen      8000; 阿里雲服務器的話爲上面提到的阿里雲後臺控制檯添加的端口(對外的端口)

server_name localhost; localhost能夠替換你購買的域名(如前面django的配置文件裏的test.xq.com)

/py36_projects/pyDemo/ 是個人項目路徑

uwsgi_pass  django; 裏面的 server 127.0.0.1:8001;和上面的uwsgi.ini配置文件的 socket = 127.0.0.1:8001 的端口一致,這個端口8000以上把(不要和nginx配置的端口相同就行)

 

 

include     /etc/nginx/uwsgi_params;引入/etc/nginx/目錄下的uwsgi_params文件,首先你要到該路徑下看有沒有這個文件,默認是有的,沒有就新建一個並寫上:

uwsgi_param  QUERY_STRING       $query_string;

uwsgi_param  REQUEST_METHOD     $request_method;

uwsgi_param  CONTENT_TYPE       $content_type;

uwsgi_param  CONTENT_LENGTH     $content_length;

 

uwsgi_param  REQUEST_URI        $request_uri;

uwsgi_param  PATH_INFO          $document_uri;

uwsgi_param  DOCUMENT_ROOT      $document_root;

uwsgi_param  SERVER_PROTOCOL    $server_protocol;

uwsgi_param  REQUEST_SCHEME     $scheme;

uwsgi_param  HTTPS              $https if_not_empty;

 

uwsgi_param  REMOTE_ADDR        $remote_addr;

uwsgi_param  REMOTE_PORT        $remote_port;

uwsgi_param  SERVER_PORT        $server_port;

uwsgi_param  SERVER_NAME        $server_name;

9.以上基本配置好了,驗證的準備:

先執行(一步一步來):

pkill -9 uwsgi

pkill -9 nginx

而後切換到項目根目錄:

cd /py36_projects/pyDemo 執行:

uwsgi --ini uwsgi.ini

最後執行:

systemctl start nginx 或者 nginx -c /etc/nginx/nginx.conf(都是啓動nginx的)

10.開始驗證:

打開瀏覽器:輸入你虛擬機或者服務器的 ip:8000,這樣就配置成功了

http://blog.csdn.net/c465869935/article/details/53242126

還有官方文檔:https://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html(直接搜uwsgi官方文檔(找到Setting up Django and your web server with uWSGI and nginx)).

瀏覽這兩篇文章很重要,對你的理解有幫助

相關文章
相關標籤/搜索