1. 運行環境python
Linux: centos 7.4 64位nginx
python: python 3.4flask
2. 安裝python虛擬環境virtualenvcentos
$easy_install virtualenv $mkdir venv $cd venv
$virtualenv -p python venv_f # 因爲本機服務器的python3已經軟鏈接到了python 全部這裏-p 後面的參數直解釋python
$cd venv_f
$source bin
虛擬環境下python版本3.4服務器
pip install flask pip install gunicorn
建立項目目錄/home/app/FV_Blog,新增一個hello.py文件app
# hello.py from flask import Flask app=Flask(__name__) @app.route('/hello') def hello_world(): return "Hello World" @app.route('/user/<name>') def hello_user(name): return "<h1>Hello %s ~</h1>" % name if __name__=='__main__': from werkzeug.contrib.fixers import ProxyFix app.wsgi_app = ProxyFix(app.wsgi_app) app.run()
gunicorn -b 127.0.0.1:5000 hello:app這裏注意一下兩個參數,hello flask啓動文件的文件名稱, app是實例化flask的變量。spa
找到你的nginx配置文件的位置rest
備份一份出來,將nginx.conf裏server部分修改爲以下:code
server { listen 8080; server_name _; # 這是HOST機器的外部域名,用地址也行 root /home/app/FV_Blog; location / { proxy_pass http://127.0.0.1:5000; proxy_redirect off; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } }
而後重啓nginxserver
$service nginx restart