用实验快速掌握Apache[原创]

由 nobody_am 在 07-13-2003 15:30 发表:

用实验快速掌握Apache[原创]

前言:Apache是大多数linux版本的标准web 服务器,掌握他的意义就非常大。

本文以实验的方式,让读者快速掌握Apache的基本配置。

[实验目的]

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

缺什么,装什么。

  1. 开启httpd服务

service httpd start

如果开启失败,查看/var/log/httpd/下的相关日志,切记要在/etc/hosts中添加类似

192.168.0.254 example1.com server1 的一行。

  1. 检查/etc/httpd/conf/httpd.conf中,有下列一行活动

DocumentRoot /var/www/html

  1. 用一个浏览器打开:

http://server1.example1.com

如果是正常情况,你会看到Apache的介绍页面。

  1. 创建新的目录和html文件。

mkdir -p /var/www/virtual/server1.example1.com/html

vi /var/www/virtual/server1.example1.com/html/index.html

1<b> Server1.example1.com </b>
  1. 编辑/etc/httpd/conf/httpd.conf,末尾追加下列文本。

NameVirtualHost 192.168.0.254

 1<virtualhost 192.168.0.254="">   
 2  
 3ServerName server1.example1.com   
 4  
 5ServerAdmin [email protected]   
 6  
 7DocumentRoot /var/www/virtual/server1.example1.com/html   
 8  
 9ErrorLog logs/server1.example1.com-error_log   
10  
11CustomLog logs/server1.example1.com-access_log combined   
12  
13<directory>   
14  
15Options Indexes Includes   
16  
17</directory>
18</virtualhost>
  1. 确保DNS能够解析你的VirtualHost

host server1.example1.com

  1. 重新启动httpd

service httpd restart

如果启动失败,看/var/log/httpd/下的相应日志,会告诉你具体哪里错误。

  1. 在浏览器里能够看到你自己写的网页了吗?

http://server1.example1.com

  1. 在机器2上能看到http://server1.example1.com吗?

3.机器1,在Apache中应用CGI

  1. 编辑/etc/httpd/conf/httpd.conf,在
  1<virtualhost>块中添加下列一行:   
  2  
  3ScriptAlias /cgi-bin/ /var/www/virtual/server1.example1.com/cgi-bin/   
  4  
  52) 创建目录,创建文件。   
  6  
  7# mkdir /var/www/virtual/server1.example1.com/cgi-bin   
  8  
  9#vi /var/www/virtual/server1.example1.com/cgi-bin/test.sh   
 10  
 11  
 12  
 13#!/bin/bash   
 14  
 15echo Content-Type: text/html   
 16  
 17echo   
 18  
 19  
 20  
 21echo “<pre>”   
 22  
 23echo My username is :   
 24  
 25whoami   
 26  
 27echo   
 28  
 29echo My id is :   
 30  
 31id   
 32  
 33echo   
 34  
 35echo Here is /etc/passwd :   
 36  
 37cat /etc/passwd   
 38  
 39echo “</pre>”   
 40  
 413) 改变test.sh的执行权限:   
 42  
 43# chmod 555 test.sh   
 44  
 454) 重启httpd服务:   
 46  
 47# service httpd restart   
 48  
 49若有错,看日志。   
 50  
 515) 浏览:http://server1.example1.com/cgi-bin/test.sh   
 52  
 536) 机器2能浏览http://server1.example1.com/cgi-bin/test.sh吗?   
 54  
 55  
 56  
 574.基本代理配置:   
 58  
 591) 机器1,检查squid 包装了没有,没有的话,则进行安装。   
 60  
 612) 机器1,启动squid 服务。   
 62  
 63# service squid start   
 64  
 65有问题? 看/var/log/messages.   
 66  
 673) 机器1,浏览器中设置代理端口为3128,举例:在mozilla: Edit | Preferences...|   
 68  
 69Advanced | proxies 中,设定手动,端口为:3128,其他不动。   
 70  
 71机器2,浏览器中类似设置为:手动,http代理:192.168.0.254 端口:3128。   
 72  
 734) 此时,机器1,能浏览server1.exmaple1.com.,若平时能上internet,此时也正常上   
 74  
 75internet。   
 76  
 775) 机器2,不能浏览server1.example1.com 或internet。   
 78  
 79查看/var/log/httpd/*的日志文件,原因是什么?   
 80  
 816) 编辑/etc/squid/squid.conf,在acl CONNECT method CONNECT行下,添加下列一行:   
 82  
 83acl examample1 src 192.168.0.0/24   
 84  
 85找到INSERT YOUR OWN RULE(S) HERE 增加下列一行:   
 86  
 87http_access allow example1   
 88  
 897) 重启squid   
 90  
 91# service squid restart   
 92  
 93有错?看日志。   
 94  
 958) 此时,机器2能够浏览server1.example1.com 或 internet网页.   
 96  
 979) 编辑/etc/squid/squid.conf ,在acl examample1 src 192.168.0.0/24行下添加   
 98  
 99acl otherdeny dstdomain .sina.com.cn   
100  
101在http_access allow example1下添加:   
102  
103http_access deny otherdeny   
104  
10510) 重启squid.   
106  
107# service squid restart   
108  
109有错? 看日志。   
110  
11111) 机器2,仍能浏览 http://www.sina.com.cn,为什么   
112  
11312) 编辑/etc/squid/squid.conf ,把http_access deny otherdeny放到   
114  
115http_access allow example1前面,重启squid,机器2还能看到   
116  
117http://www.sina.com.cn吗   
118  
119  
120  
121[实验总结]</virtualhost>
Published At
Categories with 服务器类
Tagged with
comments powered by Disqus