1.Apache服务器的安装html
首先检查是否安装httpd服务,在终端输入以下指令:web
#rpm -qa | grep httpd apache
输出以下则表示已安装httpd服务:浏览器
测试httpd服务是否安装成功,可输入以下指令启动服务:服务器
#/etc/init.d/httpd start 或#/etc/ rc.d/init.d/httpd start 或#apachectl start 或#service httpd startide
查看httpd服务运行进程,可输入以下指令:测试
#ps -eaf | grep httpdui
显示以下:编码
查看httpd服务是否在监听端口80,可输入以下指令:加密
#netstat -anp | grep :80
显示以下:
打开防火墙TCP 80端口,即选中信任服务WWW(http)。(也可在终端输入命令#setup)
在客户端的浏览器中输入Apache服务器的IP地址,出现以下图提示信息,则表示Apache服务器安装成功。
要想让Apache服务开机自动运行,可在终端输入以下指令:
#system-config-services
打开以下窗口,选中httpd项:
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
显示结果以下图:
.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>
在客户端输入要反问的地址,则会出现以下:
输入所设用户和密码便可。
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