본문 바로가기

Linux

Apache, Tomcat 설정(파이썬 구동)

Apache, Tomcat 설정(파이썬 구동)


외부포트, 서비스명, 내부포트

HTTP -> [ 8080 tomcat-cafe24(server.xml) 8009] <- /mysite2 , AJP -> [80:apache:80(httpd.conf)] <- HTTP

​ <- /mysite4 , AJP ->

​ [:apache:8888]

​ (mod_wsgi) | (execute)

​ | WSGI(Web Server Gateway Interface)

WSGI는 파이썬에서 별도로 사용하고 있는 CGI와 같은 기능을 하는 것이다. django 에서는 WSGI를 통해 프로그램을 실행시키게 된다.

HTTP -> [ 9090 tomcat-jenkins(server.xml) 9009]

주요 설정 파일

cd /usr/local/cafe24/apache
[root@localhost apache]# vi /usr/local/cafe24/apache/conf/httpd.conf
[root@localhost conf]# vi workers_jk.properties 
# 서비스가 내 외부로 열어놓은 포트 확인
cd /usr/local/cafe24/tomcat-cafe24
vi conf/server.xml
# 현재 포트가 어떻게 열려있는 상태인 지 확인 
vi /etc/sysconfig/iptables 

apache vhost 설정(도메인 기준, 포트 기준)


  • 도메인 기준(같은 포트 , 다른 도메인)
  • 포트 기준 (다른 포트, 다른 도메인) : apache multiport service
  1. httpd.conf
할일 
- Listen 80
- Listen 8888 (추가)
vi /usr/local/cafe24/apache/conf/httpd.conf
##### httpd.conf editing ####

1) 

Listen 80
Listen 8888(추가)

2) 디렉토리 권한 설정 주석처리

#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
#DocumentRoot "/usr/local/cafe24/apache/htdocs"

#
# Each directory to which Apache has access can be configured with respect
# to which services and features are allowed and/or disabled in that
# directory (and its subdirectories).
#
# First, we configure the "default" to be a very restrictive set of
# features.
#
<Directory />
    Options FollowSymLinks
    AllowOverride None
    Order deny,allow
    Deny from all
</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.
#

#
# This should be changed to whatever you set DocumentRoot to.
#
#<Directory "/usr/local/cafe24/apache/htdocs">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.2/mod/core.html#options
    # for more information.
    #
#    Options Indexes FollowSymLinks

    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    #
#    AllowOverride None

    #
    # Controls who can get stuff from this server.
    #
#    Order allow,deny
#    Allow from all

#</Directory>


3) Virtual hosts 주석 해제
# Virtual hosts
Include conf/extra/httpd-vhosts.conf

vi conf/extra/httpd-vhosts.conf
  1. vi conf/extra/httpd-vhosts.conf
##### conf/extra/httpd-vhosts.conf editing ####

# Use name-based virtual hosting.
#
NameVirtualHost *:80
NameVirtualHost *:8888

#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#


<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot "/usr/local/cafe24/apache/htdocs"
    ServerName dummy-host.example.com
    ServerAlias www.dummy-host.example.com
    ErrorLog "logs/localhost.80-error_log"
    CustomLog "logs/localhost.80-access_log" common
        <Directory "/usr/local/cafe24/apache/htdocs">
                Options Indexes FollowSymLinks
                AllowOverride None
                Order allow,deny
                Allow from all
        </Directory>
</VirtualHost>

<VirtualHost *:8888>
    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot "/home/django/python_ch3"
    ServerName dummy-host2.example.com
    ErrorLog "logs/localhost.8888-error_log"
    CustomLog "logs/localhost.8888-access_log" common
    <Directory "/home/django/python_ch3">
        Options Indexes FollowSymLinks
        AllowOverride None
        Order allow,deny
        Allow from all
   </Directory>
</VirtualHost>
# 서버재시작
/etc/init.d/httpd stop
ps -ef |grep httpd
/etc/init.d/httpd start
ps -ef |grep httpd

# 방화벽 열기 
[root@localhost apache]# vi /etc/sysconfig/iptables
/etc/init.d/iptables restart

여태까지 80으로 넘겼던 요청은 모두 tomcat으로 접근하였도록 하였으나

위와 같이 virtual host 사용 시 사용이 불가해졌음

따라서 tomcat 연동을 도와줬던 JkMountFile 설정은 Virtual Host에 할 것 !!

[root@localhost apache]# vi /usr/local/cafe24/apache/conf/httpd.conf

#### conf/httpd.conf editing ####
# JkMountFile conf/uriworkermap.properties
[root@localhost apache]# vi conf/extra/httpd-vhosts.conf

#### conf/extra/httpd-vhosts.conf editing ####

#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#


<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot "/usr/local/cafe24/apache/htdocs"
    ServerName dummy-host.example.com
    ServerAlias www.dummy-host.example.com
    ErrorLog "logs/localhost.80-error_log"
    CustomLog "logs/localhost.80-access_log" common
        <Directory "/usr/local/cafe24/apache/htdocs">
                Options Indexes FollowSymLinks
                AllowOverride None
                Order allow,deny
                Allow from all
        </Directory>
</VirtualHost>

JkMountFile conf/uriworkermap.properties

<VirtualHost *:8888>
    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot "/home/django/python_ch3"
    ServerName dummy-host2.example.com
    ErrorLog "logs/localhost.8888-error_log"
    CustomLog "logs/localhost.8888-access_log" common
    <Directory "/home/django/python_ch3">
        Options Indexes FollowSymLinks
        AllowOverride None
        Order allow,deny
        Allow from all
   </Directory>
</VirtualHost>

apache로 django 연동 하기

  1. mod_wsgi 설치
cd

wget https://github.com/GrahamDumpleton/mod_wsgi/archive/4.6.4.tar.gz

[root@localhost ~]# mv 4.6.4.tar.gz mod_wsgi_4.6.4.tar.gz

[root@localhost ~]# tar xvfz mod_wsgi_4.6.4.tar.gz

[root@localhost ~]# cd mod_wsgi-4.6.4/

./configure --with-apxs=/usr/local/cafe24/apache/bin/apxs --with-python=/usr/local/cafe24/python3.7/bin/python3

[root@localhost mod_wsgi-4.6.4]# ./configure --with-apxs=/usr/local/cafe24/apache/bin/apxs --with-python=/usr/local/cafe24/python3.7/bin/python3

make 
make install
  1. httpd.conf 설정

2-1) 모듈 로딩

#### httpd.conf editing ####

# configuration directives that give the server its instructions.
# See <URL:http://httpd.apache.org/docs/2.2> for detailed information.
# In particular, see
# <URL:http://httpd.apache.org/docs/2.2/mod/directives.html>
# for a discussion of each configuration directive.
#
# Do NOT simply read the instructions in here without understanding
# what they do.  They're here only as hints or reminders.  If you are unsure
# consult the online docs. You have been warned.
#
# Configuration and logfile names: If the filenames you specify for many
# of the server's control files begin with "/" (or "drive:/" for Win32), the
# server will use that explicit path.  If the filenames do *not* begin
# with "/", the value of ServerRoot is prepended -- so "logs/foo_log"
# with ServerRoot set to "/usr/local/cafe24/apache" will be interpreted by the
# server as "/usr/local/cafe24/apache/logs/foo_log".
LoadModule dbd_module modules/mod_dbd.so
LoadModule dumpio_module modules/mod_dumpio.so
LoadModule ext_filter_module modules/mod_ext_filter.so
LoadModule include_module modules/mod_include.so
LoadModule filter_module modules/mod_filter.so
LoadModule substitute_module modules/mod_substitute.so
LoadModule deflate_module modules/mod_deflate.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule log_forensic_module modules/mod_log_forensic.so
LoadModule logio_module modules/mod_logio.so
LoadModule env_module modules/mod_env.so
LoadModule mime_magic_module modules/mod_mime_magic.so
LoadModule cern_meta_module modules/mod_cern_meta.so
LoadModule expires_module modules/mod_expires.so
LoadModule headers_module modules/mod_headers.so
LoadModule ident_module modules/mod_ident.so
LoadModule usertrack_module modules/mod_usertrack.so
LoadModule unique_id_module modules/mod_unique_id.so
LoadModule setenvif_module modules/mod_setenvif.so
LoadModule version_module modules/mod_version.so
LoadModule mime_module modules/mod_mime.so
LoadModule dav_module modules/mod_dav.so
LoadModule status_module modules/mod_status.so
LoadModule autoindex_module modules/mod_autoindex.so
LoadModule asis_module modules/mod_asis.so
LoadModule info_module modules/mod_info.so
LoadModule cgid_module modules/mod_cgid.so
LoadModule dav_fs_module modules/mod_dav_fs.so
LoadModule vhost_alias_module modules/mod_vhost_alias.so
LoadModule negotiation_module modules/mod_negotiation.so
LoadModule dir_module modules/mod_dir.so
LoadModule imagemap_module modules/mod_imagemap.so
LoadModule actions_module modules/mod_actions.so
LoadModule speling_module modules/mod_speling.so
LoadModule userdir_module modules/mod_userdir.so
LoadModule alias_module modules/mod_alias.so
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule jk_module modules/mod_jk.so

LoadModule wsgi_module modules/mod_wsgi.so   (추가)

2-2) extra/httpd-vhosts

<VirtualHost *:8888>
    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot "/home/django/python_ch3"
    ServerName dummy-host2.example.com
    ErrorLog "logs/localhost.8888-error_log"
    CustomLog "logs/localhost.8888-access_log" common

    WSGIScriptAlias / /home/django/python_ch3/python_ch3/wsgi.py
    WSGIDaemonProcess python_ch3 python-path=/home/django/python_ch3/python_ch3

    <Directory "/home/django/python_ch3">

        <Files wsgi.py>

                Order allow,deny
                Allow from all

        </Files>

   </Directory>
</VirtualHost>

파이썬 django - venv에서 띄우기

[root@localhost django]# git clone https://github.com/MaximSungmo/python_ch3.git

[root@localhost django]# cd python_ch3
[root@localhost python_ch3]# virtualenv venv
[root@localhost python_ch3]# source venv/bin/activate

(venv) [root@localhost python_ch3]# pip3 install -r requirements.txt 


# 포트 열기 :9999
(venv) [root@localhost python_ch3]# vi /etc/sysconfig/iptables
(venv) [root@localhost python_ch3]# /etc/init.d/iptables restart


python_ch3 파일로 넘어가서
settings에서 ALLOWED_HOSTS = ['*'] 수정 후 커밋

내부 서버로 띄우기

[root@localhost django]# pip3 install -r python_ch3/requirements.txt --target=python_ch3
[root@localhost django]# export PYTHONPATH='/home/django/python_ch3/packages'
[root@localhost django]# /etc/init.d/httpd restart
[root@localhost django]# echo $PYTHONPATH
/home/django/python_ch3/packages

내부서버에서 띄울 때 export $PYTHONPATH 하지 않는 방법

export 작업을 하기 싫은 경우에는 wsgi.py에 

#### python_ch3/python_ch3/wsgi.py editing ###

"""
WSGI config for python_ch3 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.2/howto/deployment/wsgi/
"""

import os
import sys

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'python_ch3.settings')

application = get_wsgi_application()
[root@localhost extra]# vi httpd-vhosts.conf


#### httpd-vhosts.conf editing ###

<VirtualHost *:8888>
    ServerAdmin ksm5318@naver.com
    DocumentRoot "/home/django/python_ch3"
    ServerName dummy-host2.example.com
    ErrorLog "logs/localhost.8888-error_log"
    CustomLog "logs/localhost.8888-access_log" common

    WSGIScriptAlias / /home/django/python_ch3/python_ch3/wsgi.py
    WSGIDaemonProcess python_ch3 python-path=/home/django/python_ch3/python_ch3

    <Directory "/home/django/python_ch3">

        <Files wsgi.py>

                Order allow,deny
                Allow from all

        </Files>

   </Directory>
</VirtualHost>

깃 합치기

git rm -r --cached .
git add .
git commit -m "gitignore add"
git push

'Linux' 카테고리의 다른 글

[Linux] 문자열 검색 (grep)  (0) 2019.12.24
[CentOS6.9] 리눅스 명령어 정리  (0) 2019.05.07