前言:Apache虛擬主機配置有3中方法:基於IP配置、基於域名配置和基於端口配置,在一個Apache服務器上能夠配置多個虛擬主機,實現一個服務器提供多站點服務,其實就是訪問同一個服務器上的不一樣目錄。php
1、基於域名配置html
1.1 首先查看主配置文件,是否打開了虛擬主機配置選項;瀏覽器
[root@localhost test]# vi /etc/httpd/conf/httpd.conf
IncludeOptional conf.d/*.conf # 使虛擬主機配置文件生效(/usr/share/doc/httpd-2.4.6/httpd-vhosts.conf)
1.2 打開虛擬主機配置文件添加以下內容:服務器
[root@localhost test]# vi /usr/share/doc/httpd-2.4.6/httpd-vhosts.conf
<VirtualHost *:80>ide
DocumentRoot "/steven/test" #網站根目錄 ServerName www.test.com #域名 DirectoryIndex index.html index.htm index.php #這裏配置歡迎首頁面 <Directory /> Options FollowSymLinks AllowOverride None #不容許別人修改咱們的頁面 order allow,deny #設置訪問權限 Allow from all </Directory>
</VirtualHost>
1.3 在根目錄下面新建文件:測試
[root@localhost test]# cd /steven/test/
[root@localhost test]# vi index.php
<?php網站
Echo "測試";ui
1.4 設置本地域名解析code
steven:~ root# vi /etc/hosts
10.0.2.114 www.test.com
127.0.0.1 www.test.com 若是Apache安裝在本機。htm
1.5 瀏覽其中輸入www.test.com訪問測試頁面
若是出現瀏覽器狀態碼爲403,就是由於權限不足引發的,再次打開httpd.conf進行添加權限
找到下面的代碼
<Directory />
AllowOverride none Require all denied
</Directory>
將它改成:
<Directory />
AllowOverride none Require all granted
</Directory>
2、基於端口配置虛擬主機
2.1 修改主配置文件,添加多個監聽端口
[root@localhost ~]# vi /etc/httpd/conf/httpd.conf
Listen 80
Listen 8080
2.2 編輯 httpd-vhosts.conf ,添加一下信息
[root@localhost httpd-2.4.6]# vi httpd-vhosts.conf
/#虛擬機端口配置
<VirtualHost *:8080>
DocumentRoot "/steven/test2" ServerName www.test2.com ServerAlias www.test2.com
<Directory "/steven/test2">
Options FollowSymLinks ExecCGI AllowOverride All Order allow,deny Allow from all Require all granted
</Directory>
</VirtualHost>2.3 重啓Apache服務訪問www.test2.com:8080