【问题标题】:Virtual host with IP Address on Google Compute EngineGoogle Compute Engine 上具有 IP 地址的虚拟主机
【发布时间】:2018-07-25 02:20:27
【问题描述】:

我在 Google Compute 引擎上创建了 LAMP 实例,它之前可以正常工作,但是一旦我创建了生产和开发环境,它就无法正常工作。

到目前为止我所做的如下:-

  • 我没有域,因此它在公共 IP 地址上运行 - 例如30.30.30.205

  • 我在 /etc/apache2/sites-available/ 中创建了 development.conf 文件。

development.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.

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/production/application
    Alias production

    <Directory "/var/www/html/production/application">
              Options Indexes FollowSymLinks MultiViews
              AllowOverride all
              Require all granted
              Order allow,deny
              allow from all
        </Directory>

    # 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>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

基于上面的配置,我希望在 url 中实现的是 http://30.30.30.205/production,对于开发来说,它将类似于 http://30.30.30.205/development

我在 /var/www/html 中有 2 个文件夹 - 生产和开发,我在其中设置了不同的环境。

创建配置文件后,我还在 /sites-enabled 文件夹中运行 sudo a2ensite 命令。

我没有在 /etc 中设置主机,因为我相信我没有域,但对此不确定,所以如果我需要在这里做任何事情,请指导我。

执行上述步骤后,当我尝试点击 url 时,我无法访问谷歌云上的网站。

请帮助或指导我如何解决问题。

【问题讨论】:

    标签: php google-cloud-platform apache2 google-compute-engine virtualhost


    【解决方案1】:

    如果这在您尝试创建虚拟主机之前有效,则排除了防火墙或网络配置,因此我将专注于 apache2 方面。

    我刚刚尝试实现与您正在做的事情类似的事情,并设法让它工作,尽管每个虚拟主机都有一个相当简单的环境。

    在您的帖子中需要注意的一点是,您已将生产配置文件命名为“development.conf”。将其命名为“production.conf”是否更有意义(并将开发的 conf 文件命名为“development.conf”)。

    以下是我创建基本工作环境所遵循的步骤。试试这个,看看它是否有效,如果有效,你可以在此基础上进行构建。

    1) 在 /var/www/html/ 中,我创建了两个名为“development”和“production”的文件夹。

    2) 在这两个文件夹中,我创建了一个 index.html 文件:

    /var/www/html/development/index.html

    <html>
       <head>
            <p> "This is development" </p>
       </head>
    </html>
    

    /var/www/html/production/index.html

    <html>
      <head>
              <p> "This is production"
      </head>
    </html>
    

    3) 在/etc/apache2/sites-available 中创建 2 个配置文件,分别命名为“development.conf”和“production.conf”。

    development.conf

    <VirtualHost *:80>
    
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html/development
    
        <Directory "/var/www/html/development">
                  Options Indexes FollowSymLinks MultiViews
                  AllowOverride all
                  Require all granted
                  Order allow,deny
                  allow from all
        </Directory>
    
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    
    </VirtualHost>
    

    生产.conf

    <VirtualHost *:80>
    
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html/production
    
        <Directory "/var/www/html/production">
                  Options Indexes FollowSymLinks MultiViews
                  AllowOverride all
                  Require all granted
                  Order allow,deny
                  allow from all
        </Directory>
    
    
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    
    </VirtualHost>
    

    4) 现在确保您位于 /etc/apache2/sites-available 并针对您创建的两个配置文件运行以下两个命令:

    $ sudo a2ensite development.conf 
    
    $ sudo a2ensite production.conf 
    

    5) 重启apache2:

    $ sudo systemctl reload apache2
    

    现在,当您导航到 http://EXTERNAL_IP/development 时,您将看到:

    "This is development"
    

    同样,当您导航到 http://EXTERNAL_IP/production 时,您会看到:

    "This is production"
    

    编辑:

    针对您收到的“无法访问此站点”错误,您确定要使用 http 而不是 https 搜索地址吗?即确保您尝试在浏览器中解析:

    http://EXTERNAL_IP/development
    

    而不是

    https://EXTERNAL_IP/development
    

    值得注意的是,如果您在 GCP Console 的虚拟机实例页面中点击外部 IP 地址,它会尝试使用 HTTPS 解析该地址,如果您没有配置 SSL,则会收到错误消息你正在接收。

    作为另一个故障排除步骤,您可以在 /var/www/html 中放置一个基本的 index.html 文件,然后通过在浏览器中导航到 http://EXTERNAL_IP 来查看是否可以通过 http 请求解决。如果您收到相同的错误,则可能是您的防火墙规则存在问题。

    关于您的 htaccess 配置,我使用默认配置尝试了上述操作。您可能值得一开始尝试此操作以启动并运行虚拟主机,然后在此基础上一次添加更复杂的配置。

    【讨论】:

    • 非常感谢@neilH 提供的详细步骤。我按照您的建议进行操作,但我的网站仍未启动并出现“无法访问此网站”错误。只想补充一点,我已经在云上部署了 CodeIgniter PHP 应用程序,因为我有 2 个文件夹开发和生产。 .htaccess 文件还添加了标准重写规则,以确保它不会产生问题。知道如何解决这个问题吗?
    • 我已经用一些额外的故障排除建议编辑了我的答案。
    猜你喜欢
    • 2016-03-24
    • 2020-09-18
    • 2021-10-26
    • 2018-12-27
    • 1970-01-01
    • 1970-01-01
    • 2020-09-13
    • 2015-12-07
    • 2016-10-17
    相关资源
    最近更新 更多