From d8652e69e56fd8210c09d42bf049fb509b01c9ca Mon Sep 17 00:00:00 2001 From: phin09 Date: Thu, 29 Apr 2021 23:28:29 +0900 Subject: [PATCH] CREATE: local test uWSGI web server with Nginx --- .gitignore | 1 + README.md | 13 ++++++++--- config/nginx_copy/stylewe_nginx.conf | 35 ++++++++++++++++++++++++++++ config/settings.py | 2 ++ pull_request_template.md | 20 ---------------- requirements.txt | 2 ++ test.py | 6 +++++ uwsgi_params | 16 +++++++++++++ 8 files changed, 72 insertions(+), 23 deletions(-) create mode 100644 config/nginx_copy/stylewe_nginx.conf delete mode 100644 pull_request_template.md create mode 100644 test.py create mode 100644 uwsgi_params diff --git a/.gitignore b/.gitignore index a1400a4..df7de05 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ my_settings.py /csv *.csv db_uploader.py +static # Created by https://www.toptal.com/developers/gitignore/api/django,linux,git,python,vim,vscode diff --git a/README.md b/README.md index d442ecb..1a1dd79 100644 --- a/README.md +++ b/README.md @@ -3,14 +3,15 @@ ### 1. 프로젝트 선정 이유 이 프로젝트의 원본은 2021년 2월, 2주간 진행한 팀프로젝트입니다. [팀프로젝트 Github](https://github.com/phin09/17-1st-StyleWe-backend) -2021년 4월, 혼자 일주일간 리팩토링을 진행하고자 합니다. +2021년 4월, 혼자 일주일간 리팩토링을 진행합니다. 레거시를 읽고 기존 팀코드의 가독성, 효율을 개선하며 Django의 기능을 더 공부하고 싶습니다. ### 2. 리팩토링 목표 - 불분명한 변수명을 변경하고 가독성을 위해 불필요한 변수 삭제. -- 데이터베이스를 건드리지 않고 기존 백엔드 API의 효율을 개선. +- 데이터베이스 구조를 건드리지 않고 기존 백엔드 API의 효율을 개선. - 대체한 함수는 전과 후의 로직과 성능을 비교. - API 문서화. +- RDS, EC2 사용. - unit test 진행. ### 3. 일주일 기록 @@ -20,8 +21,14 @@ - models.py에 맞게 ERD 수정. 2. 4/24 - [drf-yasg](https://github.com/axnsan12/drf-yasg)를 사용해 API 문서화 시도. -3. 4.25 +3. 4/25 - [drf-yasg](https://github.com/axnsan12/drf-yasg)를 사용해 Swagger UI 기반 기존 API 문서화 완료. 엔드포인트 '/swagger' +4. 4/26 +- 웹서버 시도 - Nginx, uWSGI 설치 +5. 4/27 +- 웹서버 시도 - Nginx, uWSGI 연결 +6. 4/29 +- 로컬에서 Nginx, unix 웹소켓, uWSGI 연결 성공 ## Reference > 이 프로젝트는 스타일쉐어 사이트를 참조하여 학습목적으로 만들었습니다. diff --git a/config/nginx_copy/stylewe_nginx.conf b/config/nginx_copy/stylewe_nginx.conf new file mode 100644 index 0000000..cea67c4 --- /dev/null +++ b/config/nginx_copy/stylewe_nginx.conf @@ -0,0 +1,35 @@ +# https://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html#configure-nginx-for-your-site +# /etc/nginx/sites-available/stylewe_nginx.conf 로 넣어둔 내용. + +# the upstream component nginx needs to connect to +upstream django { + server unix:/home/phin09/devel/wecode/style-we/stylewe.sock; # for a file socket + # server 127.0.0.1:8001; # for a web port socket (we'll use this first) +} + +# configuration of the server +server { + # the port your site will be served on + listen 8000; + # the domain name it will serve for + server_name 0.0.0.0; # substitute your machine's IP address or FQDN + charset utf-8; + + # max upload size + client_max_body_size 75M; # adjust to taste + + # Django media + # location /media { + # alias /path/to/your/mysite/media; # your Django project's media files - amend as required + # } + + location /static { + alias /home/phin09/devel/wecode/style-we/static; # your Django project's static files - amend as required + } + + # Finally, send all non-media requests to the Django server. + location / { + uwsgi_pass django; + include /home/phin09/devel/wecode/style-we/uwsgi_params; # the uwsgi_params file you installed + } +} \ No newline at end of file diff --git a/config/settings.py b/config/settings.py index 7870ebc..b29bcd2 100644 --- a/config/settings.py +++ b/config/settings.py @@ -1,3 +1,4 @@ +import os from pathlib import Path import my_settings @@ -109,6 +110,7 @@ # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/3.1/howto/static-files/ +STATIC_ROOT = os.path.join(BASE_DIR, 'static') STATIC_URL = '/static/' #REMOVE_APPEND_SLASH_WARNING diff --git a/pull_request_template.md b/pull_request_template.md deleted file mode 100644 index 3cd033e..0000000 --- a/pull_request_template.md +++ /dev/null @@ -1,20 +0,0 @@ ->wecode PR message template 및 체크 리스트 입니다. -아래 사항들을 전부 작성/체크 하시고 PR 해주세요! - -## 수정 사항 간략한 한줄 요약 -(여기에 수정 사항에 대한 간략한 한줄 요약/제목 작성해 주세요.) -- 로그인 버튼 활성화 기능 구현(예시) - -## 수정 사항들 자세한 내용 -(여기에 수정 사항에 대한 자세한 내용을 작성해 주세요.) -- id '@' 포함, pw 5 글자 이상 입력 시 버튼 활성화(예시) - -## 기타 질문 및 특이 사항 -(궁금한 사항들, 하면서 느낀 점들 공유해주실 것이 있으면 자유롭게 작성해 주세요.) -- 함수 로직이 너무 긴 것 같습니다. 좀 더 효율적인 방법이 없을지 궁금합니다.(예시) - -## 체크 리스트 (아래 사항들이 전부 체크되어야만 merge가 됩니다!) -- [ ] 필요한 test들을 완료하였고 기능이 제대로 실행되는지 확인 하였습니다. -- [ ] Wecode의 코드 스타일 가이드에 맞추어 코드를 작성 하였습니다. -- [ ] 제가 의도한 파일들과 수정 사항들만 커밋이 된 것을 확인 하였습니다. -- [ ] 본 수정 사항들을 팀원들과 사전에 상의하였고 팀원들 모두 해당 PR에 대하여 알고 있습니다. diff --git a/requirements.txt b/requirements.txt index 8debdb1..41dfca1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -12,6 +12,7 @@ django-countries==7.0 djangorestframework==3.12.4 drf-yasg==1.20.0 flex==6.14.1 +gunicorn==20.1.0 idna==2.10 inflection==0.5.1 itypes==1.2.0 @@ -34,4 +35,5 @@ sqlparse==0.4.1 strict-rfc3339==0.7 uritemplate==3.0.1 urllib3==1.26.4 +uWSGI @ file:///home/conda/feedstock_root/build_artifacts/uwsgi_1602758936018/work validate-email==1.3 diff --git a/test.py b/test.py new file mode 100644 index 0000000..69410a1 --- /dev/null +++ b/test.py @@ -0,0 +1,6 @@ +# uwsgi 테스트용 +# https://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html#basic-test + +def application(env, start_response): + start_response('200 OK', [('Content-Type','text/html')]) + return [b"testtest"] # python3 \ No newline at end of file diff --git a/uwsgi_params b/uwsgi_params new file mode 100644 index 0000000..d26f2ff --- /dev/null +++ b/uwsgi_params @@ -0,0 +1,16 @@ +uwsgi_param QUERY_STRING $query_string; +uwsgi_param REQUEST_METHOD $request_method; +uwsgi_param CONTENT_TYPE $content_type; +uwsgi_param CONTENT_LENGTH $content_length; + +uwsgi_param REQUEST_URI $request_uri; +uwsgi_param PATH_INFO $document_uri; +uwsgi_param DOCUMENT_ROOT $document_root; +uwsgi_param SERVER_PROTOCOL $server_protocol; +uwsgi_param REQUEST_SCHEME $scheme; +uwsgi_param HTTPS $https if_not_empty; + +uwsgi_param REMOTE_ADDR $remote_addr; +uwsgi_param REMOTE_PORT $remote_port; +uwsgi_param SERVER_PORT $server_port; +uwsgi_param SERVER_NAME $server_name; \ No newline at end of file