nginx+uwsgi部署flask应用

来源:互联网 发布:手机钢琴软件哪个好 编辑:程序博客网 时间:2024/05/21 10:35
sudo apt-get update && sudo apt-get upgradesudo apt-get install gitsudo apt-get install python-pipsudo apt-get install libmysqld-devsudo apt-get install python-dev sudo pip install virtualenv

在home主目录下创建文件夹:/home/www/wechat

cd到wechat文件夹,创建virtualenv文件夹:virtualenv venv

启动virtualenv:source venv/bin/activate ,启动后在命令行用户名前面会出现(venv)

deactivate退出虚拟环境

上传flask项目文件夹(nova_weixin为例)至wechat文件夹下

nova_weixin项目结构:https://github.com/imndszy/nova_weixin

启动虚拟环境,通过pip install -r requirements.txt安装依赖库

安装uwsgi:pip install uwsgi

配置uwsgi,在wechat文件夹下创建config.ini:

[uwsgi]# uwsgi 启动时所使用的地址与端口socket = 127.0.0.1:8001 # 指向网站目录chdir = /home/www/wechat/nova_weixin# python 启动程序文件wsgi-file = manage.py # python 程序内用以启动的 application 变量名callable = app # 处理器数processes = 4# 线程数threads = 2#状态检测地址stats = 127.0.0.1:9191#修改代码后自动重启py-autoreload = 1daemonize = /home/www/wechat/uwsgi.log

安装supervisor,sudo apt-get install supervisor

配置supervisor,vim /etc/supervisor/conf.d/wechat.conf:

[program:wechat]# 启动命令入口command=/home/www/wechat/venv/bin/uwsgi /home/www/wechat/config.ini# 命令程序所在目录directory=/home/www/wechat#运行命令的用户名user=rootautostart=trueautorestart=true#日志地址stdout_logfile=/home/www/wechat/wechat_uwsgi_supervisor.log 

安装nginx,sudo apt-get install nginx

配置nginx,修改/etc/nginx/sites-available/default文件,使其内容为:

server {
listen 80;
server_name XXX.XXX.XXX; #公网地址

  location / {    include      uwsgi_params;    uwsgi_pass   127.0.0.1:8001;  # 指向uwsgi 所应用的内部地址,所有请求将转发给uwsgi 处理    uwsgi_param UWSGI_PYHOME /home/wwwwechat/venv; # 指向虚拟环境目录    uwsgi_param UWSGI_CHDIR  /home/www/wechat/nova_weixin; # 指向网站根目录    uwsgi_param UWSGI_SCRIPT manage:app; # 指定启动程序  }}

安装mysql:sudo apt-get install mysql-server mysql-client并设置密码
修改mysql配置,参考:http://blog.csdn.net/imjtrszy/article/details/4697553,sudo vim /etc/mysql/my.cnf,在[client]和[mysqld]下分别添加default-character-set = utf8,character-set-server= utf8,然后修改max_allowed_packet=128M(防止导入数据库文件失败),保存退出

重启mysql服务:sudo service mysql restart

修改config文件中的相关配置

启动supervisor:sudo service supervisor start

启动nginx:sudo service nginx start

0 0
原创粉丝点击