wanlifeipeng

  博客园 :: 首页 :: 博问 :: 闪存 :: :: 联系 :: 订阅 订阅 :: 管理 ::

配置apache服务器允许cgi执行,首先要保证对应的模块已经在.conf文件中通过LoadModule指令被加载

#Linux下形如:
LoadModule cgid_module modules/mod_cgid.so
或者: LoadModule cgi_module modules/mod_cgi.so

# windows下形如:
LoadModule cgi_module modules/mod_cgi.so

 

ubuntu下通过apt-get安装的apache2默认配置文件根目录在/etc/apache中,主配置文件为apache2.conf

主配置文件中通过如下指令,加载其他的配置文件和模块

# Include module configuration:
IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf
# Include list of ports to listen on
Include ports.conf
# Include generic snippets of statements
IncludeOptional conf-enabled/*.conf
# Include the virtual host configurations:
IncludeOptional sites-enabled/*.conf

从而形成了如下结构:

而mods-enabled、conf-enabled、sites-enabled目录下的文件其实是相应available目录下的对应文件的软链接

 

通过 apachectl -M | grep cgid 发现cgid模块并未被加载,而mods-available目录下恰好存在对应的配置文件,因此只要在mods-enabled目录下创建cgid.conf和cgid.load的软链接,最后重启服务器即可。

hupeng@hupeng-vm:/etc/apache2$ apachectl -M | grep cgid
hupeng@hupeng-vm:/etc/apache2$ ls mods-available/ | grep cgid
cgid.conf
cgid.load

  hupeng@hupeng-vm:/etc/apache2$ cat mods-available/cgid.conf
  # Socket for cgid communication
  ScriptSock ${APACHE_RUN_DIR}/cgisock

  hupeng@hupeng-vm:/etc/apache2$ cat mods-available/cgid.load
  LoadModule cgid_module /usr/lib/apache2/modules/mod_cgid.so

hupeng@hupeng-vm:/etc/apache2$ cd mods-enabled/
hupeng@hupeng-vm:/etc/apache2/mods-enabled$ sudo ln -s ../mods-available/cgid.conf  cgid.conf
hupeng@hupeng-vm:/etc/apache2/mods-enabled$ sudo ln -s ../mods-available/cgid.load  cgid.load
hupeng@hupeng-vm:/etc/apache2/mods-enabled$ ls -l | grep cgid
lrwxrwxrwx 1 root root 27 2月  18 14:01 cgid.conf -> ../mods-available/cgid.conf
lrwxrwxrwx 1 root root 27 2月  18 14:02 cgid.load -> ../mods-available/cgid.load
# 重启服务器
hupeng@hupeng-vm:/etc/apache2/mods-enabled$ sudo apachectl -k restart
hupeng@hupeng-vm:/etc/apache2/mods-enabled$ apachectl -M | grep cgid
cgid_module (shared)

conf-enabled/serve-cgi-bin.conf正好是关于cgi的相关配置,将cgi存目录为/usr/lib/cgi-bin

hupeng@hupeng-vm:/etc/apache2$ cat conf-enabled/serve-cgi-bin.conf 
<IfModule mod_alias.c>
    <IfModule mod_cgi.c>
        Define ENABLE_USR_LIB_CGI_BIN
    </IfModule>

    <IfModule mod_cgid.c>
        Define ENABLE_USR_LIB_CGI_BIN
    </IfModule>

    <IfDefine ENABLE_USR_LIB_CGI_BIN>
        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
            AllowOverride None
            Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
            Require all granted
        </Directory>
    </IfDefine>
</IfModule>

在/usr/lib/cgi-bin目录下创建hello.c编译成.cgi,最后通过浏览器访问:

hupeng@hupeng-vm:/etc/apache2$ cd /usr/lib/cgi-bin/
hupeng@hupeng-vm:/usr/lib/cgi-bin$ cat hello.c
#include <stdio.h>

int main()
{
    printf("Content-Type: text/html;charset=utf8\r\n\r\n");
    printf("hello cgi");
    printf("<br />");
    printf("hello apache2, hello ubuntu");
    return 0;
}
hupeng@hupeng-vm:/usr/lib/cgi-bin$ sudo gcc hello.c -o hello.cgi

效果图:

 

 参考:

http://httpd.apache.org/docs/current/howto/cgi.html

Apache的动态共享对象(DSO)

 

posted on 2018-02-18 14:25  wanlifeipeng  阅读(557)  评论(0编辑  收藏  举报