在centos上安装httpd,并实现html文件和cgi文件在网页上显示
简介
-
Apache-httpd的简介
-
Apache-httpd的安装
-
html文件和cgi文件的编写
-
网页访问html和cgi文件
-
遇到相关问题解决
搭建静态web网页实验环境:
腾讯云上申请的centos7系统,使用的是mobaxterm远程操作:
mobaxterm网盘链接:https://pan.baidu.com/s/1fdiFTVpDQJIF9R8_N7b4vA
提取码:kelq
复制这段内容后打开百度网盘手机App,操作更方便哦
Apache简介:
Apache HTTP Server(简称Apache)是Apache软件基金会的一个开放源代码的网页服务器软件,可以在大多数电脑操作系统中运行,由于其跨平台和安全性(尽管不断有新的漏洞被发现,但由于其开放源代码的特点,漏洞总能被很快修补。因此总合来说,其安全性还是相当高的。)。被广泛使用,是最流行的Web服务器软件之一。它快速、可靠并且可通过简单的API扩充,将Perl/Python等解释器编译到服务器中。
Apache-httpd的安装:
我们使用命令安装:
yum install httpd
#安装成功我们可以看到在/var/目录下会产生一个www的目录,该目录下还包含/cgi-bin/ /html/连个目录
cgi-bin目录下主要存放cgi文件
html目录下主要存放html网页文件
此时可启动httpd服务,查看启动后的状态;
[root@VM_0_16_centos cgi-bin]# systemctl start httpd.service
[root@VM_0_16_centos cgi-bin]# systemctl status httpd.service
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: active (running) since Sat 2019-10-12 22:47:27 CST; 12h ago
Docs: man:httpd(8)
man:apachectl(8)
Main PID: 9395 (httpd)
Status: "Total requests: 61; Current requests/sec: 0; Current traffic: 0 B/sec"
CGroup: /system.slice/httpd.service
├─ 9395 /usr/sbin/httpd -DFOREGROUND
├─ 9400 /usr/sbin/httpd -DFOREGROUND
├─ 9401 /usr/sbin/httpd -DFOREGROUND
├─ 9402 /usr/sbin/httpd -DFOREGROUND
├─ 9403 /usr/sbin/httpd -DFOREGROUND
├─ 9404 /usr/sbin/httpd -DFOREGROUND
├─10291 /usr/sbin/httpd -DFOREGROUND
├─10299 /usr/sbin/httpd -DFOREGROUND
└─10300 /usr/sbin/httpd -DFOREGROUND
Oct 12 22:47:27 VM_0_16_centos systemd[1]: Starting The Apache HTTP Server...
Oct 12 22:47:27 VM_0_16_centos httpd[9395]: AH00558: httpd: Could not reliab...e
Oct 12 22:47:27 VM_0_16_centos systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.
此时打开浏览器,输入自己虚拟机IP地址:http://122.51.14.***,即可看到apache的默认界面;
html文件和cgi文件的编写:
创建HTML文件:
在电脑记事本上创建一个test.html文件,也可以在命令窗口创建,但必须先转到储存该文件的文件夹下,这里我用电脑记事本创建,然后利用上面的远程操作软件直接上传到/var/www/html文件夹中:
<html>
<head><title>测试</title></head>
<body>
<font size=30 text=隶书>你好</font>
<form action="/cgi-bin/test.cgi" method ="post" > //点击后自动跳转到cgi-bin/test.cgi文件夹
Hit Me:<input type="submit//通过点击的方式调动
</form>
</body>
</html>
创建cgi文件:
使用命令创建,必须先转到储存该文件的文件夹下,也就是/cgi-bin文件下,先使用命令创建一个test.c文件:
vim test.c
#然后输入一个简单的hello world程序
保存后开始使用gcc编译
helloworld代码:
在编辑是必须加入这段代码: printf(“Content-Type:text/html\n\n”);
#include<stdio.h>
int main()
{
printf("Content-Type:text/html\n\n");//要将以下的输出语句输出在页面上就必须加此行代码。相当于输出重定位
printf("Hello World!!");
return 0;
}
使用命令gcc编译:
[root@VM_0_16_centos ~]# gcc -o test.cgi test.c
#这样编译自动生成一个test.cgi文件
网页访问html和cgi文件:
先必须开放你的centos系统的80端口,httpd默认访问端口是80
接下来在网页中输入网址:http://122.51.14.35:80/test.html (即http://ip:端口号/html文件名) 访问结果如图:
能运行出以上结果表示我们的安装已经成功,并且能正确打开HTML文件
接下来我们测试cgi文件:
在上面的页面中,我们点击提交按钮,会跳转出下面的界面,因为我们上面的html文件里面编写了,所以可以自接跳转 :
也可以直接通过网址访问到cgi文件,下面我们再创建一个cgi文件:
[root@VM_0_16_centos cgi-bin]# cd hjw.c
[root@VM_0_16_centos cgi-bin]# gcc -o hjw.cgi hjw.c
#到此已经自动生成了一个hjw.cgi文件了
hjw.cgi文件代码:
#include <stdio.h>
int main()
{ printf("Content-Type:text/html\n\n");//要将以下的输出语句输出在页面上就必须加此行代码。相当于输出重定位
printf("欢迎各位,你们好");
}
接下来我们输入:http://122.51.14.35/cgi-bin/hjw.cgi访问cgi文件:
遇到相关问题解决:
1.访问不到apache的默认界面:
查看是否打开httopd:使用下面命令启动
systemctl start httpd
2.访问不到html文件:
查看是否打开了虚拟机的80端口
3.访问不到cgi文件:
查看是否在编辑c程序是加入了这段代码: printf(“Content-Type:text/html\n\n”);
搭建一个静态web网页:
我们先在/var/www/html文件夹下新建一个index.html文件:
vim /var/www/html/index.html
#编辑的代码如下:
<h1>测试主页</h1>
此时输入http://122.51.14.35,访问的就不是Apache主页了,而是index.html:网页显示的是:测试主页
修改httpd服务目录,搭建web主页:
新建一个httpd服务目录文件夹
[root@VM_0_16_centos ~]# /data/web/html -pv
[root@VM_0_16_centos ~]#vim /etc/httpd/conf/httpd.conf
119行"DocumentRoot "/var/www/html"修改为
"DocumentRoot "/data/web/html"
还有131行"# Further relax access to the default document root:
<Directory "/var/www/html">"需要修改为
<Directory "/data/web/html">
修改完后,使用httpd -t检查语法是否有误;
[root@VM_0_16_centos ~]# httpd -t
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using ::1. Set the 'ServerName' directive globally to suppress this message
Syntax OK
到此已经更改好了httpd的服务目录
新建一个测试主页看一下效果:
vim /data/web/html/index.html
#编辑的内容:<h2>效果展示</h2>
修改完毕重启httpd服务:
systemctl restart httpd.service
打开浏览器刷新看效果:
也可以输入http://122.51.14.35/hjw.html访问在web/html下的其他网页: