可以直接参考 uwsgi中文文档
以下内容也是我参考文档,自己实践的一些总结。
-
创建 虚拟环境 不熟悉的可以参考 这篇文章
-
在虚拟环境内安装 uwsgi
pip install uwsgi
pip install uwsgi 之后,运行uwsgi 报错:
[uwsgi: command not found]
解决方案:建立软链接
ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi
在这一步 可以进行uwsgi的测试,查看是否连接成功,我在这就不做了。
需要的移步上面的文档。
-
在虚拟环境内 ,安装你的项目所需要的依赖包
pip install -r requirements.txt
-
使用
python3 manage.py runserver 0.0.0.0:8000
看项目是否可以正常启动- 云服务器用户 记得添加安全组 开放自定义端口8000
Invalid HTTP_HOST header: 'xxx.xx.xxx.xxx:8000'. You may need to add 'xxx.xx' to ALLOWED_HOSTS
如果出现以上错误,修改 django项目的 setting文件ALLOWED_HOSTS = []改为ALLOWED_HOSTS = ['*']
- 再次运行 项目应该是可以启动了。但此时 网页的静态文件并没有正常访问到。
-
在 4 正常启动的情况下,我们就可以使用 uwsgi启动项目了
请在事先 准备好的虚拟环境中运行!!!uwsgi --chdir /your/project/ --home /your/virtualenv/ --http :8000 --module mysite.wsgi:application
此时 静态文件还是没有正常访问到, 稍等解决。
到这里 这个栈 the web client <-> uWSGI <-> Django
工作正常
=======================================================================
-
接下来 要安装 nginx ,不熟悉的同学可以参考 这里
-
启动 nginx ,浏览器访问出现下面的画面。
-
在nginx下创建 sites-available 和 sites-enabled 目录
-
在/nginx/nginx.conf配置文件中的http区块添加如下行:
include ....../nginx/sites-enabled/*;
-
所有的虚拟主机配置文件放在sites-available目录,然后你可以在sites-enabled目录创建软链接指向sites-available里的配置文件
ln -s ...nginx/sites-available/xxx.conf .../nginx/sites-available/xxx.conf
使用软连接 要使用 绝对路径
-
在sites-available 中创建 mysite_nginx.conf 文件,内容如下:
#mysite_nginx.conf #the upstream component nginx needs to connect to #upstream django { # server unix:///path/to/your/mysite/mysite.sock; # for a file socket #server 127.0.0.1:8001; # for a web port socket (we'll use this first) #} #configuration of the server server { # the port your site will be served on listen 80; # the domain name it will serve for server_name mxonline; # substitute your machine's IP address or FQDN charset utf-8; # max upload size client_max_body_size 75M; # adjust to taste # Django media location /media { alias /your/project/media; # your Django project's media files - amend as required } location /static { alias /your/project/static; # your Django project's static files - amend as required } # Finally, send all non-media requests to the Django server. location / { uwsgi_pass 127.0.0.1:8001; **这是nginx与uwsgi通信的端口** include /your/project/uwsgi_params; # the uwsgi_params file you installed } }
-
由于每次启动 uwsgi 都需要在命令行 指定参数 很麻烦,所以我们可以通过从 ini 文件读取配置启动 uwsgi。
在这里需要在 系统环境 安装 uwsgi 添加软连接 到 /usr/bin/ 上面讲过了。
我在我的项目根目录下 新建了 mysite_uwsgi.ini
内容如下:
# mysite_uwsgi.ini file
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /your/project
# Django's wsgi file
module = ProjectName.wsgi:application
# the virtualenv (full path)
home = /your/virtualenv
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 4
max-requests = 5000
harakiri = 60
# the socket (use the full path to be safe
#socket = /your/project/ProjectName.sock
socket = 127.0.0.1:8001
uid = root
gid = root
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
pidfile = /your/project/master.pid
daemonize = /your/project/xxx.log
-
启动 uwsgi
uwsgi --ini /your/project/mysite_uwsgi.ini
-
启动nginx , 启动之前可以通过 -t 查看 nginx 状态
-
到这里 nginx 和 uwsgi 全部启动了 . 在浏览器尝试 输入 ip:80 访问
可能出现的问题
- 403 错误
查看 nginx errors.log 日志 发现
调查
- 发现启动 nginx的用户是nobody
- 修改 nginx.conf 文件 将用户 设置为 root
- 重启 nginx ./nginx -s reload