[Django Install] 12. Install Django for Centos8/RedhatLinux8

[Django Install]

pip install django
장고 설치

django-admin startproject projectname
장고 프로젝트 생성


project directory 구조

projectname/
    manage.py
    projectname/
        __init__.py
        settings.py
        urls.py
        asgi.py
        wsgi.py



[Visual Code simpleFTP Setting]


PC에 Visual Code를 설치한다. (사이트에서 설치 : https://code.visualstudio.com )

Visual Code의 Extensions에서 "ftp-simple"을 설치한다.



"FTP-simple"설치 후 "F1"를 눌러서 "ftp-simple : Config - FTP connection setting"을 선택한다.


아래의 내용처럼 추가하고 저장한다.

[
  {
        "name": "projectname",
        "host": "ec2-13-209-72-174.ap-northeast-2.compute.amazonaws.com",  # domain or IP
        "port": 22,
        "type": "sftp",
        "username": "ec2-user",
        "path": "/home/ec2-user/projectname", # 접속 디렉토리
        "autosave": true,
        "confirm": true,
        "privateKey": "/Users/iDongkil/dev/key/prefity2019.pem"  # 키페어 파일 (PC내 위치)


  }
]

다시 "F1"키를 눌러서 "ftp : Remote directory open to workspace"를 선택한다.



그리고 해당 프로젝트의 디렉토리를 선택하고 ". current directory"를 선택한다.
그러면 해당 디렉로리의 파일들이 PC에 저장되고 수정할 수 있게 된다.
파일들을 수정하고 저장하면 자동으로 서버에 저장된다.


[mysql server Install]

aws rds에서 mysql 서버를 설치한다.
설치 시 옵션에서 퍼블릭 접근을 ""로 한다.
Inbound 포트는 "3306"만 열어둔다.

[mysql client Install]

mysql workbench를 설치하고 rds에 접속한다.
mysql workbench에서 projectname의 database를 생성한다.


[oracle server Install]

aws rds에서 oracle을 설치한다.
설치 시 옵션에서 퍼블릭 접근을 ""로 한다.
Inbound 포트는 "1521"만 열어둔다.

[oracle client Install]

oracle instantclient basic을 설치한다.

$ORACLE_HOME 설정

$TNS_ADMIN 설정

$LD_LIBRARY_PATH 설정

oracle instantclient sqlplus를 설치한다.

sql developer를 설치한다. 

프로젝트에 사용할 oracle 사용자를 생성하고 권한을 부여한다.

create user username identified by password;

grant connect, dba, resource to username;



[Django Settings]

vi projectname/projectname/settings.py
아래 내용으로 변경하기 (빨간색 변경/추가)

--- 아래 시작 ---

import os

ALLOWED_HOSTS = '*'

# mysql
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'projectname',
        'USER': 'dbadmin',
        'PASSWORD': 'nadaimma',
        'HOST': 'prefity.cxngkgvluoq4.ap-northeast-2.rds.amazonaws.com',
        'PORT': '3306',
        'OPTIONS': {
            "init_command": "SET foreign_key_checks = 0;",
        },
    }
}

# oracle12c (oracle12 버전만 지원함, 11g는 지원하지 않음)
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.oracle',
        'NAME': 'SID',
        'USER': 'oracle user name',
        'PASSWORD': 'oracle user password',
        'HOST': 'hostname or ip',
        'PORT': 'port',
    }
}

# logging Settings

import logging.config
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    "formatters": {
        "basic": {
        "format": "%(asctime)s - %(module)s - [%(levelname)s] %(message)s",
        "datefmt": "%Y-%m-%d %H:%M:%S"
        }
    },
    "handlers": {
        "file_log": {
        "class": "logging.FileHandler",
        "level": "INFO",
        "formatter": "basic",
        "filename": "/home/ec2-user/projectname/log/server.log"
        }
    },
    "loggers": {
        "django": {
        "level": "INFO",
        "handlers": ["file_log"],
        "propagate": False
        },
        "projectname": {
        "level": "INFO",
        "handlers": ["file_log"],
        "propagate": False
        }
    }
}

logging.config.dictConfig(LOGGING)

LANGUAGE_CODE='ko'
TIME_ZONE = 'Asia/Seoul'

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'mediafiles')

# Redirect Settings

LOGIN_REDIRECT_URL = "/"
LOGOUT_REDIRECT_URL = "/"

--- 아래 끝 ---

mkdir $HOME/project/log (로그 디렉토리를 생성한다)


[Django log 활용]

view, model, form에서 log는 아래와 같이 활용

--- 아래 시작 ---

import logging 


logger = logging.getLogger('project name')

logger.info('message')

--- 아래 끝 ---


[Django default schema 생성]

python manage.py migrate 
(django 기본 스키마 생성)

아래의 에러는 Django 3.x 버전에서는 settings.py의 Korean LANGUAGE_CODE='ko'로 변경됨 (3.0 이전 버전은 'ko-kr'임)


ERRORS:
?: (translation.E004) You have provided a value for the LANGUAGE_CODE setting that is not in the LANGUAGES setting.


[Django admin shell 생성]

python manage.py makemigrations

(서버를 실행 - mk.sh로 작성, chmod 777 mk.sh)


python manage.py migrate

(서버를 실행 - mg.sh로 작성, chmod 777 mg.sh)


python manage.py showmigrations

(서버를 실행 - sh.sh로 작성, chmod 777 sh.sh)


python manage.py shell

(서버를 실행 - shell.sh로 작성, chmod 777 shell.sh)

python manage.py runserver 0.0.0.0:8080
(서버를 실행 - run.sh로 작성, chmod 777 run.sh)


nohup python manage.py runserver 0.0.0.0:8080 > logs/nohup_server.log &
(서버를 백그라운드로 실행 - server.sh 로 작성, chmod 777 server.sh)


[Django admin user 생성]

python manage.py createsuperuser
(관리자 id 생성)

Admin URL : domain/admin


댓글

이 블로그의 인기 게시물

[Django Install] 9.1 sc제일은행 nginx-django 연결

[Django APP] django-widget-tweaks

[Django App] django-user-agents