Linux-centos/-Linux-centos__nginx-uwsgi

flask + nginx + uwsgi

말하는감자 2019. 9. 25. 10:42

1. flask 설치

pip install flask, flask_restful

 


(+flask 프로젝트가 없는 사람을 위하여)

flask 테스트 프로젝트를 생성해서 테스트하기

cd /home

mkdir flask_test

cd flask_test

 

vim wsgi.py 내용 작성 후에 저장

from app import app

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=아무번호나)

vim app.py 내용 작성 후에 저장

from flask import Flask
import request

app = Flask(__name__)

@app.route("/test1/<test>", methods=["GET"])
def test1(test):
    print(test)
    
@app.route("/test2", methods=["POST"])
def test2():
    test2 = request.form['test2']
    print(test2)

 

2. uwsgi 설치

pip install uwsgi

 

 

3.uwsgi.ini 파일을 작성해준다(이름은 다르게 해도 괜츈)

vim uwsgi.ini

[uwsgi]

chdir = /파일경로
module = wsgi:app

master = true
processes = 프로세스는몇개로할지

socket = /tmp/소켓이름은무엇으로할것인가.sock
chmod-socket = 666
vacuum = true

die-on-term = true
logto = /var/log/uwsgi/로그파일이름.log
pyargv=123

#processes = 20
socket-timeout = 3000
http-timeout = 3000

ignore-sigpipe = true
ignore-write-errors = true
disable-write-exception = true
~

 

 

4.  uwsgi 설치 후에 테스트를 한번 해본다

만약 여기서 실패한다면 nginx 설치해도 무쓸모...

uwsgi --socket 0.0.0.0:포트번호 --protocol=http --wsgi-file wsgi.py
 uwsgi --socket :포트번호 --protocol=http --wsgi-file wsgi.py --callable app

 

만약 이때 getopt_long() 오류가 발생한다거나 

--wsgi-file 을 읽지 못한다거나

설치한 각종 라이브러리들이 설치되어 있지 않는다고 노출된다면

uwsgi --plugin python uwsgi.ini &

 

5. nginx 버젼 설정 후에 설치

vim /etc/yum.repos.d/nginx.repo

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

yum install nginx

 

 

6. nginx 시작 및 설정 추가

systemctl start nginx
systemctl tnable nginx

 

7. vim /etc/nginx/nginx.conf 내용 변경

user  nginx;
worker_processes  1; #만약 2개 띄울거면 2로 바꿔야하는듯 ㅠㅠㅠㅠ 정확히 모르겠다..

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    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;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;

    uwsgi_max_temp_file_size 20480m;
}

 

8. vim /etc/nginx/conf.d/default.conf 설정 변경

server {
    listen       외부에서 접근할 PORT;
    server_name  localhost;

    location / {
        try_files $uri @api요청 들어올 주소;
    }

    location @scrap {
        include uwsgi_params;
        uwsgi_pass unix:/tmp/소켓이름따다다다.sock;
    }

    error_page  500 502 503 504 /50x.html;

    location = /50x.html{
        root /usr/share/nginx/html;
    }
}

#-------------------------------------------------------- 만약 2개를 띄울거라면

server {
    listen       외부에서 접근할 PORT2번;
    server_name  localhost;

    location / {
        try_files $uri @api요청 들어올 주소;
    }

    location @scrap {
        include uwsgi_params;
        uwsgi_pass unix:/tmp/또다른소켓이름따다다당.sock;
    }

    error_page  500 502 503 504 /50x.html;

    location = /50x.html{
        root /usr/share/nginx/html;
    }
}

 

9. 실행

ps -eaf | grep uwsgi

uwsgi 살아있는거 있는지 확인하고 

kill -9 포트넘버로 죽여줌

 

service nginx restart

엔진엑스 재시작 해주고..

 

uwsgi.ini 파일 작성해놓은 경로로 가거나 그 파일 경로대로..

uwsgi uwsgi.ini &

 

9. curl로 간단하게 테스트

curl -L -v -d '{"test2" : "냠냠"}' -H "Accept: application/json" -H "Content-Type: application/json" http://localhost:포트번호/api호출파라미터?

 

10. 만약 테스트 실패라면

nginx log를 봐야합니다.

cd /var/log/nginx

vim error.log

vim access.log

'Linux-centos > -Linux-centos__nginx-uwsgi' 카테고리의 다른 글

nginx security setting  (0) 2020.09.18
pip install uwsgi 실패시  (0) 2020.02.17
nginx no such file or dirctory  (0) 2019.07.22