Apache2 在Ubuntu环境下部署多个站点(单ip配置多域名)

一,需求描述

现有一台云服务器,且只绑定一个IP;要求在同一个服务器中搭建多个站点;

且一个站点对应一个解析域名,达到在同一服务器同一IP下访问不同域名显示不同站点的目的

目前已经在Apache Server上部署了WordPress CMS,还需要再部署另一个站点,并使用不同域名访问;

即:浏览器输入 www.site1.com 打开WordPress站点

​ 浏览器输入 www.site2.com 打开另一个站点

二,服务器环境,安装软件

操作系统:Ubuntu 18.04

Web服务器:Apache 2.4.29

数据库:Mysql 5.7.34

三,基本原理讲解

目前在一台服务器上搭建多个网站的方法主要由以下几种:

  1. 基于IP地址 这种方法适用于一台服务器有多个IP的情况,现在IPv4地址已经枯竭,公有IP非常珍贵,故此方法不适用
  2. 基于端口号 这种方法使用不同的端口号来识别不同的网站,实际访问时使用网址加端口号的方式来实现,如 http://www.site1.com:80,http:// www.site1.com:81,http:// www.site1.com:82,该方式配置后需要在网站后加上端口号来访问不同的网站,适用于网站域名短缺但服务器的端口号充足的情况,缺点是网站后需要加上端口号,不利于用户访问
  3. 基于主机名 这种方法使用不同的域名来区分不同的网站,所有的域名解析都指向同一个IP,Apache通过在HTTP头中附带的host参数来判断用户需要访问哪一个网站,如http://www.site1.com,http://www.site2.com,http://www.site3.com,多数情况下多个网站架在一台服务器上均使用该方法 。

明显根据我们的需求我们肯定是需要用第三种方法,基于主机名搭建多个网站。

下面我们主要讲一下如何通过修改apache配置信息来实现第3种方法!

apache2在启动的时候自动读取/etc/apache2/apache2.conf文件的配置信息,不同的配置项按功能分布在不同的文件中,然后被Include包含到apache2.conf这个主配置文件中,方便管理。

就是说事实上apache2主配置文件只有一个,即apache2.conf,其他的都是被include进来的。

可以把所有的配置都放在apache2.conf或者任何一个配置文件中,但是划分到不同文件会让我们管理起来方便很多。

Apache2目录结构

root@cloudserver:/etc/apache2# tree
.
├── apache2.conf    全局配置
├── conf-available  可用的配置文件
├── conf-enabled    已启用的配置文件
├── envvars         环境变量
├── magic
├── mods-available  已安装的模块
├── mods-enabled    已启用的模块
├── ports.conf      http服务端口信息
├── sites-available 可用站点信息
│   ├── 000-default.conf
│   └──default-ssl.conf
└── sites-enabled   已经启用的站点信息,当中的文件是到/etc/apache2/sites-available/ 文件的软连接
    └── 000-default.conf -> ../sites-available/000-default.conf

其中,apache2.conf是apache2的主配置文件,包括三个级别的配置。

  • 控制apache服务器执行过程的全局配置。

  • 定义主服务或者默认服务器的参数的配置,这些配置会响应virtual host不处理的请求。这类配置也为所有的virtual hosts配置提供默认值。

  • virtual hosts相关的配置,使得同一个apache服务进程处理向不同IP地址或者主机名发送的请求。

Apache2配置简介

Apache2主配置文件:使用编辑器打开 /etc/apache2/apache2.conf 查看发现,最后两行为:

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

可以明显看到/etc/apache2/sites-enabled下存放着有关虚拟站点(Virtual Host)的配置 。

经过查看,在初始情况下,该目录下包含一个符号链接:

└── sites-enabled   已经启用的站点信息,当中的文件是到/etc/apache2/sites-available/ 文件的软连接
    └── 000-default.conf -> ../sites-available/000-default.conf

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

/etc/apache2/sites-available下有两个文件:000-default.conf和default-ssl.conf。

注意:sites-enabled中的000-default.conf链接的文件为sites-available中的000-default.conf。

以000-default.conf为例,我们来看一下VirtualHost的配置是啥样的:

<VirtualHost *:80>
	# The ServerName directive sets the request scheme, hostname and port that
	# the server uses to identify itself. This is used when creating
	# redirection URLs. In the context of virtual hosts, the ServerName
	# specifies what hostname must appear in the request's Host: header to
	# match this virtual host. For the default virtual host (this file) this
	# value is not decisive as it is used as a last resort host regardless.
	# However, you must set it for any further virtual host explicitly.
	#ServerName www.example.com

	ServerAdmin webmaster@localhost
	DocumentRoot /var/www/html

	# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
	# error, crit, alert, emerg.
	# It is also possible to configure the loglevel for particular
	# modules, e.g.
	#LogLevel info ssl:warn

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

	# For most configuration files from conf-available/, which are
	# enabled or disabled at a global level, it is possible to
	# include a line for only one particular virtual host. For example the
	# following line enables the CGI configuration for this host only
	# after it has been globally disabled with "a2disconf".
	#Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

DocumentRoot是这个站点的根目录,这样Apache2启动时会扫描/etc/apache2/sites-enabled中可用的站点配置并加载。

当用户访问localhost:80时,Apache2就将default站点根目录/var/www/html下的index.html作为请求的响应返回给浏览器。

四,利用Apache2配置多站点

根据我们的需求,我们需要修改Apache2的配置文件,来满足我们根据不同域名访问不同的站点的需求。

假设我们需要在一个Apache2服务器上配置两个站点:

站点一:www.site1.com

站点二:www.site2.com

4.1 建立配置文件

在 sites-available 中建立两个站点的配置文件 www_site1_com.conf 和 www_site2_com.conf:

cd /etc/apache2/sites-available
sudo cp 000-default.conf www_site1_com.conf
sudo cp 000-default.conf www_site2_com.conf

4.2 编辑上面两个配置文件,以 www_site1_com.conf 为例

<VirtualHost *:80>
	# The ServerName directive sets the request scheme, hostname and port that
	# the server uses to identify itself. This is used when creating
	# redirection URLs. In the context of virtual hosts, the ServerName
	# specifies what hostname must appear in the request's Host: header to
	# match this virtual host. For the default virtual host (this file) this
	# value is not decisive as it is used as a last resort host regardless.
	# However, you must set it for any further virtual host explicitly.
	ServerName www.site1.com

	ServerAdmin webmaster@localhost
	DocumentRoot /var/www/html1/

    <Directory "/var/www/html1/">
        Options Indexes FollowSymLinks
        AllowOverride all
        Order Deny,Allow
        Deny from none
        Allow from all
	</Directory>

	# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
	# error, crit, alert, emerg.
	# It is also possible to configure the loglevel for particular
	# modules, e.g.
	#LogLevel info ssl:warn

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

	# For most configuration files from conf-available/, which are
	# enabled or disabled at a global level, it is possible to
	# include a line for only one particular virtual host. For example the
	# following line enables the CGI configuration for this host only
	# after it has been globally disabled with "a2disconf".
	#Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

注意:

上面配置中:ServerName、DocumentRoot是我们重点关注的配置点 。

ServerName后面填写我们要绑定域名,DocumentRoot是该站点的根目录,如上所示,我设置的根目录为/var/www/html1(没有的,自己建一个),注意html1文件夹的名字可以随便起,你自己知道什么含义就行。

(同样的,站点二:www.site2.com 也做同样的改动)

4.3 在sites-enabled目录下建立符号链接

将我们在上一步配置好的两个配置文件 www_site1_com.conf 和 www_site2_com.conf 都拷贝到 sites-enabled 文件夹下:

cd /etc/apache2/sites-available
sudo cp /etc/apache2/sites-available/www_site1_com.conf /etc/apache2/sites-enabled/www_site1_com.conf
sudo cp /etc/apache2/sites-available/www_site2_com.conf /etc/apache2/sites-enabled/www_site2_com.conf

然后在sites-enabled目录下建立符号链接

cd /etc/apache2/sites-enabled
sudo ln -s /etc/apache2/sites-available/site1_com.conf /etc/apache2/sites-enabled/site1_com.conf
sudo ln -s /etc/apache2/sites-available/site2_com.conf /etc/apache2/sites-enabled/site2_com.conf

4.4 部署项目

  • 将你要部署的站点拷贝到 /vat/www/ 目录下,并重命名为 html1 和html2(按自己习惯命名就行啦)
  • 在html1目录下新建文件.htaccess,以重构路由:
<IfModule mod_rewrite.c>
	  RewriteEngine On
    RewriteBase /
	  RewriteRule ^index\.html$ - [L]
	  RewriteCond %{REQUEST_FILENAME} !-f
	  RewriteCond %{REQUEST_FILENAME} !-d
	  RewriteRule . /index.html [L]
</IfModule>

重写Apache2配置

sudo a2enmod rewrite

4.5 重启Apache2使配置生效

sudo service apache2 restart

4.6 修改/etc/hosts文件,便于测试

添加如下两行:

127.0.0.1 www.site1.com
127.0.0.1 www.site2.com

4.7 测试

打开浏览器,输入URL:http://www.site1.com 即可看到我们设置的站点一。

问题补充:

关于通过IP访问会显示哪个站点,一般情况下,默认的配置文件:000-default.conf 优先级最高,通过ip访问后,会优先访问该文件所配置的站点。

参考文章:

Ubuntu环境下利用Apache2部署多个站点_Jochen的博客-CSDN博客

使用Apache2配置多个站点 | Tony Bai

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值