nginx+uwsgi+python3+flask+腾讯云
1 部署python3环境
http://note.youdao.com/noteshare?id=afb10f8f05f7cb81c11944fa9f4c91ce
2 安装virtualenv
- 2.1 在\usr\local目录下面创建python3文件夹,安装python3
- 2.2 创建一个虚拟环境,用于运行flask项目
python -m virtualenv + 虚拟环境名称
# 激活虚拟环境
source venvname/bin/activate
# 取消激活虚拟环境
deactivate
3 创建虚拟环境安装python包
- 将开发环境中用到的pip包打包到文件中;在生产环境(服务器)中批量安装python包
# 批量导出包
pip freeze -> requirment.txt
#批量安装包
pip install -r requirment.txt
4 测试flask框架直接运行
# 在根目录下创建一个test1文件夹
mkdir /test1
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello1():
return "god bye"
@app.route('/hello')
def hello2():
return "人生苦短;我用python"
if __name__ == '__main__':
# host 必须是0.0.0.0;port端口必须在云服务器安全组中开放
app.run(host='0.0.0.0', port=8002, debug=True)
- 4.3 使用python python文件路径+文件名运行python项目
#运行test1文件夹下面的app.py程序
python /test1/app.py
5 flask+uwsgi运行
pip install uwsgi
- 5.2 检查uwsgi的版本与python版本的兼容性
#安装的uwsgi是项目所需的python x.x版本,如果不是
uwsgi --python-version
- 5.3 在项目目录或者虚拟环境目录中创建uwsgi的config.ini配置文件
#选择好路径:
touch config.ini
vim config.ini
[uwsgi]
# uwsgi 启动时所使用的地址与端口
# 与nginx通讯时使用socket,地址要与nginx配置文件的uwsgi_pass相同
socket = 127.0.0.1:8001
# 如果直接使用uwsgi+flask,使用http协议,ip+端口,ip规定是0.0.0.0,端口必须是开放的端口
#http = 0.0.0.0:8001
# 指向网站目录
chdir = /test1
# python 启动程序文件
wsgi-file = app.py
# python 程序内用以启动的 application 变量名
callable = app
# 处理器数
processes = 4
# 线程数
threads = 2
# 状态检测地址
# 地址与app.run(host='0.0.0.0', port=8002, debug=True)中地址要完全相同
stats = 0.0.0.0:8002 # 使用flask项目默认的端口,可以换但必须是flask运行时使用的端口
#默认的uwsgi分配一个小的buffer(4k)来接收每个请求的头信息,如果在日志中看见"invalid request block size",它意味着你需要一 个大一点的buffer,报错invalid request block size: 4161 (max 4096)...skip
buffer-size = 8192
# 将日志文件存放到项目文件夹下面
daemonize = /test1/uwsgi.log
# python文件变更的时候,uwsgi进程自动重启
python-autoreload=1
- 5.5启动uwsgi之前要先彻底关掉uwsgi的服务
killall -9 uwsgi # kill正在运行的uwsgi程序
#杀掉所有与uwsgi有关的进程后启动uwsgi
uwsgi config.ini #配置文件名可以随意,一般是uwsgi.ini config.ini 这两个
6 flask+uwsgi+nginx运行
- 6.1 安装nginx
- 6.2 创建nginx安装路径
# 一般在这个路径安装nginx
mkdir /usr/local/nginx
# 安装依赖包
#gcc安装,nginx源码编译需要
yum install gcc-c++
#PCRE pcre-devel 安装,nginx 的 http 模块使用 pcre 来解析正则表达式
yum install -y pcre pcre-devel
#zlib安装,nginx 使用zlib对http包的内容进行gzip
yum install -y zlib zlib-devel
#OpenSSL 安装,强大的安全套接字层密码库,nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http)
yum install -y openssl openssl-devel
#下载版本号可根据目前官网最新稳定版自行调整
wget -c https://nginx.org/download/nginx-1.16.1.tar.gz
#根目录使用ls命令可以看到下载的nginx压缩包,然后解压
tar -zxvf nginx-1.16.1.tar.gz
#解压后进入目录
cd nginx-1.16.1
#使用默认配置
./configure
#编译安装
make
make install
#查找安装路径,默认都是这个路径
[root@VM_0_12_centos ~]# whereis nginx
nginx: /usr/local/nginx
#启动、停止nginx
cd /usr/local/nginx/sbin/
./nginx #启动
./nginx -s stop #停止,直接查找nginx进程id再使用kill命令强制杀掉进程
./nginx -s quit #退出停止,等待nginx进程处理完任务再进行停止
./nginx -s reload #重新加载配置文件,修改nginx.conf后使用该命令,新配置即可生效
#重启nginx,建议先停止,再启动
./nginx -s stop
./nginx
#查看nginx进程,如下返回,即为成功
[root@VM_0_12_centos ~]# ps aux|grep nginx
root 5984 0.0 0.0 112708 976 pts/1 R+ 14:41 0:00 grep --color=auto nginx
root 18198 0.0 0.0 20552 612 ? Ss 11:28 0:00 nginx: master process ./nginx
nobody 18199 0.0 0.0 23088 1632 ? S 11:28 0:00 nginx: worker process
#在rc.local增加启动代码即可
vi /etc/rc.local
#增加一行 /usr/local/nginx/sbin/nginx,增加后保存
#设置执行权限
cd /etc
chmod 755 rc.local
#进入nginx配置文件目录,找到nginx的配置文件nginx.conf
cd /usr/local/nginx/conf/
#直接修改
vi nginx.conf
#修改完成后,重新加载配置文件
cd /usr/local/nginx/sbin/
./nginx -s reload
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
# 监听的端口:默认是80端口,可以修改成其他开放的端口
listen 80;
# server_name 填写公网IP地址或者域名
server_name 49.233.15.221;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
include uwsgi_params; # 是uwsgi默认的配置参数名
# 与uwsgi的配置文件的socket地址要完全相同
uwsgi_pass 127.0.0.1:8001; # 指向uwsgi 所应用的内部地址,所有请求将转发给uwsgi 处理
uwsgi_param UWSGI_PYHOME /usr/local/python3/venv1; # 指向虚拟环境目录
uwsgi_param UWSGI_CHDIR /test1; # 指向网站根目录
uwsgi_param UWSGI_SCRIPT app:app; # 指定启动程序
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}