在Ubuntu 14.04 LTS系统中设置Apache虚拟主机
创始人
2024-03-01 12:46:35
0

虚拟主机常用于在一个单独的IP地址上提供多个域名的网站服务。如果有人想在单个VPS的单个IP地址运行多个网站,这是非常有用的。

在这个教程中,让我告诉你如何设置在Ubuntu 14.04 LTS的Apache网页服务器设置虚拟主机。请注意,这个教程只针对Ubuntu14.04的32位版本。

我不保证它也可以工作在其它更低的Ubuntu版本或者Ubuntu衍生版本(虽然可能过程是类似的)。

方案

在这个教程中,我会使用Ubuntu 14.04 32位 LTS,并搭建2个测试网站分别命名为“unixmen1.local” 和 “unixmen2.local”.我的测试机分别为192.168.1.250/24server.unixmen.local。你可以根据你的需要更改虚拟域名。

安装Apache网站服务器

安装apache服务器之前,我们来更新一下我们的Ubuntu服务器:

sudo apt-get update

然后,用下面命令来安装apache网络服务器:

sudo apt-get install apache2

安装apache服务器之后,让我们通过这个URL http://你的服务器的IP地址/ 来测试网站服务器是否正常工作

如你所见,apache服务器已经工作了。

设置虚拟主机

1.创建虚拟目录

现在,让我们继续安装虚拟主机。正如我先前所述,我要新建2台虚拟主机分别命名为“unixmen1.local”和“unixmen2.local”.

创建一个公用的文件夹来存放这两台虚拟主机的数据。

首先,让我们为unixmen1.local这个站点创建一个目录:

sudo mkdir -p /var/www/unixmen1.local/public_html

接着,为for unixmen2.local站点创建一个目录:

sudo mkdir -p /var/www/unixmen2.local/public_html

2. 设置所有者和权限

上面目录现在只有root拥有权限。我们需要修改这2个目录的拥有权给普通用户,而不仅仅是root用户。

sudo chown -R $USER:$USER /var/www/unixmen1.local/public_html/
sudo chown -R $USER:$USER /var/www/unixmen2.local/public_html/

$USER”变量指向了当前的登录用户。

设置读写权限给apache网页根目录(/var/www)及其子目录,这样每个人都可以从目录中读取文件。

sudo chmod -R 755 /var/www/

这样,我们就创建好了一些文件夹来保存网络相关数据并分配必要的权限和所属用户。

4. 为虚拟主机创建示例页

现在,我们给网站增加示例页。第一步,让我们给虚拟主机unixmen1.local创建一个示例页。

给unixmen1.local虚拟主机创建一个示例页,

sudo vi /var/www/unixmen1.local/public_html/index.html

添加以下内容:


 
 www.unixmen1.local
 
 
 

Welcome To Unixmen1.local website

保存并关闭文件。

同样的,添加示例页到第二台虚拟主机。

sudo vi /var/www/unixmen2.local/public_html/index.html

添加以下内容:


 
 www.unixmen2.local
 
 
 

Welcome To Unixmen2.local website

保存并关闭文件。

5. 创建虚拟主机配置文件

默认情况下,apache有一个默认的虚拟主机文件叫000-default.conf。我们将会复制000-default.conf文件内容到我们新的虚拟主机配置文件中。

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/unixmen1.local.conf
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/unixmen2.local.conf

确保虚拟主机配置文件末尾包含.conf扩展名。

现在,修改unximen1.local.conf文件以符合需求。

sudo vi /etc/apache2/sites-available/unixmen1.local.conf

使相关的变化直接呈现在unixmen1站点中(译注:以“#”开头的注释行可以忽略。)。


        # 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@unixmen1.local
        ServerName unixmen1.local
        ServerAlias www.unixmen1.local
        DocumentRoot /var/www/unixmen1.local/public_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

同理,修改第二台主机文件。

sudo vi /etc/apache2/sites-available/unixmen2.local.conf

使相关的修改在unixmen2 站点呈现出来。


        # 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@unixmen2.local
        ServerName unixmen2.local
        ServerAlias www.unixmen2.local
        DocumentRoot /var/www/unixmen2.local/public_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

修改虚拟主机文件后,禁用默认的虚拟主机配置(000.default.conf),然后启用新的虚拟主机配置,如下所示。

sudo a2dissite 000-default.conf
sudo a2ensite unixmen1.local.conf
sudo a2ensite unixmen2.local.conf

最后,重启apache服务器。

sudo service apache2 restart

就是这样。现在,我们成功地配置了apach虚拟主机在我们的Ubuntu服务器上

测试虚拟主机

编辑**/etc/hosts**文件,

sudo vi /etc/hosts

在文件末尾添加如下所示的虚拟域名。

192.168.1.250   unixmen1.local
192.168.1.250   unixmen2.local

保存并关闭文件。

打开你的浏览器并访问**http://unixmen1.local** 或 http://unixmen2.local。你将会看到我们之前创建的示例页。

Unixmen1.local 测试页:

Unixmen2.local 测试页

如果你想从你的远程系统访问这些站点,你需要在你的DNS服务器添加实际域名记录。不过,我没有真实的域名和DNS服务器,我只想通过我的本地系统测试,那么它刚好如我所愿地工作。

Cheers!


via: http://www.unixmen.com/setup-virtual-hosts-apache-ubuntu-14-04-lts/

译者:disylee 校对:wxy

本文由 LCTT 原创翻译,Linux中国 荣誉推出

相关内容

Mistral 发布 De...
5 月 22 日消息,科技媒体 WinBuzzer 昨日(5 月 ...
2025-05-22 13:18:21
CentOS安装phpMy...
在CentOS系统中部署phpMyAdmin是管理MySQL数据库...
2025-04-18 19:17:39
在CentOS系统上高效搭...
在CentOS系统上部署Apache HTTP服务器的高效指南 A...
2025-04-17 06:17:20
如何在阿里云CentOS系...
阿里云CentOS搭建LAMP环境全流程指南 在阿里云服务器上搭建...
2025-03-06 13:49:47
阿里发布并开源最新的推理模...
每经AI快讯,3月6日凌晨,阿里云通义千问官微宣布发布并开源最新的...
2025-03-06 11:21:08
IBM推出Granite ...
(全球TMT2025年3月5日讯)IBM推出其Granite大型语...
2025-03-05 16:29:13

热门资讯

Helix:高级 Linux ... 说到 基于终端的文本编辑器,通常 Vim、Emacs 和 Nano 受到了关注。这并不意味着没有其他...
使用 KRAWL 扫描 Kub... 用 KRAWL 脚本来识别 Kubernetes Pod 和容器中的错误。当你使用 Kubernet...
JStock:Linux 上不... 如果你在股票市场做投资,那么你可能非常清楚投资组合管理计划有多重要。管理投资组合的目标是依据你能承受...
通过 SaltStack 管理... 我在搜索Puppet的替代品时,偶然间碰到了Salt。我喜欢puppet,但是我又爱上Salt了:)...
Epic 游戏商店现在可在 S... 现在可以在 Steam Deck 上运行 Epic 游戏商店了,几乎无懈可击! 但是,它是非官方的。...
《Apex 英雄》正式可在 S... 《Apex 英雄》现已通过 Steam Deck 验证,这使其成为支持 Linux 的顶级多人游戏之...
如何在 Github 上创建一... 学习如何复刻一个仓库,进行更改,并要求维护人员审查并合并它。你知道如何使用 git 了,你有一个 G...
2024 开年,LLUG 和你... Hi,Linuxer,2024 新年伊始,不知道你是否已经准备好迎接新的一年~ 2024 年,Lin...
什么是 KDE Connect... 什么是 KDE Connect?它的主要特性是什么?它应该如何安装?本文提供了基本的使用指南。科技日...
Opera 浏览器内置的 VP... 昨天我们报道过 Opera 浏览器内置了 VPN 服务,用户打开它可以防止他们的在线活动被窥视。不过...