ubuntu에서 python cgi 연동하기

CGI 실행 허용을 위한 설정

  1. .py확장자를 CGI로 실행하도록 아파치 설정
sudo vi /etc/apache2/sites-enabled/000-default.conf
<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        ServerName www.magpie.com
        DocumentRoot 루트경로
        <Directory 루트경로>
                AddHandler cgi-script .py
                Options ExecCGI
        </Directory>

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

2. cgi 기능 켜기

sudo a2enmod cgi

3. 아파치 서버 재기동

sudo service apache2 restart

4. 테스트 파일 추가

#!/usr/bin/python3
print("content-type:text/html; charset=UTF-8\n")

5. 파일에 권한 변경

sudo chmod a+x helloworld.py

6. 오류 발생시 로그 확인

 sudo tail -f /var/log/apache2/error.log

댓글 남기기

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다