flask+gunicorn+nginx 部署web应用全攻略

flask快速使用

1.安装flask

pip install flask

2.编写一个最小的应用,文件名为run.py

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'

if __name__ == '__main__':
    app.run()

3.在网页上打开http://127.0.0.1:5000/,即进入到web应用

nginx安装与配置

1.安装nginx

#在ubuntu下,可以使用下面命令快速安装
sudo apt-get install nginx

2.检测是否安装成功

ps -ef|grep nginx

出现

root      5005     1  0 19:58 ?        00:00:00 nginx: master process nginx
www-data  5006  5005  0 19:58 ?        00:00:00 nginx: worker process
ubuntu    5852  5808  0 20:07 pts/1    00:00:00 grep --color=auto nginx

3.查询nginx的位置,打开终端输入

whereis nginx

出现
nginx: /usr/sbin/nginx /etc/nginx /usr/share/nginx

4.进入配置文件的路径,发现下面有一个defalut的文件,这就是nginx的服务器配置文件

cd /etc/nginx/sites-available/

5.修改defalut,修改过的地方已经备注

sudo vi default
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
        listen 80 default_server;
        #已修改
        listen [::]:80 ipv6only=on default_server;

        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name _;
	
        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                 #已修改
                proxy_pass  http://127.0.0.1:8080;
                access_log  /var/log/EMiao/nginx/nginx.log;
                proxy_read_timeout 300;
                try_files $uri $uri/ =404;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #       include snippets/fastcgi-php.conf;
        #
        #       # With php7.0-cgi alone:
        #       fastcgi_pass 127.0.0.1:9000;
        #       # With php7.0-fpm:
        #       fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        #} 
 
        # deny access to .htaccess files, if Apache's document root 
        # concurs with nginx's one 
        # 
        #location ~ /\.ht { 
        #       deny all; 
        #} 
} 
 
 
# Virtual Host configuration for example.com 
# 
# You can move that to a different file under sites-available/ and symlink that 
# to sites-enabled/ to enable it. 
# 
#server { 
#       listen 80; 
#       listen [::]:80; 
# 
#       server_name example.com; 
# 
#       root /var/www/example.com; 
#       index index.html; 
# 
#       location / { 
#               try_files $uri $uri/ =404; 
#       } 
#}


ps:关于location里面的几个参数
#监听的地址,由于使用了gunicorn,在这里即gunicorn的启动地址
proxy_pass http://127.0.0.1:8080;
#日志路径
access_log /var/log/EMiao/nginx/nginx.log;
#最迟返回时间
proxy_read_timeout 300;

6.重启nginx

ps -ef|grep nginx

出现

root      5005     1  0 19:58 ?        00:00:00 nginx: master process nginx
www-data  5006  5005  0 19:58 ?        00:00:00 nginx: worker process
ubuntu    7548  5808  0 20:25 pts/1    00:00:00 grep --color=auto nginx

先杀死所有nginx

kill -9 5005
kill -9 5006
kill -9 7548

再执行ps -ef|grep nginx,不再出现

启动nginx

sudo nginx

到这里nginx已经部署完成,打开网页,输入 http://你的服务器ip,会出现502的nginx报错,说明已经启动成功,错误的原因是找不到这个服务,因为gunicorn还没起来

gunicorn安装与配置

1.安装gunicorn,在ubuntu下安装,非常简单

pip install gunicorn

2.进入到flask应用的文件夹,即第一步的run.py的文件夹下

3.启动gunicorn

gunicorn -w 4 -b 127.0.0.1:8080 run:app

ps:这里的8080即上面nginx监听的端口
-w 4是指预定义的工作进程数为4,
-b 127.0.0.1:4000指绑定地址和端口
run是flask的启动python文件,app则是flask应用程序实例

4.gunicorn部署完成,打开网页,输入 http://你的服务器ip, 就会进入你的应用了!

猜你喜欢

转载自blog.csdn.net/makegame88/article/details/84961340
今日推荐