Centos7下搭建Django+uWSGI+nginx基於python3

1.電腦環境

Centos7 + python3.6 + virtualenvphp

因爲centos自帶的是python2.7版本,因此要本身安裝新的版本,這裏就不對此描述了,直接開工html

2.具體流程

使用python虛擬環境

1.在本身想要建立虛擬環境的文件夾下建立虛擬環境,咱們的uWSGI也是安裝在虛擬環境中

1 virtualenv Env
2 cd Env
3 cd bin
4 啓動虛擬環境  source activate  (關閉的命令 deactivate)

 

2.啓動以後會看到命令行左邊有括號括起的環境名稱

1 pip3 install uwsgi 安裝uwsgi庫

3.安裝pycharm

1 直接用瀏覽器下載tar.gz包
2 解壓 tar xzvf xxx.tar.gz -C 指定解壓到的目錄
3 進入該目錄
4 cd bin/
5 sh pycharm.sh
就開啓了pycharm

4.建立django項目

由於咱們沒有事先安裝Django,直接用pycharm建立新的Django項目,指定咱們剛纔的虛擬環境pycharm會在這個環境下自動安裝較新版本的Django,若是須要指定版本能夠先用pip安裝到虛擬環境,建立時項目,指定虛擬環境。python

使用manager.py Task 工具測試一下項目,runserver,瀏覽器訪問一下咱們的adminnginx

沒有毛病繼續下一步操做web

5.測試uWSGI,在項目下建立一個test.py文件

1 def application(env, start_response):
2     start_response('200 OK', [('Content-Type','text/html')])
3     return "Hello World"
運行 sudo uwsgi --http 0.0.0.0:8000 --wsgi-file test.py

而後瀏覽 http://127.0.0.1:8000,有」Hello World」輸出即安裝成功。django

說明uWSGI沒有毛病,接下一步centos

6.下載nginx並安裝

1 wget http://nginx.org/download/nginx-1.13.10.tar.gz
2 進入下載目錄,解壓
tar xzvf nginx-1.13.10.tar.gz
3 進入解壓目錄  
cd nginx-1.13.10
4
./configure --prefix=/usr/local/nginx #把全部資源文件放在/usr/local/nginx的路徑中,不會雜亂。
5 make
6 make install

7. 配置uwsgi

uwsgi支持ini、xml等多種配置方式,本文ini爲例, 在項目下新建wifiproject_uwsgi.ini,添加以下配置:瀏覽器

#wifiproject_uwsgi.ini file
[uwsgi]

#與nginx通訊
socket 127.0.0.1:8080

#讓uwsgi做爲單獨的web-server,這裏註釋掉
#http 127.0.0.1:8080

#django項目根目錄
chdir = /home/wei/pythonproject/wifiproject

#wsgi.py在項目中的位置
module  = wifiproject.wsgi

enable-threads = true
#進程數
processes 4
#線成
threads 2
#退出時清空環境變量
vacuum true

#配uWSGI搜索靜態文件目錄,(及django項目下存放的static文件目錄,用uwsgi做爲單獨服務器時才須要設置,此時咱們用nginx處理靜態文件
#check-static = /home/wei/pythonproject/wifiproject

#日誌存儲路徑
daemonize = /home/wei/pythonproject/wifiproject/log/uwsgi.log

8. 配置nginx

進入到 /usr/local/nginx 安裝目錄下服務器

 

  1. 咱們不使用nginx的默認配置運用咱們的項目,
  2. 在項目下建立一個conf文件夾和logs文件夾分別存放配置文件和日誌文件

 

  把這三個文件分別複製拷貝到項目的conf文件夾,也能夠重命名session

 

  而後對nginx.conf配置文件進行設置

 

  1 user  root; #使用root不然靜態文件可能加載失敗
  2 worker_processes  1; #運行nginx工做進程,通常幾個cpu核心就寫ji
  3 
  4 #error_log  logs/error.log;
  5 #error_log  logs/error.log  notice;
  6 #error_log  logs/error.log  info;
  7 
  8 #pid        logs/nginx.pid;
  9 
 10 
 11 events {
 12     worker_connections  1024; #一個進程能同時處理1024個請求
 13 }
 14 
 15 
 16 http {
 17     include       mime.types;
 18     default_type  application/octet-stream;
 19 
 20     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
 21     #                  '$status $body_bytes_sent "$http_referer" '
 22     #                  '"$http_user_agent" "$http_x_forwarded_for"';
 23 
 24     #access_log  logs/access.log  main;
 25 
 26     sendfile        on;
 27     #tcp_nopush     on;
 28 
 29     #keepalive_timeout  0;
 30     keepalive_timeout  65;
 31 
 32     #gzip  on;
 33 #開始配置一個域名,一個server配置通常對應一個域名
 34     server {
 35         listen       8002; #暴露給外部的端口,等於瀏覽器訪問的端口
 36         server_name  localhost; #域名
 37         charset utf-8;
 38         error_log   /home/wei/pythonproject/wifiproject/log/nginx_error.log;
 39         access_log  /home/wei/pythonproject/wifiproject/log/nginx_access.log;
 40 #能夠有多個location
 41         location / {
 42             #root   html; #站點根目錄
 43             #index  index.html index.htm; #索引文件
 44     include  /home/wei/pythonproject/wifiproject/conf/uwsgi_params; #設置將全部請求轉發給uwsgi服務器處理
 45     uwsgi_pass  127.0.0.1:8080; #指定uwsgi的url,與uwsgi通訊
 46 
 47          }
 48 location /static/ {
 49 #設置將/static的靜態請求交給nginx,並指定靜態文件的目錄
 50 
 51 alias  /home/wei/pythonproject/wifiproject/static/;
 52 }
 53 
 54 
 55         #error_page  404              /404.html;
 56 
 57         # redirect server error pages to the static page /50x.html
 58         error_page   500 502 503 504  /50x.html;
 59         #定義頁面錯誤,若是出現這些錯誤,把站點根目錄下的50x.html返回給用戶
 60 location = /50x.html {
 61             root   html;
 62         }
 63 
 64         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
 65         #
 66         #location ~ \.php$ {
 67         #    proxy_pass   http://127.0.0.1;
 68         #}
 69 
 70         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
 71         #
 72         #location ~ \.php$ {
 73         #    root           html;
 74         #    fastcgi_pass   127.0.0.1:9000;
 75         #    fastcgi_index  index.php;
 76         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
 77         #    include        fastcgi_params;
 78         #}
 79 
 80         # deny access to .htaccess files, if Apache's document root
 81         # concurs with nginx's one
 82         #
 83         #location ~ /\.ht {
 84         #    deny  all;
 85         #}
 86     }
 87 
 88 
 89     # another virtual host using mix of IP-, name-, and port-based configuration
 90     #
 91     #server {
 92     #    listen       8000;
 93     #    listen       somename:8080;
 94     #    server_name  somename  alias  another.alias;
 95 
 96     #    location / {
 97     #        root   html;
 98     #        index  index.html index.htm;
 99     #    }
100     #}
101 
102 
103     # HTTPS server
104     #
105     #server {
106     #    listen       443 ssl;
107     #    server_name  localhost;
108 
109     #    ssl_certificate      cert.pem;
110     #    ssl_certificate_key  cert.key;
111 
112     #    ssl_session_cache    shared:SSL:1m;
113     #    ssl_session_timeout  5m;
114 
115     #    ssl_ciphers  HIGH:!aNULL:!MD5;
116     #    ssl_prefer_server_ciphers  on;
117 
118     #    location / {
119     #        root   html;
120     #        index  index.html index.htm;
121     #    }
122     #}
123 
124 }

 

9 建立相關日誌文件

  這樣 就不會在終端打印日誌了

 

10  生產模式下diango設置

  settings.py 中設置debug = False ,Django將不會代管靜態文件,咱們是交給nginx管理,因此要把django的相關靜態文件收集到一個目錄下,並在nginx的配置文件裏指向該目錄

  settings.py 添加 

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

  調用manage命令 collectstatic    就會自動把相關靜態文件收集到上面的路徑裏

11. 使用配置文件開啓 uwsgi

  使用虛擬環境進入項目

  而後運行

  

   若是沒有報錯就開啓了uwsgi

12  在nginx啓動目錄下使用配置文件開啓  nginx 

  

13 使用瀏覽器訪問

相關文章
相關標籤/搜索