apt-get update
apt-get upgrade
apt-get install python3-pip
pip3 install virtualenv
apt-get install nginx
pip3 install uwsgi
cd /srv
which python3
virtualenv -p /usr/bin/python3 flask-env
. flask-env/bin/activate
pip install flask
(在虛擬環境中不管是py2仍是py3用的時候都是用pip)deactivate
(安裝成功後退出)mkdir myproject
cd /myproject
vim run.py
from flask import Flask
app = Flask(__name__)
@app.route("/")
def helloWorld():
return "Hello World"
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
複製代碼
保存並退出:esc : wq
service nginx start
cd /srv/myproject/
vim test.ini
[uwsgi]
chdir = /srv/myproject
module = run
home = /srv/flask-env
master = true
processes = 10
socket = /srv/myproject/myproject.sock
wsgi-file = /srv/myproject/run.py
callable = app
chmod-socket = 666
plugins = python35
log-maxsize = 50000000
disable-logging = true
pidfile = /tmp/uwsgi.pid
vacuum = true
複製代碼
cd /etc/nginx/conf.d/
vim myproject.conf
upstream myproject {
server unix:///srv/myproject/myproject.sock;
}
server {
listen 8099;
server_name 66.42.100.165;
charset utf-8;
client_max_body_size 75M;
location / {
uwsgi_pass myproject;
include /etc/nginx/uwsgi_params;
}
}
複製代碼
nginx -t
或者 service nginx configtest
iptables -A INPUT -p tcp --dport 8099 -j ACCEPT
iptables -A INPUT -p tcp --dport 8099 -j DROP
service nginx restart
cd /srv/myproject/
uwsgi --ini test.ini
66.42.100.165:8099
Ctrl + c
ps:這個真的是深坑,網上的教程都Tm缺斤少兩各類不通,本人查閱大量文章-文檔與調試,斷斷續續花費近兩週時間-最終得出如下配置方式,但願大夥伴認真閱讀而且跟着操做操做。python
apt-get install supervisor
cd /etc/supervisor/
cd /etc/supervisor/conf.d
vim myproject.conf
名字隨便取[program:myproject]
command = uwsgi --ini /srv/myproject/test.ini
directory = /srv/myproject
startsecs = 0
stopwaitsecs = 0
startretries = 3
autostart = true
autorestart = true
stdout_logfile = /srv/myproject/super_log/supervisord.log
stderr_logfile = /srv/myproject/super_log/supervisord.err
user = root
stdout_logfile_maxbytes = 20MB
stdout_logfile_backups = 20
environment=PYTHONPATH = "/usr/bin/python3"
redirect_stderr = false
stopasgroup = false
killasgroup = false
複製代碼
cd /srv/myproject
mkdir super_log
supervisorctl update
supervisorctl reload
supervisorctl status
cd /etc/supervisor/
vim supervisord.conf
按shift+G 跳至末尾添加
[inet_http_server]
port = IP:9001
username = user
password = 666666
複製代碼
supervisorctl update
supervisorctl reload
error: <class 'socket.error'>, [Errno 2] No such file or directory: file: /usr/lib/python2.7/socket.py line: 228
不要慌重啓一下服務器繼續執行以上的update和reloadiptables -A INPUT -p tcp --dport 9001 -j ACCEPT
cd /etc/rc.local
service supervisor start
保存!