文本记录了当使用python+flask的应用时,如何配合ngnix实现通过80端口访问,从而也就可以使用域名直接访问python app。
https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-uswgi-and-nginx-on-ubuntu-18-04
评论中有502错误解决方法。
如果有多个app,virtualenv可以共用,只要在service的路径里面写上实际路径即可
virtualenv webprojectenv
source webprojectenv/bin/activate
502
For those that have the 502 gateway error try changing chmod-socket = 660
uwsgi_pass unix:///home/sammy/myproject/myproject.sock;
假设目录为test1
运行create.sh
createsh test1
source myprojectenv/bin/activate
增加.py or template
测试是否正常
- uwsgi –socket 0.0.0.0:5000 –protocol=http -w wsgi:app
如下代码存为create.sh, 即使目录存在,也可以使用此脚本重新配置
if [ $# -ne 1 ]
then
echo “Error input, must be a project name”
exit
fi
NAME=$1
DIRNAME=$1
echo $NAME
mkdir ${NAME}
#NAME=$1/$1
echo “[uwsgi]” > ${NAME}.ini
echo “module = wsgi:app” >> ${NAME}.ini
echo “” >> ${NAME}.ini
echo “master = true” >> ${NAME}.ini
echo “processes = 1” >> ${NAME}.ini
echo “”>>${NAME}.ini
echo “socket = ${NAME}.sock”>>${NAME}.ini
echo “chmod-socket = 666”>>${NAME}.ini
echo “vacuum = true”>>${NAME}.ini
echo “”>>${NAME}.ini
echo “die-on-term = true”>>${NAME}.ini
mv ${NAME}.ini $DIRNAME/
echo “============================”
echo “from ${NAME} import app” > wsgi.py
echo “”>>wsgi.py
echo “if __name__ == \”__main__\”:”>>wsgi.py
echo ” app.run()” >> wsgi.py
mv wsgi.py $DIRNAME
cp ./dict/webprojectenv $DIRNAME -rf
echo “============================”
echo “[Unit]”> ${NAME}.service
echo “Description=uWSGI instance to serve ${NAME} api”>>${NAME}.service
echo “After=network.target”>>${NAME}.service
echo “[Service]”>>${NAME}.service
echo “User=sunny”>>${NAME}.service
echo “Group=www-data”>>${NAME}.service
echo “WorkingDirectory=/home/sunny/webproject/${NAME}”>>${NAME}.service
echo “Environment=\”PATH=/home/sunny/webproject/${NAME}/webprojectenv/bin\””>>${NAME}.service
echo “ExecStart=/home/sunny/webproject/${NAME}/webprojectenv/bin/uwsgi –ini ${NAME}.ini”>>${NAME}.service
echo “[Install]”>>${NAME}.service
echo “WantedBy=multi-user.target”>>${NAME}.service
echo “============================”
mv ${NAME}.service $DIRNAME/
sudo cp ./${DIRNAME}/${NAME}.service /etc/systemd/system
sudo cp ./${DIRNAME}/${NAME}.service /etc/systemd/system
sudo systemctl start ${NAME}
sudo systemctl enable ${NAME}
sudo systemctl status ${NAME}
sudo systemctl restart nginx