可参考:https://www.cnblogs.com/liyuanhong/articles/7355545.html
一、开启apache代理模块,开启vhost虚拟主机功能
开启代理模块,编辑http.conf 文件:
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
LoadModule proxy_http_module modules/mod_proxy_http.so
开启虚拟主机功能,引入vhost文件
Include conf/extra/httpd-vhosts.conf
开启想要监听的端口:
Listen 80
Listen 8080
二、配置vhost文件
如下,参见我的配置:
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "/opt/lampp/htdocs/"
ServerName www.xxxx.cn
ServerAlias www.xxxx.cn
ErrorLog "logs/dummy-host.example.com-error_log"
CustomLog "logs/dummy-host.example.com-access_log" common
<Directory "/opt/lampp/htdocs/">
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:8080>
ServerAdmin 192.168.0.108
DocumentRoot "/opt/lampp/htdocs/devman/public/"
ServerName 192.168.0.108
ErrorLog "logs/dummy-host2.example.com-error_log"
CustomLog "logs/dummy-host2.example.com-access_log" common
<Directory "/opt/lampp/htdocs/devman/public/">
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:8080>
ServerAdmin www.xxxx.cn
DocumentRoot "/opt/lampp/htdocs/devman/public/"
ServerName www.xxxx.cn
ErrorLog "logs/dummy-host2.example.com-error_log"
CustomLog "logs/dummy-host2.example.com-access_log" common
<Directory "/opt/lampp/htdocs/devman/public/">
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
解释:<VirtualHost *:80>
- VirtualHost 后面的参数表示的是该VHost的IP/域名/和端口
- IP: port, 例如 <VirtualHost 192.168.0.108:8080>, 访问的时候通过IP访问
- Domain, 例如 <VirtualHost www.xxxx.cn>, 访问的时候通过域名访问, 也可以指定里面的ServerName来指定域名
- *, 表示匹配所有对Apache监听主机的请求, 只要是apache监听到的请求都可以匹配该虚拟主机
- 此处表示的就是监听所有80端口的请求, 但是由于ServerName里写了www.xxxx.cn, 所以这个vhost匹配的是www.xxxx.cn:80
以上:
- 当我们访问 www.xxxx.cn 的时候,会访问到 /opt/lampp/htdocs/ 目录下的内容
- 当我们访问 www.xxxx.cn:8080 的时候,会访问到 /opt/lampp/htdocs/devman/public/ 目录下的内容