三者关系
- nginx是web服务器
- flask是web框架
- wsgi只用于python的,连接web服务器与web框架
安装nginx
1 | yum install -y nginx |
nginx缺省使用80端口,这是可以访问127.0.0.1:80或者是云服务器的对外网址也能看到服务正常启动了
安装uwsgi
1 | yum install -y uwsgi uwsgi-plugin-python |
安装virtualenv
1 | pip install virtualenv |
配置Python虚拟环境
1 | mkdir -p /data/website/myapp |
激活虚拟环境且安装flask
1 | source env/bin/activate |
这是会看到命令提示符出现env字眼,表示进入到1
pip install flask
创建第一个flask程序
创建/data/website/myapp/run.py并修改如下内容1
2
3
4
5
6
7from flask import Flask
app = Flask(__name__)
def index():
return "<h1>hello, myapp</h1>"
if __name__ == "__main__":
app.run(host="0.0.0.0",port=5000,debug=True)
使用python run.py执行,在浏览器输入127.0.0.1:5000会看到内容
配置uwsgi
创建/data/website/myapp/run.py并修改如下内容1
2
3
4
5
6
7[uwsgi]
socket = 127.0.0.1:5000
pythonpath = /data/website/myapp
module = run
callable = app
processes = 4
threads = 2
执行uwsgi uwsgi.ini –plugin python 无误就按ctrl c杀掉进程
解释说明下uwsgi的配置参数内容:
- socket:通讯端口,外界可以通过127.0.0.1:8001访问,相当于我们在本地运行flask,并通过127.0.0.1:5000访问;并负责与nginx通信。
- pythonpath:项目目录。
- module:启动文件的文件名,我们可以在本地用Python run.py启动flask项目。
- callable:程序内启用的application变量名。
- processes:处理器个数。
- threads:线程数。
注意:最好不要在配置文件写中文注释,别问我为什么
配置nginx
不同版本不同的配置路劲,我的是修改/etc/nginx/nginx.conf1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:5000;
uwsgi_param UWSGI_PYHOME /data/website/myapp/env;
uwsgi_param UWSGI_CHDIR /data/website/myapp;
uwsgi_param UWSGI_SCRIPT run:app;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
修改部分就是http/server/location部分
重启nginx服务1
systemctl restart nginx
启动uwsgi
使用后台常驻运行1
2cd /data/website/myapp
nohup uwsgi uwsgi.ini --plugin python &
完成
通过浏览器输出你的域名或者你的serverip来获取之前在127.0.0.1:5000的页面
进阶配置https
生成https证书
1 | cd /etc/nginx/ |
配置nginx开启https
1 | server { |
重启nginx
1 | nginx -t |
浏览器查看
浏览器会报不安全站点
可以找一家云供应商免费有一年的证书使用,例如腾讯云,这样就是正规的证书了