微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

1    部署Nginx

1.1    安装Nginx服务

[email protected]:~# apt-get install Nginx -y

1.2    验证Nginx服务是否启动

[email protected]:~# 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
[email protected]:~# 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",fd=8),fd=8))

1.3    客户端浏览Nginx页面

分享图片

2    配置Nginx

2.1    创建Nginx用户

[email protected]:~# useradd -M Nginx
[email protected]:~# id Nginx uid=1001(Nginx) gid=1001(Nginx) groups=1001(Nginx)

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

[email protected]:~# 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

[email protected]:~# pip3 install flup

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

[email protected]:~# cd /var/www/
[email protected]:/var/www# mkdir flasks

3.3    创建FastCGI服务器配置文件

[email protected]:/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配置文件有可执行权限
[email protected]:/var/www/flasks# chmod +x flasks.fcgi

3.4    创建app应用文件

[email protected]:/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配置文件用户及组权限

[email protected]:/var/www# chown -R Nginx:Nginx ./flasks
[email protected]:/var/www# ls -ld ./flasks/
drwxr-xr-x 3 Nginx Nginx 4096 4月  14 15:43 ./flasks/
[email protected]:/var/www# cd flasks/
[email protected]:/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进程

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

3.7    验证应用可以成功浏览

分享图片

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐