使用 Nginx 部署 Django

文章
林里克斯

使用 Nginx 部署 Django 项目

一: 实验平台:CentOS 7.3

二: Nginx版本:nginx-1.12.2



一、安装环境


1.安装 Nginx 这里测试就直接用 yum 安装

$ yum -y install nginx

2.安装 supervisor,一个用来管理进程的工具,用来管理 uwsgi

$ pip install supervisor

3.安装 uwsgi 依赖包

$ yum -y install python-devel

python-devel未安装uwsgi会报以下错误:

$ pip install uwsgi --upgrade

...

*** uWSGI compiling embedded plugins ***
    [gcc -pthread] plugins/python/python_plugin.o
    In file included from plugins/python/python_plugin.c:1:0:
    plugins/python/uwsgi_python.h:2:20: fatal error: Python.h: No such file or directory
     #include <Python.h>
                        ^
    compilation terminated.

    ----------------------------------------
Command "/usr/bin/python2 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-xIcJip/uwsgi/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-B_IpPf-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-build-xIcJip/uwsgi/

4.再次安装 uwsgi

$ pip install uwsgi --upgrade

二、测试


1.使用 uwsgi 部署

$ uwsgi --http :8080 --chdir /blog/ --home=/usr/bin/env --module project.wsgi

#这样就跑起来了,--chdir /blog/项目地址内含(mang)--home 指定virtualenv 路径,如果没有可以去掉。project.wsgi 指的是 project/wsgi.py 文件

但是现在访问,我们会发现,静态文件都没正常显示。接着看下去


三、管理进程


1.使用 supervisor 来管理进程

生产supervisor的配置文件--> /etc/supervisor.conf

$ echo_supervisord_conf > /etc/supervisord.conf

编辑配置文件/etc/supervisor.conf

$ vim supervisor.conf

[program:blog]
command=/usr/bin/uwsgi --http :80 --chdir /blog --module django_blog.wsgi
directory=/blog
startsecs=0
stopwaitsecs=0
autostart=true
autorestart=true

command 中写上对应的命令,这样,就可以用 supervisor 来管理了。

2.启用

$ supervisord -c /etc/supervisord.conf

3.重启项目

$ supervisorctl -c /etc/supervisord.conf restart blog
blog: stopped
blog: started

$ supervisorctl -c /etc/supervisord.conf [start|stop|restart] [program-name|all]

uwsgi 为例,上面这样使用一行命令太长了,我们使用 ini 配置文件来搞定,比如项目在 /blog/ 这个位置,

$ vim /blog/uwsgi.ini

[uwsgi]
socket = /blog/blog.sock
chdir = /blog
wsgi-file = django_blog/wsgi.py
touch-reload = /blog/reload

processes = 2
threads = 4

chmod-socket = 664
chown-socket = tu:www-data

vacuum = true

注意:不建议把 sock 文件放在 /tmp 下,比如 /tmp/xxx.sock (不建议)!有些系统的临时文件是 namespaced 的,进程只能看到自己的临时文件,导致 nginx 找不到 uwsgisocket 文件,访问时显示502nginxaccess log 中显示 unix: /tmp/xxx.sock failed (2: No such file or directory),所以部署的时候建议用其它目录来放 socket 文件,比如放在运行nginx用户目录中,也可以专门弄一个目录来存放 sock 文件,比如 /tmp2/

4.创建空白文件 reload

$ touch /blog/reload

5.修改 supervisor 配置文件中的 command 一行:

command=/usr/bin/uwsgi --http :80 --chdir /blog --module django_blog.wsgi

修改为:

command=/usr/bin/uwsgi --ini /blog/uwsgi.ini

6.重启 supervisor

$ supervisorctl -c /etc/supervisord.conf restart blog

四、配置 Nginx


1.新建一个 Nginx 配置文件

$ mkdir /etc/nginx/sites-available/
$ vim /etc/nginx/sites-available/blog.conf 

server {
    listen      80;
    server_name www.kjarbo.com;
    charset     utf-8;

    client_max_body_size 75M;

    location /media  {
        alias /blog/media;
    }

    location /static {
        alias /blog/static;
    }

    location / {
        uwsgi_pass  unix:///blog/blog.sock;
        include     /etc/nginx/uwsgi_params;
    }
}

2.创建超链接

$ mkdir /etc/nginx/sites-enabled/
$ ln -s /etc/nginx/sites-available/blog.conf /etc/nginx/sites-enabled/blog.conf

3.测试Nginx配置语法

$ nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Over~

版权协议须知!

本篇文章来源于 Uambiguous ,如本文章侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意

783 0 2020-09-16


分享:
icon_mrgreen.gificon_neutral.gificon_twisted.gificon_arrow.gificon_eek.gificon_smile.gificon_confused.gificon_cool.gificon_evil.gificon_biggrin.gificon_idea.gificon_redface.gificon_razz.gificon_rolleyes.gificon_wink.gificon_cry.gificon_surprised.gificon_lol.gificon_mad.gificon_sad.gificon_exclaim.gificon_question.gif
博主卡片
林里克斯 博主大人
一个致力于Linux的运维平台
运维时间
搭建这个平台,只为分享及记载自己所遇之事和难题。

现在时间 2024-04-26

今日天气
站点统计
  • 文章总数:240篇
  • 分类总数:29个
  • 评论总数:10条
  • 本站总访问量 215884 次

@奥奥

@Wong arrhenius 牛比

@MakerFace 厉害了!