centos http建立多站点

119 阅读1分钟

准备工作

1、安装httpd服务

sudo yum install httpd

2、启动httpd服务

sudo systemctl start httpd

查询http服务的状态:

sudo systemctl status httpd

3、更改http服务器的默认目录

在配置文件 /etc/httpd/conf/httpd.conf 中一共有三个地方需要修改。

(1)修改参数 “DocumentRoot”:

# DocumentRoot: The directory out of which you will serve your
DocumentRoot "/var/www/html"
    # access content that does not live under the DocumentRoot.

改成自己需要的目录路径

(2)修改目录参数

#
# Relax access to content within /var/www.
#
#<Directory "/var/www">
<Directory "自定义的目录"> (路径层数匹配) 例如/var/xxx/
    AllowOverride None
    # Allow open access:
    Require all granted
</Directory>
...

(3)再次修改目录参数

# Further relax access to the default document root:
#<Directory "/var/www/html">
<Directory "自定义目录"> (路径层数匹配) 例如/var/xxx/xxx
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #

(4)重启服务生效

sudo systemctl restart httpd

创建多站点步骤如下:

1、创建站点目录文件

在 /var/www/ 下 创建文件夹 (例如test)

2、添加站点

在 /etc/httpd/conf.d 中的 vhost.conf 文件中加入以下东西 (vhost.conf 文件没有请自行创建)

创建命令:vim vhost.conf

<VirtualHost _default_:80>
DocumentRoot "/var/www/html"
  <Directory "/var/www/html">
    Options +Indexes +FollowSymLinks +ExecCGI
    AllowOverride All
    Order allow,deny
    Allow from all
    Require all granted
  </Directory>
</VirtualHost>
 
<VirtualHost *:80>
    DocumentRoot "/var/www/html/test/"
    ServerName www.fanzexin.com
    ServerAlias fanzexin.com
  <Directory "/var/www/html/test/">
      Options FollowSymLinks ExecCGI
      AllowOverride All
      Order allow,deny
      Allow from all
      Require all granted
  </Directory>
</VirtualHost>

如果更换tcp端口 例如8080 头部添加Listen 8080

3、配置本地DNS服务器

在 /etc/hosts 中加入解析

例如:你的域名为 www.fanzexin.cn

加入 127.0.0.1 www.fanzexin.cn

4、附录

开启http服务

sudo systemctl start httpd

重启http服务

sudo systemctl restart httpd

关闭http服务

sudo systemctl stop httpd