先源码安装apache
虚拟主机有三类:
相同IP不同端口
不同IP相同端口
相同IP相同端口不同域名
相同ip不同端口:
[root@localhost ~]# ls /usr/local/apache/bin/
ab apxs dbmmanage envvars-std htcacheclean htdigest httpd logresolve
apachectl checkgid envvars fcgistarter htdbm htpasswd httxt2dbm rotatelogs
[root@localhost ~]# vim /etc/profile.d/httpd.sh
[root@localhost ~]# cat /etc/profile.d/httpd.sh
export PATH=/usr/loacl/apache/bin:$PATH
[root@localhost ~]# source /etc/profile.d/httpd.sh 读
[root@localhost ~]# apachectl start
[root@localhost ~]# cd /usr/local/apache
[root@localhost apache]# ls
bin build cgi-bin error htdocs icons include logs man manual modules
[root@localhost apache]# cd htdocs
[root@localhost htdocs]# ls
index.html
[root@localhost htdocs]# mkdir site{1,2,3}
[root@localhost htdocs]# ls
index.html site1 site2 site3
[root@localhost htdocs]# echo 'site1' > site1/index.html
[root@localhost htdocs]# echo 'site2' > site2/index.html
[root@localhost htdocs]# echo 'site3' > site3/index.html
[root@localhost htdocs]# tree
.
├── index.html
├── site1
│ └── index.html
├── site2
│ └── index.html
└── site3
└── index.html
3 directories, 4 files
效果如下图
[root@localhost htdocs]# cd /etc/httpd24
[root@localhost httpd24]# pwd
/etc/httpd24
[root@localhost httpd24]# ls
extra httpd.conf magic mime.types original
[root@localhost httpd24]# vim httpd.conf
#Include /etc/httpd24/extra/httpd-vhosts.conf //把#删掉
[root@localhost httpd24]# vim extra/httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "/usr/local/apache/htdocs/site1"
ServerName site1.example.com
ErrorLog "logs/site1.example.com-error_log"
CustomLog "logs/site1.example.com-access_log" common
</VirtualHost>
[root@localhost httpd24]# vim extra/httpd-vhosts.conf
同一个ip端口号不同,出来的效果就不一样,如果不加端口号,那么默认的就是第一个设置的80端口
<VirtualHost *:80>
DocumentRoot "/usr/local/apache/htdocs/site1"
ServerName site1.example.com
ErrorLog "logs/site1.example.com-error_log"
CustomLog "logs/site1.example.com-access_log" common
</VirtualHost>
listen 81 //加上监听81端口
<VirtualHost *:81>
DocumentRoot "/usr/local/apache/htdocs/site2"
ServerName site2.example.com
ErrorLog "logs/site2.example.com-error_log"
CustomLog "logs/site2.example.com-access_log" common
</VirtualHost>
[root@localhost httpd24]# source /etc/profile.d/httpd.sh
[root@localhost httpd24]# apachectl restart //每次修改之后都要重启Apache
[root@localhost httpd24]#
不同ip相同端口
[root@localhost ~]# ip addr add 192.168.136.250/24 dev ens160 //添加一个ip地址
[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:51:a3:82 brd ff:ff:ff:ff:ff:ff
inet 192.168.136.134/24 brd 192.168.136.255 scope global noprefixroute ens160
valid_lft forever preferred_lft forever
inet 192.168.136.250/24 scope global secondary ens160
valid_lft forever preferred_lft forever
inet6 fe80::7fa:afea:b17a:44be/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@localhost ~]# cd /etc/httpd24
[root@localhost httpd24]# vim extra/httpd-vhosts.conf
<VirtualHost 192.168.136.134:80> //设置两个ip,同一个端口号
DocumentRoot "/usr/local/apache/htdocs/site1"
ServerName site1.example.com
ErrorLog "logs/site1.example.com-error_log"
CustomLog "logs/site1.example.com-access_log" common
</VirtualHost>
<VirtualHost 192.168.136.250:80>
DocumentRoot "/usr/local/apache/htdocs/site2"
ServerName site2.example.com
ErrorLog "logs/site2.example.com-error_log"
CustomLog "logs/site2.example.com-access_log" common
</VirtualHost>
[root@localhost httpd24]# source /etc/profile.d/httpd.sh
[root@localhost httpd24]# apachectl restart 重启
如果不行就看看防火墙关了没
[root@localhost httpd24]# systemctl stop firewalld
[root@localhost httpd24]# setenforce 0
[root@localhost httpd24]# apachectl restart 重启
相同IP相同端口不同域名
[root@localhost httpd24]# vim extra/httpd-vhosts.conf
[root@localhost httpd24]# apachectl restart
[root@localhost httpd24]# cat extra/httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "/usr/local/apache/htdocs/site1"
ServerName site1.example.com
ErrorLog "logs/site1.example.com-error_log"
CustomLog "logs/site1.example.com-access_log" common
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/usr/local/apache/htdocs/site2"
ServerName site2.example.com
ErrorLog "logs/site2.example.com-error_log"
CustomLog "logs/site2.example.com-access_log" common
</VirtualHost>
c: \windows\system32\drivers\etc\hosts拖到桌面上
右键以管理员身份用写字板打开,写入以下内容 site1.example.comsite2.example.com site3.example.com
把hosts拖回c: \windows\system32\drivers\etc\hosts目录