Bash Shell로 Apache에 CGI 설정하는 방법

입질쾌감 물때표

아파치 설치 후 userdir 모듈 enable

@@ 아파치 리스타트
sudo a2enmod userdir
sudo service apache2 restart

userdir.conf 파일 수정

user1@ubuntu-msi-laptop:~/public_html/cgi-bin$ cat /etc/apache2/mods-enabled/userdir.conf

<IfModule mod_userdir.c>
        UserDir public_html
        UserDir disabled root

        <Directory /home/*/public_html>
                AllowOverride FileInfo AuthConfig Limit Indexes
                Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
                <Limit GET POST OPTIONS>
                        Order allow,deny
                        Allow from all
                </Limit>
                <LimitExcept GET POST OPTIONS>
                        Order deny,allow
                        Deny from all
                </LimitExcept>
        </Directory>

    <Directory /home/*/public_html/cgi-bin/>
        Options ExecCGI
        SetHandler cgi-script
    </Directory>

</IfModule>

샘플 HTML생성 예제

user1@ubuntu-msi-laptop:~/public_html/cgi-bin$ cat sample.sh
#!/bin/bash
echo “Content-type: text/html”;
echo “Charset: UTF-8″;
echo ”;
echo “<html>”;
echo “<head>”;
echo “<meta http-equiv=\”Content-type\” content=\”text/html; charset=utf-8\”>”;
echo “</head>”;
echo “<body>”;
echo “$QUERY_STRING”;
mysql -uusername -p1234 -h192.168.0.104 -e \
“SELECT DATE, NUMBER_OF_TURN FROM FOREIGN_EXCHANGE_RATE ORDER BY ID DESC LIMIT 1” dacom
echo ‘안녕하세요 CGI Bash Example’
ID=`echo “$QUERY_STRING” | grep -oE “(^|[?&])id=[0-9]+” | cut -f 2 -d “=” | head -n1`
INFO=`echo “$QUERY_STRING” | grep -oE “(^|[?&])info=[^&]+” | sed “s/%20/ /g” | cut -f 2 -d “=”`
echo “</body>”;
echo “</html>”;

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다