Ubuntu16.04采用FastCGI方式部署Flask web框架

1    部署nginx

1.1    安装nginx服务

root@desktop:~# apt-get install nginx -y

1.2    验证nginx服务是否启动

root@desktop:~# ps -ef | grep nginx | grep -v grep
root       1005      1  0 15:07 ?        00:00:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
nginx      1006   1005  0 15:07 ?        00:00:00 nginx: worker process
root@desktop:~# ss -lntup | grep 80
tcp    LISTEN     0      128       *:80                    *:*                   users:(("nginx",pid=1006,fd=7),("nginx",pid=1005,fd=7))
tcp    LISTEN     0      128      :::80                   :::*                   users:(("nginx",pid=1006,fd=8),("nginx",pid=1005,fd=8))

1.3    客户端浏览nginx页面

2    配置nginx

2.1    创建nginx用户

root@desktop:~# useradd -M nginx
root@desktop:~# id nginx
uid=1001(nginx) gid=1001(nginx) groups=1001(nginx)

2.2    修改/etc/nginx/nginx.conf配置文件(添加标红部分)

root@desktop:~# grep -v '^#' /etc/nginx/nginx.conf    
user nginx nginx;
worker_processes auto;
pid /run/nginx.pid;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        server{
                listen 5000;
                server_name localhost;
                charset utf-8;

                location / { try_files $uri @flasks; }
                location @flasks {
                        include fastcgi_params;
                        fastcgi_param PATH_INFO $fastcgi_script_name;
                        fastcgi_param SCRIPT_NAME "";
                        fastcgi_pass unix:/tmp/flasks-fcgi.sock;
                }
        }
        ##
        # Gzip Settings
        ##

        gzip on;
        gzip_disable "msie6";

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}

3    部署FastCGI

3.1    安装flup

root@desktop:~# pip3 install flup

3.2    创建FastCGI配置文件存放目录

root@desktop:~# cd /var/www/
root@desktop:/var/www# mkdir flasks

3.3    创建FastCGI服务器配置文件

root@desktop:/var/www/flasks# vim flasks.fcgi 
#!/usr/bin/python3
from flup.server.fcgi import WSGIServer
from flasks import app

if __name__ == '__main__':
#    WSGIServer(app).run()
    WSGIServer(app, bindAddress='/tmp/flasks-fcgi.sock').run()
#赋予FastCGI配置文件有可执行权限
root@desktop:/var/www/flasks# chmod +x flasks.fcgi

3.4    创建app应用文件

root@desktop:/var/www/flasks# vim flasks.py 
#!/usr/bin/python3

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return 'Index Page'

#if __name__ == '__main__':
#    #app.run(debug=True)
#    app.debug =True
#    app.run()

3.5    修改FastCGI配置文件的用户及组权限

root@desktop:/var/www# chown -R nginx:nginx ./flasks
root@desktop:/var/www# ls -ld ./flasks/
drwxr-xr-x 3 nginx nginx 4096 4月  14 15:43 ./flasks/
root@desktop:/var/www# cd flasks/
root@desktop:/var/www/flasks# ls -l *
-rwxr-xr-x 1 nginx nginx  309 4月  14 01:41 flasks.fcgi
-rw-r--r-- 1 nginx nginx  213 4月  14 01:49 flasks.py

3.6    运行FastCGI进程

root@desktop:~# nohup /var/www/flasks/flasks.fcgi &
root@desktop:~# jobs
[1]+  Running                 nohup /var/www/flasks/flasks.fcgi &
root@desktop:~# chmod 757 /tmp/flasks-fcgi.sock

3.7    验证应用可以成功浏览

至此采用FastCGI方式部署Flask web框架完成,FastCGI服务的启动脚本可以自行编写实现!!!

猜你喜欢

转载自www.cnblogs.com/Wolf-Dreams/p/10706422.html