CentOS 7 Apache 绑定域名和网站

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jinhangdev/article/details/81254748

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 目录下的这个网站。若还要添加站点,则再另行添加配置中的结点信息即可。

猜你喜欢

转载自blog.csdn.net/jinhangdev/article/details/81254748