【问题标题】:How to create a virtual host in ubuntu 13.10如何在 ubuntu 13.10 中创建虚拟主机
【发布时间】:2014-03-28 20:00:49
【问题描述】:

我正在使用 ubuntu 13.10 操作系统和 LAMP、Apache 2.4。

我想在 apache 上创建一个虚拟主机。我尝试了一些代码,但没有成功。

进行了以下修改。但它不起作用。

首先我在etc\apache2\apache2.conf 文件上将HostnameLookups off 更改为HostnameLookups on。然后我添加了下面的代码,

<VirtualHost *:80>
ServerName local.scholarships.theiet.in
DocumentRoot /home/www/my_project/public_html
<Directory path_to_code_base/public>
    Options -Indexes
    Require all granted
    DirectoryIndex index.php
    AllowOverride All
</Directory>
</VirtualHost>

重启 apache 后,我运行了 http://localhost/ 。该网站未加载。

运行http://localhost/时如何加载我的网站

【问题讨论】:

    标签: apache ubuntu virtualhost lamp ubuntu-13.10


    【解决方案1】:

    这是在 Apache/Ubuntu 上创建虚拟主机的方法:

    我的 000-default.conf 文件:

    <VirtualHost *:80>
        DocumentRoot /var/www/php/frbit/l4blog/public/
        <Directory /var/www/php/frbit/l4blog/public/>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
        </Directory>
        ServerName l4blog
    </VirtualHost>
    

    请注意,我创建了 ServerName,这是我的新主机的名称。

    您可以像这样在 /etc/hosts 文件中添加新主机名:

    127.0.0.1   your_host_name
    

    为了不输入长网址,例如而不是

    http://localhost/path/directory/file/...
    

    您只需在地址栏中输入 your_host_name:

    your_host_name
    

    【讨论】:

      【解决方案2】:

      sites-available 目录中的配置文件文件名现在必须以“.conf”结尾,因此在 /etc/apache2/sites-available/ 添加您的 .conf 文件,以 example.com.conf 的样式命名;在以下方面进行建模:

      <VirtualHost *:80>
      ServerAdmin you@example.com
          ServerName www.example.com
          DocumentRoot /var/www/example.com
          <Directory />
                  Options FollowSymLinks
                  Options Indexes FollowSymLinks MultiViews
                  AllowOverride All
          require all granted
          </Directory>
      
          ErrorLog /var/log/apache2/example.com.error.log
      
          # Possible values include: debug, info, notice, warn, error, crit,
          # alert, emerg.
          LogLevel warn
      
          CustomLog /var/log/apache2/example.com.access.log combined    
      </VirtualHost>
      

      在 apache 中启用它:

      $ sudo a2ensite example.com
      

      (如果您以后需要禁用它,请使用 $sudo a2dissite example.com)

      您可能还需要在 /etc/hosts 文件中添加一行:

      127.0.0.1 example.com
      

      不要忘记,虽然您已经使用 a2ensite 将站点添加到 apache,但您还需要重新启动 apache。

      【讨论】:

      • 重启 apache 时出现错误“sudo:无法初始化策略插件”
      • 那是完全不同的东西——上面你有两个与你的虚拟主机问题相关的高质量答案。如果您有更多问题,请创建新帖子。
      • 但是为了更有帮助,我认为这个问题(re sudo)已经在 askubuntu 网站上解决了,askubuntu.com/questions/170216/sudo-doesnt-work :-)
      【解决方案3】:

      这是在 ubuntu 13.10 中创建虚拟主机的另一种方式

      下面的例子展示了如何创建一个虚拟主机,

      第一步:在/home/user/www/上创建一个名为site1.com的PHP项目

      第 2 步:将/etc/apache2/apache2.conf 中的HostnameLookups off 更改为HostnameLookups on

      第 3 步:在 /etc/apache2/sites-available/ 上创建一个名为 site1.com.conf 的配置文件

      将此代码添加到site1.com.conf,

      <VirtualHost *:80>
      ServerName site1.com
      ServerAlias www.site1.com
      ServerAdmin info@site1.com
      DocumentRoot /var/www/site1.com
      ErrorLog ${APACHE_LOG_DIR}/error.log
      CustomLog ${APACHE_LOG_DIR}/access.log combined
      <Directory "/var/www/site1.com">
          Options All
          AllowOverride All
          Require all granted
      </Directory>
      </VirtualHost>
      

      第四步:然后将127.0.0.1 site1.com添加到/etc/hosts.txt

      第 5 步:打开终端并运行命令,

      sudo a2ensite site1.com
      
      sudo /etc/init.d/apache2 restart
      

      第六步:打开浏览器运行http://site1.com/

      试试这个

      【讨论】:

        猜你喜欢
        • 2013-10-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-21
        • 2016-06-15
        • 2013-10-10
        • 2016-02-08
        • 2014-01-25
        相关资源
        最近更新 更多