.在httpd配置完成的情况下进行一下操作
#提前做一个软连接
[root@localhost ~]# echo "export PATH=/usr/local/apachectl/bin:$PATH" > /etc/profile.d/httpd.sh
[root@localhost ~]# . /etc/profile.d/httpd.sh
[root@localhost ~]# apachectl start
#设置是否能访问
[root@localhost ~]# vim /etc/httpd24/httpd.conf
<Directory />
AllowOverride none
Require all denied
</Directory>
<Directory "/usr/local/apache/htdocs/whb">
<RequireAll>
Require all denied #禁止任何服务器访问
Require ip 192.168.86.137 #只允许访问192.168.86.137
</RequireAll>
</Directory>
相同IP相同端口不同域名
[root@localhost ~]# find / -name *vhosts.conf
/etc/httpd24/extra/httpd-vhosts.conf
/etc/httpd24/original/extra/httpd-vhosts.conf
/root/httpd-2.4.38/docs/conf/extra/httpd-vhosts.conf
[root@localhost ~]# vim /etc/httpd24/extra/httpd-vhosts.conf
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "/usr/local/apache/docs/dummy-host.example.com"
ServerName dummy-host.example.com
ServerAlias www.dummy-host.example.com
ErrorLog "logs/dummy-host.example.com-error_log"
CustomLog "logs/dummy-host.example.com-access_log" common
</VirtualHost> #复制这些代码为参照模板
[root@localhost ~]# vim /etc/httpd24/httpd.conf
<VirtualHost *:80> #编译后的样式
DocumentRoot "/usr/local/apache/htdocs/123"
ServerName whb.example.com
ErrorLog "logs/error_log"
CustomLog "logs/access_log" common
</VirtualHost>
#*是代表所有ip,80代表所有端口
<VirtualHost *:80>
DocumentRoot "/usr/local/apache/htdocs/456"
ServerName aaa.example.com
ErrorLog "logs/error_log"
CustomLog "logs/access_log" common
</VirtualHost>
[root@localhost ~]# cd /usr/local
[root@localhost apache]# cd htdocs/
[root@localhost htdocs]# ls
index.html
[root@localhost htdocs]# mkdir 123 456
[root@localhost htdocs]# ls
123 456 index.html
#最好把他们的属主和属组都改为apache
[root@localhost htdocs]# chown -R apache.apache 123
[root@localhost htdocs]# chown -R apache.apache 456
[root@localhost htdocs]# ll
总用量 4
drwxr-sr-x 2 apache apache 6 3月 30 14:40 123
drwxr-sr-x 2 apache apache 6 3月 30 14:40 456
-rw-r--r-- 1 root root 45 6月 12 2007 index.html
[root@localhost htdocs]# ls
123 456 index.html
[root@localhost htdocs]# cd 123/
[root@localhost 123]# ls
[root@localhost 123]# echo "I miss you" > index.html
[root@localhost 123]# cd ..
[root@localhost htdocs]# cd 456/
[root@localhost 456]# echo "I LOVE YOU" > index.html
[root@localhost 456]# apachectl -t
Syntax OK
[root@localhost 456]# apachectl restart
不同ip相同端口:
[root@localhost 456]# vim /etc/httpd24//httpd.conf
<VirtualHost 192.168.86.137:80>
DocumentRoot "/usr/local/apache/htdocs/123"
ServerName whb.example.com
ErrorLog "logs/error_log"
CustomLog "logs/access_log" common
</VirtualHost>
<VirtualHost 192.168.86.131:80>
DocumentRoot "/usr/local/apache/htdocs/456"
ServerName aaa.example.com
ErrorLog "logs/error_log"
CustomLog "logs/access_log" common
</VirtualHost>
#添加ip
[root@localhost 456]# ip addr add 192.168.86.131/24 dev ens33
[root@localhost htdocs]# ip a
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:f8:ef:15 brd ff:ff:ff:ff:ff:ff
inet 192.168.86.137/24 brd 192.168.86.255 scope global dynamic ens33
valid_lft 1065sec preferred_lft 1065sec
inet 192.168.86.131/24 scope global secondary ens33
valid_lft forever preferred_lft forever
[root@localhost 456]# apachectl -t
Syntax OK
[root@localhost 456]# apachectl restart
相同IP不同端口:
[root@localhost htdocs]# vim /etc/httpd24//httpd.conf
<VirtualHost 192.168.86.137:80>
DocumentRoot "/usr/local/apache/htdocs/123"
ServerName whb.example.com
ErrorLog "logs/error_log"
CustomLog "logs/access_log" common
</VirtualHost>
<VirtualHost 192.168.86.137:81>
DocumentRoot "/usr/local/apache/htdocs/456"
ServerName aaa.example.com
ErrorLog "logs/error_log"
CustomLog "logs/access_log" common
</VirtualHost>
:/Listen
Listen 80
Listen 81
[root@localhost htdocs]# apachectl -t
Syntax OK
[root@localhost htdocs]# apachectl restart
来源:51CTO
作者:wx5e782fbb12ee3
链接:https://blog.51cto.com/14763231/2483861