阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

在Ubuntu 14.04 LTS系统中设置Apache虚拟主机

130次阅读
没有评论

共计 7870 个字符,预计需要花费 20 分钟才能阅读完成。

虚拟主机 常用于在一个单独的 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 服务器:

  1. sudo aptget update

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

  1. sudo aptget install apache2

安装 apache 服务器之后,让我们通过这个 URL http:// 你的服务器的 IP 地址 / 来测试网站服务器是否正常工作 在 Ubuntu 14.04 LTS 系统中设置 Apache 虚拟主机

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

————————————– 分割线 ————————————–

Ubuntu 13.04 安装 LAMP\Vsftpd\Webmin\phpMyAdmin 服务及设置 http://www.linuxidc.com/Linux/2013-06/86250.htm

CentOS 5.9 下编译安装 LAMP(Apache 2.2.44+MySQL 5.6.10+PHP 5.4.12) http://www.linuxidc.com/Linux/2013-03/80333p3.htm

RedHat 5.4 下 Web 服务器架构之源码构建 LAMP 环境及应用 PHPWind http://www.linuxidc.com/Linux/2012-10/72484p2.htm

LAMP 源码环境搭建 WEB 服务器 Linux+Apache+MySQL+PHP http://www.linuxidc.com/Linux/2013-05/84882.htm

LAMP+Xcache 环境搭建 http://www.linuxidc.com/Linux/2014-05/101087.htm

————————————– 分割线 ————————————–

设置虚拟主机

1. 创建虚拟目录

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

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

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

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

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

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

2. 设置所有者和权限

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

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

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

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

  1. sudo chmod R 755/var/www/

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

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

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

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

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

添加以下内容:

  1. <html>
  2. <head>
  3. <title>www.unixmen1.local</title>
  4. </head>
  5. <body>
  6. <h1>Welcome To Unixmen1.local website</h1>
  7. </body>
  8. </html>

保存并关闭文件。

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

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

添加以下内容:

  1. <html>
  2. <head>
  3. <title>www.unixmen2.local</title>
  4. </head>
  5. <body>
  6. <h1>Welcome To Unixmen2.local website</h1>
  7. </body>
  8. </html>

保存并关闭文件。

更多详情见请继续阅读下一页的精彩内容:http://www.linuxidc.com/Linux/2014-06/102871p2.htm

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

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

  1. sudo cp /etc/apache2/sitesavailable/000default.conf /etc/apache2/sitesavailable/unixmen1.local.conf
  2. sudo cp /etc/apache2/sitesavailable/000default.conf /etc/apache2/sitesavailable/unixmen2.local.conf

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

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

  1. sudo vi /etc/apache2/sitesavailable/unixmen1.local.conf

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

  1. <VirtualHost *:80>
  2. # The ServerName directive sets the request scheme, hostname and port that
  3. # the server uses to identify itself. This is used when creating
  4. # redirection URLs. In the context of virtual hosts, the ServerName
  5. # specifies what hostname must appear in the request’s Host: header to
  6. # match this virtual host. For the default virtual host (this file) this
  7. # value is not decisive as it is used as a last resort host regardless.
  8. # However, you must set it for any further virtual host explicitly.
  9. #ServerName www.example.com
  10. ServerAdmin webmaster@unixmen1.local
  11. ServerName unixmen1.local
  12. ServerAlias www.unixmen1.local
  13. DocumentRoot /var/www/unixmen1.local/public_html
  14. # Available loglevels: trace8, …, trace1, debug, info, notice, warn,
  15. # error, crit, alert, emerg.
  16. # It is also possible to configure the loglevel for particular
  17. # modules, e.g.
  18. #LogLevel info ssl:warn
  19. ErrorLog ${APACHE_LOG_DIR}/error.log
  20. CustomLog ${APACHE_LOG_DIR}/access.log combined
  21. # For most configuration files from conf-available/, which are
  22. # enabled or disabled at a global level, it is possible to
  23. # include a line for only one particular virtual host. For example the
  24. # following line enables the CGI configuration for this host only
  25. # after it has been globally disabled with “a2disconf”.
  26. #Include conf-available/serve-cgi-bin.conf
  27. </VirtualHost>

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

  1. sudo vi /etc/apache2/sitesavailable/unixmen2.local.conf

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

  1. <VirtualHost *:80>
  2. # The ServerName directive sets the request scheme, hostname and port that
  3. # the server uses to identify itself. This is used when creating
  4. # redirection URLs. In the context of virtual hosts, the ServerName
  5. # specifies what hostname must appear in the request’s Host: header to
  6. # match this virtual host. For the default virtual host (this file) this
  7. # value is not decisive as it is used as a last resort host regardless.
  8. # However, you must set it for any further virtual host explicitly.
  9. #ServerName www.example.com
  10. ServerAdmin webmaster@unixmen2.local
  11. ServerName unixmen2.local
  12. ServerAlias www.unixmen2.local
  13. DocumentRoot /var/www/unixmen2.local/public_html
  14. # Available loglevels: trace8, …, trace1, debug, info, notice, warn,
  15. # error, crit, alert, emerg.
  16. # It is also possible to configure the loglevel for particular
  17. # modules, e.g.
  18. #LogLevel info ssl:warn
  19. ErrorLog ${APACHE_LOG_DIR}/error.log
  20. CustomLog ${APACHE_LOG_DIR}/access.log combined
  21. # For most configuration files from conf-available/, which are
  22. # enabled or disabled at a global level, it is possible to
  23. # include a line for only one particular virtual host. For example the
  24. # following line enables the CGI configuration for this host only
  25. # after it has been globally disabled with “a2disconf”.
  26. #Include conf-available/serve-cgi-bin.conf
  27. </VirtualHost>

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

  1. sudo a2dissite 000default.conf
  2. sudo a2ensite unixmen1.local.conf
  3. sudo a2ensite unixmen2.local.conf

最后,重启 apache 服务器。

  1. sudo service apache2 restart

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

测试虚拟主机

编辑 /etc/hosts 文件,

  1. sudo vi /etc/hosts

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

  1. 192.168.1.250 unixmen1.local
  2. 192.168.1.250 unixmen2.local

保存并关闭文件。

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

Unixmen1.local 测试页:

在 Ubuntu 14.04 LTS 系统中设置 Apache 虚拟主机

Unixmen2.local 测试页

在 Ubuntu 14.04 LTS 系统中设置 Apache 虚拟主机

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

Cheers!

更多 Ubuntu 相关信息见Ubuntu 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=2

虚拟主机 常用于在一个单独的 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 服务器:

  1. sudo aptget update

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

  1. sudo aptget install apache2

安装 apache 服务器之后,让我们通过这个 URL http:// 你的服务器的 IP 地址 / 来测试网站服务器是否正常工作 在 Ubuntu 14.04 LTS 系统中设置 Apache 虚拟主机

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

————————————– 分割线 ————————————–

Ubuntu 13.04 安装 LAMP\Vsftpd\Webmin\phpMyAdmin 服务及设置 http://www.linuxidc.com/Linux/2013-06/86250.htm

CentOS 5.9 下编译安装 LAMP(Apache 2.2.44+MySQL 5.6.10+PHP 5.4.12) http://www.linuxidc.com/Linux/2013-03/80333p3.htm

RedHat 5.4 下 Web 服务器架构之源码构建 LAMP 环境及应用 PHPWind http://www.linuxidc.com/Linux/2012-10/72484p2.htm

LAMP 源码环境搭建 WEB 服务器 Linux+Apache+MySQL+PHP http://www.linuxidc.com/Linux/2013-05/84882.htm

LAMP+Xcache 环境搭建 http://www.linuxidc.com/Linux/2014-05/101087.htm

————————————– 分割线 ————————————–

设置虚拟主机

1. 创建虚拟目录

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

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

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

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

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

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

2. 设置所有者和权限

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

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

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

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

  1. sudo chmod R 755/var/www/

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

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

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

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

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

添加以下内容:

  1. <html>
  2. <head>
  3. <title>www.unixmen1.local</title>
  4. </head>
  5. <body>
  6. <h1>Welcome To Unixmen1.local website</h1>
  7. </body>
  8. </html>

保存并关闭文件。

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

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

添加以下内容:

  1. <html>
  2. <head>
  3. <title>www.unixmen2.local</title>
  4. </head>
  5. <body>
  6. <h1>Welcome To Unixmen2.local website</h1>
  7. </body>
  8. </html>

保存并关闭文件。

更多详情见请继续阅读下一页的精彩内容:http://www.linuxidc.com/Linux/2014-06/102871p2.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-20发表,共计7870字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中