DJango项目部署
网站备案
在国内,互联网必须由开发者或者拥有者进行实名备案:
- 进入某云网站(腾讯云,阿里云,华为云)进行备案
- 网站备案需要有合法的公网服务器,并且服务器可用时长超过3个月
域名备案
目的是减少IP重复和记忆对用户带来的困难
- 需要有网站备案
- 购买云服务器
- 勾选分配免费公网IP
- 服务器就会被分配一个公网IP和内网IP
项目打包
| activate 虚拟环境名 |
| pip freeze > requirement.txt |
服务器环境部署
python3环境部署
| |
| |
| yum install zlib-devel bzip2-devel ncurses-devel sqlite-devel readline-devel tk-devel openssl-devel gcc* make libffi-devel -y |
| |
| |
| wget https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tgz |
| |
| tar -zxvf python-3.7.2.tgz |
| |
| ./configure prefix=/usr/local/python3 |
| |
| make && make install |
| |
| ln -s /usr/local/python3/bin/python3 /usr/bin/python3 |
| ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3 |
安装mariadb
mariadb就是开源不收费的mysql
| |
| yum install mariadb mariadb-server.x86_64 mariadb-devel -y |
| |
| systemctl start mariadb |
| |
| mysql |
默认是开发模式,不需要密码就能进入数据库,需要命令进入生产模式
| mysql_secure_installation |
| |
| |
| |
| |
| |
| |
| mysql -uroot -p**** |
| |
| >create database 库名 charset=utf8 |
项目模块安装
安装python包
| pip3 install -r requirement.txt -i https://pypi.tuna.tsinghua.edu.cn/simple |
关闭debug,开启访问接口
| DEBUG = False |
| ALLOWED_HOST = [*] |
uwsgi服务安装
| pip3 install uwsgi -i https://pypi.tuna.tsinghua.edu.cn/simple |
| ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi |
nginx服务安装
| |
| tar -zxvf nginx-1.12.2.tar |
| |
| ./configure \ |
| |
| make && make install |
| |
| ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx |
| |
| nginx |
项目配置
编写uwsgi.ini
| makdir /opt/scripts |
| touch uwsgi.ini |
| vim uwsgi.ini |
| ----------------------------------- |
| |
| [uwsgi] |
| |
| chdir=项目路径 |
| module=项目名.wsgi:application |
| socket=/opt/scripts/uwsgi.sock |
| workers=5 |
| pidfile=/opt/scripts/uwsgi.pid |
| http=内网IP:端口 |
| static-map=/static=/opt/项目路径/static |
| uid=root |
| gid=root |
| master=true |
| vacumm=true |
| enable-thread=true |
| thunder-lock=true |
| harakiri=30 |
| post-buffering=4096 |
| daemonize=/opt/项目路径/uwsgi.log |
配置nginx.conf
| |
| cp nginx.conf nginx.conf.bak |
| |
| http{ |
| ... |
| access_log logs/access.log main; |
| ... |
| gzip on; |
| gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; |
| |
| server{ |
| listen 80; |
| server_name 服务名; |
| |
| charset utf-8; |
| |
| location /{ |
| include uwsgi_params; |
| uwsgi_connect_timeout 30; |
| uwsgi_pass unix:/opt/sricpts/uwsgi.sock; |
| } |
| error_page 500 502 503 504 /50x.html; |
| location = /static/{ |
| alias 项目路径/static; |
| index index.htm index.html; |
| } |
| } |
| } |
启动服务
| uwsgi --init uwsgi.ini |
| |
| nginx |
| |
| |
| |
| |
pymysql模块配置
| |
| |
| import pymysql |
| pymysql.install_as_MySQLsb() |
| |
| python3 manage.py makemigrations |
| python3 manage.py migrate |
域名解析
在云平台中找到自己的域名,点击域名解析与对公网IP进行绑定
本文作者:注入灵魂
本文链接:https://www.cnblogs.com/totopian/p/16020694.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步