天天看點

centos7下将本地django項目通過nginx+uwsgi部署上線

安裝思路整理

  1. centos系統自帶python2,但是它不是我們需要的版本。(一定不要移除系統自帶的python2,yum依賴的就是python2)安裝python3,并且設定軟連接配接
  2. 安裝nginx、uwsgi、virtualenv等項目依賴
  3. 關聯uwsgi、nginx檔案
  4. 踩過的坑(我還記得的)

一、編譯安裝python3(我自己的安裝方法我忘記了,但是網上有很詳細的。我隻是貼出一個我覺得講的很好的)

#為centos系統增加編譯功能:
yum -y install gcc gcc-c++

#防止編譯安裝python3出現各種異常:
yum install wget openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel

#下載下傳python3安裝包:
cd /home/<username>/Downloads/ #<username>用自己的使用者名代替
wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz

#解壓:
tar -zxvf Python-3.6.3.tgz

#配置,将python3安裝到/usr/local/python3/路徑下:
cd Python-3.6.3
./configure --prefix=/usr/local/python3

#編譯安裝:
make -j2
make install -j2

#建立軟連結,友善在終端中直接使用python3和pip3指令:
ln -s /usr/bin/python /usr/bin/python2   #将原來的版本移動到python2
ln -s /usr/local/python3/bin/python3.6 /usr/bin/python
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip  #如果之前有pip就和python2轉移方法一樣

#安裝成功性測試,顯示相應版本就表示成功了:
python -V
pip -V
           

二、安裝虛拟環境,為項目安裝所有依賴

#更新pip3至最新版本
pip install --upgrade pip

#安裝virtualenv
pip install virtualenv

ln -s /usr/local/python3/lib/python3.6/site-packages/virtualenv.py /usr/bin/virtualenv

#進入你自己想要進去的目錄下
cd /XXX
mkdir XXX #建立一個存放的目錄

cd XXX

virtualenv -p /usr/bin/python3 venv  #-p是為了指定python版本

#激活虛拟環境
source /你的虛拟環境路徑/bin/activate

#退出虛拟環境
deactivate


pip install uwsgi 

#添加nginx存儲庫
yum install epel-release
#安裝nginx
yum install nginx

pip install -r requirements.txt安裝  (pip freeze >requirements.txt收集依賴)
           

三、測試uwsgi

建立一個名為test.py檔案

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

運作uwsgi

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

當浏覽器通路8000端口時, 可看到hello world

curl http://127.0.0.1:8000
           

安裝成功就開始項目的配置,在項目目錄下添加wsgi.ini。下面是我的檔案,具體參數什麼作用可以去看官網

[uwsgi]
#the local unix socket file than commnuincate to Nginx
socket = :8001
# the base directory (full path)
chdir = /XXX  #項目目錄

# the virtualenv (full path)
home = /XXX  #虛拟環境目錄

# master
master = true

# Django's wsgi file
wsgi-file = XXX/wsgi.py  #這個是項目自帶的
# maximum number of worker processes
processes = 4
#thread numbers startched in each worker process
threads = 2
 
module = 你的項目名字.wsgi:application

deamonize = /XXX/log/uwsgi.log
logto = /XXX/log/uwsgi.log
disable-logging =  true

# clear environment on exit
vacuum   = true

#Set the internal cache size for uwsgi package parsing. The default is 4k.
buffer-size = 65536

post-buffering = 32768

#Unix socket is a file, so it will be restricted by the Unix system.
chmod-socket = 755


stats=%(chdir)/uwsgi/uwsgi.status

pidfile=%(chdir)/uwsgi/uwsgi.pid
           

四、配置nginx

主配置檔案/etc/nginx/nginx.conf ,我為了友善寫就直接貼在這裡了。但是最好是建立一個單獨的檔案

upstream django {
        # server unix:///path/to/your/mysite/mysite.sock; # for a file socket
        server 127.0.0.1:80; # for a web port socket (we'll use this first)
     }

#OW/// 
     server {
         listen       80;
         server_name  www.XXX.com XXX.com;
         #server_name  www.XXX.com
         root         /usr/share/nginx/html;
         client_max_body_size 75M;  #上傳檔案大小限制
         charset utf-8;
         # Load configuration files for the default server block.
         include /etc/nginx/default.d/*.conf;
 
         location / {
               uwsgi_pass  django;
               proxy_set_header Host $host;
               proxy_set_header X-Forwarded-Host $server_name;
               uwsgi_read_timeout 2;
               root html;
               index index.html index.htm;
               include /etc/nginx/uwsgi_params;
               #add_header Cache-Control public;
               #設定不緩存
               add_header Cache-Control no-cache;
               add_header Pragma no-cache;
               add_header Expires 0;
         }
         location /static{
              # expires 30d;
               add_header Cache-Control no-store;
               autoindex on;
               alias /var/XXX/static;
         }
         location /media{
              alias /var/XXX/media;
         }
 
         error_page 404 /404.html;
             location = /40x.html {
         }
 
         error_page 500 502 503 504 /50x.html;
             location = /50x.html {
         }
     }

                     
           

注意:

 include /usr/share/nginx/modules/*.conf;   #添加這兩行

  include /etc/nginx/sites-enabled/*;

将setting.py檔案中

#DEBUG = True
DEBUG = False
ALLOWED_HOSTS = [ ] #修改前
ALLOWED_HOSTS = ['*'] #修改後或者改成你的域名和外網ip更為嚴謹
           

指令彙總

source /usr/XXX/activate  激活虛拟環境
systemctl restart nginx 運作nginx服務
netstat -lnp|grep 80 檢視是否再成功運作
uwsgi -d --init  /var/XXX/wsgi.ini
ps aux | grep uwsgi  檢視是否再成功運作
netstat -lntp監聽端口
 vim /etc/nginx/nginx.conf配置檔案 
cat /var/log/nginx/error.log   檢視錯誤日志 
netstat -apn|grep 80
kill -9 <pid>
           

還有一些細節我有些想不起了,讓我想起來了我再回來添加吧

五、踩過的坑

[error] 1214#0: *64 upstream prematurely closed connection while reading response header from upstream, client:

#[error] 1214#0: *64 upstream prematurely closed connection while reading response header from upstream, client:
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 256k;         #以下四個參數已加大,如果設定太小也會出現timeout 504
fastcgi_buffers 16 256k;
fastcgi_busy_buffers_size 512k;
fastcgi_temp_file_write_size 512k;

           

403 forbiddenYou don't have permission to access / on this server.

為項目目錄添權重限。

參考資料:

1、  https://uwsgi-docs-zh.readthedocs.io/zh_CN/latest/tutorials/Django_and_nginx.html官網

2、 https://www.zhihu.com/question/22850801/answer/656762026  程式員向東的那個回答

3、https://zhuanlan.zhihu.com/p/49200125

如果有不對的地方,歡迎在評論指出