配置apache

1. 修改httpd.conf文件
# vi  /usr/local/apache/conf/httpd.conf
1) 设置根目录的路径
根目录是指Apache存放配置文件和日志文件的目录,配置参数为ServerRoot,默认位于“/usr/local/apache”。命令如下:
 ServerRoot /usr/local/apache
2) 设置监听IP地址及端口号
默认侦听本机所有IP地址的TCP80端口,命令如下:
Listen 80
用户也可以按自己的需求,使用多个Listen语句在多个地址和端口上侦听客户端请求。比如:
Listen 192.168.99.9:80
Listen 172.16.0.20:8080
3) 设置系统管理员E-mail
使用ServerAdmin参数设置管理员E-mail,比如管理员的Email地址为root@guoxuemin.cn:
 ServerAdmin root@guoxuemin.cn
4) 设置服务器主机的名称
参数ServerName用来设置服务器的主机名称,如果没有域名则填入服务器的IP地址,比如服务器的IP地址为192.168.99.9:80
 ServerName 192.168.99.9:80
5) 设置主目录的路径
用户可以使用参数DocumentRoot配置服务器主目录默认路径,比如,主目录路径为:
 DocumentRoot /usr/local/apache/htdocs
6) 设置默认文件
Apache的默认文件名为index.html,可以使用Directory Index参数来配置,比如,将index.php设置为默认文件名:
 <IfModulf dir_moudle>
Directory Index index.html
</IfModulf>
7)测试:
打开浏览器,输入地址:http://192.168.99.9,可以打开站点了:
 
 
2. 配置目录权限
使用<Directory 目录路径>和</Directory>设置目录的权限。比如:
<Directory  “/var/www/icons”>
Options  Indexes  MultiViews
AllowOverride  None
Order  allow,deny
Allow  from  all
</Directory>
说明:
1)定义目录特性选项Options
可选参数:
Indexes:该特性表明目录允许“目录浏览”;
MultiViews:该特性表明目录允许内容协商的多重试图;
All:包含了除MultiViews外的所有特性;
ExecCGI:该特性表明允许在该目录下执行CGI脚本;
FollowSymLinks:该特性表明允许在该目录下使用符号连接。
2).htaccess文件
可以通过.htaccess文件(访问控制文件)设置目录的权限。
AccessFileName  .htaccess
配置参数AllowOverride指定目录的.htaccess文件中指令的类型,包括All、None与Options、FileInfo、AuthConfig、Limit的任意组合。一般将AllowOverride设置为“None”,禁止使用.htaccess文件,当AllowOverride参数为All时,.htaccess文件可以覆盖任何以前的配置。
3)设置访问控制
使用Order选项来定义访问权限。
比如以下语句表明允许所有客户机的访问:
Order  allow,deny
Allow  from  all
以下语句表明只允许网段192.168.99.0/24的客户机访问,但IP地址为192.168.99.254这个客户机除外:
Order  allow,deny
Allow from  192.168.99.0/24
Deny from  192.168.99.254
用户可以根据需要,按上述方法配置自己的目录权限。
 
3. 创建虚拟目录
使用Alias选项创建虚拟目录,比如,建立“/icons/”这个虚拟目录,其对应的物理路径为“/var/www/icons/”:
Alias  /icons/  “/var/www/icons/”
 
4. 用户认证
比如,有一个名为myweb的虚拟目录,其对应的物理路径是“/usr/local/myweb”,现对其启用用户认证功能,只允许用户Tonyguo和Wayne访问。
1)建立虚拟目录并设置用户认证:
 
2) 建立口令文件并为用户设置口令
 
-c选项表示无论口令文件是否已经存在,都会重新写入文件并删除原内容。所以第二个用户wayne不需要使用-c选项。
3)测试
在浏览器中输入:http://192.168.99.9/myweb,可以看到如下对话框:
 
输入用户名和密码后就可以访问网站了:
 
 
三、配置虚拟主机
1. 配置基于IP的虚拟主机
1)IP地址相同,但端口号不同的虚拟主机配置
比如使用192.168.99.9的两个不同端口80和8080发布两个不同站点, 虚拟主机分别对应的目录为/usr/local/apache/htdocs/web1和/usr/local/apache/htdocs/web2:
Listen 80
Listen 8080
<VirtualHost  192.168.99.9:80>
  ServerSignature  email
  DocumentRoot  /usr/local/apache/htdocs/web1
  DirectoryIndex  index.html  index.htm
  LogLevel  warm
  HostNameLookups  off
</VirtualHost>
<VirtualHost  192.168.99.9:8080>
  ServerSignature  email
  DocumentRoot  /usr/local/apache/htdocs/web2
  DirectoryIndex  index.html  index.htm
  LogLevel  warm
  HostNameLookups  off
</VirtualHost>
2)配置基于域名的虚拟主机
比如服务器有两个IP地址192.168.99.9和192.168.99.10,使用这两个IP创建两台虚拟主机,虚拟主机分别对应的目录为/usr/local/apache/htdocs/web1和/usr/local/apache/htdocs/web2。设置方法如下:
<VirtualHost  192.168.99.9>
  ServerName  192.168.99.9:80
  DocumentRoot  /usr/local/apache/htdocs/web1
  DirectoryIndex  index.html  index.htm
</VirtualHost>
<VirtualHost  192.168.99.10>
  ServerName  192.168.99.10:80
  DocumentRoot  /usr/local/apache/htdocs/web2
  DirectoryIndex  index.html  index.htm
</VirtualHost>
 
2. 配置基于域名的虚拟主机
比如有两个域名guoxuemin.cn和tonyguo.com需要使用同一台服务器192.168.99.9,那么可以这样配置:
NameVirtualHost  192.168.99.9
<VirtualHost  www.guoxuemin.cn>
  ServerName  www.guoxuemin.cn:80
  ServerAdmin  admin@guoxuemin.cn
  DocumentRoot  /usr/local/apache/htdocs/web1
  DirectoryIndex  index.html  index.htm
  ErrorLog  logs/web1/error_log
  Customlog  logs/web1/access_log  combined
</VirtualHost>
<VirtualHost  www.tonyguo.com>
  ServerName   www.tonyguo.com:80
  ServerAdmin  admin@tonyguo.com 
DocumentRoot  /usr/local/apache/htdocs/web2
  DirectoryIndex  index.html  index.htm
  ErrorLog  logs/web1/error_log
  Customlog  logs/web1/access_log  combined
</VirtualHost>
posted @ 2016-11-29 18:14  信则成  阅读(294)  评论(0编辑  收藏  举报