简体   繁体   English

在Google Compute Engine上具有IP地址的虚拟主机

[英]Virtual host with IP Address on Google Compute Engine

I have created LAMP instance on Google Compute engine and it was working properly earlier but as soon as I created production and development environment its not working. 我已经在Google Compute引擎上创建了LAMP实例,并且该实例较早就可以正常工作,但是一旦创建了生产和开发环境,它便无法正常工作。

What I did so far is as follow :- 我到目前为止所做的如下:

  • I don't have the domain so its running on public ip address - For eg 30.30.30.205 我没有域,因此它在公共IP地址上运行-例如30.30.30.205

  • I have created development.conf file in /etc/apache2/sites-available/. 我已经在/ etc / apache2 / sites-available /中创建了development.conf文件。

Content of development.conf is as follow : 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

Based on above config what I wish to achieve in url is http://30.30.30.205/production and for development it will be something like http://30.30.30.205/development . 基于上面的配置,我希望在url中实现的是http://30.30.30.205/production ,对于开发来说,它将类似于http://30.30.30.205/development

I have 2 folders inside /var/www/html - production and development in which I have set the different environment. 我在/ var / www / html中有2个文件夹-在生产和开发环境中设置了不同的环境。

After creating config file I also run the sudo a2ensite command in /sites-enabled folder. 创建配置文件后,我还要在/ sites-enabled文件夹中运行sudo a2ensite命令。

I haven't set the hosts in /etc as I believe I don't have the domain but not sure on this so please kindly guide me if i need to do anything here. 我尚未在/ etc中设置主机,因为我认为我没有域,但不确定是否在此范围内,因此如果我需要在此处进行任何操作,请引导我。

After following above steps, when i try to hit the url I am not able to access the site on google cloud. 按照上述步骤操作后,当我尝试点击网址时,我无法访问Google云上的网站。

Please kindly help or guide me how to resolve the issue. 请帮助或指导我如何解决此问题。

If this was working before you tried creating the virtual hosts, that rules out a firewall or network configuration, so I'll concentrate on the apache2 side of things. 如果在尝试创建虚拟主机之前这是可行的,那排除了防火墙或网络配置,因此我将专注于apache2方面。

I've just tried to implement something similar to what you are doing and managed to get this working, albeit with a fairly simple environment for each virtual host. 我刚刚尝试实现与您正在执行的操作类似的操作,并且设法使此功能正常运行,尽管每个虚拟主机的环境都非常简单。

One thing of note in your post is that you've named the configuration file for production 'development.conf'. 帖子中需要注意的一件事是,您已经将生产的配置文件命名为“ development.conf”。 Would it not make more sense to name it 'production.conf' (and name the conf file for the development 'development.conf' instead). 将其命名为“ production.conf”(而将开发的conf文件命名为“ development.conf”)是否更有意义。

Here are the steps I have followed to create a basic working environment. 这是我创建基本工作环境所遵循的步骤。 Try this, and see if it works, then you can build on it if so. 试试这个,看看它是否有效,如果可以的话,您可以在其上构建。

1) In /var/www/html/ I created two folders named 'development' and 'production'. 1)在/ var / www / html /中,我创建了两个名为“开发”和“生产”的文件夹。

2) In both folders I created an index.html file: 2)在两个文件夹中,我都创建了一个index.html文件:

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

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

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

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

3) In /etc/apache2/sites-available create 2 configuration files named 'development.conf' and 'production.conf'. 3)在/etc/apache2/sites-available创建两个名为'development.conf'和'production.conf'的配置文件。

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

production.conf production.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) Now ensure you are in /etc/apache2/sites-available and run the following two commands targetting both configuration files you created: 4)现在,确保您在/ etc / apache2 / sites-available中,并针对创建的两个配置文件运行以下两个命令:

$ sudo a2ensite development.conf 

$ sudo a2ensite production.conf 

5) Restart apache2: 5)重新启动apache2:

$ sudo systemctl reload apache2

Now when you navigate to http://EXTERNAL_IP/development you will see: 现在,当您导航到http:// EXTERNAL_IP / development时,您将看到:

"This is development"

And likewise, when you navigate to http://EXTERNAL_IP/production you will see: 同样,当您浏览到http:// EXTERNAL_IP / production时,您将看到:

"This is production"

EDIT: 编辑:

In response to the "this site can't be reached" error you are receiving, are you sure you are searching for the address using http rather than https? 响应您收到的“无法访问此站点”错误,您确定要使用http而不是https搜索地址吗? ie ensure you are trying to resolve in your browser: 即确保您尝试在浏览器中进行解析:

http://EXTERNAL_IP/development

rather than 而不是

https://EXTERNAL_IP/development

It's worth noting that if you click on the external IP address in the VM instances page in the GCP Console, it will attempt to resolve the address using HTTPS, and if you don't have SSL configured, you will receive the error you are receiving. 值得注意的是,如果您在GCP控制台的“ VM实例”页面中单击外部IP地址,它将尝试使用HTTPS解析该地址,并且如果未配置SSL,则会收到您收到的错误消息。

As another troubleshooting step, you could just place a basic index.html file in /var/www/html and see if that resolves with a http request by navigating to http://EXTERNAL_IP in a browser. 作为另一个故障排除步骤,您只需将基本index.html文件放在/ var / www / html中,然后通过在浏览器中导航到http:// EXTERNAL_IP来查看是否可以通过http请求解决。 If you receive the same error maybe you do have an issue with your firewall rules. 如果您收到相同的错误,则可能是防火墙规则存在问题。

In relation to your htaccess configuration, I tried the above with the default configuration. 关于您的htaccess配置,我尝试使用默认配置进行上述操作。 It might be worth you trying this initially to get the virtual hosts up and running, and then adding more complex configurations on top of this a step at a time. 您可能值得一开始尝试使虚拟主机启动并运行,然后一次在此之上添加更复杂的配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM