[ Django+vue3+uwsgi+nginx+https本地部署 ]


1. vue3打包

会在项目目录下生成一个dist文件夹, 直接挂载到nginx中就行

npm run build

2. https本地版本配置

  1. openssl genrsa -des3 -out server.key 2048
  2. openssl req -new -key server.key -out server.csr 说明:需要依次输入国家,地区,城市,组织,组织单位,Common Name和Email。其中Common Name,可以写自己的名字或者域名,如果要支持https,Common Name应该与域名保持一致,否则会引起浏览器警告。
  3. openssl rsa -in server.key -out server.key
  4. openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
  5. openssl x509 -in server.crt -out server.pem -outform PEM
  6. /etc/hosts 添加 127.0.0.1 chen.haifeng.com

3. nginx配置文件

default.conf 需要匹配vue3里面的路由需要配置 - try_files $uri $uri/ @router; - rewrite ^.*$ /index.html last;

server {
    listen       443 ssl;
    #listen  [::]:443;
    server_name  chen.haifeng.com;

    ssl_certificate      /cert/server.crt;
    ssl_certificate_key  /cert/server.key;

    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout 5m;   
    ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
    ssl_prefer_server_ciphers on;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html/vue;
        try_files $uri $uri/ @router;
        index  index.html index.htm;
    }
    location @router {
        rewrite ^.*$ /index.html last;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}

}
server {
    listen 5000;
    server_name localhost;
    location / {
        uwsgi_pass 192.168.50.213:8000;
        include /etc/nginx/uwsgi_params;
    }
    location /static/ {
        expires max;
        root /code/django_test_01/collectedstatic;
    }
}

server {
    listen 80;
    server_name chen.haifeng.com;
    rewrite ^(.*)$ https://chen.haifeng.com permanent;
    root /usr/share/nginx/html/vue;
    index index.html index.htm;
}

3. docker-compose.yml

version: '3'

services:
  django_test_01:
    build: .
    restart: always
    command: bash -c 'sh /code/django_test_01/bin/docker_start.sh'
    ports:
      - "8000:8000"
    volumes:
      - ./collectedstatic:/code/django_test_01/collectedstatic
    container_name: django_test_01
  nginx:
    image: nginx
    restart: always
    ports:
      - "443:443"
      - "5000:5000"
    volumes:
      - ../conf/default.conf:/etc/nginx/conf.d/default.conf
      - ../vue-test/dist/:/usr/share/nginx/html/vue/
      - ../conf/cert/:/cert/
      - ./collectedstatic:/code/django_test_01/collectedstatic
    links:
      - django_test_01:django_test_01
    container_name: nginx

4. dockerfile

FROM python:3
ENV PYTHONUNBUFFERED 1

WORKDIR /code/django_test_01/

RUN apt-get install default-libmysqlclient-dev -y && \
    ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

ADD requirements.txt requirements.txt

RUN pip install --upgrade pip && \
        pip install -Ur requirements.txt && \
        pip install uwsgi && \
        pip cache purge

ADD . .
RUN chmod +x /code/django_test_01/bin/docker_start.sh
ENTRYPOINT ["/code/django_test_01/bin/docker_start.sh"]

5. docker_start.sh

#!/usr/bin/env bash
NAME="django_test_01"
DJANGODIR=/code/django_test_01

echo "Starting $NAME as `whoami`"

cd $DJANGODIR

export PYTHONPATH=$DJANGODIR:$PYTHONPATH

python manage.py makemigrations && \
  python manage.py migrate && \
  python manage.py collectstatic --noinput && \

exec uwsgi --socket 0.0.0.0:8000 --chdir $DJANGODIR --wsgi-file $NAME/wsgi.py --master --processes 4 --threads 2

uwsgi.ini配置(暂时用不上)

module=xxx.wsgi
socket = 0.0.0.0:8000
chdir = /xxx/xxx
wsgi-file = xxx/wsgi.py
processes = 4
threads = 4
max-request = 20480
chmod-socket = 666
vacuum = true
master=True
pidfile=/xxx/xxx/uwsgi_web.pid
daemonize=/xxx/xxx/uwsgi_web.log

6. settings.py

STATIC_ROOT = os.path.join(BASE_DIR, 'collectedstatic')
STATIC_URL = '/static/'
STATICFILES = os.path.join(BASE_DIR, 'static')

运行

docker compose up -d