UBUNTU 11.04 配置 apache2 + cgi

安装apache2服务器。

zhang@zhang:~/Desktop$ sudo apt-get install apache2

建立cgi目录

zhang@zhang:~/Desktop$ sudo mkdir /var/www/cgi-bin


配置文件目录在/etc/apache2/sites-enabled/000-default

zhang@zhang:~/Desktop$ sudo vi /etc/apache2/sites-enabled/000-default

<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    DocumentRoot /var/www
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/www/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

将其改为下面的样式:

<VirtualHost *:80>
        ServerAdmin webmaster@localhost

        DocumentRoot /var/www
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

        ScriptAlias /cgi-bin/ /var/www/cgi-bin/
        <Directory "/var/www/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

修改完保存,重启apache2。

zhang@zhang:~/Desktop$ sudo /etc/init.d/apache2 restart

cgi测试(test.cpp):

#include <iostream>
using namespace std;
 
int main ()
{
 
   cout << "Content-type:text/html\r\n\r\n";
   cout << "<html>\n";
   cout << "<head>\n";
   cout << "<title>Hello World - First CGI Program</title>\n";
   cout << "</head>\n";
   cout << "<body>\n";
   cout << "<h2>Hello World! This is my first CGI program</h2>\n";
   cout << "</body>\n";
   cout << "</html>\n";
 
   return 0;
}

编译该程序:

zhang@zhang:~/Desktop$ g++ -o test.cgi test.cpp

zhang@zhang:~/Desktop$ chmod 755 test.cgi

zhang@zhang:~/Desktop$ sudo cp test.cgi /var/www/cgi-bin/

开启浏览器chromium,输入“http://localhost/cgi-bin/test.cgi”, 浏览器显示如下:

Hello World! This is my first CGI program


注意,你的CGI程序属性一定要设为可运行(755),而与CGI有关的HTML文件的目录如果要被CGI程序写入,其权限一定要设为可写(666)。


你可能感兴趣的