一、安装Apache


下载地址:http://httpd.apache.org/


1. 安装Apache

# tar  zxvf  httpd-2.2.11.tar.gz

# cd  httpd-2.2.11

# ./configure  --prefix=/usr/local/apache  --enable-so

//编译时加上加载模块参数--enable-so

# make

# make  install


2. 配置系统启动时自动启动Apache服务。

# vi  /etc/rc.d/rc.local

 //在rc.local上加入一行/usr/local/apache/bin/apachectl –k  start。


二、配置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

Listen 172.16.0.20:8080


3) 设置系统管理员E-mail

使用ServerAdmin参数设置管理员E-mail,比如管理员的Email地址为root@guoxuemin.cn


4) 设置服务器主机的名称

参数ServerName用来设置服务器的主机名称,如果没有域名则填入服务器的IP地址,比如服务器的IP地址为192.168.99.9:


5) 设置主目录的路径

用户可以使用参数DocumentRoot配置服务器主目录默认路径,比如,主目录路径为:/usr/local/apache2/htdocs


6) 设置默认文件

Apache的默认文件名为index.html,可以使用Directory Index参数来配置,比如,将index.php设置为默认文件名:index.php index.html


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)建立虚拟目录并设置用户认证:

Alias /myweb/ “/usr/local/myweb/”

<Directory  “/usr/local/myweb/”>

Options  none

AllowOverride  None

Order  allow,deny

Allow  from  all

AuthType Basic

AuthName “Please Login: ”

AuthUserFile/usr/local/apache/bin/mywebpwd

Require User Tongguo wayne

</Directory>


2) 建立口令文件并为用户设置口令

/usr/local/apache/bin/htpasswd –c /usr/local/apache/bin/mywebpwd Tonyguo

-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不同的虚拟主机配置

比如服务器有两个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>



<VirtualHost *:8088>

serverAdmin new@student.com

DocumentRoot "/web/web1"

<Directory /web/web1>

Options FollowSymlinks

AllowOverride None

Order Allow,Deny

Allow from all

</Directory>

DirectoryIndex index.html index.php index.htm

</VirtualHost>


<VirtualHost *:8089>

serverAdmin new@student.com

DocumentRoot "/web/web2"

<Directory /web/web2>

Options FollowSymlinks

AllowOverride None

Order Allow,Deny

Allow from all

</Directory>

DirectoryIndex index.html index.php index.htm

</VirtualHost>



<VirtualHost 192.168.88.144:80>

serverAdmin new@student.com

DocumentRoot "/web/web3"

<Directory /web/web3>

Options FollowSymlinks

AllowOverride None

Order Allow,Deny

Allow from all

</Directory>

DirectoryIndex index.html index.php index.htm

</VirtualHost>


<VirtualHost 192.168.88.145:80>

serverAdmin new@student.com

DocumentRoot "/web/web4"

<Directory /web/web4>

Options FollowSymlinks

AllowOverride None

Order Allow,Deny

Allow from all

</Directory>

DirectoryIndex index.html index.php index.htm

</VirtualHost>





<VirtualHost ftp.com>

ServerName ftp.com:80

DocumentRoot /web/ftp

<Directory /web/ftp>

Options FollowSymlinks

AllowOverride None

Order Allow,Deny

Allow from all

</Directory>

DirectoryIndex index.html index.php index.htm

</VirtualHost>


<VirtualHost mail.com>

ServerName mail.com:80

DocumentRoot /web/mail

<Directory /web/mail>

Options FollowSymlinks

AllowOverride None

Order Allow,Deny

Allow from all

</Directory>

DirectoryIndex index.html index.php index.htm

</VirtualHost>




负载均衡


#访问test目录时负载均衡


在modules目录下:导入mod

/usr/local/apache2/bin/apxs -c -i mod_proxy.c proxy_util.c

/usr/local/apache2/bin/apxs -c -i mod_proxy_balancer.c

/usr/local/apache2/bin/apxs -c -i  mod_proxy_http.c



vi http.conf


LoadModule proxy_module            modules/mod_proxy.so

LoadModule proxy_balancer_module   modules/mod_proxy_balancer.so

LoadModule proxy_http_module       modules/mod_proxy_http.so


ProxyRequests Off

<Proxy balancer://clusterphpinfo>

BalancerMember http://192.168.88.134:8089  loadfactor=5

BalancerMember http://192.168.88.134:8088  loadfactor=1

#weight

ProxySet lbmethod=bytraffic

</Proxy>

ProxyPass /test balancer://clusterphpinfo stickysession=STICK_PORT_TOKEN nofailover=On

ProxyPa***everse /test balancer://clusterphpinfo


<Location /balancer-manager>

SetHandler balancer-manager

Order Deny,Allow

Allow from all

#Allow from 192.168.88.*

</Location>