Ubuntu 14.04配置虚拟主机

虚拟主机经常使用于在一个单独的IP地址上提供多个域名的网站服务。若是有人想在单个VPS的单个IP地址运行多个网站,这是很是有用的。在这个教程中,让我告诉你如何设置在Ubuntu 14.04 LTS的Apache网页服务器设置虚拟主机。请注意,这个教程只针对Ubuntu14.04的64位版本。html

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

方案apache

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

安装Apache网站服务器网络

安装apache服务器以前,咱们来更新一下咱们的Ubuntu服务器:app

  • sudo apt-get update

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

  • sudo apt-get install apache2

安装apache服务器以后,让咱们经过这个URL http://你的服务器的IP地址/ 来测试网站服务器是否正常工做 ide

如你所见,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/

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

3. 为虚拟主机建立示例页

如今,咱们给网站增长示例页。第一步,让咱们给虚拟主机unixmen1.local建立一个示例页。

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

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

添加如下内容:

  • <html>
  • <head>
  • <title>www.unixmen1.local</title>
  • </head>
  • <body>
  • <h1>Welcome To Unixmen1.local website</h1>
  • </body>
  • </html>

保存并关闭文件。

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

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

添加如下内容:

  • <html>
  • <head>
  • <title>www.unixmen2.local</title>
  • </head>
  • <body>
  • <h1>Welcome To Unixmen2.local website</h1>
  • </body>
  • </html>

保存并关闭文件。

4. 建立虚拟主机配置文件

默认状况下,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站点中(译注:以“#”开头的注释行能够忽略。)。

  • <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@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
  • </VirtualHost>

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

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

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

  • <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@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
  • </VirtualHost>

修改虚拟主机文件后,禁用默认的虚拟主机配置(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服务器上

相关文章
相关标签/搜索