阿里云CentOS 7.4 使用Flask建站的方法

2019-10-10 12:07:09王振洲

四、使用nginx

nginx,一个高性能的web服务器。通常用来在前端做反向代理服务器。代理服务,简而言之,一个请求经过代理服务器从局域网发出,然后到达互联网上服务器,这个过程的代理为正向代理。如果一个请求,从互联网过来,先进入代理服务器,再由代理服务器转发给局域网的目标服务器,这个时候,代理服务器为反向代理(相对正向而言)。

1. gcc 安装

安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装:

yum install gcc-c++

2. PCRE pcre-devel 安装

PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。命令:

yum install -y pcre pcre-devel

3. zlib 安装

zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。

yum install -y zlib zlib-devel

4. OpenSSL 安装

OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。

nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。

yum install -y openssl openssl-devel

5. 使用wget下载nginx:

wget https://nginx.org/download/nginx-1.13.12.tar.gz
tar -zxvf nginx-1.13.12.tar.gz
cd nginx-1.13.12
make 
make install #编译安装

然后配置Nginx,刚才安装了Nginx之后,我们打开/etc/nginx/conf.d/default.conf,然后修改默认的default.conf为:

server {
  listen    80;
  server_name localhost;
  location / {
    proxy_pass http://127.0.0.1:5000;
    proxy_redirect off;
    proxy_set_header Host $host:80;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }

}

其中server_name就是你的域名,这里用localhost代表通过ip访问,配置好default.conf之后试着启动Nginx!

[root@server ~]# service nginx start
Starting nginx:                      [ OK ]
[root@server ~]# nginx -s reload

ok!到这一步,整个部署过程就完成了!

六、为了方便管理使用supervisor

1.安装 supervisor

pip install supervisor
echo_supervisord_conf > supervisor.conf  # 生成 supervisor 默认配置文件
vim supervisor.conf            # 修改 supervisor 配置文件,添加 gunicorn和nginx