Apache Web Server Settings (hosting django)

Web

아파치 웹서버 설정 파일 수정 및 서비스 실행 및 장고 웹 애플리케이션 아파치 웹 서버에 배포

#파이썬, pip 그리고 wsgi 설정을 위해 설치 작업				
sudo apt-get install python3-pip apache2 libapache2-mod-wsgi-py3

# 파이썬, pip 그리고 wsgi 설정을 위해 설치 작업
virtualenv myprojectenv
git clone "https://github.com/Waji-97/PROmet.git" 

# 가상 환경 들어가기        
source myprojectenv/bin/activate

# 장고 프로젝트 Base 디렉터리 아래에 router.py 파일 생성
# Database 라우팅 설정
from config.settings import USE_REPLICA_DATABASE
class ReplicaRouter:
		def db_for_read(self, model, **hints):
				if USE_REPLICA_DATABASE:
							return 'replica'
				return None
		def db_for_write(self, mode, **hints):
				return 'default'
		def allow_relation(self, obj1, obj2, **hints):
				db_set = {'default', 'replica'}
				if obj1._state.db in db_set and obj2._state.db in db_set:
						return True
				return None
		def allow_migrate(self, db, app_label, model_name=None,**hints):
				return True

# Static 파일경로 지정과 Database 설정 그리고 장고 앱 프로덕션 레디 상태 만들기
sudo vi config/settings.py
import os
        
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
STATICFILES = [STATIC_ROOT]
        
ALLOWED_HOSTS=['*']
DEBUG = False

DATABASES = {
	'default': {
			'ENGINE': 'django.db.backends.mysql',
			'NAME': 'PROmetDB',
			'USER': 'Master',
			'PASSWORD': '1',
			'HOST': '10.1.4.10',
			'PORT': '3306'
	},

	'replica': {
			'ENGINE': 'django.db.backends.mysql',
			'NAME': 'PROmetDB',
			'USER': 'Slave',
			'PASSWORD': '1',
			'HOST': '10.1.4.20',
			'PORT': '3306'
	}

}

USE_REPLICA_DATABASE = 'TRUE'
DATABASE_ROUTERS=['config.router.ReplicaRouter']

# 프로젝트 실행을 위해 필요한 요소들 설치        
pip install -r requirements.txt

# 데이터베이스 마이그레이션과 static 파일들 모으기        
python manage.py makemigrations 
python manage.py migrate 
python manage.py migrate --run-syncdb
python manage.py collectstatic

# 기존 VirtualHost설정 파일은 비웠으나 내용을 추가해야 아파치 서버가 장고 파일들과 접근 가능       
sudo vi /etc/apache2/sites-available/000-default.conf

<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /static /mnt/glusterfs/static/static
	<Directory /mnt/glusterfs/static/static>
		Require all granted
	</Directory>

Alias /media /mnt/glusterfs/media/media
	<Directory "/mnt/glusterfs/media/media">
		Options FollowSymLinks
		Order allow,deny
		Allow from all
		Require all granted
	</Directory>

<Directory /home/itbank/django/PROmet/config>
	<Files wsgi.py>
		Require all granted
	</Files>
</Directory>

# WSGI 데몬 프로세스 그룹과 경로 설정하고 인증 헤더를 어플리케이션으로 전달하는 설정
WSGIPassAuthorization On
WSGIDaemonProcess Test python-path=/home/itbank/django/PROmet/ python-home=/home/itbank/django/myprojectenv
WSGIProcessGroup Test
WSGIScriptAlias / /home/itbank/django/PROmet/config/wsgi.py
</VirtualHost>

# 필요한 권한과 소유권을 아파치 유저 한테 주기
chmod 664 ~/django/PROmet/
sudo chown :www-data ~/django/PROmet/
sudo chown :www-data ~/django/PROmet/
sudo chown :www-data ~/django/PROmet/config/
sudo chown -R :www-data ~/django/PROmet/media/
sudo chmod 755 /home/ubuntu


# 프록시 로그 설정 추가
vi /etc/httpd/conf/httpd.conf

   # HEAD 메시지 로그기록에서 제외 설정
   196 SetEnvIf Request_Method HEAD Health-Check # 내용 추가 / HEAD Method를 지정하는 환경변수 Health-Check를 생성
   197 LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined # X-Forwarede-For 필드에 있는 Client IP를 이용한 Access Log가 저장되도록 설정
       LogFormat "%{x-forwarded-for}i %l %u %t \"%r\" %>s %b" common

   218 CustomLog "logs/access_log" common env=!Health-Check # 환경변수 Health-Check를 Access Log에서 제외하는 설정값을 추가로 작성

# 방화벽 설정 > http service 추가, httpd 데몬 재실행
firewall-cmd --add-service=http
firewall-cmd --reload
systemctl restart httpd

# 아파치 서버 재시작
sudo service apache2 restart

← back