部署前檢查
在部署web應用之前,需要先檢查一下即將部署的web應用的端口是否被佔用,若被佔用則需要考慮是否更換web應用的端口,或者關閉佔用端口的其他應用。
查看所有監聽端口:
netstat -ntlp
查看具體端口:
netstat -lnp|grep 80 #查看80端口
部署步驟:
1. 安裝WSGI
組件gunicorn
通過python安裝工具pip
安裝gunicorn
pip install gunicorn
安裝成功,測試運行
gunicron -w4 -b0.0.0.0:8000 myapp:app #其中myapp爲項目文件名,app爲application程序
2. 安裝進程任務管理器supervisor
pip install supervisor
3. 生成 supervisor 默認配置文件
echo_supervisord_conf > supervisor.conf
4. 修改 supervisor 配置文件,添加 gunicorn 進程管理
在myapp supervisor.conf 配置文件底部添加
vim supervisor.conf
[program:myapp]
command=/myproject/MyProject1/venv/bin/gunicorn -w4 -b0.0.0.0:2170 myapp:app ; directory=/myproject/MyProject1 ;
startsecs=0 ;
stopwaitsecs=0 ;
autostart=true ;
autorestart=true ;
stdout_logfile=/myproject/MyProject1/log/gunicorn.log ;
stderr_logfile=/myproject/MyProject1/log/gunicorn.err ;
5. 調用supervisor
進程任務管理器
supervisor基本使用命令:
supervisord -c supervisor.conf 通過配置文件啓動
supervisorctl -c supervisor.conf status 察看
supervisor的狀態 supervisorctl -c supervisor.conf reload 重新載入 配置文件
supervisorctl -c supervisor.conf start [all]|[appname] 啓動指定/所有 supervisor管理的程序進程
supervisorctl -c supervisor.conf stop [all]|[appname] 關閉指定/所有 supervisor管理的程序進程
supervisorctl -c supervisor.conf update 更新配置文件(當supervisor.conf被修改的時候)
# 取消配置文件
unlink [配置文件位置] # 一般爲/tmp/supervisor.sock
6. 安裝配置 nginx
1) 安裝依賴
yum -y install pcre-devel #支持正則的pcre模塊
yum -y install openssl openssl-devel
2) 下載並解壓
wget http://nginx.org/download/nginx-1.10.3.tar.gz
tar -xvf nginx-1.10.3.tar.gz
3) 配置路徑
./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid
4) 編輯安裝
make && make install
5) 測試
/usr/local/nginx/nginx -t
出現以下信息表示安裝成功
nginx: the configuration file /usr/local/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/nginx.conf test is successful
7. 配置nginx
:
將/usr/local/nginx/nginx.conf中相應的部分改爲以下:
server {
listen 80;
server_name example.org; # 這是HOST機器的外部域名,用地址也行
location / {
proxy_pass http://127.0.0.1:8080; # 這裏是指向 gunicorn host 的服務地址
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
8. https配置
server {
listen 443;
server_name your-domain-name;
ssl on;
root html;
index index.html index.htm;
ssl_certificate cert/214293826510663.pem;
ssl_certificate_key cert/214293826510663.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
}
}
server {
listen 443 ssl;
server_name www.example.com;
ssl on;
ssl_certificate /usr/local/nginx/key/2141051716***.pem;
ssl_certificate_key /usr/local/nginx/key/2141051716***.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #指定SSL服務器端支持的協議版本
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; #指定加密算法
ssl_prefer_server_ciphers on; #在使用SSLv3和TLS協議時指定服務器的加密算法要優先於客戶端的加密算法
location /{
proxy_pass http://localhost:8081;
}
}
最後運行:supervisorctl -c supervisor.conf reload