打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Ubuntu/CentOS下Apache多站点配置
userphoto

2017.11.01

关注

前言:

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

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

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

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

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

1、如何配置根据访问的域名区分配置不通的站点?
(比如,访问 www.linuxidc.com 访问的是 /home/www/linuxidc 项目,访问 www.wordpress.com 访问的是 /home/www/wordpress 项目)

2、在相同域名地址的情况下,如何通过访问不同的端口获得不同的站点?
(比如,访问 www.linuxidc.com 访问的是 /home/www/linuxidc 项目,访问 www.linuxidc.com:8080 访问的是 /home/www/linuxidc_admin 项目(即 linuxidc 的后台管理系统))

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

在这里我先介绍 Ubuntu 系统下的配置,在来讨论 CentOS 下(因为我在学习过程中也是先 ubuntu,再配置 centos 的时候都是参考的 ubuntu 的配置)

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的默认站点我们不要去动它。我们新增站点配置来满足我们的要求。

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

在 /etc/apache2/sites-available 目录中中建立两个站点的配置文件 www-linuxidc-com.conf 和 www-wordpress-com.conf:

#进入虚拟主机配置文件夹cd /etc/apache2/sites-available/#复制默认的虚拟主机配置文件sudo cp 000-default.conf www-linuxidc-com.confsudo cp 000-default.conf www-wordpress-com.conf

编辑这两个配置文件,以 www-linuxidc-com.conf 为例:

# Created By zhongjin on 2016-12-12 冬至<VirtualHost *:80>    ServerAdmin 1054840542@qq.com    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>

注意上面配置中:ServerName、DocumentRoot 和 Directory 是我们重点关注的配置点。linuxidc 的 ServerName 为www.linuxidc.com,根目录为 /home/www/linuxidc,Directory 同 DocumentRoot。

针对 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.confsudo ln -s /etc/apache2/sites-available/www-wordpress-com.conf /etc/apache2/sites-enabled/www-wordpress-com.conf

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

如上面所示,我们的 linuxidc 项目代码放在 /home/www/linuxidc,wordpress 项目代码放在 /home/www/wordpress,那么我们在对应目录下创建对应目录并赋予相应权限,以 linuxidc 为例:

sudo mkdir -p /home/www/linuxidc

然后在 /home/www/linuxidc 下放入项目代码,这里为了测试就新建 index.php,内容如下:

<?phpecho "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.com127.0.0.1   www.wordpress.com#保存退出

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

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

systemctl restart apache2.service

在浏览器(如果是桌面版的话)中访问 www.linuxidc.com 或 www.wordpress.com,看看是否输出了 index.php 文件中的内容。

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

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/httpdsudo mkdir vhost-conf.d

我们在 vhost-conf.d 目录下新增我们的虚拟主机配置文件 www-linuxidc-com.conf 和 www-wordpress-com.conf,以 www-linuxidc-com.conf 为例,我们输入以下内容:

<VirtualHost *:80>    #Created by zhongjin on 2016-12-21 冬至    Serveradmin linuxidc@linuxidc.com    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

在浏览器(如果是桌面版的话)中访问 www.linuxidc.com 或 www.wordpress.com,看看是否输出了 index.php 文件中的内容。

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

curl www.linuxidc.com

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

注意:

在 Centos 下,由于默认的虚拟主机配置(localhost)是直接在 /etc/httpd/conf/httpd.conf 文件中配置的,所以我们在后面添加我们的配置的时候,会覆盖前面的相同的配置,所以当你 curl localhost 的时候,可能访问的是 linuxidc 项目下的代码,也可能是 wordpress 下的代码。

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

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

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

这里以 linuxidc 为例,通过访问 www.linuxidc.com:80(默认就是80),返回的是“hello,welcome to linuxidc“,而访问 www.linuxidc.com:8080,返回的是 ”hello,welcome to linuxidc background management“。

Ubuntu 环境下:

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

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

NameVirtualHost *:8080Listen 8080
  • 在 /etc/apache2/sites-available/ 下增加 www-linuxidc-com-8080.conf,并在 /etc/apache2/sites-enabled/ 下建立符号链接。方法参考前面。

www-linuxidc-com-8080.conf 的主要配置内容如下:

<VirtualHost *:8080>    #Created by zhongjin on 2016-12-21 冬至    Serveradmin 1054840542@qq.com    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>

大家一定要看清楚需要修改的地方,VirtualHost 改为 8080,DocumentRoot 和 Directory 均改为 /home/www/linuxidc_admin,但是 ServerName 不用改,还是用 www.linuxidc.com

  • 创建目录 /home/www/linuxidc_admin,添加文件 index.php,输出 “hello,welcome to linuxidc background management”。

  • 重启 apache 服务器,测试

CentOS 环境下:

  • 让 httpd 监听 8080 端口:

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

# created by zhongjin on 2016-12-21 冬至NameVirtualHost *:8080Listen 8080
  • 在 /etc/httpd/vhost-conf.d 下面添加 www-linuxidc-com-8080.conf

直接复制 www-linuxidc-com.conf 文件,改名为 www-linuxidc-com-8080.conf,并修改为:

<VirtualHost *:8080>    #Created by zhongjin on 2016-12-21 冬至    Serveradmin 1054840542@qq.com    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>

注意修改的地方!

  • 创建目录 /home/www/linuxidc_admin,添加文件 index.php,输出 “hello,welcome to linuxidc background management”。

  • 重启 apache 服务器,测试

后话:

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

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

CentOS6.8编译安装Apache2.4.25、MySQL5.7.16、PHP5.6.29  http://www.linuxidc.com/Linux/2016-12/138993.htm

CentOS 6.6下安装Apache 2.2.31 http://www.linuxidc.com/Linux/2017-02/140803.htm

Apache 配置多站点访问及二级域名配置  http://www.linuxidc.com/Linux/2017-03/141339.htm

Ubuntu 16.04 LTS安装Apache2+PHP7.0+MySQL+phpMyAdmin图文详解  http://www.linuxidc.com/Linux/2017-02/140098.htm

CentOS 6下Apache和Tomcat整合  http://www.linuxidc.com/Linux/2017-04/143021.htm

Apache启用gzip压缩模块节约网站带宽  http://www.linuxidc.com/Linux/2017-04/142821.htm

Apache 配置 https  http://www.linuxidc.com/Linux/2017-02/140801.htm

使用Apache搭建Web网站服务器  http://www.linuxidc.com/Linux/2017-05/143468.htm

Linux下Apache安装及实例  http://www.linuxidc.com/Linux/2017-02/140800.htm

Apache2.4.6服务器安装及配置  http://www.linuxidc.com/Linux/2017-01/140006.htm

Ubuntu 16.04下搭建Web服务器(MySQL+PHP+Apache) 教程  http://www.linuxidc.com/Linux/2017-01/139570.htm

CentOS 7下 Apache 2.4.18编译安装详解  http://www.linuxidc.com/Linux/2017-03/142003.htm

本文永久更新链接地址http://www.linuxidc.com/Linux/2017-05/143590.htm

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
服务器架设:CentOS搭建LAMP详细教程
CentOS Apache配置详解
centos6.5下Zabbix系列之Zabbix安装搭建及汉化
linux下WordPress apache伪静态规则设置
CentOS7 yum安装Apache(httpd)并实现多域名配置
Mac自带apache配置小记
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服