nginx+gunicorn項目部署

1.1安裝虛擬環境
建立文件夾
mkdir data          目錄文件夾
cd data             進入data文件夾
mkdir nginx         建立安裝nginx的文件夾
mkdir server        存放代碼的文件夾    
mkdir logs          存放日誌的文件夾
mkdir backup        備份代碼的文件夾
mkdir softs         軟件存放的位置
mkdir virtual       虛擬環境的位置
mkdir scripts       腳本的運行位置
mkdir scp_codes     上傳的代碼
安裝虛擬環境
apt-get install python-virtualenv (ubuntu已經安裝過,可省略)
virtualenv -p /usr/bin/python3 api_server (採用他,安裝在本文件夾,)
mkvirtualenv api_server(不要用它,它會本身安裝到其餘地方)
source api_server/bin/activate   (在虛擬環境的active進入虛擬環境)
安裝的軟件會在虛擬環境下的bin目錄下
安裝nginx的環境
解壓
cd ~/data/softs/   
tar xf pcre-8.39.tar.gz
配置
cd ~/data/softs/pcre-8.39
./configure
編譯
make
安裝
sudo make install
安裝nginx
解壓
cd ~/data/softs/
tar xf nginx-1.10.2.tar.gz
配置
cd nginx-1.10.2/
./configure --prefix=/root/data/nginx    安裝在root/data下面的nginx文件夾
編譯
make
安裝
make install
查看進程
ps aux | grep nginx
nginx簡單基礎操做
檢查
sudo ~/data/nginx/sbin/nginx -t
開啓
sudo ~/data/nginx/sbin/nginx
關閉
sudo ~/data/nginx/sbin/nginx -s stop
重載
sudo ~/data/nginx/sbin/nginx -s reload
一、首先安裝Gunicorn
pip install gunicorn
複製代碼
二、在入口文件的app.run()加上如下內容
from werkzeug.contrib.fixers import ProxyFix
app.wsgi_app = ProxyFix(app.wsgi_app)
複製代碼

例:python

from flask import Flask

app = Flask(__name__)


@app.route('/')
def hello_world():
    return 'Hello World!'

if __name__ == '__main__':
    from werkzeug.contrib.fixers import ProxyFix
    app.wsgi_app = ProxyFix(app.wsgi_app)
    app.run()
複製代碼
三、啓動Gunicorn

最簡單的方式爲nginx

gunicorn 入口文件名:app
複製代碼

默認是監聽127.0.0.1:8000git

若是是要處理高併發則要開多個進程和修改監聽端口的畫sql

gunicorn -w 4 -b 127.0.0.1:8000 入口文件名:app
複製代碼保存在supervisord 裏面的etc/supervisord.d目錄下添加一個logo.ini配置文件
添加配置文件

vim /etc/supervisord.d/logo.ininpm

[program:logo_api_server]
directory = /data/api-service #代碼存放的地方
command = /usr/local/python3/bin/gunicorn -w 4 -b :5005 LogoSeverApi:app  #-w的參數根據CPU的核數來定,不要超過CPU的核數
#process_name = %(process_num)s  ; process_name expr (default %(program_name)s)
#process_name = %(process_num)s
#numprocs = 4                    ; number of processes copies to start (def 1)
numprocs_start = 1
autostart = true                ; start at supervisord start (default: true)
autorestart = unexpected        ; whether/when to restart (default: unexpected)
startsecs = 10                  ; number of secs prog must stay running (def. 1)
startretries = 3                ; max # of serial start failures (default 3)
user = dev
redirect_stderr = true
stdout_logfile_maxbytes = 20MB
stdout_logfile_backups = 20
stdout_logfile = /data/api-service/logs/supervisor.log

這樣就能夠啓動4個進程同時處理HTTP請求,提升系統的使用效率及性能。 還能夠把端口8000改成其餘flask

而在實際應用中,應使用後臺執行的方式啓動服務ubuntu

nohup 啓動服務的命令 &
複製代碼

vim

nohup gunicorn -w 4 -b 127.0.0.1:8000 入口文件名:app &
複製代碼

這時你能夠在本機的瀏覽器上訪問127.0.0.1:8000,瀏覽器上就會出現Hello World!api

注意:若是是想經過外網訪問的話就要把ip改成內網的ip瀏覽器

四、配置nginx

配置改成

server {
    listen 80;
    server_name example.org; # 這是HOST機器的外部域名,用地址也行

    location / {
        proxy_pass http://127.0.0.1:8000; # 這裏是指向 gunicorn host 的服務地址
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
 }
複製代碼

這樣啓動以後80端口就能夠轉發到8000端口了

五、檢查配置
nginx -t
複製代碼

若出現一下內容,則表示配置成功

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
複製代碼
六、更新nginx配置
nginx -s reload
複製代碼

在已經運行了Gunicorn的前提下,在瀏覽器訪問127.0.0.1就會出現Hello World!了

七、重啓Gunicorn

輸入命令

pstree -ap|grep gunicorn
1.2:安裝wget(若是已經安裝跳過)
yum install wget
1.3:安裝python3.6(若是已經安裝跳過)

2.1.下載python3.6

wget http://cdn.npm.taobao.org/dist/python/3.6.5/Python-3.6.5.tgz

2.2.解壓安裝包

tar -zxvf Python-3.6.5.tgz

---------------------

2.3安裝python3.6的依賴包
yum install -y gcc zlib*

yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel

---------------------

2.4.配置安裝路徑爲/usr/local/python3
./configure --prefix=/usr/local/python3 --with-ssl

make && make install
2.5.將python3.6的路徑加入到PATH中
echo "export PATH=$PATH:/usr/local/python3/bin/" >>/etc/profile

source /etc/profile
2.6.創建軟鏈接
ln -s /usr/local/python3/bin/python3 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3

---------------------

2.7.安裝supervisor
yum install supervisor -y

若是報以下錯誤,安裝yum的擴展源

安裝yum的擴展源

yum install epel-*
2.8.添加supervisor的配置

在/etc/supervisord.d目錄下添加一個logo.ini配置文件

vim /etc/supervisord.d/logo.ini

內容以下:

[program:logo_api_server]

directory = /data/api-service #代碼存放的地方

command = /usr/local/python3/bin/gunicorn -w 4 -b :5005 LogoSeverApi:app  #-w的參數根據CPU的核數來定,不要超過CPU的核數

\#process_name = %(process_num)s  ; process_name expr (default %(program_name)s)

\#process_name = %(process_num)s

\#numprocs = 4                    ; number of processes copies to start (def 1)

numprocs_start = 1

autostart = true                ; start at supervisord start (default: true)

autorestart = unexpected        ; whether/when to restart (default: unexpected)

startsecs = 10                  ; number of secs prog must stay running (def. 1)

startretries = 3                ; max # of serial start failures (default 3)

user = dev

redirect_stderr = true

stdout_logfile_maxbytes = 20MB

stdout_logfile_backups = 20

stdout_logfile = /data/api-service/logs/supervisor.log
2.9建立data目錄
mkdir /data
2.10從git上面將代碼下載下來,git在內網,若是是線上請將git下載下來而後經過ftp上傳到線上壞境
cd /data

git clone http://zhangxiaoyang:messi1020@git.epweike.net:3000/graphics/logo_api api-servicels -
2.11安裝程序依賴包
cd /data/api-service/

yum install cairo-devel

pip3 install --upgrade pip

pip3 install -r requirements.txt
相關文章
相關標籤/搜索