패키지, 라이브러리 설치
$ sudo apt-get update #패키지 정보 업데이트
$ sudo apt-get dist-upgrade #패키지 의존성 검사 및 업그레이드
$ sudo apt-get install nginx
$ sudo apt install python3-pip
$ pip3 install flask
$ pip3 install uwsgi
Flask 파일 생성 project.py
x# path = /home/ubuntu/works/myproject/project.py
from flask import Flask
app = Flask(__name__)
@app.route("/")
def main():
return "Hello World!"
if __name__ == "__main__":
app.run(host="0.0.0.0", port="5000")
uwsgi 실행
xxxxxxxxxx
$ uwsgi --http :5000 --virtualenv /home/ubuntu/works/myproject/venv \
--chdir = /home/ubuntu/works/myproject -w config.wsgi.deploy
uwsgi.ini 파일 생성
매번 복잡한 실행 명령어 입력대신 ini파일 생성
xxxxxxxxxx
[uwsgi]
chdir = /home/ubuntu/works/myproject [프로젝트 경로]
virtualenv = /home/ubuntu/works/myproject/venv [가상환경 경로 없다면 할 필요 x]
module = project:app
master = true
processes = 1 [프로세스 갯수]
socket = /tmp/myproject.sock [소켓이름 및 경로]
chmod-socket = 666
vacuum = true
die-on-term = true
logto = /var/log/uwsgi/myprojectuwsgi.log [로그명 및 경로]
pyargv = 123
해당과정에서 no internal routing support, rebuild with pcre support 에러발생시 path를 확인하거나
xxxxxxxxxx
$ sudo apt-get install libpcre3 libpcre3-dev
$ pip3 uninstall uwsgi
$ sudo apt-get remove uwsgi
$ pip3 install uwsgi
위와 같은 방법으로 재설치를 시도해보자.
NGINX 설정
xxxxxxxxxx
$ sudo vi /etc/nginx/sites-available/myproject[설정파일명]
server {
listen 80; (nginx를 실행할 포트)
server_name localhost;[도메인]
location / {
include uwsgi_params;
uwsgi_pass unix:/tmp/myproject.sock [uwsgi.ini에서 설정한 socket]
}
# 한개의 프로젝트만 할 경우 여기까지만 해도 됨.
# 하위 경로에 다른서버 추가
location /test/ {
rewrite ^/test(/.*)$ $1 break;
proxy_pass http://127.0.0.1:8080;[다른 프로젝트 실행 포트]
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_redirect off;
}
# static파일이 적용 안될 경우
location /static {
root /home/ubuntu/works/[static file path]
rewrite ^/monitoring(/.*)$ $1 break;
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_redirect off;
}
}
심볼릭 링크 생성
xxxxxxxxxx
$ ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled
NGINX 재시작
xxxxxxxxxx
$ service nginx restart
# nginx 추가 명령어
$ service nginx start / stop (시작 or 정지)