1、环境需求和软件需求
Ubuntu
Nginx
Uwsgi
Django
Python3
2、环境安装
安装PIP3(已安装跳过本步骤)
sudo apt install python3-pip
安装Django(已安装跳过本步骤)
pip3 install Django
安装uwsgi(已安装跳过本步骤)
pip3 install uwsgi
安装Nginx(已安装跳过本步骤)推荐使用 宝塔面板 进行安装
yum install -y wget && wget -O install.sh http://download.bt.cn/install/install.sh && sh install.sh
3、项目部署
新建Django项目
django-admin startproject mystie
使用uwsgi运行Django项目(测试项目以正确建立和Uwsgi安装正确)
uwsgi --http :8000 --file mysite/wsgi.py --static-map=/static=static
新建uwsgi.ini文件来部署项目(通常配置文件在项目根目录,新建一个文件夹进行存放:script)
[uwsgi] #指定IP端口 // 直接外部访问 http = 0.0.0.0:8080 #项目目录 chdir = /www/mysite/ #指定项目的application module = mysite.wsgi:application #启用主进程 master = true #指定Socket路径 //内部访问 #权限和nginx一样 #socket = /www/wwwroot/mysite/script/uwsgi.sock socket = :8080 #进程个数 processes = 5 #pidfile = /www/wwwroot/mysite/script/uwsgi.pid #自动移除unix Socket 和 Pid 文件 当服务停止的时候 vacuum = true #序列化接受的内容,如果可能的话 thunder-lock = true #启用线程 enable-threads = true #设置自动中断时间 harakiri = 30 #设置缓冲 post-buffering = 4096 #uwsgi启动用户名和用户组 #uid = www #gid = www #socket权限设置 #chmod-socket = 755 #设置静态文件 #static-map = /static=//www/wwwroot/mysite/static #设置日志目录 #daemonize = /www/wwwroot/mysite/uwsgi.log #uwsgi --ini uwsgi.ini # 启动 #uwsgi --reload uwsgi.pid # 重启 #uwsgi --stop uwsgi.pid # 关闭
使用配置文件启动项目
uwsgi --ini /www/wwwroot/mysite/script/uwsgi.ini
配置Nginx
server { listen 80; server_name mysite.com; index index.php index.html index.htm default.php default.htm default.html; root /www/wwwroot/mysite; #ERROR-PAGE-START 错误页配置,可以注释、删除或修改 error_page 404 /404.html; error_page 502 /502.html; #ERROR-PAGE-END # location / { include uwsgi_params ; #导入Nginx的uwsgi模块 uwsgi_connect_timeout 30; #设置连接uwsgi超时时间 uwsgi_pass unix:///www/wwwroot/mysite/script/uwsgi.sock; #权限和uwsgi一样 #uwsgi_pass 127.0.0.1:8080; } location /static/ { root /www/wwwroot/mysite/; #静态文件路径,到Static上级目录即可。 } #禁止访问的文件或目录 location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md) { return 404; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; error_log off; access_log off; } location ~ .*\.(js|css)?$ { expires 12h; error_log off; access_log off; } access_log /www/wwwlogs/mysite.com.log; error_log /www/wwwlogs/mysite.com.error.log; }
以上在 阿里云上测试通过
本文章为学习笔记。
如果您有更好的建议请留言,谢谢!