Python FLask 腾讯云服务器部署

时间:2023-03-09 20:27:54
Python FLask 腾讯云服务器部署

CentOs 7.0云服务器部署Python Flask

使用:

  • Python 2.7
  • Flask
  • nginx
  • gunicorn

easy_install python-dev

yum install python-pip

yum install python-virtualenv

yum install nginx

mkdir /var/www/myFlask

sudo chmod 777 /var/www/myFlask

pip install gunicorn

pip install Flask

index.py

     1	from flask import Flask
2 from jinja2 import Environment, PackageLoader
3 import time
4
5 env = Environment(loader=PackageLoader('index', 'templates'))
6 app = Flask(__name__)
7 application = app
8
9 @app.route('/')
10 def index():
11 template = env.get_template('index.html')
12 t = time.time()
13 f = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
14 return template.render(time=t, format=f)
15
16
17 if __name__ == '__main__':
18 application.run()

/etc/nginx/site-avalidable/default

     1	server{
2 listen 80;
3 server_name 119.29.158.145;
4
5 location / {
6 proxy_pass http://127.0.0.1:8080;
7 proxy_set_header Host $host;
8 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
9 }
10 }

启动gunicorn:

gunicorn index:application -w 4 -b 0.0.0.0:8000

访问:<ip address>:8000