1.Apache服务器的安装

首先检查是否安装httpd服务,在终端输入如下指令:

#rpm -qa | grep httpd

输出如下则表示已安装httpd服务:

200022210.jpg

测试httpd服务是否安装成功,可输入如下指令启动服务:

#/etc/init.d/httpd start 或#/etc/ rc.d/init.d/httpd start 或#apachectl start 或#service httpd start

查看httpd服务运行进程,可输入如下指令:

#ps -eaf | grep httpd

显示如下:

200024153.jpg

查看httpd服务是否在监听端口80,可输入如下指令:

#netstat -anp | grep :80

显示如下:

200026460.jpg

打开防火墙TCP 80端口,即选中信任服务WWW(http)。(也可在终端输入命令#setup)

200029306.jpg

200032382.jpg

在客户端的浏览器中输入Apache服务器的IP地址,出现如下图提示信息,则表示Apache服务器安装成功。

200036496.jpg


要想让Apache服务开机自动运行,可在终端输入如下指令:

#system-config-services

打开如下窗口,选中httpd项:

200040384.jpg


2.Apache服务的基本配置

Apache服务的主配置文件是/etc/httpd/conf/httpd.conf,它共有以下三部分。

### Section 1: Global Environment ,主要进行全局环境变量的设置。

### Section 2: 'Main' server configuration,进行主服务器配置。

### Section 3: Virtual Hosts,进行虚拟主机设置。

(1)Section 1: Global Environment(Apache全局设置)。

ServerRoot "/etc/httpd"

此为Apache的根目录,配置文件、记录文件、模块文件都在该目录下。

PidFile run/httpd.pid

此文件保存着Apache父目录ID。

KeepAlive Off

不允许客户端同事提出多个请求,设为on表示允许。

#Listen 12.34.56.78:80

Listen 80

设置Apache服务的监听端口。一般在使用非80端口时设置。

#EntendStatus On

用于检测Apache的状态信息,预设为Off。

User apache

Group apache

设置Apache工作时使用的用户和组。

(2)Section 2: 'Main' server configuration(主服务器配置)。

ServerAdmin root@localhost

管理员的电子邮件地址。如果Apache有问题,则会寄信给管理员。

#ServerName www.example.com:80

此处为主机名称,如果没有申请域名,使用IP地址也可以。

DocumentRoot "/var/www/html"

设置Apache主服务器网页存放地址。

<Directory />

Options FollowSynLinks

AllowOverride None

</Directory>

设置Apache根目录的访问权限和访问方式。

<Directory "/var/www/html">

Options Indexes FollowSynLinks

AllowOverride None

Order allow,deny

Allow from all

</Directory>

设置Apache主服务器网页文件存放目录的访问权限。

<IfModule mod_userdir.c>

UserDir disable

#UserDir public_html

</IfModule>

设置用户是否可以在自己的目录下建立 public_html 目录来放置文件。如果设置为“UserDir public_html”,则用户就可以通过 “http://服务器IP地址:端口/~用户名称”来访问其中的内容。

DirectoryIndex index.html index.html.var

设置预设首页,默认是index.html。设置以后,用户通过“http://服务器IP地址:端口/”访问的就是“http://服务器IP地址:端口/index.html”。

AccessFileName .htaccess

设置Apache目录访问权限的控制文件,预设为.htaccess,也可以是其他名字。

<Files ~ "^\.ht">

Order allow,deny

Deny from all

</Files>

防止用户看到以“.ht” 开头的文件,保护.htaccess、.htpasswd 的内容。主要是为了防止其他人看到预设可以访问相关内容的用户名和密码。

HostnameLookups Off

如果设置为On,则每次都会向DNS服务器要求解析该IP,这样就会花费额外的服务器资源,并且降低服务器端响应速度,所以一般设置为Off。

Alias /icons/ "/var/www/icons/"

<Directory "/var/www/icons">

Options Indexes MultiViews

AllowOverride None

Order allow,deny

Allow from all

</Directory>

定义一个图标虚拟目录,并设置访问权限。

ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"

<Directory "/var/www/cgi-bin">

AllowOverride None

Options None

Order allow,deny

Allow from all

</Directory>

同Alias一样,只不过设置的是脚本文件目录。

#DefaultLanguage nl

设置页面的默认语言。

(3)Section 3: Virtual Hosts,(虚拟主机设置)。

#NameVirtualHost *:80

设置虚拟主机的名字和监听端口。

#<VirtualHost *:80>

# ServerAdmin webmaster@dummy-host.example.com

# DocumentRoot /www/docs/dummy-host.example.com

# ServerName dummy-host.example.com

# ErrorLog logs/dummy-host.example.com-error_log

# CustomLog logs/dummy-host.example.com-access_log common

#</VirtualHost>

虚拟主机的所有相关信息。主服务器中采用的很多控制命令都可以在这里使用。


下面讲述一些对Apache访问权限的设置:

1.查看服务器状态

设置只允许IP地址为192.168.91.240的主机查看服务器状态。

在httpd.conf中修改如下:

去掉下行文字前面的#:

#EntendStatus On

还有下面文字前面的#:

#<Location /server-status>

# SetHandler server-status

# Order deny,allow

# Deny from all

# Allow from 192.168.91.240 //后面改为192.168.91.240

#</Location>

重新启动httpd服务。 #apachectl restart

在192.168.91.240主机浏览器中输入如下:

http://192.168.91.128/server-status

显示结果如下图:

200044858.jpg


.htaccess文件控制存取

.htaccess是一个访问控制文件,用来配置相应目录的访问方法。不过,按照默认的配置时是不会读取相应目录下的.htaccess文件来进行访问控制的。这是因为AllowOverride中配置为AllowOverride None。因此只需修改AllowOverride的格式即可(有多项格式如:AuthConfig 、FileInfo、All等)。


3.用户身份认证

创建用户名和密码

在/usr/local/httpd/bin目录下,有一个htpasswd可执行文件,它就是用来创建.htaccess文件身份认证使用的密码的,它的语法格式如下:

#htpasswd [-bcD] [-mdps] 密码文件名 用户名

参数说明如下:

-c :新创建一个密码文件。

-D:删除一个用户。

-m:采用MD5编码加密。

修改httpd.conf文件

在httpd.conf文件中输入如下即可(可在文中</Directory>下):

<Directory "/var/www/html/mytest">

AllowOverride None

AuthType Basic

AuthName "myrealm"

AuthUserFile /var/www/passwd/.htpasswd

require user test //设置允许访问的用户,require Group则设置可以访问的群组。

</Directory>

在客户端输入要反问的地址,则会出现如下:

200047464.jpg

输入所设用户和密码即可。

4.虚拟主机

去除下行前面的#:

NameVirtualHost *:80

在文件结尾可输入如下:

<VirtualHost *:80>

DocumentRoot /var/www/docs //设置虚拟主机文档路径

ServerName aas.test.com //设置主机名

</VirtualHost>

<VirtualHost *:80>

DocumentRoot /var/www/html/mytest

ServerName web.test.com

DirectoryIndex index.htm //设置预设首页,默认是index.html

</VirtualHost>

在客户端浏览器中输入服务器虚拟主机名即可

如输入:http://web.test.com。

注:由于默认客户端并不能解析域名aas.test.com,web.test.com。可在客户端中修改文件C:\WINDOWS\system32\drivers\etc\hosts。

在hosts文件末尾添加如下两行:

192.168.91.128 aas.test.com

192.168.91.128 web.test.com