uwsgi及nginx部署

端口對應:32771(宿主機的指向9800的端口)-9800(容器端口、nginx監聽端口)-9801(uwsgi端口)
調用方式:
在宿主機,curl 宿主機ip:9800/hello
在容器內,curl localhost:9800/hello
在外部瀏覽器使用宿主機ip:9800/hello訪問,

uwsgi部署流程

1、利用pip install uwsgi下載python對應的庫
2、新建flask的python文件,例如test_1.py,如下

from flask import Flask
app = Flask(__name__)

@app.route("/hello")
def hello():
    print("hello world ")
    return "<h1 style='color:blue'>Hello There hahahaha!!!!</h1>"

if __name__ == "__main__":
    app.run(host='0.0.0.0')

3、新建調用test_1.py的python文件,例如run.py,如下

from test_1 import app

if __name__ == "__main__":
    app.run()

4、配置uwsgi的ini文件,例如test_1.ini,如下。同時在同一目錄下新建空的log文件與pid文件,例如test_1.pid、test_1.log

[uwsgi]

wsgi-file=run.py # python文件
chdir = /opt/my # python文件的地址
socket =127.0.0.1:9801 # uwsgi的端口
callable=app
processes=4
threads=4
stats=127.0.0.1:9803
daemonize=test_1.log # log文件名稱
pidfile=test_1.pid

5、啓動uwsgi
在命令行直接輸入uwsgi,會顯示錯誤,可以建立uwsgi的軟連接,如下

# 爲uwsgi創建軟連接
ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi

在ini文件所在目錄下,運行

uwsgi --ini test_1.ini

此時uwsgi已經啓動起來,輸入一下命令看一下

ps -ef|grep uwsgi

若顯示以下內容,則uwsgi啓動成功

root        47     1  4 09:38 ?        00:00:00 uwsgi --ini test_1.ini
root        48    47  0 09:38 ?        00:00:00 uwsgi --ini test_1.ini
root        49    47  0 09:38 ?        00:00:00 uwsgi --ini test_1.ini
root        53    47  0 09:38 ?        00:00:00 uwsgi --ini test_1.ini
root        54    47  0 09:38 ?        00:00:00 uwsgi --ini test_1.ini
root        65    18  0 09:38 pts/1    00:00:00 grep --color=auto uwsgi

nginx部署流程

1、安裝依賴包

//一鍵安裝上面四個依賴
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel

2、下載並解壓安裝包,nginx安裝在/usr/local/目錄下

//創建一個文件夾
cd /usr/local
mkdir nginx
cd nginx
//下載tar包
wget http://nginx.org/download/nginx-1.13.7.tar.gz
tar -xvf nginx-1.13.7.tar.g

3、安裝nginx

//進入nginx目錄
cd /usr/local/nginx
//執行命令
./configure
//執行make命令
make
//執行make install命令
make install

4、查看nginx是否可正常打開
在/usr/local/nginx/sbin/目錄下,運行./nginx -t 或者nginx -t,若出現以下,則成功

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

5、建立軟連接,如uwsgi一樣,此時直接運行nginx即啓動。可利用ps -ef|grep nginx查看

# 爲nginx創建軟連接
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx

uwsgi與nginx連接

1、配置nginx的conf文件

# 打開配置文件
vi /usr/local/nginx/conf/nginx.conf

conf內主要內容如下,不要忘了分號!!!其中listen爲docker容器的端口,可通過docker ps查看。uwsgi_pass爲對應的uwsgi的端口

server {
        listen       9800; # docker容器的端口
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            include     uwsgi_params;
            uwsgi_pass 127.0.0.1:9801; # uwsgi的端口
        }

2、啓用nginx與uwsgi

uwsgi: uwsgi -–ini test_1.ini
nginx

利用ps -ef|grep 命令可以查看兩者都已啓動
3、在容器內部curl,這裏的端口爲容器的端口

curl http://localhost:9800/hello
curl 127.0.0.1:9800/hello

若顯示如下,則連接成功

<h1 style='color:blue'>Hello There</h1>

4、在宿主機上訪問服務

參考資料
LINUX安裝nginx詳細步驟

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章