站长

阿里云ubuntu django+vue+nginx生产环境部署配置

阿里云ubuntu 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

访问

这个网上教程可以 用,留着备用

阿里云ubuntu django+vue+nginx生产环境部署配置已关闭评论