Jolly

Centos7 nginx + uswgi 部署Django应用
前些天写了flask应用的部署,今天来讲讲Django的部署吧,毕竟Django部署坑确实多,写篇教程记录下具体步...
扫描右侧二维码阅读全文
12
2019/02

Centos7 nginx + uswgi 部署Django应用

前些天写了flask应用的部署,今天来讲讲Django的部署吧,毕竟Django部署坑确实多,写篇教程记录下具体步骤要点和踩过得坑。

首先是linux环境的配置,比如说安装python3.6;安装虚拟环境;安装mysql数据库;安装nginx,这些其实都在之前的一篇教程中写过了,可以点击链接查看:
点我查看linux具体环境配置

接下来就是具体Django的部署了。


1.代码和数据的修改

上线的代码是需要修改的,主要修改的地方有两点,如下:

在settings中增加:
STATIC_ROOT = os.path.join(BASE_DIR, 'collected_static')

这是用作后面统一存放静态文件,以便nginx来管理。

在settings中:
DEBUG = True 改成False

这是为了网站安全考虑,报错影藏,生产环境必须这样。

2.代码项目的迁移

我的nginx安装目录是在usl/local/nginx,将项目放在usl/local/nginx/html/下面

3.安装uwsgi

安装前需要安装一些依赖包,输入如下命令

yum -y install gcc  gcc-c++ zlib zlib-devel openssl openssl-devel pcre pcre-devel GeoIP gd libXpm libxslt sqlite-devel

然后:

进入虚拟环境里:
pip  install uwsgi

然后新建一个uwsgi.ini配置文件,放置在应用根部目下,
打开并写入以下内容:

# mysite_uwsgi.ini file
[uwsgi]

# Django-related settings
# the base directory (full path)
wsgi-file=/usr/local/nginx/html/MxOnline/uwsgi.ini  #项目目录下的uwsgi.ini

chdir = /usr/local/nginx/html/MxOnline
# Django's wsgi file
module = MxOnline.wsgi:application
# the virtualenv (full path)

# process-related settings
# master
master = true
# maximum number of worker processes
processes = 10
# the socket (use the full path to be safe
socket = 127.0.0.1:8000
# ... with appropriate permissions - may be needed
#chmod-socket    = 660
# clear environment on exit
home = /root/.virtualenvs/mxonline
env = DJANGO_SETTINGS_MODULE=MxOnline.settings
vacuum = true     #退出时清理环境
buffer-size = 65536  #最大缓冲区,如果设置得太小,请求的数据超过buffer-size的话,网站会起不来

注意不要写错了。
然后uwsgi的命令是:

uwsgi 启动 :虚拟环境下uwsgi config.ini
uwsgi 关闭:虚拟环境下killall -9 uwsgi

启动后去过出现一下报文说明成功启动:

(mxonline) [root@VM_0_5_centos MxOnline]# uwsgi uwsgi.ini 
[uWSGI] getting INI configuration from uwsgi.ini
*** Starting uWSGI 2.0.17.1 (64bit) on [Fri Aug 24 21:24:29 2018] ***
compiled with version: 4.8.5 20150623 (Red Hat 4.8.5-28) on 16 August 2018 10:08:12
os: Linux-3.10.0-514.26.2.el7.x86_64 #1 SMP Tue Jul 4 15:04:05 UTC 2017
nodename: VM_0_5_centos
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 1
current working directory: /usr/local/nginx/html/MxOnline
detected binary path: /root/.virtualenvs/mxonline/bin/uwsgi
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
chdir() to /usr/local/nginx/html/MxOnline
your processes number limit is 7282
your memory page size is 4096 bytes
detected max file descriptor number: 100001
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to TCP address 127.0.0.1:8000 fd 3
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
Python version: 3.6.3 (default, Aug 15 2018, 16:38:27)  [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]
Set PythonHome to /root/.virtualenvs/mxonline
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x13d3230
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 1477949 bytes (1443 KB) for 10 cores
*** Operational MODE: preforking ***
WSGI app 0 (mountpoint='') ready in 3 seconds on interpreter 0x13d3230 pid: 16267 (default app)
mountpoint  already configured. skip.
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 16267)
spawned uWSGI worker 1 (pid: 16271, cores: 1)
spawned uWSGI worker 2 (pid: 16272, cores: 1)
spawned uWSGI worker 3 (pid: 16273, cores: 1)
spawned uWSGI worker 4 (pid: 16274, cores: 1)
spawned uWSGI worker 5 (pid: 16275, cores: 1)
spawned uWSGI worker 6 (pid: 16276, cores: 1)
spawned uWSGI worker 7 (pid: 16277, cores: 1)
spawned uWSGI worker 8 (pid: 16278, cores: 1)
spawned uWSGI worker 9 (pid: 16279, cores: 1)
spawned uWSGI worker 10 (pid: 16280, cores: 1)

当然你也可以用用ps -ef|grep uwsgi命令来查看是否启动。

4.nginx的配置

nginx的安装前文已经详解过了,如今需要做的就是对他做相关的配置。
这里注意几个路径:

nginx安装路径:usr/local/nginx/
nginx主配置文件路径:usr/local/nginx/conf
nginx日志路径路径:usr/local/nginx/logs
nginx子配置文件路径:etc/nginx/vhosts/
这些路径和你的可以不一样,这里只是为了让后面的配置更清晰~~~

这里主要还是用主配置文件加include子配置文件的方式来配置,因为你的服务器可能会有多个项目需要nginx来反向代理,这样配置的好处就是清晰,简单,后续扩展容易。

下面是主配置文件:
nginx.conf,存放在usr/local/nginx/conf中

worker_processes 4;
events {
    worker_connections 262140;
}
http {
    include mime.types;
    default_type application/octet-stream;
    sendfile on;

    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 256k;         #以下四个参数已加大,如果设置太小也会出现timeout 504
    fastcgi_buffers 16 256k;
    fastcgi_busy_buffers_size 512k;
    fastcgi_temp_file_write_size 512k;
    keepalive_timeout 60;
    limit_conn_zone $binary_remote_addr zone=addr:5m;
    client_max_body_size 200m;

        include /etc/nginx/vhosts/*.conf; #注意这里是引入子配置文件,重要
}

下面引入子配置文件,就是说在子配置文件中所有以conf结尾的文件都会被引入,厉害吧。

子配置文件:
mxonline.conf 存放在etc/nginx/vhosts/下

server {
    # the port your site will be served on
    listen      8080; #监听端口是8080,也就是访问的端口
    # the domain name it will serve for
    server_name localhost; # substitute your machine's IP address or FQDN
    charset     utf-8;
    proxy_http_version 1.1;
    proxy_set_header Connection "";

    # max upload size
    #client_max_body_size 75M;   # adjust to taste

    # Django media
    location /media  {
        alias /usr/local/nginx/html/MxOnline/media;  # 指向django的media目录
    }

    location /static {
        alias /usr/local/nginx/html/MxOnline/collected_static; # 指向django的static目录
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        include     /usr/local/nginx/conf/uwsgi_params; # the uwsgi_params file you installed

        #root html;
        #index index.html index.htm;
        uwsgi_pass  127.0.0.1:8000; # 指向uwsgi 所应用的内部地址,所有请求将转发给uwsgi 处理 
        uwsgi_param UWSGI_PYHOME /root/.virtualenvs/mxonline; # 指向虚拟环境目录 
        uwsgi_param UWSGI_CHDIR /usr/local/nginx/html/MxOnline; # 指向网站根目录 
        uwsgi_connect_timeout 600;
        uwsgi_ignore_client_abort off;
        uwsgi_read_timeout 600;
        uwsgi_send_timeout 600;

    }
}

这里需要注意的有几点, uwsgi_pass需要写uwsgi.ini配置文件中socket的地址;静态文件路径要指向我们之前在settings中加的STATIC_ROOT地址。

这里附上nginx的启动、停止和重启命令:

启动:到usr/sbin/下, ./nginx启动
停止:nginx -s stop
重启:nginx -s reload

5.收集静态资源

其实到了这一步已经完成了百分之90啦,什么叫做收集静态资源呢,这是因为,将项目迁移至生产环境后,因为DEBUG = False,项目不会自己去管控寻找静态资源了,这里就将静态资源的管理交给了nginx,这也就是为什么之前nginx的static配置路径中需要指定STATIC_ROOT路径的原因。
我们需要将静态资源放置在根目录下的collected_static文件夹下,进入虚拟环境,进入到项目根目录,执行以下语句:

python manage.py  collectstatic

这样,所有静态资源都会被收集到这个目录下。
注意这里有个坑。就是静态资源的文件名,比如图片文件名不要有中文名,不然会报编码错误,这个暂时不知道什么原因,先暂时避免吧。

6.数据表的迁移或生成

数据表是很关键的,首先要确保settings文件中的数据库配置项没错,服务器上mysql已经有建好了数据库。

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mxonline',
        'USER': 'root',
        'PASSWORD': '123456',
        'HOST':'127.0.0.1'

    }
}

这里数据表有两种处理方式:
一个是迁移,将本地的开发环境数据库传输至服务器的对应数据库中,这个适用于你想保留所有数据的情况下,可以用navicat操作,也很简单,这里不做赘述。

另一个是在线生成数据表,以下是具体操作:

1.进入虚拟环境中
2.进入到项目根目录
3.执行 python manage.py makemigrations,生成数据库描述文件
4.执行 python manage.py migrate,生成表文件

这样就生成了数据表。
然后执行python manage.py createsuperuser生成超级用户。

7.完成啦

这样就已经完成了部署,只需要先启动项目,然后reload一下nginx,通过localhost:8080, 就能访问啦。

补充:

1.mysql的启动操作

# systemctl start mariadb.service //启动服务  
# systemctl enable mariadb.service //开机启动服务  
# mysql -u root -p //登录mysql 

因为安装的其实是mariadb,启动和重启方式有所不同。

2.腾讯云默认封禁25端口(气愤)
因为项目中用到了通过第三方smtp发送注册激活邮件的功能,然而部署后邮件一直发不出,很奇怪,页面也是一直panding着。查看nginx的错误日志,显示是connecting out time !! what?在我怀疑项目问题并折腾许久后,才发现腾讯云其实默认封禁了25端口,也就是说你无法通过25端口发送邮件。
但是这是可以解封的,申请一下1秒钟的事。第三方smtp发送邮件腾讯是支持的,所以解封了也没事。

3.查看错误日志
因为使用nginx代理了项目,我们项目报错就可以去nginx的日志中查看啦,具体路径在usr/local/nginx/logs/下,可以通过 tail -500f ./error.log打印查看~

Last modification:November 26th, 2019 at 10:17 am
如果觉得我的文章对你有用,请随意赞赏

Leave a Comment

🌓