django+vue+nginx生產環境部署配置


部署環境:

1. linux redhat 7.1

2.python 3.6.3

3. vue

4. nginx

5. gunicorn

6. supervisord

安裝:

一. 基礎環境安裝

1. python3.6.3安裝

下載python3.6.3安裝包,下載地址:https://www.python.org/downloads/release/python-363/

解壓,編譯安裝,不多加贅述,安裝成功后setuptools也會安裝,pip3自行安裝

2. django 環境部署

  如果是gitlab服務器,git clone項目地址到部署服務器,如果不是打包上傳源項目包,解壓安裝依賴環境,如果服務器有連解外網pip3 install -r 依賴文件

  如果服務無外網那么在官網查找下載各個依賴包,通過setuptools安裝,不贅述

3. VUE安裝

  安裝npm, vue,不贅述,具體安裝不會查找教程

二. 其他托管服務,Nginx, gunicorn安裝配置

1. gunicorn安裝配置

  pip3 install gunicorn

  服務器無連接外網請在https://pypi.org/search/?q=gunicorn 源碼包下載安裝

   配置啟動Runit --gunicorn_start.sh 

#!/bin/bash

NAME="projectName"                                   # Name of the application
DJANGODIR=/opt/projectName             # Django project directory
SOCKFILE=/tmp/projectName.sock   # we will communicate using this unix socket
PIDFILE=/opt/projectName/projectName.pidfile
NUM_WORKERS=4                                  # How many worker processes should Gunicorn spawn
DJANGO_SETTINGS_MODULE= projectName.settings.base          # Which settings file should Django use
DJANGO_WSGI_MODULE=projectName.wsgi                  # WSGI module name

echo "Starting $NAME as `whoami`"

# Activate the virtual environment
cd $DJANGODIR

# Start your Django Unicorn
echo "Exec $NAME as `whoami`"
exec gunicorn ${DJANGO_WSGI_MODULE}:application \
  --name $NAME \
  --workers $NUM_WORKERS \
  --bind=unix:$SOCKFILE
  --pid=$PIDFILE
  --log-level=debug \
  --log-file=/opt/projectName/gunicorn.log \
  --access-logfile=/opt/projectName/access.log \
  --worker-class=gevent \
  --max-requests 500
  "$@"

 增加X權限, sudo chmod +x gunicorn_start.sh 

試運行

2. supervisord服務安裝配置,將gunicorn托管

下載supervisord,安裝,安裝成功后,echo_supervisord_conf > /etc/supervisord.conf 生成配置文件

配置文件更改:

# dbmanager.conf
[program:dbmanager]
command = /opt/projectName/gunicorn-start.sh                    ; Start app
stdout_logfile = /var/log/supervisor/gunicorn-supervisor.log  ; Where to write log messages
redirect_stderr = true
autostart = true
autorestart = true
stderr_logfile = /var/log/supervisor/gunicorn-supervisor.err.log

;停止信號
stopsignal=INT

3. nginx安裝配置

下載nginx ,pcre:

curl -C - -O http://mirrors.sohu.com/nginx/nginx-1.13.7.tar.gz
curl -C - -O https://ftp.pcre.org/pub/pcre/pcre-8.41.tar.gz

安裝pcre

tar xf pcre-8.41.tar.gz -C /usr/local/src/
# 編譯
./configure --prefix=/opt/pcre make
&& make install

安裝nginx

tar xf nginx-1.13.7.tar.gz -C /usr/local/src/
# 編譯
./configure --prefix=/opt/nginx --with-pcre --with-http_ssl_module --user=nginx --group=nginx --with-threads --with-stream --with-stream_ssl_module --with-http_v2_module --with-http_gunzip_module

make && make install 

修改配置文件

vi /opt/nginx/conf/nginx.conf

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 {
    accept_mutex on;
    multi_accept on;
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
        access_log /var/log/nginx/access.log combined;

        upstream app_server {
    # for UNIX domain socket setups
    server unix:/tmp/gunicorn.sock fail_timeout=0;
  }
  
    server {
        charset utf-8;
        listen       9000;
        gzip on;
        gzip_min_length  1k;
        gzip_buffers     4 16k;
        gzip_http_version 1.0;
        gzip_comp_level 2;
        gzip_types  text/plain application/x-javascript text/css application/xml;
        gzip_vary on;
        client_max_body_size 300m;
        server_name  localhost;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        # 后端接口轉發
    # /apiv1 對應后台接口URL
        location /apiv1 {
                proxy_pass http://unix:/tmp/projectName.sock;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_read_timeout 300;
                proxy_connect_timeout 300;
                proxy_redirect off;
        }

        #  前端vue轉發
        location / {
                try_files $uri $uri/ @router;
                index index.html;
        }
        root /opt/projectName/front/dist; #vue打包的文件目錄
        #index login.html;
        location @router {
                rewrite ^.*$ /index.html last;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
             root   html;
         }
    }
}

測試配置文件:

/opt/nginx/sbin/nginx -t

無問題,啟動服務: /opt/nginx/sbin/nginx

啟動supervisord : sudo supervisord -c /etc/supervisord.conf

放通nginx端口:

/sbin/iptables -I INPUT -p tcp --dport 9000 -j ACCEPT

訪問


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM