[Backend] Python Flask simple REST API with nginx
Install nginx
choi@airi:~$ sudo add-apt-repository ppa:nginx/stable
choi@airi:~$ sudo apt-get update && sudo apt-get -f install nginx
choi@airi:~$ /etc/init.d/nginx status
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2019-10-28 16:19:11 KST; 8min ago
Docs: man:nginx(8)
Main PID: 11982 (nginx)
Tasks: 9 (limit: 4915)
CGroup: /system.slice/nginx.service
├─11982 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
├─11983 nginx: worker process
├─11984 nginx: worker process
├─11985 nginx: worker process
├─11986 nginx: worker process
├─11987 nginx: worker process
├─11988 nginx: worker process
├─11989 nginx: worker process
└─11990 nginx: worker process
choi@airi:~$ /usr/sbin/nginx -v
nginx version: nginx/1.16.1
하나의 master process는 많은 worker process들을 가지고 있다. 마스터 프로세스는 configuration file를 처리하고 worker process들을 관리하는 역할을 한다. 위치에 대한 정보는 아래와 같다.
- nginx는 /usr/sbin/nginx 에서 실행이 가능하다.
- configuration files은 /etc/nginx 에 저장되어 있다.
- Log file들은 /var/log/nginx 에서 볼 수 있다.
Set Virtual Environment and Install Flask
’'’console choi@airi:~/myapp$ python -m virtualenv rest choi@airi:~/myapp$ source rest/bin/activate (rest) choi@airi:~/myapp$ pip install flask flask_restful ‘’’
Simple Flask REST API server
In myproject.py
from flask import Flask
from flask_restful import Resource, Api
app = Flask(__name__)
api = Api(app)
class HelloWorld(Resource):
def get(self):
return {'hello': 'world'}
api.add_resource(HelloWorld, '/')
if __name__ == '__main__':
app.run(debug=True)
flask run --host=0.0.0.0 --port=5000
만약 address already in used
가 발생 시 해당 포트를 점유하는 process를 kill한다.
netstat -lntp
kill -9 pid
Leave a comment