一、项目简介
1、项目架构:django2+python3+vue框架
2、项目部署方式:centos7+nginx+python3+uwsgi+node.js
二、部署过程
1、下载项目源码到centos上根目录
2、创建虚拟环境并激活
安装命令: pip3 install virtualenv
创建命令: virtualenv --python=/usr/bin/python3 my_env(举例)
激活环境:进入虚拟环境bin目录执行命令source activate激活
退出环境:进入虚拟环境bin目录执行命令deactivate 退出
3、创建项目目录并进入目录
cd /usr/local/api_automation_test-master
剪切项目到该目录下并解压
安装项目依赖
pip install -r requirements.txt (项目已生成该依赖文件或者根据项目pip freeze生成到txt文件中)
pip install https://github.com/darklow/django-suit/tarball/v2(安装django-suit依赖包)
4、安装mysql,进入到api_automation_test.setting.py,配置项目数据库连接
DATABASES = { ‘default‘: { # ‘ENGINE‘: ‘django.db.backends.sqlite3‘, # ‘NAME‘: os.path.join(BASE_DIR, ‘db.sqlite3‘), ‘ENGINE‘:‘django.db.backends.mysql‘, # 数据库类型,mysql ‘NAME‘:‘api_test‘, # database名 ‘USER‘:‘root‘, # 登录用户 ‘PASSWORD‘:‘123456‘, # 登录用户名 ‘HOST‘:‘127.0.0.1‘, # 数据库地址 ‘PORT‘:‘3306‘ # 数据库端口 } }
5、模型变更和创建表结构
python manage.py makemigrations
python manage.py migrate
6、创建超级用户
python manage.py createsuperuser
7、下载node.js配置环境
下载包解压安装
创建软链接:ln -s /usr/local/node-v12.14.0-linux-x64/bin/node /usr/local/bin/node
ln -s /usr/local/node-v12.14.0-linux-x64/bin/npm /usr/local/bin/npm
检查是否安装成功:node -v;npm -v
8、安装vue环境和相关依赖包
安装vue环境:npm install --global vue-cli
安装依赖:直接npm install 可能会报错所以安装国内淘宝镜像源,npm install -g cnpm --registry=https://registry.npm.taobao.org
9、执行前端打包
进入到前端目录下:frontend/
打包前删掉dist目录下之前的node_modules和package.json文件或者目录
进入项目前端api_automation_test /frontend/src/api/api.js注释其他IP,保留127.0.0.1,否则登录请求的域名会是错的
执行打包:npm run build
若打包失败提示:npm ERR! node-sass@4.14.1 postinstall: `node scripts/build.js`的报错需执行以下命令
安装对应报错的node_sass版本:cnpm install node-sass@4.14.1 --save-dev,然后再编译打包
10、uwsgi配置
在项目uwsgi目录的ini文件中修改相关配置的路径,需根据实际项目的位置修改如下:
# uwsig使用配置文件启动 [uwsgi] chdir=/usr/local/data/env/pyweb/api_automation_test-master # 指定项目的application module=api_automation_test.wsgi:application # 指定sock的文件路径 socket=/usr/local/data/env/pyweb/api_automation_test-master/UwsgiScript/uwsgi.sock # 进程个数 workers=5 pidfile=/usr/local/data/env/pyweb/api_automation_test-master/UwsgiScript/uwsgi.pid # 指定IP端口 http=127.0.0.1:8000 # 指定静态文件 static-map=/static=/usr/local/data/env/pyweb/api_automation_test-master/frontend/dist/static # 启动uwsgi的用户名和用户组 uid=root gid=root # 启用主进程 master=true # 自动移除unix Socket和pid文件当服务停止的时候 vacuum=true # 序列化接受的内容,如果可能的话 thunder-lock=true # 启用线程 enable-threads=true # 设置自中断时间 harakiri=30 # 设置缓冲 post-buffering=4096 # 设置日志目录 daemonize=/usr/local/data/env/pyweb/api_automation_test-master/logs/uwsgi.log http-time=10
11、安装nginx并配置conf文件
安装过程参考:https://blog.csdn.net/qq_37870901/article/details/85088302
进入到nginx.conf文件中,根据项目提供的nginx配置文件修改nginx配置
user root; worker_processes 1; error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; #log_format main ‘$remote_addr - $remote_user [$time_local] "$request" ‘ # ‘$status $body_bytes_sent "$http_referer" ‘ # ‘"$http_user_agent" "$http_x_forwarded_for"‘; access_log logs/access.log; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name 127.0.0.1; #charset koi8-r; charset utf-8; #access_log logs/host.access.log main; location ^~ /static { proxy_pass http://127.0.0.1:8000; } location / { include uwsgi_params; uwsgi_connect_timeout 30; uwsgi_pass unix:/usr/local/data/env/pyweb/api_automation_test-master/UwsgiScript/uwsgi.sock; #uwsgi_pass 127.0.0.1:8000; root /usr/local/data/env/pyweb/api_automation_test-master/frontend/dist/static; try_files $uri $uri/ /; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #root html; # #index index.html index.htm; } #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 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 8001; server_name 127.0.0.1; client_max_body_size 5M; if ( $http_user_agent ~* "(Android|iPhone|Windows Phone|UC|Kindle)"){ rewrite ^/(.*)$ http://127.0.0.1:8002$uri redirect; } location / { include uwsgi_params; uwsgi_connect_timeout 30; uwsgi_pass unix:/etc/script/Ituwsgi.sock; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 8002; server_name 127.0.0.1; client_max_body_size 5M; if ( $http_user_agent !~* "(Android|iPhone|Windows Phone|UC|Kindle)"){ rewrite ^/(.*)$ http://127.0.0.1:8001$uri redirect; } location / { include uwsgi_params; uwsgi_connect_timeout 30; uwsgi_pass unix:/etc/script/MItuwsgi.sock; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
11、启动项目
启动uwsgi:uwsgi --ini uwsgi.ini ;重启:uwsgi --reload uwsgi.pid ;关闭:uwsgi --stop uwsgi.pid
启动nginx:cd nginx/sbin 执行:./nginx启动 ps -ef | grep nginx 查看进程后可杀掉进程关闭nginx
登录项目前端:127.0.0.1
登录项目后台管理:127.0.0.1:8000/admin
12、项目可不安装uwsgi和nginx直接运行
python3 manage.py runserver
项目部署可参考链接文档:https://www.cnblogs.com/eosclover/p/11263359.html