在ubuntu下安装apache一个命令就能够搞定 sudo apt-get install apche (连续按两次tab,会出现提示),顺其天然的安装就好了。若是说安装过程失败,提示须要某些依赖库的时候,运行sudo apt-get -f install就能够了。html
安装完成以后运行 apache2 -v 查看apache版本ios
Server version: Apache/2.4.7 (Ubuntu)
Server built: Jan 14 2016 17:45:23apache
利用浏览器访问网页 192.168.0.112(换成你的ip地址) 若是能正常看到网页打开了表示apache安装没问题。ubuntu
运行cd /etc/apache2 进入到apache2目录下,在这个目录查找httpd.conf。若是没有,直接新建一个httpd.conf。vim
在里面写入, #表示注释内容浏览器
LoadModule cgi_module /usr/lib/apache2/modules/mod_cgi.so #加载cgi库文件
addHandler cgi-script .cgi .pl .py .sh #cgi支持的后缀名 测试
在用vim打开apache2.confui
在最后面加上 Include httpd.conf 表示包含httpd.conf,在apache在启动的时候会加入这个配置文件。因此要重启apache(apaech2 restart)。spa
提示:sudo vim httpd.conf命令行
1.输入i表示开始插入,在vim下面能够看到当前的vim的insert状态
2.写完以后,按下esc表示命令行模式,这时在按下shfit+: 再按下wq 表示退出并保存(注意没有的+,+表示多个键一块儿按下)
3.修改apache2.conf 时一样使用sudo vim apache2.conf
下面是测试代码
#include <iostream> using namespace std; int main(void) { 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 << "<h1>\n"; cout << "hello world!\n"; cout << "</h1>\n"; cout << "<h1>\n"; cout << "hello apache!\n"; cout << "</h1>\n"; cout << "</body>\n"; cout << "</html>\n"; return 0; }
使用 g++ -Wall -g hello.cpp -o hello.cgi 注意要以cgi结尾不然执行脚本的时候可能不能正常执行。
再将hello.cgi 拷贝到 /var/www/cgi-bin/. 若是提示目录不存在的就去建立目录 sudo mkdir /var/www/cgi-bin -p
再从浏览器访问 192.168.0.112/cgi-bin/hello.cgi 表示完成cgi配置。若是没有成功不用担忧,多试几回,查看apache错误日志分析问题(这个很重要,由于我第一次也没有配置成功的),日志地址为/var/log/apache2/