CentOS7下如何配置apache2.4.6的虚拟主机

第一步:确认apache版本号

    httpd -v

            Server version: Apache/2.4.6 (CentOS)
            Server built:   Jun 27 2018 13:48:59

第二步:寻找apache配置文件

updatedb;
            locate httpd.conf

            /etc/httpd/conf/httpd.conf

第三步:配置apache配置

    vim /etc/httpd/conf/httpd.conf
                #包含apache2.4.6扩展            
                IncludeOptional conf.d/*.conf
                #网站根目录的绝对地址
                DocumentRoot "/var/www/html"

第四步:在测试的客户端(windows系统中)配置本地hosts

            C:\Windows\System32\drivers\etc\hosts
            #12.34.56.78更换为linux的地址(可通过在ifconfig获取)

                12.34.56.78 www.test1.com

                12.34.56.78 www.test2.com

第五步:新建项目

            #新建项目1
                mkdir /var/www/html/test1       
                touch /var/www/html/test1/index.php
            #新建项目2
                mkdir /var/www/html/test2       
                touch /var/www/html/test2/index.php

第六步:新建扩展文件(vhost.conf)

        vim /etc/httpd/conf.d/vhost.conf

        #DocumentRoot和Directory更换自己的项目绝对路径
        #ServerName和ServerAlias更换自己的项目地址

            <VirtualHost *:80>
                DocumentRoot "/var/www/html/test1"
                ServerName test1.com
                ServerAlias www.test1.com
                <Directory "/var/www/html/test1">
                     Options FollowSymLinks
                     AllowOverride All
                     Require all granted
                </Directory>
            </VirtualHost>

            <VirtualHost *:80>
                DocumentRoot "/var/www/html/test2"
                ServerName test2.com
                ServerAlias www.test2.com
                <Directory "/var/www/html/test2">
                     Options FollowSymLinks
                     AllowOverride All
                     Require all granted
                </Directory>
            </VirtualHost>

第七步:确认apache配置是否正确并重启apache服务

        #确认apache配置是否正确
             httpd -t

                AH00558: httpd: Could not reliably determine the server's fully qualified 
                domain name, using localhost.localdomain. Set the 'ServerName' directive 
                globally to suppress this message
                Syntax OK

            #重启apache服务
                 service httpd restart  

第八步:客户端访问www.test1.co和www.test2.com

猜你喜欢

转载自blog.csdn.net/qq_42384830/article/details/81452250