文章目錄
查看apache的配置
查找apache的配置文件
[root@localhost ~]# rpm -qc httpd
/etc/httpd/conf.d/autoindex.conf
/etc/httpd/conf.d/userdir.conf
/etc/httpd/conf.d/welcome.conf
/etc/httpd/conf.modules.d/00-base.conf
/etc/httpd/conf.modules.d/00-dav.conf
/etc/httpd/conf.modules.d/00-lua.conf
/etc/httpd/conf.modules.d/00-mpm.conf
/etc/httpd/conf.modules.d/00-proxy.conf
/etc/httpd/conf.modules.d/00-systemd.conf
/etc/httpd/conf.modules.d/01-cgi.conf
/etc/httpd/conf/httpd.conf //該文件爲httpd的配置文件
/etc/httpd/conf/magic
/etc/logrotate.d/httpd
/etc/sysconfig/htcacheclean
/etc/sysconfig/httpd
查看DocumentRoot
[root@localhost ~]# cat /etc/httpd/conf/httpd.conf | grep "^DocumentRoot"
DocumentRoot "/var/www/html"
DocumentRoot是站點文件的根目錄,html、css、js文件都會以站點作爲文件夾放在該目錄下。
查看配置文件的根目錄
[root@localhost httpd]# cat /etc/httpd/conf/httpd.conf | grep "^ServerRoot"
ServerRoot "/etc/httpd"
配置文件都會存放在"/etc/httpd"目錄下,主配置文件中的相對路徑也是以這個目錄爲參考點。
查看站點配置文件存放位置
[root@localhost ~]# cat /etc/httpd/conf/httpd.conf | grep "^Include"
Include conf.modules.d/*.conf
IncludeOptional conf.d/*.conf
可見主配置文件中將conf.d目錄下的所有.conf文件都include了,故我們可以在conf.d目錄下創建站點的.conf配置文件,也可以另外添加站點配置,形式爲“Include conf_file_path”。
三種方式配置虛擬主機
httpd的虛擬主機也就是虛擬站點,我們通過“基於域名的虛擬主機”、“基於IP地址的虛擬主機”、“基於端口的虛擬主機”三種方式進行配置。
三種方式在配置上的區別也在配置文件上不同,下面配置的例子都將把配置寫在/etc/httpd/conf.d/vhost.conf文件中。
基於域名的虛擬主機
# 接收來自本機任意IP地址的80端口訪問
<VirtualHost *:80>
# 站點目錄爲/var/www/html/mydomaincom,需手動創建並添加文件
DocumentRoot /var/www/html/mydomaincom
ServerName www.mydomain.com
# 日誌文件,是個相對路徑,可通過查看主配置文件得到其根目錄
ErrorLog "logs/www.mydomain.com.error_log"
CustomLog "logs/www.mydomain.com.access_log" common
# 對站點的子目錄進行訪問限制
<Directory "/var/www/html/mydomaincom">
# 開放訪問
Require all granted
</Directory>
</VirtualHost>
創建站點html目錄
[root@localhost httpd]# mkdir /var/www/html/mydomaincom
創建站點html文件
[root@localhost httpd]# echo "this is mydomain.html" >> /var/www/html/mydomaincom/index.html
測試
# 重啓httpd服務
[root@localhost httpd]# systemctl restart httpd
# 因爲這是我們自定義的域名,故需添加域名解析
[root@localhost httpd]# echo "192.168.218.6 www.mydomain.com" >> /etc/hosts
[root@localhost httpd]# curl www.mydomain.com
this is mydomain.html
# 訪問成功
基於端口的虛擬主機
配置了兩個站點
<VirtualHost *:8080>
# 也可以配置ServerName,這裏沒有配
DocumentRoot "/var/www/html/8080com"
ErrorLog "logs/8080com.error_log"
CustomLog "logs/8080com.access_log" common
<Directory "/var/www/html/">
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:9090>
DocumentRoot "/var/www/html/9090com"
ErrorLog "logs/9090com.error_log"
CustomLog "logs/9090com.access_log" common
<Directory "/var/www/html/">
Require all granted
</Directory>
</VirtualHost>
Listen 8080
Listen 9090
用同樣的方法創建站點目錄和文件
[root@localhost html]# pwd
/var/www/html
[root@localhost html]# tree
.
├── 8080com
│ └── index.html
├── 9090com
│ └── index.html
└── mydomaincom
└── index.html
3 directories, 3 files
測試
[root@localhost html]# curl 192.168.218.6:8080
this is 8080com.html
[root@localhost html]# curl 192.168.218.6:9090
this is 9090com.html
# 訪問成功
基於IP的虛擬主機
該主機的另一個IP地址爲192.168.31.128
在vhost.conf中添加站點
<VirtualHost 192.168.31.128:80>
ServerName www.mydomain2.com
DocumentRoot "/var/www/html/mydomain2com"
ErrorLog "logs/mydomain2.error_log"
CustomLog "logs/mydomain2.access_log" common
<Directory "/var/www/html/">
Require all granted
</Directory>
</VirtualHost>
Listen 192.168.31.128:80
用上面同樣的方法創建站點目錄和文件
[root@localhost html]# pwd
/var/www/html
[root@localhost html]# ls
8080com 9090com mydomain2com mydomaincom
[root@localhost html]# ls mydomain2com/
index.html
測試
[root@localhost conf.d]# systemctl restart httpd
[root@localhost conf.d]# curl 192.168.31.128
this is mydomain2com
# 訪問成功