docker环境下,部署django和vue项目
docker + python3.8 + Django + uwsgi + nginx + vue 项目的配置
一、Dcoker配置
docker pull centos:7 #拉取centos7的docker镜像
docker run -it -d --name my_project -v /home/yang:/home/ymy -p 80:80 -p 8000:8000 centos:7 /bin/bash
注意点:
1.这里需要指定两个映射端口,一个是为前端项目使用,一个是为后端项目使用。
2.为了方面部署文件,通过-v设置镜像的映射文件夹
二、python3.8
这里是通过miniconda进行python环境的搭建。miniconda,镜像配置如下:
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --set show_channel_urls yes
# 设置conda的镜像
conda config --remove-key channels
# 恢复默认镜像源
#配置pip的镜像
cd ~
mkdir .pip
vim pip.conf
# 粘贴如下内容
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host = https://pypi.tuna.tsinghua.edu.cn
# 创建
conda config --set auto_activate_base false # 关闭终端开启时自动进入conda环境
conda create -n py38 python=3.8 # 创建环境
conda remove -n py38 --all # 删除环境
python的包配置,主要是产品运用到的各种库,这里记录几个容易产生问题的:
1.解决python_django的数据跨域问题:
# 解决数据跨越
pip3 install django-cors-headers
# 增加跨域忽略
CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_CREDENTIALS = True
# 允许跨域的域名
# CORS_ORIGIN_WHITELIST = (
# '127.0.0.1:8081', # Frontend
# '127.0.0.1:8080', # Backend
# '127.0.0.1:8000', # Frontend on dev mode
# )
# 设置允许访问的方法
CORS_ALLOW_METHODS = (
'DELETE',
'GET',
'OPTIONS',
'PATCH',
'POST',
'PUT',
'VIEW',
)
# 设置允许的header
CORS_ALLOW_HEADERS = (
'XMLHttpRequest',
'X_FILENAME',
'accept-encoding',
'authorization',
'content-type',
'dnt',
'origin',
'user-agent',
'x-csrftoken',
'x-requested-with',
'Pragma',
'token'
)
2.安装uwsgi,这里主要记录centos7环境的安装经历
# 安装各种依赖
yum install libxml*
yum install python-devel -y
yum install python3-devel
yum install -y gcc* pcre-devel openssl-devel
# 安装uwsgi
pip install uwsgi
本次安装提示在虚拟环境找不到libpython3.8.a的文件,解决的方法是在python的虚拟环境py38的lib中找到然后copy到gcc加载的目录。然后把另外一个放进linux根目录,/lib64中,解决uwsgi的安装和使用。
3.安装mysqlclient
conda install mysqlclient
至此python需要的库安装完毕。
三、uwsgi的配置
[uwsgi]
socket=127.0.0.1:8080
chdir=/home/python_pro/python_view
wsgi-file=python_view/wsgi.py
process=4
threads=2
pidfile=uwsgi.pid
daemonize=uwsgi.log
master=true
这里需要主要,因为后面是用nginx进行代理,所以必须是socket,由于使用8000端口进行映射,所以这里socket的配置不能占用8000端口。
uwsgi --ini uwsgi.ini # 启动命令
uwsgi --stop uwsgi.pid # 停止命令
[uwsgi] # 这个一定要放在首行,必须存在
http=IP地址加端口号
chdir=/home/ymy/<项目名称> # 这里放的是绝对引用的地址
wsgi-file=<项目名称>/wsgi.py # 这里的地址是以chdir为目标,相对饮用的地址信息
process=4 # 进程数,cpu核数
threads=2 # 线程数
pidfile=uwsgi.pid # wsgi进程启动后,会将主进程的ID写进该文件,会运用该文件来进行wsgi服务的关闭
daemonize=uwsgi.log # 这个选项存在就表示后台启动,并且所有之前在前台现实的信息,都会录入进该文件
master=true
**uwsgi**的启动失败原因排查
- 端口被占用,通过lsof -i:端口号查询出具体进程,关闭进程后,重新启动uwsgi
- 停止失败,stop无法关闭,原因可能是重复启动uwsgi,导致pid文件中的进程号失准。通过ps命令查询出uwsgi进程,手动kill。
四、nginx
搭建nginx的依赖环境:
yum install gcc-c++ # 安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装
yum install -y pcre pcre-devel # PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库
yum install -y zlib zlib-devel # zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库
yum install -y openssl openssl-devel # OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
nginx的安装
./configure
make && make install
cd /usr/local/nginx/sbin/
./nginx # 启动命令
./nginx -s stop # 快速关闭,不管有没有正在处理的请求
./nginx -s quit # Nginx在退出前完成已经接受的连接请求
./nginx -s reload # 重新加载
nginx的配置文件:
vim /usr/local/nginx/conf/nginx.conf
server {
listen 8000;
server_name python;
# server_name somename alias another.alias;
location / {
uwsgi_pass 127.0.0.1:8080;
include /usr/local/nginx/conf/uwsgi_params;
}
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步