码迷,mamicode.com
首页 > Web开发 > 详细

Ubuntu || LinuxMint 配置apache虚拟主机

时间:2015-09-15 11:07:20      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:

2015-09-15

文章转自https://linux.cn/article-3164-1.html


设置虚拟主机

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>

保存并关闭文件。

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

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

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

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

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

  1. sudo vi /etc/apache2/sites-available/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.  
  11. ServerAdmin webmaster@unixmen1.local
  12. ServerName unixmen1.local
  13. ServerAlias www.unixmen1.local
  14. DocumentRoot /var/www/unixmen1.local/public_html
  15.  
  16. # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
  17. # error, crit, alert, emerg.
  18. # It is also possible to configure the loglevel for particular
  19. # modules, e.g.
  20. #LogLevel info ssl:warn
  21.  
  22. ErrorLog ${APACHE_LOG_DIR}/error.log
  23. CustomLog ${APACHE_LOG_DIR}/access.log combined
  24.  
  25. # For most configuration files from conf-available/, which are
  26. # enabled or disabled at a global level, it is possible to
  27. # include a line for only one particular virtual host. For example the
  28. # following line enables the CGI configuration for this host only
  29. # after it has been globally disabled with "a2disconf".
  30. #Include conf-available/serve-cgi-bin.conf
  31. </VirtualHost>

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

  1. sudo vi /etc/apache2/sites-available/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.  
  11. ServerAdmin webmaster@unixmen2.local
  12. ServerName unixmen2.local
  13. ServerAlias www.unixmen2.local
  14. DocumentRoot /var/www/unixmen2.local/public_html
  15.  
  16. # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
  17. # error, crit, alert, emerg.
  18. # It is also possible to configure the loglevel for particular
  19. # modules, e.g.
  20. #LogLevel info ssl:warn
  21.  
  22. ErrorLog ${APACHE_LOG_DIR}/error.log
  23. CustomLog ${APACHE_LOG_DIR}/access.log combined
  24.  
  25. # For most configuration files from conf-available/, which are
  26. # enabled or disabled at a global level, it is possible to
  27. # include a line for only one particular virtual host. For example the
  28. # following line enables the CGI configuration for this host only
  29. # after it has been globally disabled with "a2disconf".
  30. #Include conf-available/serve-cgi-bin.conf
  31. </VirtualHost>

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

  1. sudo a2dissite 000-default.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.local 或 http://unixmen2.local。你将会看到我们之前创建的示例页。

Ubuntu || LinuxMint 配置apache虚拟主机

标签:

原文地址:http://www.cnblogs.com/laozhangOnTheWay/p/4809411.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!