ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

django+vue+nginx生产环境部署配置

2019-02-27 10:51:11  阅读:402  来源: 互联网

标签:opt vue gunicorn projectName django nginx 安装 log


部署环境:

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

访问

标签:opt,vue,gunicorn,projectName,django,nginx,安装,log
来源: https://www.cnblogs.com/jl-bai/p/10438864.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有