sfwe2 发表于 2015-4-20 12:54:55

在ubuntu14.04 LTS下当完成lamp的安装后如何添加多个虚拟主机

当我们安装好lamp的环境后,我们通常是使用localhost进行本地地址的访问,然而,当我们在实验的环境中,需要配置多个域名,进行不同的访问时,比如说,一个localhost和一个test.cn两个主机时,增加一个网站test.cn做实验时,这是我们就要用到虚拟主机了。


在这里我用到的php环境是lamp-server,操作系统为ubuntu 14.04 LTS。#号为注释的内容


[*]在/etc/hosts下里添加这个127.0.0.1 food.cn

127.0.0.1    localhost
127.0.0.1   test.cn    #添加自己需要的域名
# The following lines are desirable for IPv6 capable hosts
::1   ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

[*]xc@wen:cat /etc/apache2/sites-enabled/000-default.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.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>



[*]修改上面的文件内的配置,sudo gedit /etc/apache2/sites-enabled/000-default.conf 配置虚拟主机,复制和localhost的配置并去掉相关注释
xc@wen:sudo gedit /etc/apache2/sites-enabled/ 000-default.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.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>
#这里为新添加的虚拟主机,主机名为test.cn
<VirtualHost *:80>
    ServerAdmin test.cn   #这里也定义一个域名

    ServerName test.cn      #添加自己定义的域名
    DocumentRoot /var/www/test   #添加自己域名所对应的目录
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>


[*]重启apache服务
sudo service apache2 restart


[*]打开浏览器,输入test.cn即可以用这个域名访问自己定义的文件了。



   注意:在这个配置的过程中,最主要的就是找到虚拟主机的配置文件了,在utubtu LTS下是放在,/etc/apache2/sites-enabled/000-default.conf,打开即可

页: [1]
查看完整版本: 在ubuntu14.04 LTS下当完成lamp的安装后如何添加多个虚拟主机