[实验目的] 1.Apache的基本安装和配置。 2.Apache中应用CGI。 3.基本代理配置。 [实验要求] 1.两台互通的RH8.0机。 2.这两台机已配置过DNS。 [实验步骤] 1.准备工作。 由于web 服务器与DNS关系非常紧密。作好这步准备工作,请参考本人的另一篇文章 “用实验快速掌握DNS配置”。这篇文章发表在linuxsir的网络版块。 DNS 能够实现的主要功能就是能能解析:station1.exmaple1.com, server1.example1.com 两个域名的IP,如果你想在/etc/hosts中实现,我想也不是不可 以。这里不提倡这么做。 本文中作法比较教条,这里指明机器1为:server1.example1.com,机器2为: station1.example1.com .读者也可以自己变通。 2.在机器1上安装和配置简单的Apache。 1) 检查下列包是否安装, httpd httpd-manual 缺什么,装什么。 2) 开启httpd服务 # service httpd start 如果开启失败,查看/var/log/httpd/下的相关日志,切记要在/etc/hosts中添加类似 192.168.0.254 example1.com server1 的一行。 3) 检查/etc/httpd/conf/httpd.conf中,有下列一行活动 documentRoot /var/www/html 4) 用一个浏览器打开: http://server1.example1.com如果是正常情况,你会看到Apache的介绍页面。 5) 创建新的目录和html文件。 # mkdir -p /var/www/virtual/server1.example1.com/html # vi /var/www/virtual/server1.example1.com/html/index.html Server1.example1.com 6) 编辑/etc/httpd/conf/httpd.conf,末尾追加下列文本。 NameVirtualHost 192.168.0.254 ServerName server1.example1.com ServerAdmin root@server1.example1.comdocumentRoot /var/www/virtual/server1.example1.com/html ErrorLog logs/server1.example1.com-error_log CustomLog logs/server1.example1.com-access_log combined Options Indexes Includes 7) 确保DNS能够解析你的VirtualHost # host server1.example1.com 重新启动httpd # service httpd restart 如果启动失败,看/var/log/httpd/下的相应日志,会告诉你具体哪里错误。 9) 在浏览器里能够看到你自己写的网页了吗? http://server1.example1.com10) 在机器2上能看到http://server1.example1.com吗?3.机器1,在Apache中应用CGI 1) 编辑/etc/httpd/conf/httpd.conf,在块中添加下列一行: scriptAlias /cgi-bin/ /var/www/virtual/server1.example1.com/cgi-bin/ 2) 创建目录,创建文件。 # mkdir /var/www/virtual/server1.example1.com/cgi-bin #vi /var/www/virtual/server1.example1.com/cgi-bin/test.sh #!/bin/bash echo Content-Type: text/html echo echo “ ”echo My username is :whoamiechoecho My id is :idechoecho Here is /etc/passwd :cat /etc/passwdecho “ ” 3) 改变test.sh的执行权限: # chmod 555 test.sh 4) 重启httpd服务: # service httpd restart 若有错,看日志。 5) 浏览:http://server1.example1.com/cgi-bin/test.sh6) 机器2能浏览http://server1.example1.com/cgi-bin/test.sh吗?4.基本代理配置: 1) 机器1,检查squid 包装了没有,没有的话,则进行安装。 2) 机器1,启动squid 服务。 # service squid start 有问题? 看/var/log/messages. 3) 机器1,浏览器中设置代理端口为3128,举例:在mozilla: Edit | Preferences...| Advanced | proxies 中,设定手动,端口为:3128,其他不动。 机器2,浏览器中类似设置为:手动,http代理:192.168.0.254 端口:3128。 4) 此时,机器1,能浏览server1.exmaple1.com.,若平时能上internet,此时也正常上 internet。 5) 机器2,不能浏览server1.example1.com 或internet。 查看/var/log/httpd/*的日志文件,原因是什么? 6) 编辑/etc/squid/squid.conf,在acl CONNECT method CONNECT行下,添加下列一行: acl examample1 src 192.168.0.0/24 找到INSERT YOUR OWN RULE(S) HERE 增加下列一行: http_access allow example1 7) 重启squid # service squid restart 有错?看日志。 此时,机器2能够浏览server1.example1.com 或 internet网页. 9) 编辑/etc/squid/squid.conf ,在acl examample1 src 192.168.0.0/24行下添加 acl otherdeny dstdomain .sina.com.cn 在http_access allow example1下添加: http_access deny otherdeny 10) 重启squid. # service squid restart 有错? 看日志。 11) 机器2,仍能浏览 http://www.sina.com.cn,为什么12) 编辑/etc/squid/squid.conf ,把http_access deny otherdeny放到 http_access allow example1前面,重启squid,机器2还能看到 http://www.sina.com.cn吗[实验总结] Apache配置是否成功,与DNS有很大关系,所以,要求读者先花时间作好DNS工作。本文以最通俗的方式,教你简单的配置Apache,如果想深入掌握它,还得研究Apache2.0的文档。