Ossian Story
article thumbnail

 

 

[WEB & WAS] Apache & Django Python 2.x

 

WEB & DB Server 구성


WEB Server에는 Apache & Django, DB Server에는 MariaDB로 구성하여 WEB Server와 DB Server를 나눈 형태로 구성합니다.

이번 포스팅에서는 Apache & Django를 Python 2.x환경에서 구성하는 포스팅을 진행합니다.

 
 

 

Apache 및 mod_wsgi 모듈 설치


Apache 설치

 

[root@web-test-01 ~]# yum -y install httpd

 

mod_wsgi 모듈 설치, mod_wsgi는 Apache와 Django를 연결해주는 모듈입니다.

 

[root@web-test-01 ~]# yum -y install mod_wsgi

 

 

 

python-pip 설치


CentOS에는 기본적으로 Python 2.x 버전이 설치되어 있으므로 Django 설치를 위한 python-pip를 설치합니다.

[root@web-test-01 ~]# yum -y install python-pip

 

 

pip 업그레이드

[root@web-test-01 ~]# pip install --upgrade pip

 

 

 

Django 설치


[root@web-test-01 ~]# pip install django

 

 

Django 프로젝트 생성


Django 프로젝트를 생성할 디렉토리로 이동 및 Django 프로젝트 생성

 

[root@web-test-01 ~]# cd /opt
 
[root@web-test-01 opt]# django-admin startproject web01

 

 

 

Apache 설정


vhost.conf 파일 생성 및 아래 내용 작성

 

[root@web-test-01 opt]# vi /etc/httpd/conf.d/vhost.conf
 
<VirtualHost *:80>
  ServerName localhost
 
  WSGIScriptAlias / /opt/web01/web01/wsgi.py
  WSGIDaemonProcess web01 python-path=/opt/web01/web01
 
  <Directory /opt/web01/web01>
    <Files wsgi.py>
      AllowOverride none
      Require all granted
    </Files>
  </Directory>
</VirtualHost>
 

 

 

wsgi.py 수정

[root@web-test-01 opt]# vi /opt/web01/web01/wsgi.py
 
 
"""
WSGI config for web01 project.
 
It exposes the WSGI callable as a module-level variable named ``application``.
 
For more information on this file, see
https://docs.djangoproject.com/en/2.1/howto/deployment/wsgi/
"""
 
import os
import sys
 
from django.core.wsgi import get_wsgi_application
 
path = '/opt/web01'
if path not in sys.path:
    sys.path.append(path)
 
os.environ.setdefault('DJANGO_SETTINGS_MODULE''web01.settings')
 
application = get_wsgi_application()

 

 

setting.py 수정 - ALLOWED_HOSTS의 값을 '*'로 수정

[root@web-test-01 opt]# vi /opt/web01/web01/settings.py
 
 
import os
 
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
 
 
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/
 
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '^ficswq#s7h71fvi86oe=15sy5csk6hi%eh6ti6g3dj%j@vnmx'
 
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
 
ALLOWED_HOSTS = ['*']
 
 
# Application definition
 

 

 

httpd.conf 수정

[root@web-test-01 opt]# vi /etc/httpd/conf/httpd.conf
 
 
# Deny access to the entirety of your server's filesystem. You must
# explicitly permit access to web content directories in other
# <Directory> blocks below.
#
<Directory />
    AllowOverride none
    Require all granted
</Directory>
 
#
# Note that from this point forward you must specifically allow
# particular features to be enabled - so if something's not working as
# you might expect, make sure that you have specifically enabled it
# below.
 

 

 

Apache 재시작

[root@web-test-01 opt]# systemctl restart httpd

 

 

Django 연동 확인


브라우저에서 Web 접속하여 확인

 

 

 

 

 

profile

Ossian Story

@ossians