Python Tornado Nginx搭建和使用
Python Tornado Nginx搭建和使用
先說主流程
我用的伺服器是CentOS,騰訊雲
uname -a
Linux VM_0_11_centos 3.10.0-862.11.6.el7.x86_64
1.先通過pip安裝Python Tornado
sudo pip install tornado
2.將網站上傳到伺服器
通過scp就可以上傳了,通過下面的命令就會將你的網站的檔案都上傳到伺服器的目錄 /var/www/testsite
下
scp -r testsite [email protected]
_server_ip:/var/www/testsite
3.安裝supervisor用來管理程式
sudo pip install supervisor
supervisord -v #檢視版本,輸出為3.3.4
4.通過yum安裝Nginx,然後給Nginx配置
sudo yum install nginx
nginx -v # 檢視版本,輸出為nginx version: nginx/1.12.2
一般網站的檔案放在目錄 /var/www/
下
假如,我們不使用nginx
,在啟動supervisor
來監管我們的網站程式時也是可以。那可以在終端上登入伺服器,然後通過curl
看下關係圖
然後配置網站程序的監管配置
先生成一個配置檔案
echo_supervisord_conf > supervisord.conf
用vi開啟編輯,滑動到最底部,修改 files = relative/directory/*.ini
為 files = /etc/supervisor/*.conf
,並且取消前面的分號註釋
vi supervisord.conf
按ESC + :wq 儲存退出
然後將 supervisord.conf
檔案挪到目錄 /etc/
目錄下
sudo mv supervisord.conf /etc/
然後新增網站的程序配置,在 /etc/
下新建一個目錄 supervisor
,然後在 /etc/supervisor/
目錄下,新建一個配置檔案 tornado.conf
。這個檔案會被上面修改的那個配置檔案(/etc/supervisord.conf
)所讀取。
tornado.confg
檔案內容如下
[group:tornadoes]
programs=tornado-8000,tornado-8001,tornado-8002,tornado-8003
[program:tornado-8000]
command=/home/centos/myenvpy3/bin/python3 /var/www/testsite/main.py --port=8000
directory=/var/www/testsite
user=root
autorestart=true
redirect_stderr=true
stdout_logfile=/var/www/testsite/tornado.log
loglevel=info
[program:tornado-8001]
command=/home/centos/myenvpy3/bin/python3 /var/www/testsite/main.py --port=8001
directory=/var/www/testsite
user=root
autorestart=true
redirect_stderr=true
stdout_logfile=/var/www/testsite/tornado.log
loglevel=info
[program:tornado-8002]
command=/home/centos/myenvpy3/bin/python3 /var/www/testsite/main.py --port=8002
directory=/var/www/testsite
user=root
autorestart=true
redirect_stderr=true
stdout_logfile=/var/www/testsite/tornado.log
loglevel=info
[program:tornado-8003]
command=/home/centos/myenvpy3/bin/python3 /var/www/testsite/main.py --port=8003
directory=/var/www/testsite
user=root
autorestart=true
redirect_stderr=true
stdout_logfile=/var/www/testsite/tornado.log
loglevel=info
其中command配置說明:
/home/centos/myenvpy3/bin/python3
表示要執行的python
/var/www/testsite/main.py --port=8000
表示你的網站主啟動檔案和指定啟動埠
啟動網站程序的監管程式
supervisord -c /etc/supervisord.conf
檢視程序
ps aux | grep supervisord
檢視監管的網站程序狀態
supervisorctl status
輸出是
tornadoes:tornado-8000 RUNNING pid 6705, uptime 0:35:29
tornadoes:tornado-8001 RUNNING pid 6706, uptime 0:35:29
tornadoes:tornado-8002 RUNNING pid 6707, uptime 0:35:29
tornadoes:tornado-8003 RUNNING pid 6708, uptime 0:35:29
更多的命令,可以看這裡
常用的命令如下
sudo supervisorctl reread # 重新讀取
sudo supervisorctl reload # 重新載入
sudo supervisorctl status # 檢視狀態
supervisorctl status # 檢視程式狀態
stop tornadoes:* # 關閉 tornadoes組 程式
start tornadoes:* # 啟動 tornadoes組 程式
restart tornadoes:* # 重啟 tornadoes組 程式
update # 重啟配置檔案修改過的程式
在伺服器上訪問網站
curl http://127.0.0.1:8000
curl http://127.0.0.1:8001
curl http://127.0.0.1:8002
curl http://127.0.0.1:8003
以上四個連線都可以訪問你的網站
Nginx的配置
nginx
安裝完後,會在/etc/
目錄下多一個nginx
目錄,在該 nginx
目錄下,有一個 nginx.conf
檔案,我們對它進行編輯
vi /etc/nginx/nginx.conf
檔案要修改和新增的是
upstream tornadoes {
server 127.0.0.1:8000;
server 127.0.0.1:8001;
server 127.0.0.1:8002;
server 127.0.0.1:8003;
}
第二處
location /static {
root /var/www/testsite;
if ($query_string) {
expires max;
}
}
location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme; # 協議 http https
proxy_pass http://tornadoes;
}
完整的檔案如下:
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
upstream tornadoes {
server 127.0.0.1:8000;
server 127.0.0.1:8001;
server 127.0.0.1:8002;
server 127.0.0.1:8003;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location /static {
root /var/www/testsite;
if ($query_string) {
expires max;
}
}
location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme; # 協議 http https
proxy_pass http://tornadoes;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
儲存,退出
然後啟動nginx
Nginx的命令
service nginx start # 啟動
service nginx stop # 停止
service nginx restart # 重啟
sudo nginx -s reload # 重啟
此時在你的客戶端電腦上就可以訪問你剛配置的網站了,訪問地址是:
你的伺服器ip,屬於http訪問。如下圖所示
Nginx官方指導:
https://www.nginx.com/resources/wiki/start/topics/examples/full/
另一個指導:
https://www.linode.com/docs/web-servers/nginx/how-to-configure-nginx/
如果發現nginx啟動不了,顯示nginx Address already in use
解決方案是:
1.檢視80埠和443埠誰在用
sudo netstat -tulpn
2.關閉80和443埠
sudo fuser -k 80/tcp
sudo fuser -k 443/tcp
3.重啟nginx
sudo service nginx restart
安裝和配置參考:
https://blog.csdn.net/tichimi3375/article/details/82153171
Nginx無法啟動的參考:
https://stackoverflow.com/questions/35868976/nginx-not-started-and-cant-start