Django上线部署之uWSGI

环境:
  1.CentOS 7.2 64位
  2.SQL Server 2016 Enterprise 64位
  3.Python 3.6.5 64位
  4.root用户

要求:
  按照顺序部署

1.Windows Server 2016 Datacenter 64位 操作系统下安装数据库

2.CentOS 7.2 下  Python环境搭建

3.安装ODBC for SQL Server

 1 curl https://packages.microsoft.com/config/rhel/7/prod.repo > /etc/yum.repos.d/mssql-release.repo
 2 yum remove unixODBC-utf16 unixODBC-utf16-devel
 3 ACCEPT_EULA=Y yum install msodbcsql
 4 yum install unixODBC-devel
 5 
 6 # 以下这三行是安装连接数据库的工具,一般项目上用不到
 7 ACCEPT_EULA=Y yum install mssql-tools
 8 ln -s /opt/mssql-tools/bin/bcp /usr/bin/bcp
 9 ln -s /opt/mssql-tools/bin/sqlcmd /usr/bin/sqlcmd
10 
11 # 以下是Django的settings.py中数据库配置
12 DATABASES = {
13     'default': {
14         'ENGINE': 'sql_server.pyodbc',
15         'NAME': 'db',
16         'USER': 'user',
17         'PASSWORD': 'pwd',
18         'HOST': 'ip',
19         'PORT': '1433',
20         'OPTIONS': {
21             'driver': 'ODBC Driver 13 for SQL Server',
22         },
23     },
24 }
25 
26 DATABASE_CONNECTION_POOLING = True

4.安装uwsgi和uwsgitop(查看uwsgi状态的工具,如果使用securecrt 连接centos,终端需设置成 xterm)

 1 pip install uwsgi uwsgitop
 2 
 3 # uwsgi 配置如下:
 4 [uwsgi]
 5 socket = /srv/www/uwsgi.sock #采用socket通信
 6 master = true
 7 processes = 4 #4个进程,一般按照CPU个数设置
 8 threads = 5 #每个进程5个线程
 9 enable-threads = true #由于Python的GIL限制,用处不大,没有做过测试,还是开启
10 chdir = /srv/www #项目根目录
11 wsgi-file = /srv/www/project/wsgi.py
12 stats = /srv/www/uwsgi.stats #配合uwsgitop使用,命令:uwsgitop uwsgi.stats 查看状态
13 pidfile = /srv/www/uwsgi.pid #uwsgi --stop uwsgi.pid;uwsgi --reload uwsgi.pid
14 daemonize = /srv/www/uwsgi.log #日志
15 log-maxsize = 10000000
16 disable-logging = true #开启后不记录请求日志
17 vacuum = true #uwsgi关闭后清理相关文件
18 #chmod-socket = 644
19 #chroot
20 #gid
21 #uid

5.安装nginx

 1 tar -zxvf nginx-1.16.1.tar.gz
 2 cd nginx-1.16.1/
 3 ./configure --prefix=/opt/nginx
 4 make && make install
 5 ln -s /opt/nginx/sbin/ /usr/bin/nginx
 6 # nginx.conf配置如下:
 7 #user  nobody;
 8 worker_processes  4;
 9 
10 #error_log  logs/error.log;
11 #error_log  logs/error.log  notice;
12 #error_log  logs/error.log  info;
13 
14 pid        logs/nginx.pid;
15 
16 
17 events {
18     worker_connections  1024;
19 }
20 
21 http {
22     include       mime.types;
23     default_type  application/octet-stream;
24 
25     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
26                       '$status $body_bytes_sent "$http_referer" '
27                       '"$http_user_agent" "$http_x_forwarded_for"';
28 
29     sendfile        on;
30     keepalive_timeout  65;
31 
32     gzip  on;
33 
34     server {
35         listen       80;
36         server_name  192.168.70.110;
37         charset utf-8;
38         access_log  logs/host.access.log  main;
39         client_max_body_size 100M;
40 
41         location /media  {
42             alias /srv/www/media;
43         }
44     
45         location /static {
46             alias /srv/www/static;
47         }
48 
49         location / {
50             uwsgi_pass  unix:///srv/www/uwsgi.sock;
51             include     /srv/www/uwsgi_params; #文件在nginx/conf目录下,可拷贝至项目文件夹下
52         }
53 
54         error_page  404              /404.html;
55     }
56 }

6.启动

1 uwsgi /path/to/uwsgi.ini && nginx

7.安装supervisor

猜你喜欢

转载自www.cnblogs.com/Siriusmath/p/11433117.html