当前位置:   article > 正文

Linux Ubuntu22/debian11 Apache2 使用 Cgi 执行C语言程序_linux系统下c语言项目怎么配置cgi

linux系统下c语言项目怎么配置cgi

1. 修改源 /etc/apt/sources.list 为以下内容:

-----------------------------------------

deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free
deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free
deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free
#deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-backports main contrib non-free
#deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-backports main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian-security bullseye-security main contrib non-free
deb-src https://mirrors.tuna.tsinghua.edu.cn/debian-security bullseye-security main contrib non-free

-----------------------------------------

2. 安装apache和编译工具

sudo apt update

sudo apt upgrade

sudo apt install apache2 build-essential sudo

检查是否安装成功:

sudo systemctl status apache2

如果不成功,很可能是因为80端口被占用,查看端口是否被占用
lsof -i:80
如果被占用,或者是因为没有备案无法访问80端口,可以修改apache使用的端口:
sudo nano /etc/apache2/ports.conf
------------------
Listen 8080 
------------------

同时要修改VirtualHost的端口

sudo nano /etc/apache2/sites-enabled/000-default.conf

------------------

<VirtualHost *:8080>

------------------

重启apache2: 

sudo systemctl restart apache2 

在浏览器中打开htttp://server-ip:8080

3. 赋予apache2用户www-data root权限
nano /etc/sudoers
添加一行:
--------------------------
www-data ALL=(ALL) NOPASSWD:ALL
--------------------------

4. 启用cgi模块

cd /etc/apache2
sudo ln -s mods-available/cgid.conf mods-enabled/cgid.conf
sudo ln -s mods-available/cgid.load mods-enabled/cgid.load
sudo a2enmod cgid

5. 创建配置cgi文件夹

sudo mkdir /var/www/cgi

允许apache用户www-data操作这个文件夹:

chown www-data /var/www/cgi

修改配置文件 

sudo nano /etc/apache2/sites-enabled/000-default.conf在<VirtualHost>里面添加:

--------------------------------------------------------------

<VirtualHost *:8080> 

  ......................

  DocumentRoot /var/www/html

  ScriptAlias "/cgi/" "/var/www/cgi/" 

  ......................

  <Directory "/var/www/cgi">
    Options ExecCGI
    SetHandler cgi-script
  </Directory> 

</ VirtualHost>  

-------------------------------------------------------------- 

6. 配置完毕,重启debian: 

reboot

7. 编译配置可执行程序

需要注意的是:
all output from your CGI program must be preceded by a  MIME-type  header. This is HTTP header that tells the client what sort of content it is receiving. Most of the time, this will look like:
Content-type: text/html

--------------~/code/ChangeIP/main.c---------------------

#include <stdio.h>
int main ()
{
printf ( "Content-type:text/html \r\n\r\n Hello, world \n " );
return 0 ;
}

--------------end of main.c--------------------- 

gcc main.c -o ChangeIP

sudo cp ChangeIP /var/www/cgi/changeip.cgi

cd /var/www/cgi

sudo chmod 755 changeip.cgi

查看:

http://localhost:8000

http://localhost:8000/cgi/changeip.cgi

参考资料:

https://httpd.apache.org/docs/2.4/howto/cgi.html

https://blog.csdn.net/qq_51470638/article/details/121581696

https://blog.csdn.net/weixin_34975139/article/details/116729647

https://askubuntu.com/questions/683126/1404-your-mpm-seems-to-be-threaded-selecting-cgid-instead-of-cgi-module-cgid

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/218010
推荐阅读
相关标签
  

闽ICP备14008679号