详解Ubuntu环境下部署Django+uwsgi+nginx总结
前言
这是我在搭建Django项目时候的过程,拿来总结记录,以备不时之需。
项目采用nginx+uwsgi的搭配方式。
项目依赖包采用requirements.txt文件管理的方式。
本地准备工作
确认项目能够运行起来,没有bug
将当前环境的包导出pipfreeze>requirements.txt
将项目上传到服务器上的/srv目录下。这里以git的形式为例,打开终端,依次输入如下命令:
$gitinit $gitremoteaddoriginxxx.git#替换成你的项目git地址 $gitadd. $gitcommit-m'firstcommit' $gitpulloriginmaster--allow-unrelated-histories $gitpushoriginmaster
部署项目到服务器
安装python
安装好项目用到的python。
$sudoaptinstallpython $sudoaptinstallpython-pip $pipinstall--upgradepip
安装virtualenv以及virutalenvwrapper,并创建虚拟环境。
$pipinstallvirtualenv $pipinstallvirtualenvwrapper $sudoaptinstallvim
编辑文件~/.bashrc
$vim~/.bashrc #添加如下2行代码 exportWORKON_HOME=$HOME/.virtualenvs source/usr/local/bin/virtualenvwrapper.sh #保存文件,让文件成效 $source~/.bashrc
安装git:
$sudoaptinstallgit #为了方便XShell或者CRT连接服务器,建议安装OpenSSH $sudoaptinstallopenssh-serveropenssh-client $servicesshrestart
安装MySQL
$sudoaptinstallmysql-servermysql-client $sudoapt-getinstalllibmysqld-dev
测试配置
- 安装依赖包,进入虚拟环境workon***,进入项目根目录,执行命令pipinstall-rrequirements.txt
- 创建数据库,新打开一个终端,登录数据库,mysql-uroot-p,创建相应的数据库CREATEDATABASEIFNOTEXISTSmy_dbdefaultcharsetutf8mb4;
- 迁移数据,pythonmanage.pymigrate
- 收集静态文件,pythonmanage.pycollectstatic
- 启动服务器,执行pythonmanage.pyrunserver0.0.0.0:8000,然后在你自己电脑上,在浏览器中输入http://
:8000,访问下网站所有页面,确保所有页面都没有错误。
注意:
- 设置ALLOW_HOST为你的域名或ip地址。
- 设置DEBUG=False。
安装uwsgi
uwsgi是一个应用服务器,非静态文件的网络请求就必须通过他完成,他也可以充当静态文件服务器,但不是他的强项。
uwsgi是使用python编写的,因此通过pipinstalluwsgi就可以了。(uwsgi必须安装在系统级别的Python环境中,不要安装到虚拟环境中)。
命令行启动uwsgi:
$uwsgi--http:8000--moduletest.wsgi--vritualenv=/root/.virtualenvs/django-env-py36
如果能够在浏览器中访问到测试的页面,说明uwsgi可以加载项目了。
配置文件方式启动uwsgi:
在项目的根路径下面,创建一个文件djangotest.ini,填写以下代码:
[uwsgi] #Django相关的配置 #必须全部为绝对路径 #项目的路径 chdir=/srv/djangotest #Django的wsgi文件 module=djangotest.wsgi #Python虚拟环境的路径 home=/root/.virtualenvs/django-env-py36 #进程相关的设置 #主进程 master=true #最大数量的工作进程 processes=10 #socket文件路径,绝对路径 socket=/srv/djangotest/djangotest.sock #设置socket的权限 chmod-socket=666 #退出的时候是否清理环境 vacuum=true
然后使用命令uwsgi--inidjangotest.ini,看下是否还能启动这个项目。
安装nginx
nginx是一个web服务器。用来加载静态文件和接收http请求的。
通过命令sudoaptinstallnginx即可安装。
nginx常用命令:
- 启动nginx:servicenginxstart
- 关闭nginx:servicenginxstop
- 重启nginx:servicenginxrestart
收集静态文件:
静态文件应该让nginx来处理,而不是让django来做。
首先确保你的settings.py文件中有一个STATIC_ROOT配置,这个配置应该指定你的静态文件要放在哪个目录下。
那么我们可以执行以下命令:pythonmanage.pycollectstatic来收集所有静态文件(已经执行过请忽略)。
编写nginx配置文件,在/etc/nginx/conf.d目录下,新建一个文件djangotest.conf,然后将以下代码贴进去:
upstreamdjangotest{ serverunix:///srv/djangotest/djangotest.sock; } #配置服务器 server{ #监听的端口号 listen80; #域名 server_name192.168.0.101; charsetutf-8; #最大的文件上传尺寸 client_max_body_size75M; #静态文件访问的url location/static{ #静态文件地址 alias/srv/djangotest/static_dist; } #最后,发送所有非静态文件请求到django服务器 location/{ uwsgi_passdjangotest; #uwsgi_params文件地址 include/etc/nginx/uwsgi_params; } }
测试配置文件:servicenginxconfigtest。注意:每次修改完配置需要重启nginx:servicenginxrestart
使用supervisor
让supervisor管理uwsgi,可以在uwsgi发生意外的情况下,自动重启。
安装supervisor:在系统级别的python环境下pipinstallsupervisor。
在项目根目录下创建一个文件my_supervisor.conf。编写内容:
#supervisor的程序名字 [program:mysite] #supervisor执行的命令 command=uwsgi--inizlkt_uwsgi.ini #项目的目录 directory=/srv/djangotest #开始的时候等待多少秒 startsecs=0 #停止的时候等待多少秒 stopwaitsecs=0 #自动开始 autostart=true #程序挂了后自动重启 autorestart=true #输出的log文件 stdout_logfile=/srv/djangotest/log/supervisord.log #输出的错误文件 stderr_logfile=/srv/djangotest/log/supervisord.err [supervisord] #log的级别 loglevel=info #使用supervisorctl的配置 [supervisorctl] #使用supervisorctl登录的地址和端口号 serverurl=http://127.0.0.1:9001 #登录supervisorctl的用户名和密码 username=admin password=123 [inet_http_server] #supervisor的服务器 port=:9001 #用户名和密码 username=admin password=123 [rpcinterface:supervisor] supervisor.rpcinterface_factory=supervisor.rpcinterface:make_main_rpcinterface
运行supervisor,执行supervisord-cmy_supervisor.conf。
进入supervisor管理控制台,supervisorctl-cmy_supervisor.conf
supervisor管理控制台常用命令
#查看状态 status #启动程序 startprogram_name #重新启动程序 restartprogram_name #关闭程序 stopprogram_name #重新加载配置文件 reload #退出控制台 quit
到此这篇关于Ubuntu环境下部署Django+uwsgi+nginx总结的文章就介绍到这了,更多相关Ubuntu环境下部署Django+uwsgi+nginx总结内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。