gunicorn nginx flask supervisor deploy

supervisor

1
2
brew install supervisor
brew info supervisor

get info for supervisor brew services start supervisor or ini in /usr/local/etc/supervisord.ini

modify ini

get

1
2
[include]
files = /usr/local/etc/supervisor.d/*.conf

gunicorn

doc

install

1
2
pip install gevent
pip install gunicorn

gunicorn + gevent + supervisor

make log rotate and program autostart

1
2
3
4
5
6
7
8
9
10
11
[program:gemsky]
command = xx/gemsky/.env/bin/gunicorn -w 4 -k gevent app:app -b localhost:5000 --access-logfile - --log-file -
directory = xx/gemsky/
autostart = true
autorestart = true
startsecs = 5
startretries = 3
redirect_stderr         = true
stdout_logfile_maxbytes = 50MB
stdout_logfile_backups  = 3
stdout_logfile          = xx/gemsky/logs/gunicorn-supervisor.log

nginx

doc

1
brew install nginx
1
2
3
4
5
/usr/local/etc/nginx/nginx.conf

nginx will load all files in /usr/local/etc/nginx/servers/

brew services start nginx

注释 nginx.conf 里面的server add

1
2
3
4
error_log   /usr/local/etc/nginx/logs/error.log;


include servers/*.conf;

在 ‘servers/’添加

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
upstream flask_servers {
    server 127.0.0.1:5000;
}

server {
    listen       80;
    server_name  192.168.1.101;
    charset     utf-8;

    location / {
        proxy_pass         http://flask_servers;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

    }
}

ERROR

Nginx: 24: Too Many Open Files Error

IN OSX

1
2
3
4
launchctl limit maxfiles
# mode 644
sudo vim /Library/LaunchDaemons/limit.maxfiles.plist

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
        "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>Label</key>
    <string>limit.maxfiles</string>
    <key>ProgramArguments</key>
    <array>
      <string>launchctl</string>
      <string>limit</string>
      <string>maxfiles</string>
      <string>64000</string>
      <string>524288</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>ServiceIPC</key>
    <false/>
  </dict>
</plist>

加载plist文件(或重启系统后生效 launchd在启动时会自动加载该目录的plist)

1
sudo launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist

确认更改后的限制

1
launchctl limit maxfiles

Linux

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
ulimit -a


查看当前系统打开的文件数量
lsof | wc -l
watch "lsof | wc -l"

查看某一进程的打开文件数量
lsof -p pid | wc -l
lsof -p 1234 | wc -l





/etc/security/limits.conf

* soft nofile 10240
* hard nofile 10240

Hard limit 跟Soft limit的差別

  • Hard limit: 一般指不管任何狀況下,使用者都不被允許超出使用限制量
  • Soft limit: 則是允許使用者在一定時間範圍內,可以使用超過使用限制量,之後才會作處理(甚至根本不處理)