有时候为了提高网站后台的安全性,除了后台本身的密码验证外,还可以在apache上设置网站后台目录的密码认证。而有的时候我们需要在一个服务器上运营多个网站,例如,避免服务器资源的浪费,那就可以通过设置apache的虚拟主机来满足项目需求,虚拟主机主要有三种(域名虚拟主机,ip虚拟主机,端口虚拟主机);

  首先安装apache

tar zxf http-2.2.17.tar.gz

cd http-2.2.17

./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite

--enable-charset-lite --enable-cgi

make && make install

/usr/local/httpd/bin/apachectl start

生成加密的http账号密码文件:

/usr/bin/htpasswd -c /usr/local/httpd/conf/.awspwd webadmin

配置apache主配置文件:

<Directory "/usr/local/httpd/htdocs">

    Options Indexes FollowSymLinks

    AuthNAME "AWStats Directory"

        AuthType Basic

        AuthUserfile /usr/local/httpd/conf/.awspwd

        require  valid-user

    AllowOverride None

    Order allow,deny

    Allow from all

</Directory>

重新启动一下服务,通过浏览器验证是否有效

/usr/local/httpd/bin/apachectl restart

基于域名的虚拟主机

ifconfig eth0 20.20.20.1/16

ifconfig eth0:0 20.20.20.3/16

ifconfig eth0:1 20.20.20.4/16

mkdir /usr/local/httpd/htdocs/web1

mkdir /usr/local/httpd/htdocs/web2

echo '<h1>this is web1</h1>' >> /usr/local/httpd/htdocs/web1/index.html

echo '<h1>this is web2</h1>' >> /usr/local/httpd/htdocs/web2/index.html

基于域名的虚拟主机需要域名解析,安装系统光盘的bind服务帮助解析:

mkdir /media/cdrom

mount /dev/cdrom  /media/cdrom

rpm -Uvh /media/cdrom/Server/bind-9.3.3-7.el5.i386.rpm 

rpm -Uvh /media/cdrom/Server/caching-nameserver-9.3.3-7.el5.i386.rpm 

rpm -Uvh /media/cdrom/Server/bind-chroot-9.3.3-7.el5.i386.rpm 

cd /var/named/chroot/etc/

cp -p named.caching-nameserver.conf named.conf

vim named.conf

options {

        directory       "/var/named";

};

zone "jiajia.com" IN {

        type    master;

        file    "dragon.com.zone";

}; 

cd /var/named/chroot/var/named

cp -p localhost.zone  dragon.com.zone

vim jiajia.com.zone

$TTL    86400

@               IN SOA  dargon.com.       admin.dragon.com. (

                                        42              ; serial (d. adams)

                                        3H              ; refresh

                                        15M             ; retry

                                        1W              ; expiry

                                        1D )            ; minimum

                IN NS           web1.dragon.com

web1            IN      A       20.20.20.1

web2            IN      A       20.20.20.3

service named restart

echo 'nameserver 20.20.20.1' >/etc/resolv.conf

nslookup web1.dragon.com

nslookup web2.dragon.com

把配置文件写入到httpd.conf里

echo "

namevirtualhost 20.20.20.1

<virtualhost 20.20.20.1>

documentroot /usr/local/httpd/htdocs/web1

servername web1.dragon.com

</virtualhost>

<virtualhost 20.20.20.3>

documentroot /usr/local/httpd/htdocs/web2

servername web2.dragon.com

</virtualhost>

" >>/usr/local/httpd/conf/httpd.conf

/usr/local/httpd/bin/apachectl restart

基于ip地址的虚拟主机:

echo "

<virtualhost 20.20.20.1>

documentroot /usr/local/httpd/htdocs/web1

servername web1.dragon.com

</virtualhost>

<virtualhost 20.20.20.3>

documentroot /usr/local/httpd/htdocs/web2

servername web2.dragon.com

</virtualhost>

" >>/usr/local/httpd/conf/httpd.conf

/usr/local/httpd/bin/apachectl restart

基于端口的虚拟主机

echo "

listen 20.20.20.1:81

listen 20.20.20.1:82

<virtualhost 20.20.20.1:81>

documentroot /usr/local/httpd/htdocs/web1

servername web1.dragon.com

</virtualhost>

<virtualhost 20.20.20.1:82>

documentroot /usr/local/httpd/htdocs/web2

servername web2.dragon.com

</virtualhost>

" >>/usr/local/httpd/conf/httpd.conf

/usr/local/httpd/bin/apachectl restart