{"id":766,"date":"2021-03-28T10:10:34","date_gmt":"2021-03-28T02:10:34","guid":{"rendered":"https:\/\/www.sunjw.cn\/?p=766"},"modified":"2022-02-12T21:51:21","modified_gmt":"2022-02-12T13:51:21","slug":"python-flask-ngix","status":"publish","type":"post","link":"https:\/\/www.sunjw.cn\/index.php\/2021\/03\/28\/python-flask-ngix\/","title":{"rendered":"[python] flask + ngnix"},"content":{"rendered":"<p>\u6587\u672c\u8bb0\u5f55\u4e86\u5f53\u4f7f\u7528python+flask\u7684\u5e94\u7528\u65f6\uff0c\u5982\u4f55\u914d\u5408ngnix\u5b9e\u73b0\u901a\u8fc780\u7aef\u53e3\u8bbf\u95ee\uff0c\u4ece\u800c\u4e5f\u5c31\u53ef\u4ee5\u4f7f\u7528\u57df\u540d\u76f4\u63a5\u8bbf\u95eepython app\u3002<\/p>\n<p>https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-serve-flask-applications-with-uswgi-and-nginx-on-ubuntu-18-04<\/p>\n<p>\u8bc4\u8bba\u4e2d\u6709502\u9519\u8bef\u89e3\u51b3\u65b9\u6cd5\u3002<\/p>\n<p>\u5982\u679c\u6709\u591a\u4e2aapp\uff0cvirtualenv\u53ef\u4ee5\u5171\u7528\uff0c\u53ea\u8981\u5728service\u7684\u8def\u5f84\u91cc\u9762\u5199\u4e0a\u5b9e\u9645\u8def\u5f84\u5373\u53ef<\/p>\n<p>virtualenv webprojectenv<\/p>\n<p>source webprojectenv\/bin\/activate<\/p>\n<p>502<\/p>\n<p>For those that have the 502 gateway error try changing\u00a0chmod-socket = 660<\/p>\n<p>uwsgi_pass unix:\/\/\/home\/sammy\/myproject\/myproject.sock;<\/p>\n<p>&nbsp;<\/p>\n<p>\u5047\u8bbe\u76ee\u5f55\u4e3atest1<\/p>\n<p>\u8fd0\u884ccreate.sh<\/p>\n<p>createsh test1<br \/>\n<span class=\"token builtin class-name\">source<\/span> <mark>myprojectenv<\/mark>\/bin\/activate<br \/>\n\u589e\u52a0.py or template<\/p>\n<p>\u6d4b\u8bd5\u662f\u5426\u6b63\u5e38<\/p>\n<ol>\n<li data-prefix=\"(myprojectenv) $\">uwsgi &#8211;socket <span class=\"token number\">0.0<\/span>.0.0:5000 &#8211;protocol<span class=\"token operator\">=<\/span>http -w wsgi:app<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p>\u5982\u4e0b\u4ee3\u7801\u5b58\u4e3acreate.sh, \u5373\u4f7f\u76ee\u5f55\u5b58\u5728\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u6b64\u811a\u672c\u91cd\u65b0\u914d\u7f6e<\/p>\n<p>if [ $# -ne 1 ]<br \/>\nthen<br \/>\necho &#8220;Error input, must be a project name&#8221;<br \/>\nexit<br \/>\nfi<\/p>\n<p>NAME=$1<br \/>\nDIRNAME=$1<br \/>\necho $NAME<br \/>\nmkdir ${NAME}<br \/>\n#NAME=$1\/$1<br \/>\necho &#8220;[uwsgi]&#8221; &gt; ${NAME}.ini<br \/>\necho &#8220;module = wsgi:app&#8221; &gt;&gt; ${NAME}.ini<br \/>\necho &#8220;&#8221; &gt;&gt; ${NAME}.ini<br \/>\necho &#8220;master = true&#8221; &gt;&gt; ${NAME}.ini<br \/>\necho &#8220;processes = 1&#8221; &gt;&gt; ${NAME}.ini<br \/>\necho &#8220;&#8221;&gt;&gt;${NAME}.ini<br \/>\necho &#8220;socket = ${NAME}.sock&#8221;&gt;&gt;${NAME}.ini<br \/>\necho &#8220;chmod-socket = 666&#8221;&gt;&gt;${NAME}.ini<br \/>\necho &#8220;vacuum = true&#8221;&gt;&gt;${NAME}.ini<br \/>\necho &#8220;&#8221;&gt;&gt;${NAME}.ini<br \/>\necho &#8220;die-on-term = true&#8221;&gt;&gt;${NAME}.ini<br \/>\nmv ${NAME}.ini $DIRNAME\/<\/p>\n<p>echo &#8220;============================&#8221;<br \/>\necho &#8220;from ${NAME} import app&#8221; &gt; wsgi.py<br \/>\necho &#8220;&#8221;&gt;&gt;wsgi.py<br \/>\necho &#8220;if __name__ == \\&#8221;__main__\\&#8221;:&#8221;&gt;&gt;wsgi.py<br \/>\necho &#8221; app.run()&#8221; &gt;&gt; wsgi.py<br \/>\nmv wsgi.py $DIRNAME<br \/>\ncp .\/dict\/webprojectenv $DIRNAME -rf<\/p>\n<p>echo &#8220;============================&#8221;<br \/>\necho &#8220;[Unit]&#8221;&gt; ${NAME}.service<br \/>\necho &#8220;Description=uWSGI instance to serve ${NAME} api&#8221;&gt;&gt;${NAME}.service<br \/>\necho &#8220;After=network.target&#8221;&gt;&gt;${NAME}.service<br \/>\necho &#8220;[Service]&#8221;&gt;&gt;${NAME}.service<br \/>\necho &#8220;User=sunny&#8221;&gt;&gt;${NAME}.service<br \/>\necho &#8220;Group=www-data&#8221;&gt;&gt;${NAME}.service<br \/>\necho &#8220;WorkingDirectory=\/home\/sunny\/webproject\/${NAME}&#8221;&gt;&gt;${NAME}.service<br \/>\necho &#8220;Environment=\\&#8221;PATH=\/home\/sunny\/webproject\/${NAME}\/webprojectenv\/bin\\&#8221;&#8221;&gt;&gt;${NAME}.service<br \/>\necho &#8220;ExecStart=\/home\/sunny\/webproject\/${NAME}\/webprojectenv\/bin\/uwsgi &#8211;ini ${NAME}.ini&#8221;&gt;&gt;${NAME}.service<br \/>\necho &#8220;[Install]&#8221;&gt;&gt;${NAME}.service<br \/>\necho &#8220;WantedBy=multi-user.target&#8221;&gt;&gt;${NAME}.service<br \/>\necho &#8220;============================&#8221;<br \/>\nmv ${NAME}.service $DIRNAME\/<br \/>\nsudo cp .\/${DIRNAME}\/${NAME}.service \/etc\/systemd\/system<br \/>\nsudo cp .\/${DIRNAME}\/${NAME}.service \/etc\/systemd\/system<br \/>\nsudo systemctl start ${NAME}<br \/>\nsudo systemctl enable ${NAME}<br \/>\nsudo systemctl status ${NAME}<br \/>\nsudo systemctl restart nginx<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u672c\u8bb0\u5f55\u4e86\u5f53\u4f7f\u7528python+flask\u7684\u5e94\u7528\u65f6\uff0c\u5982\u4f55\u914d\u5408ngnix\u5b9e\u73b0\u901a\u8fc780\u7aef\u53e3\u8bbf\u95ee\uff0c\u4ece\u800c\u4e5f\u5c31\u53ef\u4ee5\u4f7f\u7528\u57df\u540d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[],"_links":{"self":[{"href":"https:\/\/www.sunjw.cn\/index.php\/wp-json\/wp\/v2\/posts\/766"}],"collection":[{"href":"https:\/\/www.sunjw.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sunjw.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sunjw.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sunjw.cn\/index.php\/wp-json\/wp\/v2\/comments?post=766"}],"version-history":[{"count":9,"href":"https:\/\/www.sunjw.cn\/index.php\/wp-json\/wp\/v2\/posts\/766\/revisions"}],"predecessor-version":[{"id":834,"href":"https:\/\/www.sunjw.cn\/index.php\/wp-json\/wp\/v2\/posts\/766\/revisions\/834"}],"wp:attachment":[{"href":"https:\/\/www.sunjw.cn\/index.php\/wp-json\/wp\/v2\/media?parent=766"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sunjw.cn\/index.php\/wp-json\/wp\/v2\/categories?post=766"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sunjw.cn\/index.php\/wp-json\/wp\/v2\/tags?post=766"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}