Ubuntu/CentOS下Apache多站点配置

前言:

情景一:平时在我们开发的时候,一般项目都只存放在 localhost 指定的根目录下,当有好几个项目的时候,只能在根目录下以不同的文件夹区分,特别的不方便。

情景二:平时在看教学视频的时候,总是看到那些老师在单机下使用不同的域名,访问的却是本地的项目代码,每当这时候我都会问,这个怎么实现的。

情景三:在最近去实习面试的时候,面试官问我如何在 apache 服务器上搭建多站点。我只能回答我不会。

为了解决上面的几个问题,我决定把 apache 多站点配置这个知识点搞清楚。关键是搞懂 apache 虚拟主机,Apache的虚拟主机是一种允许在同一台机器上,运行超过一个网站的解决方案。后面围绕的就是 apache 虚拟主机展开的。

在本篇博客中我将介绍两种方法来实现 apache 多站点的配置:

一:根据访问的域名区分站点

Ubuntu 环境下:

我的环境是:

操作系统:Ubnutu 16.04 LTS
apache 服务:Apache/2.4.18 (Ubuntu)(使用 apache2 -v 命令获取 )

在 Ubnutu 上,apache 服务叫 apache2,而不是 httpd(在 Centos 上叫 httpd),主配置文件为 /etc/apache2/apache2.conf,我们打开 /etc/apache2/apache2.conf,发现最后两行为:

# Include the virtual host configurations:
IncludeOptional sites-enabled/*.conf

显然 /etc/apache2/sites-enabled 下存放着有关虚拟站点(VirtualHost)的配置。经查看,初始情况下,该目录下包含一个符号连接(软链接,相当于快捷方式):

000-default.conf -> ../sites-available/000-default.conf

这里又引出另外一个配置目录:/etc/apache2/sites-available。这个目录下放置了所有可用站点的真正配置文件,对于 Enabled 的站点,Apache2 在 sites-enabled 目录建立一个到 sites-available 目录下文件的符号链接。

/etc/apache2/sites-available 下有两个文件:

000-default.conf    default-ssl.conf

/etc/apache2/sites-enabled/000-default.conf 链接的文件是 /etc/apache2/sites-available/000-default.conf,我们就以 /etc/apache2/sites-available/000-default.conf 文件为例,看看一个 VirtualHost 的配置是啥样的(为了简洁,所有的注释我都去掉了):

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

DocumentRoot 是这个站点的根目录,这样 Apache2 启动时会扫描 /etc/apache2/sites-enabled 中可用的 website 配置并加载。当用户访问localhost:80 时,Apache2 就将 default 站点根目录 /var/www/html 下的index.html(或 index.PHP 等,可配置)作为请求的回应返回给浏览器,你就会欣赏到的就是 /var/www/html/index.html 这个文件中的内容了。

Apache2的默认站点我们不要去动它。我们新增站点配置来满足我们的要求。

第一步:新增站点配置文件

#进入虚拟主机配置文件夹
cd /etc/apache2/sites-available/
#复制默认的虚拟主机配置文件
sudo cp 000-default.conf www-linuxidc-com.conf
sudo cp 000-default.conf www-wordpress-com.conf
# Created By zhongjin on 2016-12-12 冬至
<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName www.linuxidc.com
    DocumentRoot /home/www/linuxidc

    <Directory "/home/www/linuxidc">
        Options FollowSymLinks
        AllowOverride All 
        #Require all denied
        Require all granted
    </Directory>                            

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

针对 www-wordpress-com.conf 改变相应的配置,即修改 ServerName 为 www.wordpress.com,DocumentRoot 和 Directory 修改为 /home/www/wordpress。

第二步:在sites-enabled目录下建立符号链接

cd /etc/apache2/sites-enabled
#建立对应的软链接
sudo ln -s /etc/apache2/sites-available/www-linuxidc-com.conf /etc/apache2/sites-enabled/www-linuxidc-com.conf
sudo ln -s /etc/apache2/sites-available/www-wordpress-com.conf /etc/apache2/sites-enabled/www-wordpress-com.conf

第三步:在对应目录放入项目代码

sudo mkdir -p /home/www/linuxidc
<?php

echo "hello,welcome to linuxidc!";

赋予相应的权限:

sudo chmod -R 777 /home/www/linuxidc

针对 wordpress 做同样的操作。

第四步:修改 /etc/hosts 文件

打开查看 /etc/hosts 文件,开头是:

127.0.0.1   localhost

我们在该行后面添加:

# modified by zhongjin on 2016-12-21 冬至
127.0.0.1   www.linuxidc.com
127.0.0.1   www.wordpress.com

#保存退出

第五步:重启 apache 服务器并测试

重启 apache 服务器使得配置生效:

systemctl restart apache2.service

如果你不是桌面版(服务器),那么可以使用命令行测试:

curl www.linuxidc.com

看看返回的字符串是不是正确输出!

Centos 环境下:

我的环境是:

操作系统:Centos 7
apache 服务: Apache/2.4.6 (CentOS)(通过 httpd -v 获取)

在这里我们实现 Ubuntu 环境下同样的效果。

Centos 下,apache 的服务叫 httpd,主配置文件为 /etc/httpd/conf/httpd.conf,我们浏览 httpd.conf 文件,搜索关键字 vhost,发现根本找不到相关的东西,不过在最后两行有以下内容:

# Load config files in the "/etc/httpd/conf.d" directory, if any.
IncludeOptional conf.d/*.conf

难道虚拟主机的配置也在 /etc/httpd/conf.d 下面?LZ 去看了一下该目录下面所有文件的内容,发现根本没有关于虚拟主机的配置,如果你再仔细观察 http.conf 配置文件,你就会发现,其实在 Centos 下,其默认主机只有 localhost ==> /var/www/html ,如果需要的话,我们得自己扩展了。

我们有两种方式实现自己的扩展,一种是直接在 /etc/httpd/conf/httpd.conf 后面追加配置内容,一种是在外部文件先配置好,再类似 IncludeOptional conf.d/*.conf 那样引入我们的配置。一般不推荐直接修改主配置文件,所以我们使用第二种方式。

我们还是模仿上面的配置:

第一步:新增站点配置文件

cd /etc/httpd
sudo mkdir vhost-conf.d
<VirtualHost *:80>
    #Created by zhongjin on 2016-12-21 冬至
    Serveradmin [email protected]
    ServerName www.linuxidc.com
    DocumentRoot /home/www/linuxidc

    <Directory "/home/www/linuxidc">
          Options FollowSymLinks
          AllowOverride All
          #Require all denied
          Require all granted
    </Directory>
</VirtualHost>

针对 www-wordpress-com.conf 改变相应的配置,即修改 ServerName 为 www.wordpress.com,DocumentRoot 和 Directory 修改为 /home/www/wordpress。

第二步:在主配置中引入我们的虚拟主机配置

sudo vim /etc/httpd/conf/httpd.conf
# 在主配置文件末尾添加以下内容
# Load vhost-config files in the "/etc/httpd/vhost-conf.d" directory if any
# created by zhongjin on 2016-12-21 冬至
Include vhost-conf.d/*.conf

第三步:在对应目录放入项目代码

直接参考 Ubnutu 环境下的第三步配置。

第四步:修改 /etc/hosts 文件

直接参考 Ubnutu 环境下的第四步配置。

第五步:重启 apache 服务器并测试

重启 apache 服务器使得配置生效:

sudo systemctl restart httpd.service

如果你不是桌面版(服务器),那么可以使用命令行测试:

curl www.linuxidc.com

看看返回的字符串是不是正确输出!

注意:

我的解决方案是:按照上面的步骤重新建一个 localhost 域名,使它指向 /var/www/html 目录即可。

二、同域名下,通过访问不同的端口获得不同的站点

其实在这里实现的步骤跟上面的没多大差别,我就说说需要做的额外操作。

Ubuntu 环境下:

  • 让我们的 apache2 监听 8080 端口:

修改 /etc/apache2/ports.conf 文件,在 Listen 80 后面添加两行:

NameVirtualHost *:8080
Listen 8080
  • 在 /etc/apache2/sites-available/ 下增加 www-linuxidc-com-8080.conf,并在 /etc/apache2/sites-enabled/ 下建立符号链接。方法参考前面。
<VirtualHost *:8080>
    #Created by zhongjin on 2016-12-21 冬至
    Serveradmin [email protected]
    ServerName www.linuxidc.com
    DocumentRoot /home/www/linuxidc_admin

    <Directory "/home/www/linuxidc_admin">
          Options FollowSymLinks
          AllowOverride All
          #Require all denied
          Require all granted
    </Directory>
</VirtualHost>
  • 重启 apache 服务器,测试

CentOS 环境下:

  • 让 httpd 监听 8080 端口:

直接修改 /etc/httpd/conf/httpd.conf 配置文件,在 Listen 80 后面添加两行:

# created by zhongjin on 2016-12-21 冬至
NameVirtualHost *:8080
Listen 8080
  • 在 /etc/httpd/vhost-conf.d 下面添加 www-linuxidc-com-8080.conf
<VirtualHost *:8080>
    #Created by zhongjin on 2016-12-21 冬至
    Serveradmin [email protected]
    ServerName www.linuxidc.com
    DocumentRoot /home/www/linuxidc_admin

    <Directory "/home/www/linuxidc_admin">
        Options FollowSymLinks
        AllowOverride All 
        #Require all denied
        Require all granted
    </Directory>
</VirtualHost>

注意修改的地方!

  • 重启 apache 服务器,测试

后话:

多站点的配置还有另外一种配置,就是通过不同的IP进行多站点的配置,由于我没有进行实验(前面的内容都是LZ 亲身体会过),所以这里就不给出方法了。

更多Apache相关教程见以下内容

猜你喜欢

转载自www.linuxidc.com/Linux/2017-05/143590.htm