首先,确定系统python版本环境python3.5.2,确保安装virtualenv,uwsgi,supervisor.
安装python3-pip:sudo apt-get install python3-pip
安装python virtualenv:sudo apt-get install python-virtualenv
安装Nginx:sudo apt-get install nginx
python3在指定的目录下创建虚拟环境(安装 pip3 install virtualenv)
如:/home/demo/pyapp/下 执行 virtualenv -p /usr/bin/python3 venv 创建虚拟环境
激活虚拟环境 source venv/bin/activate(取消虚拟环境,直接输入 deactivate),查看当前环境下python的版本 python,然后开始安装python项目依赖包(生成当前环境依赖 pip3 freeze > requirements.txt),将pycharm中的requirements.txt放在pyapp目录下,执行pip install -r requirements.txt进项安装;
next:在venv环境下安装uwsgi: pip install uwsgi,并在pyapp目录下创建uwsgi的配置文件如:
nano uwsgi-config.ini,配置内容如下:
[uwsgi]
socket = 127.0.0.1:3031
plugins = python
chdir = /home/jtzhang/pyapp/workload
wsgi-file = workloader.py
callable = app
processes = 2
threads = 2
stats = 127.0.0.1:3011
然后,启动uwsgi,看当前配置是否有问题:uwsgi uwsgi-config.ini 查看执行结果是否成功(***start server enable on *.*.*.*:**** fb:* ***,此格式代表成功,ctrl + c结束)。
third:安装配置supervisor,安装:sudo apt-get install supervisor
创建supervisor配置文件:cd /etc/supervisor/conf.d 并 sudo nano blogSupervisor.conf
具体内容如下
[program:workload]
command = /home/jtzhang/pyapp/workload/venv/bin/uwsgi /home/jtzhang/pyapp/workload/uwsgi-config.ini #(重点:确保在命令行输入 uwgsi 能启动该命令,即保证是在虚拟环境里安装的 uwsgi, 且在虚拟环境下启动该命令)
directory=/home/jtzhang/pyapp/workload
stopsignal=QUIT
user=jtzhang
autostart=true
autorestart=true
stdout_logfile=/home/jtzhang/pyapp/workload/logs/worksupervisor.log
[supervisord]
启动supervisor :supervisorctl reload或supervisord -c /etc/supervisor/conf.d/apps_supervisor.conf或sudo service supervisor start
检查uwsgi进程是否正常运行 ps aux|grep uwsgi 杀掉所有进程 sudo killall uwsgi
若再次通过ps aux|grep uwsgi查看发现有新的uwsgi进程在运行,那差不多可以祝你成功了
最后配置nginx:
进入/etc/nginx/sites-available/,编辑里面default文件,其中内容是:
server {
listen 80;
server_name 192.168.81.130;
charset utf-8;
client_max_body_size 75M;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:3031;
uwsgi_param UWSGI_PYHOME /home/jtzhang/pyapp/workload/venv;
uwsgi_param UWSGI_CHDIR /home/jtzhang/pyapp/workload;
uwsgi_param UWSGI_SCRIPT workloader:app;
}
}
重启Nginx : sudo service nginx restart
ip输入浏览器验证结果