ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Django] 배포하기
    라이브러리/Django 2022. 5. 21. 14:57

     지금까지 생성한 웹 사이트는 내 컴퓨터에서만 볼 수 있다.

    웹 사이트 배포를 통해 다른 사람들도 나의 사이트를 볼 수 있게 해보자.

     

    인터넷 상에 서버를 제공해주는 업체가 많은데 그 중 비교적 배포 과정이 간단한  PythonAnywhere을 이용해보자.

    PythonAnywhere 는방문자가 아주 많지 않은 소규모 애플리케이션을 위한 무료 서비스를 제공한다.

     

    1. github에 배포

     

    PythonAnywhere에 배포하기 위해서 github에 먼저 배포한다.

    현재 경로는 myPytonSite이며, 아래 명령어를 차례대로 입력한다.

    git init
    
    git config --global user.name "Your Name"
    git config --global user.email you@example.com

     

    1-2 gitignore 파일 생성

    myPythonSite 아래 .gitignore 라는 파일로 저장

    *.pyc
    *~
    __pycache__
    myvenv
    db.sqlite3
    /static
    .DS_Store

     

    1-3 현재까지의 코드들을 저장소에 넣기 (로컬)

    git add --all .
    git commit -m "my first commit"

    1-4 github 새로운 repository 생성

    1-5 생성한 repository의 주소 복사

    1-6 생성한 github의 repository 연결

    git remote add origin https://github.com/<your-github-username>/<your-repositoryname>
    git push -u origin master

     

     

    2. PythonAnywhere 설정

    www.pythonanywhere.com

    PythonAnywhere에서 무료 계정인 "초보자(Beginner)"로 회원가입

    (회원가입시 입력하는 사용자 이름은 블로그 주소의 일부가 됨)

     

    1-1 가상환경 생성

    아래 순서대로 명령어 입력

    (마지막 install 명령어는 약 5분 소요)

    cd <repository명>
    
    virtualenv --python=python3.6 myvenv
    
    source myvenv/bin/activate
    
    (myvenv) $  pip install django~=2.0

    1-2 데이터베이스 생성

    python manage.py migrate
    python manage.py createsuperuser

     

    1-3 웹 app으로 블로그 배포

    pythonanywhere 메인 페이지의 WEB 메뉴 클릭 > ADD A NEW WEB APP

     

    빨간 글씨의  Enter the path to a virtualenv 를 클릭 >

    /home/<your-username>/Portfolio/myvenv/ 입력 > 파란 체크박스 클릭

     

    WSGI 설정 파일을 수정하기 위해 /var/www/<your-username>_pythonanywhere_com_wsgi.py  클릭 >

    내용 모두 바꾸기

    import os
    import sys
    
    path = '/home/<your-PythonAnywhere-username>/Portfolio'  # PythonAnywhere 경로 변경
    if path not in sys.path:
        sys.path.append(path)
    
    os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
    
    from django.core.wsgi import get_wsgi_application
    from django.contrib.staticfiles.handlers import StaticFilesHandler
    application = StaticFilesHandler(get_wsgi_application())

     

    3. 배포 확인하기

    https://<pythonanywhere 사용자 명>.pythonanywhere.com 으로 들어가서 배포가 잘 되었는지 확인

     

Designed by Tistory.