본문 바로가기

Python/Django

[Django] Linode 배포 - git & GitHub

이번 시간에는 git/GitHub 기반의 버전 컨트롤과 Linode 에서 연동하는 방법에 대해서 알아보겠습니다.

 

아래와 같은 절차를 진행할 예정입니다.

  1. SSH Connection to Linode Server
  2. Install git on Linode
  3. Create GitHub Repository
  4. Connect Lindoe git repo to GitHub
  5. Connect to Gihub Repo Locally
  6. Push/Pull Changes from Local Computer to Linode Server

 

 

git 설치 - apt-get install git, 폴더 위치는 /var/ww/DjangApp 에서 실행함.

 

root@45-118-135-29:/var/www/DjangoApp# apt-get install git
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  git-man liberror-perl
Suggested packages:
  git-daemon-run | git-daemon-sysvinit git-doc git-el git-email git-gui gitk gitweb git-cvs git-mediawiki git-svn
The following NEW packages will be installed:
  git git-man liberror-perl
0 upgraded, 3 newly installed, 0 to remove and 1 not upgraded.
Need to get 7,287 kB of archives.
After this operation, 38.1 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://mirrors.linode.com/debian buster/main amd64 liberror-perl all 0.17027-2 [30.9 kB]
Get:2 http://mirrors.linode.com/debian-security buster/updates/main amd64 git-man all 1:2.20.1-2+deb10u6 [1,622 kB]
Get:3 http://mirrors.linode.com/debian-security buster/updates/main amd64 git amd64 1:2.20.1-2+deb10u6 [5,634 kB]
Fetched 7,287 kB in 1s (12.1 MB/s)
Selecting previously unselected package liberror-perl.
(Reading database ... 37405 files and directories currently installed.)
Preparing to unpack .../liberror-perl_0.17027-2_all.deb ...
Unpacking liberror-perl (0.17027-2) ...
Selecting previously unselected package git-man.
Preparing to unpack .../git-man_1%3a2.20.1-2+deb10u6_all.deb ...
Unpacking git-man (1:2.20.1-2+deb10u6) ...
Selecting previously unselected package git.
Preparing to unpack .../git_1%3a2.20.1-2+deb10u6_amd64.deb ...
Unpacking git (1:2.20.1-2+deb10u6) ...
Setting up liberror-perl (0.17027-2) ...
Setting up git-man (1:2.20.1-2+deb10u6) ...
Setting up git (1:2.20.1-2+deb10u6) ...
Processing triggers for man-db (2.8.5-2) ...
root@45-118-135-29:/var/www/DjangoApp#

 

 

설치 위치 확인

 

root@45-118-135-29:/var/www/DjangoApp# which git
/usr/bin/git
root@45-118-135-29:/var/www/DjangoApp#

 

 

gitHub 에 새로운 Repo 를 생성한다.

 

개인 엑세스 토큰을 만들어 보자

 

 

 

 

 

 

 

linode 서버에서 git 연결하는 방법 - 아이디는 본인 아이디 입력, 비번은 개인 엑세스 토큰값을 붙여넣을것.

아래 명령어들은 gitHub 에 새로운 Repo 생성시 보여주는 샘플 명령어를 참고하면 된다.

개인 엑세스 토큰은 push 할때 마다 필요하기 때문에 저장해 두고 사용해야 한다.

 

root@45-118-135-29:/var/www/DjangoApp# echo "# django-example-linode" >> README.md
root@45-118-135-29:/var/www/DjangoApp# git init
dd README.md
git commit -m "first commit"
Reinitialized existing Git repository in /var/www/DjangoApp/.git/
root@45-118-135-29:/var/www/DjangoApp# git add README.md
root@45-118-135-29:/var/www/DjangoApp# git commit -m "first commit"
[main 383c598] first commit
 Committer: root <root@45-118-135-29.ip.linodeusercontent.com>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 1 insertion(+)
root@45-118-135-29:/var/www/DjangoApp# git branch -M main
root@45-118-135-29:/var/www/DjangoApp# git remote add origin https://github.com/mike-bskim/django-example-linode.git
fatal: remote origin already exists.
root@45-118-135-29:/var/www/DjangoApp# git push -u origin main
Username for 'https://github.com': mike-bskim
Password for 'https://mike-bskim@github.com':
Enumerating objects: 9, done.
Counting objects: 100% (9/9), done.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (9/9), 738 bytes | 738.00 KiB/s, done.
Total 9 (delta 0), reused 0 (delta 0)
To https://github.com/mike-bskim/django-example-linode.git
 * [new branch]      main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.
root@45-118-135-29:/var/www/DjangoApp#

 

 

연결된 git 에 push 하기 - 여기에서도 개인 엑세스 코드가 사용되므로 잘 저장해두어야 한다.

 

root@45-118-135-29:/var/www/DjangoApp# ll
total 140
-rw-r--r-- 1 root root 131072 Dec 18 20:02 db.sqlite3
drwxr-xr-x 3 root root   4096 Dec 18 20:02 DjangoApp
-rwxr-xr-x 1 root root    665 Dec 18 20:02 manage.py
-rw-r--r-- 1 root root     72 Dec 19 18:03 README.md

root@45-118-135-29:/var/www/DjangoApp# git add .
root@45-118-135-29:/var/www/DjangoApp# git commit . -m "my second commit"
[main 139a880] my second commit
 Committer: root <root@45-118-135-29.ip.linodeusercontent.com>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 11 files changed, 200 insertions(+)
 create mode 100644 DjangoApp/__init__.py
 create mode 100644 DjangoApp/__pycache__/__init__.cpython-37.pyc
 create mode 100644 DjangoApp/__pycache__/settings.cpython-37.pyc
 create mode 100644 DjangoApp/__pycache__/urls.cpython-37.pyc
 create mode 100644 DjangoApp/__pycache__/wsgi.cpython-37.pyc
 create mode 100644 DjangoApp/asgi.py
 create mode 100644 DjangoApp/settings.py
 create mode 100644 DjangoApp/urls.py
 create mode 100644 DjangoApp/wsgi.py
 create mode 100644 db.sqlite3
 create mode 100755 manage.py
root@45-118-135-29:/var/www/DjangoApp# git push
Username for 'https://github.com': mike-bskim
Password for 'https://mike-bskim@github.com':
Enumerating objects: 16, done.
Counting objects: 100% (16/16), done.
Compressing objects: 100% (14/14), done.
Writing objects: 100% (15/15), 8.69 KiB | 2.90 MiB/s, done.
Total 15 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), done.
To https://github.com/mike-bskim/django-example-linode.git
   383c598..139a880  main -> main
root@45-118-135-29:/var/www/DjangoApp# client_loop: send disconnect: Connection reset

 

 

 

 

gitHub 를 로컬 컴퓨터에 복제(clone) 해보자.

 

 

 

새로운 장고앱 추가

 

~\DJANGO_COURSE_V2\github>python manage.py startapp other

(django) D:\workspace\Python\Django\DJANGO4\DJANGO_COURSE_V2\github>

 

 

간단한 view 를 추가해 보자.

 

views.py

 

from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.
def simple_view(request):
    return HttpResponse('Hello ~~')

 

 

urls.py

 

from django.urls import path
from . import views

# domain.com:8000/other
urlpatterns = [
    path('', views.simple_view, name='simple_view')
]

 

 

프로젝트 레벨 urls.py 에 path 추가하기

 

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('other/', include('other.urls'))
]

 

 

변경사항을 gitHub 에 push 하기 - 저는 개인적으로 sourcetree 를 사용해서 push 했습니다.

 

 

이번에는 서버 사이드에서 git pull 실행 - 명령어 라인이 좀 잘렸지만 정상적으로 변경사항을 모두 받았다.

 

root@45-118-135-29:/var/www/DjangoApp# git pull
 * [new tag]         02_other_simple_view -> 02_other_simple_view
 * [new tag]         01_add_new_app       -> 01_add_new_app
Updating 139a880..6abeb54
Fast-forward
 DjangoApp/__pycache__/__init__.cpython-310.pyc | Bin 0 -> 173 bytes
 DjangoApp/__pycache__/settings.cpython-310.pyc | Bin 0 -> 2340 bytes
 DjangoApp/urls.py                              |   3 ++-
 other/__init__.py                              |   0
 other/admin.py                                 |   3 +++
 other/apps.py                                  |   6 ++++++
 other/migrations/__init__.py                   |   0
 other/models.py                                |   3 +++
 other/tests.py                                 |   3 +++
 other/urls.py                                  |   7 +++++++
 other/views.py                                 |   6 ++++++
 11 files changed, 30 insertions(+), 1 deletion(-)
 create mode 100644 DjangoApp/__pycache__/__init__.cpython-310.pyc
 create mode 100644 DjangoApp/__pycache__/settings.cpython-310.pyc
 create mode 100644 other/__init__.py
 create mode 100644 other/admin.py
 create mode 100644 other/apps.py
 create mode 100644 other/migrations/__init__.py
 create mode 100644 other/models.py
 create mode 100644 other/tests.py
 create mode 100644 other/urls.py
 create mode 100644 other/views.py
root@45-118-135-29:/var/www/DjangoApp#

 

 

변경 사항이 바로 적용/반영된다.

실행 화면

 

 

 

 

.git/config 파일에서 아래 부분을 수정하면 git push 할때마다 아이디/비번을 생략 가능하다.

 

url = https://<user-id>:<token>@mike-bskim/django-example-linode.git
===>
url = https://mike-bskim:<personal access token>@github.com/mike-bskim/django-example-linode.git