版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_18808965/article/details/80634310
#安装 nginx
yum install nginx
cd /etc/nginx/
# 修改nginx.conf 文件
vim nginx.config
user root nginx;
worker_processes 1; #设置值和CPU核心数一致
error_log /app/webservice/nginx/logs/nginx_error.log crit; #日志位置和日志级别
pid /app/webservice/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 8000 default_server;#web访问nginx 端口
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 / {
#配置uWSGI 服务
include uwsgi_params; #设置uwsgi_params文件路径,导入的uwsgi配置
#设置为uwsgi服务器开启的端口
#需要和uwsgi的配置文件里socket项的地址
#相同,否则无法让uwsgi接收到请求。
uwsgi_pass 127.0.0.1:8090;
#python的位置(虚拟环境下)
uwsgi_param UWSGI_PYHOME /usr/local/bin/python3.6;
#启动项目的主程序(在本地上运行)这个主程序可以在flask内置的,服务器上访问你的项目
uwsgi_param UWSGI_SCRIPT test:app;#启动脚本名:flask 实例对象
#项目根目录
uwsgi_param UWSGI_CHDIR /app/project/;
}
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.
}
#验证 配置文件是否,配正确了
nginx -t -c /etc/nginx/nginx.conf
#出现下面这句代表正确了
nginx: configuration file /etc/nginx/nginx.conf test is successful
#暂时不启动 nginx 服务,先安装 wsgi
yum install Uwsgi #推荐使用这个命令来安装
#进入你所在项目的目录,新建文件,wsgi.ini 作为 wsgi 的配置文件
[uwsgi]
socket = 127.0.0.1:8090
#项目目录
chdir = /app/project/
#flask程序的启动文件
module=test.py
#程序内启用的application变量名
callable=app
#处理器个数
processes = 1
#线程个数
threads = 2
daemonize = /app/webservice/wsgi/logs/wsgi.log
#获取uwsgi统计信息的服务地址
stats=%(chdir)uwsgi.status
pidfile=/app/webservice/uwsgi.pid
touch-reload=/app/project/
test.py
#!/usr/bin/python3.6
# coding = utf-8
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return '<h1>Hello,Flask Web!</h1>'
if __name__ == '__main__':
print ('start py script ===============')
app.run(host='127.0.0.1', debug=True, port=8090)
#启动wsgi
uwsgi --ini /app/project/uwsgi.ini
#查看是否启动成功
ps aux | grep wsgi | grep -v grep
#启动nginx 服务
nginx
#查看启动进程
ps aux | grep nginx | grep -v grep
#停止服务 nginx -s -stop
#强制停止: pkill -9 nginx
#访问地址看下效果