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)测试:
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)测试
输入用户名和密码后就能够访问网站了:
3、配置虚拟主机
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>
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>
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>