一,apache安装模式
./configure --prefix=/var/apache --enable-MODULE=shared --enable-so --enable-rewrite --with-mpm=worker
解释:
--enable-MODULE=shared DSO模式 表示动态加载模块
--enable-MODULE=static默认 静态模式 表示将相关的模块静态编译
--enable-so表示支持用mod_so模块提供的功能
--with-mpm=worker表示何种模式编译,如果web服务器期望负载较大,则推荐worker模式
如果稳定性优先考虑的话,则prefork模式。同样负载情况下,prefork模式的apache占有内存会很大。
二,配置Apache
1. 修改httpd.conf文件
# vi /usr/local/apache/conf/httpd.conf
1) 设置根目录的路径
根目录是指Apache存放配置文件和日志文件的目录,配置参数为ServerRoot,默认位于“/usr/local/apache”。命令如下:
2) 设置监听IP地址及端口号
默认侦听本机所有IP地址的TCP80端口,命令如下:
Listen 80
用户也可以按自己的需求,使用多个Listen语句在多个地址和端口上侦听客户端请求。比如:
Listen 192.168.99.9:80
Linsten 172.16.0.20:8080
3) 设置系统管理员E-mail
ServerAdmin admin@pcp.com
4) 设置服务器主机的名称
ServerName www.pcp.com:80 / ServerName 172.16.0.20:8080
5) 设置主目录的路径
用户可以使用参数DocumentRoot配置服务器主目录默认路径,比如,主目录路径为:
DocumentRoot "/usr/local/apache/htdocs"
6) 设置默认文件
<IfModule dir_module>
DirectoryIndex index.html index.htm index.php
</IfModule>
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选项创建虚拟目录,比如,建立“/ldap/”这个虚拟目录,其对应的物理路径为"/usr/local/phpldapadmin”:
<IfModule alias_module>
Alias /ldap "/usr/local/phpldapadmin"
</IfModule>
4. 用户认证
1)建立虚拟目录并设置用户认证:
<Directory "/usr/local/phpldapadmin/">
AllowOverride None
Options None
Order allow,deny
Allow from all
AuthType Basic
AuthName "please login:"
AuthUserFile /etc/httpd/admin_pwd
Require User dys
</Directory>
2) 建立口令文件并为用户设置口令
htpasswd -c /etc/httpd/admin_pwd dys
-c选项表示无论口令文件是否已经存在,都会重新写入文件并删除原内容。所以第二个用户不需要使用-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地址不相同,端口相同
比如服务器有两个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. 配置基于域名的虚拟主机
比如有两个域名pcp.com和pcp2.com需要使用同一台服务器192.168.99.9,那么可以这样配置:
NameVirtualHost 192.168.99.9
<VirtualHost www.pcp.com>
ServerName www.pcp.com:80
ServerAdmin admin@pcp.com
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.pcp2.com>
ServerName www.pcp2.com:80
ServerAdmin admin@pcp.com
DocumentRoot /usr/local/apache/htdocs/web2
DirectoryIndex index.html index.htm
ErrorLog logs/web1/error_log
Customlog logs/web1/access_log combined
</VirtualHost>