简体   繁体   中英

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.

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

  • I have created development.conf file in /etc/apache2/sites-available/.

Content of development.conf is as follow :

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

I have 2 folders inside /var/www/html - production and development in which I have set the different environment.

After creating config file I also run the sudo a2ensite command in /sites-enabled folder.

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.

After following above steps, when i try to hit the url I am not able to access the site on google cloud.

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.

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'. Would it not make more sense to name it 'production.conf' (and name the conf file for the development 'development.conf' instead).

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

2) In both folders I created an index.html file:

/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) In /etc/apache2/sites-available create 2 configuration files named 'development.conf' and '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>

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:

$ sudo a2ensite development.conf 

$ sudo a2ensite production.conf 

5) Restart apache2:

$ sudo systemctl reload apache2

Now when you navigate to http://EXTERNAL_IP/development you will see:

"This is development"

And likewise, when you navigate to http://EXTERNAL_IP/production you will see:

"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? 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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