1. 程式人生 > >Nginx+Django 部署

Nginx+Django 部署

 

線上的web環境準備用Nginx+Django部署, 下面簡單記錄下操作過程:

作業系統為Centos7.4
[[email protected] ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
 
1)  安裝Python3.6.1
系統預設的是python2.7, 需要升級到Python3.6.1 ( 該Django專案跑在Python3.6.1版本下)
[[email protected] ~]# python -V
Python 2.7.5
 
下載並安裝Python3.6.1
[
[email protected]
~]# yum install -y gcc gcc-c++ [[email protected] ~]# yum -y install epel-release [[email protected] ~]# yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel [[email protected] ~]# cd /usr/local/src/ [[email protected] src]# wget wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tar.xz [
[email protected]
src]# tar xf Python-3.6.1.tar.xz [[email protected] src]# cd Python-3.6.1 [[email protected] Python-3.6.1]# ./configure --prefix=/usr/local/python3 [[email protected] Python-3.6.1]# make && make install 確認安裝的Python下有pip3 [[email protected] Python-3.6.1]# ll /usr/local/python3/bin/python3 lrwxrwxrwx 1 root root 9 12月 11 21:08 /usr/local/python3/bin/python3 -> python3.6 [
[email protected]
Python-3.6.1]# ll /usr/local/python3/bin/pip3 -rwxr-xr-x 1 root root 222 12月 11 21:08 /usr/local/python3/bin/pip3 替換python [[email protected] ~]# cd /usr/bin [[email protected] ~]# mv python python.bak [[email protected] ~]# ln -s /usr/local/python3/bin/python3.6 /usr/bin/python [[email protected] ~]# python -V Python 3.6.1 2) 安裝nginx [[email protected] ~]# useradd www [[email protected] ~]# wget http://10.0.8.50/software/nginx-1.12.2.tar.gz [[email protected] ~]# tar -zvxf nginx-1.12.2.tar.gz [[email protected] ~]# cd nginx-1.12.2 [[email protected] ~]# ./configure --prefix=/data/nginx --user=www --group=www --with-http_ssl_module [[email protected] nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream [[email protected] nginx-1.12.2]# make && make install 配置nginx [[email protected] ~]# cd /usr/local/nginx/conf/ [[email protected]eb-node conf]# cp nginx.conf nginx.conf.bak [[email protected] conf]# cat nginx.conf user www; worker_processes 4; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 65535; } http { include mime.types; default_type application/octet-stream; charset utf-8; ###### ## set access log format ###### log_format main '$http_x_forwarded_for $remote_addr $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_cookie" $host $request_time'; ####### ## http setting ####### sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; proxy_cache_path /var/www/cache levels=1:2 keys_zone=mycache:20m max_size=2048m inactive=60m; proxy_temp_path /var/www/cache/tmp; fastcgi_connect_timeout 3000; fastcgi_send_timeout 3000; fastcgi_read_timeout 3000; fastcgi_buffer_size 256k; fastcgi_buffers 8 256k; fastcgi_busy_buffers_size 256k; fastcgi_temp_file_write_size 256k; fastcgi_intercept_errors on; # client_header_timeout 600s; client_body_timeout 600s; # client_max_body_size 50m; client_max_body_size 100m; client_body_buffer_size 256k; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 9; gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php; gzip_vary on; ## includes vhosts include vhosts/*.conf; } [[email protected] conf]# mkdir /usr/local/nginx/conf/vhosts [[email protected] conf]# mkdir /var/www/cache [[email protected] conf]# ulimit 65535 [[email protected] conf]# cat vhosts/ng.conf upstream py-8001 { ip_hash; server 127.0.0.1:8001 max_fails=3 fail_timeout=15s; server 127.0.0.1:8002 max_fails=3 fail_timeout=15s; } server { listen 80; server_name kevin.com www.kevin.com btc.kevin.com eth.kevin.com; access_log /usr/local/nginx/logs/8001-access.log main; error_log /usr/local/nginx/logs/8001-error.log; location / { proxy_pass http://py-8001; proxy_redirect off ; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_connect_timeout 300; proxy_send_timeout 300; proxy_read_timeout 600; proxy_buffer_size 256k; proxy_buffers 4 256k; proxy_busy_buffers_size 256k; proxy_temp_file_write_size 256k; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_404; proxy_max_temp_file_size 128m; #proxy_cache mycache; #proxy_cache_valid 200 302 1h; #proxy_cache_valid 301 1d; #proxy_cache_valid any 1m; } location /static/ { alias /data/www/APPServer/staticfiles/; } } 3) 安裝django (專案中使用的mysql是阿里雲上的rds) [[email protected] ~]# yum install MySQL-python [[email protected] ~]# pip3 install Django==1.11 [[email protected] ~]# pip3 install pymysql django專案存放路徑: /data/www/APPServer 靜態頁存放路徑: /data/www/APPServer/staticfiles/ 靜態頁通過nginx訪問展示(匹配static的路徑), 動態頁面通過nginx轉發給python處理 [[email protected] script]# ll /data/www/APPServer/ 總用量 32 -rw-r--r-- 1 www www 12288 12月 11 17:55 db.sqlite3 drwxr-xr-x 3 www www 4096 12月 11 22:08 django_auth_example -rwxr-xr-x 1 www www 817 12月 11 17:55 manage.py drwxr-xr-x 3 www www 4096 12月 11 17:55 staticfiles drwxr-xr-x 4 www www 4096 12月 11 17:55 templates drwxr-xr-x 4 www www 4096 12月 11 18:14 users [[email protected] script]# chown -R www.www /data/www/APPServer/ 啟動python程式, 由於是互動式的, 這裡通過tmux工具 [[email protected] ~]# yum install tmux [[email protected] ~]# sh /data/script/py.8001_start.sh 啟動指令碼 [[email protected] ~]# cat /data/script/py.8001_start.sh #!/bin/bash tmux new -d -s bobo_session && tmux send -t bobo_session 'python /data/www/APPServer/manage.py runserver 0.0.0.0:8001' ENTER tmux new -d -s bobo_session1 && tmux send -t bobo1_session 'python /data/www/APPServer/manage.py runserver 0.0.0.0:8002' ENTER 關閉指令碼 [[email protected] ~]# cat /data/script/py.8001_stop.sh #!/bin/bash tmux kill-session -t bobo_session [[email protected] ~]# lsof -i:8001 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME python 11375 root 4u IPv4 35968 0t0 TCP *:vcom-tunnel (LISTEN) [[email protected] ~]# lsof -i:8002 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME python 11399 root 4u IPv4 36203 0t0 TCP *:teradataordbms (LISTEN) [[email protected] ~]# ps -ef|grep tmux root 11356 1 0 00:16 ? 00:00:00 tmux new -d -s bobo_session root 11430 11409 0 00:17 pts/0 00:00:00 tmux a root 11643 11335 0 00:26 pts/3 00:00:00 grep --color=auto tmux [[email protected] ~]# tmux ls bobo1_session: 1 windows (created Wed Dec 12 00:17:14 2018) [179x53] (attached) bobo_session: 1 windows (created Wed Dec 12 00:16:54 2018) [135x33] 訪問http://www.kevin.com 即可

uwsgi是一種線路協議而不是通訊協議,在此常用於在uwsgi伺服器與其他網路伺服器的資料通訊。uwsgi協議是一個uwsgi伺服器自有的協議,它用於定義傳輸資訊的型別。uwsgi實現了WSGI的所有介面,是一個快速、自我修復、開發人員和系統管理員友好的伺服器。uwsgi程式碼完全用C編寫,效率高、效能穩定。

uwsgi作用
Django 是一個 Web 框架,框架的作用在於處理 request 和 reponse,其他的不是框架所關心的內容。所以怎麼部署 Django 不是 Django 所需要關心的。Django 所提供的是一個開發伺服器,這個開發伺服器,沒有經過安全測試,而且使用的是 Python 自帶的 simple HTTPServer 建立的,在安全性和效率上都是不行的而uwsgi 是一個全功能的 HTTP 伺服器,他要做的就是把 HTTP 協議轉化成語言支援的網路協議。比如把 HTTP 協議轉化成 WSGI 協議,讓 Python 可以直接使用。
uwsgi 是一種 uwsgi 的內部協議,使用二進位制方式和其他應用程式進行通訊。

如果使用uwsgi, 則操作如下:

安裝uwsgi
[[email protected] ~]# pip3 install uwsgi

編寫測試:
[[email protected] ~]# cat /root/test.py 
def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"]

啟動測試:
[[email protected] ~]# uwsgi --http :9009 --wsgi-file test.py

訪問http://localhost:9009 , 即可看到Hello World

nginx+uwsgi配置
[[email protected] ~]# ll /usr/local/nginx/conf/uwsgi*
-rw-r--r-- 1 root root 395 12月 11 21:36 /usr/local/nginx/conf/uwsgi.ini
-rw-r--r-- 1 root root 664 12月 11 17:06 /usr/local/nginx/conf/uwsgi_params
-rw-r--r-- 1 root root 664 12月 11 17:06 /usr/local/nginx/conf/uwsgi_params.default

[[email protected] ~]# cat vhosts/ng.conf
server {
  listen 80;
  server_name www.kevin.com btc.kevin.com eth.kevin.com;

  access_log  /usr/local/nginx/logs/8001-access.log main;
  error_log  /usr/local/nginx/logs/8001-error.log;


  location / {
    uwsgi_pass 127.0.0.1:8000;    
    include uwsgi_params;
    access_log off;
  }

 location /static/ {
      alias /data/www/APPServer/staticfiles/;
}

}

[[email protected] ~]# cat /usr/local/nginx/conf/uwsgi.ini 
[uwsgi]
socket = 127.0.0.1:8000 
master = true
vhost = true
no-site = true
pidfile = /usr/local/nginx/logs/uwsgi.pid
processes = 4 
chdir = /data/www/APPServer
wsgi-file = django_auth_example/wsgi.py
profiler = true
memory-report = true
enable-threads = true
logdata = true
limit-as = 6048
daemonize = /usr/local/nginx/logs/django.log
pythonpath = /usr/local/python3/lib/python3.6/site-packages

[[email protected] ~]# ll /data/www/APPServer/
總用量 32
-rw-r--r-- 1 www www 12288 12月 11 17:55 db.sqlite3
drwxr-xr-x 3 www www  4096 12月 11 22:08 django_auth_example
-rwxr-xr-x 1 www www   817 12月 11 17:55 manage.py
drwxr-xr-x 3 www www  4096 12月 11 17:55 staticfiles
drwxr-xr-x 4 www www  4096 12月 11 17:55 templates
drwxr-xr-x 4 www www  4096 12月 11 18:14 users
[[email protected] ~]# ll /data/www/APPServer/django_auth_example/
總用量 32
-rw-r--r-- 1 www www   43 12月 11 17:55 __init__.py
-rw-r--r-- 1 www www  184 12月 11 18:03 __init__.pyc
drwxr-xr-x 2 www www 4096 12月 11 21:12 __pycache__
-rw-r--r-- 1 www www 3802 12月 11 17:55 settings.py
-rw-r--r-- 1 www www 3241 12月 11 18:11 settings.pyc
-rw-r--r-- 1 www www 1282 12月 11 18:14 urls.py
-rw-r--r-- 1 www www 1256 12月 11 18:14 urls.pyc
-rw-r--r-- 1 www www  416 12月 11 17:55 wsgi.py

啟動uwsgi
[[email protected] ~]# uwsgi --ini /usr/local/nginx/conf/uwsgi.ini 
[[email protected] ~]# /usr/local/nginx/sbin/nginx

最後訪問http://www.kevin.com 即可