1、基于不同端口的虚拟主机配置
[root@lamp~]# vi /etc/httpd/conf/httpd.conf
Listen 80 #设置监听不同的虚拟主机需要使用的端口
Listen 8080
Listen 8088
<Virtualhost*:80> #三个不同端口的主机
ServerName
DocumentRoot /var/www/html/80
</Virtualhost>
<Virtualhost*:8080>
ServerName
DocumentRoot /var/www/html/8080
</Virtualhost>
<Virtualhost*:8088>
ServerName
DocumentRoot /var/www/html/8088
</Virtualhost>
[root@lamp~]# mkdir -pv /var/www/html/{80,8080,8088} #建立三个网页目录
mkdir:已创建目录 "/var/www/html/80"
mkdir:已创建目录 "/var/www/html/8080"
mkdir:已创建目录 "/var/www/html/8088"
[root@lamp~]# echo "80" > /var/www/html/80/index.html #添加网页内容
[root@lamp~]# echo "8080" > /var/www/html/8080/index.html
[root@lamp~]# echo "8088" > /var/www/html/8088/index.html
[root@lamp~]# httpd -t #运行配置文件的语法检查
SyntaxOK
[root@lamp~]# service httpd restart
查看三个监听端口
[root@lamp~]# netstat -tlnp | grep httpd
tcp 0 0 :::8080 :::* LISTEN 1285/httpd
tcp 0 0 :::80 :::* LISTEN 1285/httpd
tcp 0 0 :::8088 :::* LISTEN 1285/httpd
使用自己的IP地址,然后打开网页分别测试。
192.168.3.32
192.168.3.32:8080
192.168.3.32:8088
2、基于不同的ip地址虚拟主机配置
首先给主机附加一个ip地址,再添加两个子IP地址。
[root@lamp~]# ifconfig eth0:0 192.168.3.33 netmask 255.255.255.0 up
[root@lamp~]# ifconfig eth0:1 192.168.3.34 netmask 255.255.255.0 up
[root@lamp~]# vi /etc/httpd/conf/httpd.conf
<Virtualhost192.168.3.32:80>
ServerName
DocumentRoot /var/www/html/80
</Virtualhost>
<Virtualhost192.168.3.33:80>
ServerName
DocumentRoot /var/www/html/8080
</Virtualhost>
<Virtualhost192.168.3.34:80>
ServerName
DocumentRoot /var/www/html/8088
</Virtualhost>
[root@lamp~]# httpd -t
SyntaxOK
[root@lamp~]# service httpd restart
3、基于域名的虚拟主机配置
NameVirtualHost192.168.3.32:80#apache2.2.xx版本需要开启此选项,而且要和下面的保持一致;2.4.x版本就不需要此项设置了
<Virtualhost192.168.3.32:80>
ServerName
DocumentRoot /var/www/html/80
</Virtualhost>
<Virtualhost192.168.3.32:80>
ServerName
DocumentRoot /var/www/html/8080
</Virtualhost>
<Virtualhost192.168.3.32:80>
ServerName
DocumentRoot /var/www/html/8088
</Virtualhost>
[root@lamp~]# httpd -t
[root@lamp~]# service httpd restart
我们在本地hosts添加三条记录
[root@MarsLuo桌面]#vi /etc/hosts
192.168.3.32
192.168.3.32
192.168.3.32
使用浏览器测试,这三个虚拟主机如果输入ip地址来访问的话,访问的都是第一个,使用域名可以访问三个内容
4、基于身份的目录访问。
[root@lampconf.d]# vi /etc/httpd/conf/httpd.conf
<Directory"/var/www/html/80">
Options Indexes FollowSymLinks
Order allow,deny
Allow from all
AuthName"user Access"
AuthType Basic
AuthUserFile/var/www/html/htpasswd
Requirevalid-user
</Directory>
[root@lampconf.d]# htpasswd -c /var/www/html/htpasswd admin
Newpassword:
Re-typenew password:
Addingpassword for user admin
[root@lampconf.d]# service httpd restart
浏览器输入
5、基于LDAP验证的目录访问
[root@lampconf.d]# vi /etc/httpd/conf/httpd.conf
<Directory"/var/www/html/8080">
DirectoryIndex index.php index.html
Order allow,deny
Allow from all
AuthName"LDAP Access"
AuthType Basic
AuthBasicProvider ldap
AuthzLDAPAuthoritative on
AuthLDAPURL"ldap://192.168.2.30:389/ou=People,dc=lbs,dc=com?uid" #设置LDAP地址及OU信息