아파치 mod python 연동

Jmnote (토론 | 기여)님의 2014년 7월 31일 (목) 09:39 판
아파치 mod_python 연동

1 mod_python 설치

2 httpd.conf 파일 수정

vi /etc/httpd/conf/httpd.conf
변경 전
<Directory "/var/www/html">
	Options Indexes FollowSymLinks
	AllowOverride None
	Order allow,deny
	Allow from all
</Directory>
변경 후
<Directory "/var/www/html">
	Options Indexes FollowSymLinks
	AllowOverride None
	Order allow,deny
	Allow from all
	AddHandler mod_python .psp .psp_ .py
	PythonHandler mod_python.psp | .psp .psp_
	PythonHandler mod_python.cgihandler | .py
	PythonDebug On
</Directory>
.psp 파일은 mod_python.psp 핸들러가 처리
.py 파일은 mod_python.cgihandler 핸들러가 처리

3 아파치 재시작

[root@jmnote ~]# service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]

4 hello.py 테스트

  • 아래 내용 참고하여 /var/www/html/hello.py 파일 생성
[root@jmnote ~]# cat /var/www/html/hello.py
print "Content-type: text/html\n"
print "Hello, Python!"
[root@jmnote ~]# curl http://localhost/hello.py
Hello, Python!

5 hello.psp 테스트

  • 아래 내용 참고하여 /var/www/html/hello.psp 파일 생성
[root@jmnote ~]# cat /var/www/html/hello.psp
<html>
<body>
<h2><%= 'Hello, PSP!' %></h2>
</body>
</html>
[root@jmnote ~]# curl http://localhost/hello.psp
<html>
<body>
<h2>Hello, PSP!</h2>
</body>
</html>

6 mod_python.publisher 사용

  • mod_python.cgihandler 대신 mod_python.publisher를 사용할 수도 있다.
vi /etc/httpd/conf/httpd.conf
변경 전
	PythonHandler mod_python.cgihandler | .py
변경 후
	PythonHandler mod_python.publisher | .py
  • 핸들러 코드 작성
[root@jmnote ~]# cat /var/www/html/hello2.py
from mod_python import apache

def handler(req):
	req.content_type = 'text/html'
	req.write("<html>\n")
	req.write("<body>\n")
	req.write("Hello, Publisher!\n")
	req.write("<body>\n")
	req.write("</html>\n")
  • 아파치를 재시작한 후 테스트
[root@jmnote ~]# curl http://localhost/hello2.py/handler
<html>
<body>
Hello, Publisher!
<body>
</html>

7 같이 보기

8 참고 자료

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}