Making Website with CGI
- Constructing Web Server: Download and Install Apche
Official: Installing Apache in window(https://httpd.apache.org/docs/2.4/platform/windows.html)
Beginner version: Install Bitnami Wamp Stack
push 1 or 2
click this button below
it takes some time to install it
- Constructing Web Server: Bitnami Wamp Stack
Start Bitnami Wamp Stack
Click Go to Application: If you can see the site like the picture below, success!
Start or Stop the server: Click Manage Server
- If program below is shut down, go to the folder where ‘bitnami wamp stack’ installed and click ‘manager-windows.exe’
- Using Python in Web(HTML): Setting Apache
Install python and apache
find folder where apach installed(‘D:\wamp\apache2\conf’) > ‘conf’ folder > httpd.conf
open httpd.conf file and search:
1
LoadModule cgid_module modules/mod_cgid.so
and if ‘#’ exists in front of code, delete it
Find
tag in httpd.conf and add some lines 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35<Directory "/Applications/mampstack-8.0.5-0/apache2/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.4/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:
# AllowOverride FileInfo AuthConfig Limit
#
AllowOverride None
#
# Controls who can get stuff from this server.
#
Require all granted
********** add this code **********
<Files *.py>
Options ExecCGI
AddHandler cgi-script .py
</Files>
***********************************
</Directory>- htdocs 디렉토리 내 확장자가 py인 모든 파일은 CGI기능을 활성시키고 CGI로 실행하라는 의미
Restart Apache Web Server in manager-osx
Python file setting
- index.py 가 있는 htdocs 디렉토리에서 index.py 실행 후 아래같이 입력(다른 파이썬 파일을 만들어도 상관 없음)
1
2
3#!/usr/local/bin/python3 >>> python.exe 경로 환경변수에 저장해줬다면 !Python만 해도 됨
print("Content-Type: text/html")
print()- index.html 의 코드 넣기( 다른 html 파일 이어도 됨): index.py가 실행되었을 때 index.html 의 코드가 출력되게 해주는 코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#!/usr/local/bin/python3
print("Content-Type: text/html")
print()
print('''<!doctype html> # ---> 줄바꿈을 위해 docsting (''' ''') 사용
<html>
<head>
<title>WEB1 - Welcome</title>
<meta charset="utf-8">
</head>
<body>
<h1><a href="index.html">WEB</a></h1>
<ol>
<li><a href="qs-1.html">HTML</a></li>
<li><a href="qs-2.html">CSS</a></li>
<li><a href="qs-3.html">JavaScript</a></li>
</ol>
<h2>WEB</h2>
<p>The World Wide Web (abbreviated WWW or the Web) is an information space where documents and other web resources are identified by Uniform Resource Locators (URLs), interlinked by hypertext links, and can be accessed via the Internet.[1] English scientist Tim Berners-Lee invented the World Wide Web in 1989. He wrote the first web browser computer program in 1990 while employed at CERN in Switzerland.[2][3] The Web browser was released outside of CERN in 1991, first to other research institutions starting in January 1991 and to the general public on the Internet in August 1991.
</p>
</body>
</html>
''')- 웹 브라우저 주소창에 localhost:8080/index.py 입력하고 접속
- index.html 파일의 내용이 잘 출력된다면 구현 성공
- Internal Server Error 가 확인된다면 에디터에서 apache2/logs 디렉토리 내 error_log 파일에 있는 에러 코드 확인 및 구글링
EXAMPLE
1 | #!C:\Python310\python.exe --->파이썬 경로 |