centos7+nginx+python3+django+uwsgi配置

學了一下python3,因而租了阿里雲服務器,玩一玩。後來我才發現玩一玩裝個VirtualBox就夠了:https://ninghao.net/blog/1566,關鍵虛擬機能夠任意裝玩。html

如今開始講講centos7+nginx+python3+django配置,我用的是mac,新手能夠先用VirtualBox練手,不行就刪了從新生成一個虛擬機,把各類步驟都理解熟悉了,再到服務器上實戰。一次性操做完不免有問題,一步一步來。python

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

2.安裝python3:http://www.mamicode.com/info-detail-2019356.htmllinux

3.安裝nginx: http://www.javashuo.com/article/p-nfotbrms-hp.htmlnginx

4.安裝mysql: http://www.javashuo.com/article/p-droqhzjw-hs.html。(這一步若是暫時用不上數據庫也能夠不操做)web

爲了區分開發python版本環境,你能夠用pip3裝一下virtualenv以及virtualenv的管理工具virtualenvwrapper(這步我沒作,顯得麻煩)sql

5.肯定2,3兩步安裝成功了,接下來就用pip3 安裝django 和uwsgi,命令以下:數據庫

pip3 install Django django

pip3 install uwsgivim

若是django 和uwsgi的命令不能用,就這樣操做一下:(不熟悉linux命令能夠先去了解一下,還有vim編輯器的使用)

ln -s /usr/local/python3/bin/django-admin /usr/bin/django-admin  和 ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi(或者uwsgi3(區分版本))

在進行下一步以前建議你看一下這篇博客: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)).

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

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

  def application(env, start_response):

       start_response('200 OK', [('Content-Type','text/html')])

      return "Hello World".encode()

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

  接着在你電腦的瀏覽器上輸入你虛擬機或者服務器的ip:8000,或者ctrl+n新建一個終端,進入你的虛擬機(或者服務器)

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

7.隨便找個乾淨的目錄(/py36_projects/) django-admin startproject pyDemo 新建一個django項目

  cd pyDemo/pyDemo 進入項目裏面 編輯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

py-autoreload = 1 (注意,這句代碼的做用是當你提交代碼時自動重啓uwsgi,和自動同步代碼部署有關)

並保存

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(名字隨便取),裏面寫上:

upstream 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;

並保存

你也能夠在server裏面加上access_log和error_log配置參數,定義nginx訪問日誌和錯誤日誌的存放路徑。 (暫時可不作)

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,像個人就是:120.79.14.122:8008 或者 120.79.14.122:8008/admin,

顯示的頁面分別是:

 

 

 

 

這樣就配置成功了,切記 

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)).

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

相關文章
相關標籤/搜索