In this post we will discuss about hosting more than one webisites (domains and subdomains) on single virtual machine instance of Google Cloud Compute Engine using Apache 2.
Before starting make sure that you have Enabled Billing in your project inside Google Cloud Platform Console.
Create VM instance on GCP Compute Engine :
- Just go to GCP Console.
- Now select your project form project chooser dropdown. If you have not created project just create it.
- Now from side-menu Click on “Compute Engine”.
- Now click on “VM Instances” sub-menu and click on “Create Instance”.
- Now fill the Configuration of Virtual Machine. For now select machine type as “f1-micro” and Boot Image as “Ubuntu 16.04 LTS” and check on “Allow HTTTP traffic” and “Allow HTTTPS traffic” under firewall section.
- Now Click on “Create” button.
Install apache 2 server on GCP Compute Engine :
- For host website we will need a server set up on our VM instance. We will be installing apache2 server on our VM intance.
- Navigate to VM Instances in Compute Engine and click on “SSH”.
- You will be redirected to a browser window / tab with a terminal connected to your VM instance.
- Now we will install apache2 by executeing below command on terminal (opened in browser)
- Now create “index.html” file at directory location “/var/www/html/”.
- Now navigate back to VM Intances list page and click on external IP address mentioned repective to your VM name. It will redirect you to on a new tab where you should see the index page we just created.
- At this stage we have successfully hosted a webpage. If we want our page to be accessible using a domain name, rather than IP Address then we will need to point our domain name towards our instance’s IP address.
Reserve Static IP Address for GCP Compute Engine :
- From your Google Cloud Platform dashboard navigate to “Networking > External IP addresses”.
- Now click the down arrow under the “Type” column and select “Static” for the External IP address that is connected to your instance of GCP Compute Engine.
- By reserving a Static IP Address you will not loose your access to website after server outages or restarts. And website IP Address will not change in future.
- Now Go to your repective domain provider account and find DNS settings, edit the “A Record” their and fill Static IP Address that we have reserved for VM Instance. After few moments the website will be accessible via your domain name.
Host Another Websites on Same GCP Compute Engine :
- For Host another websites, you will require to create Virtual Hosts in Apache server.
- Navigate to VM Instances in Compute Engine and go into “SSH”.
- Now in terminal, use below command to naviagate to “sites-available” directory of apache2.
$ cd /etc/apache2/sites-available/
- Now in this directory we will use default.conf file to create two files for two websites. Assume that you have two domains “domain1.com” and “domain2.com”. use below command to create two files.
$ sudo cp 000-default.conf domain1.com.conf
$ sudo cp 000-default.conf domain2.com.conf
- Using “nano” editor edit both files to reflect following content
$ sudo nano domain1.com.conf
$ sudo nano domain2.com.conf
$ ServerName example.com
$ ServerAlias www.example.com
$ ServerAdmin admin@example.com
$ DocumentRoot /var/www/example.com/html
- Now create files and folders for “domain1.com” at right location.
$ cd /var/www/
$ sudo mkdir domain1.com
$ cd domain1.com/
$ sudo mkdir html
$ cd html/
Now create “index.html” inside “html” directory.
- Now create files and folders for “domain2.com” at right location same as above.
$ cd /var/www/
$ sudo mkdir domain2.com
$ cd domain2.com/
$ sudo mkdir html
$ cd html/
Now create “index.html” inside “html” directory.
- Now enable the Configurations for apache2 and restart the server.
$ sudo a2ensite domain1.com
$ sudo a2ensite domain2.com
$ sudo service apache2 restart
- Now we have completed server setup things, As this way you can create as many domains/subdomains as you want. For pointing all your domains/subdomains to Compute Engine VM instance, Go to your repective domain provider account and find DNS settings, and edit the “A Record” their and fill the same Static IP Address that we have previously reserved for VM Instance for all your domains/subdomains configration.
Comments
Post a Comment