CentOS 7 Apache 綁定域名和網站
適用場景
一臺服務器,運行有多個網站,每個網站都希望用戶直接通過二級域名來訪問,而不是同一個域名通過子目錄來訪問
配置過程
確定自己的 Apache 服務器的管理文件
使用命令
$ httpd -V
來查看關於 httpd
的相關配置信息:
$ httpd -V
Server version: Apache/2.4.6 (CentOS)
Server built: Oct 19 2017 20:39:16
Server's Module Magic Number: 20120211:24
Server loaded: APR 1.4.8, APR-UTIL 1.5.2
Compiled using: APR 1.4.8, APR-UTIL 1.5.2
Architecture: 64-bit
Server MPM: prefork
threaded: no
forked: yes (variable process count)
Server compiled with....
-D APR_HAS_SENDFILE
-D APR_HAS_MMAP
-D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
-D APR_USE_SYSVSEM_SERIALIZE
-D APR_USE_PTHREAD_SERIALIZE
-D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
-D APR_HAS_OTHER_CHILD
-D AP_HAVE_RELIABLE_PIPED_LOGS
-D DYNAMIC_MODULE_LIMIT=256
-D HTTPD_ROOT="/etc/httpd"
-D SUEXEC_BIN="/usr/sbin/suexec"
-D DEFAULT_PIDLOG="/run/httpd/httpd.pid"
-D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
-D DEFAULT_ERRORLOG="logs/error_log"
-D AP_TYPES_CONFIG_FILE="conf/mime.types"
-D SERVER_CONFIG_FILE="conf/httpd.conf"
關注到最後一行
-D SERVER_CONFIG_FILE="conf/httpd.conf"
所以,正在運行的服務器正在使用的配置文件爲
/etc/httpd/conf/httpd.conf
編輯 Apache 配置文件
需要使用超級用戶權限對配置文件進行更改,但是我們應先備份配置文件:
$ sudo cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.bak
然後使用命令
$ sudo vim /etc/httpd/conf/httpd.conf
打開配置文件後,添加如下結點
<VirtualHost *:80>
ServerName subdomain.example.cn
DocumentRoot /home/website1
</VirtualHost>
其中,ServerName
字段爲你要綁定的域名名稱;DocumentRoot
爲訪問該綁定的域名時將訪問的目錄,以便從那裏取出默認主頁頁面響應用戶,該目錄可以任意設置,但務必保證目錄權限足夠。保存退出,然後重啓 httpd
服務:
$ sudo systemctl restart httpd.service
若沒有任何輸出到屏幕上,則爲正常開啓了服務,現在可以在客戶端瀏覽器通過訪問二級域名
subdomain.example.cn
來訪問位於 /home/website1
目錄下的這個網站。若還要添加站點,則再另行添加配置中的結點信息即可。