In this tutorial we will publish static website on google cloud for free. We will use f1 micro instance on google compute engine to get free hosting. Google provides 744 free hours per month for the f1 compute engine instance which will be same as one month of free hosting. We assume that you have working google account and have signed up for google cloud. Please follow these steps to host your static site on Google VM.
Create Compute Engine Instance
Open google cloud console and from menu select compute engine. Create new linux vm instance according to your requirement. For static website a f1 micro instance would suffice.
Enable HTTP Port
For your VM instance enable HTTP Port 80
. This will create firewall rule for your VM by opening HTTP Port so that web server can listen and serve web traffic. You can enable HTTP while creating new instance or edit current instance like below.
Install Apache Web Server
Since you have configured linux VM instance you can install apache from command line. But to have command line access you should establish ssh connectivity between your machine and cloud VM. This will come handy when you want to do file transfer from you machine to VM.
We have spun compute instance with Debian OS which have apt
packet manager. We will install apache with following command. Get external ip address of your VM to start ssh connection. (93.184.216.34)
techmonger@ubuntu:~$ ssh 93.184.216.34
techmonger@google-vm:~$ sudo apt install apache2
Visit ip address from web browser and you should see apache home page. This will ensure that apache is running on your vm.
SFTP Connection
To upload website content you will need to do file transfer from your machine to cloud vm. For this read how you can configure sftp connection with your VM and do file transfer with ftp client. If you have already configured SSH connection, sftp connection establishment will be relatively easy.
Upload Static HTML Content
With above SFTP connection you can start uploading your website content inside root directory of apache webserver. By default apache root which holds html pages is /var/www/html
. You should put your website content in this directory and also replace default index.html
with your code.
Once all website content is stored inside root directory of apache webserver, visit the external ip of vm using browser and you shall see your website up and running on vm.
Map IP Address with Domain
To get your website running on VM you should open domain configuration at your domain service provider. Add new entry in your domain for A Record. This A Record value should be mapped to your vm's external static ip address.
Once DNS has been propagated you shall see your website running on your domain. DNS propagation may take some time. Check your domain service provider if you need help with this step.
Debugging
If even after following above instructions your website is not coming up then then check for following things.
- Check Status of Apache Webserver
sudo service apache2 status
If Apache is not running then start it with following command.
sudo service apache2 start
- Check if website content is uploaded correctly inside following directory.
ls -l /var/www/html/
If website content is not present then re upload it. Also make sure it is uploaded via root user if non root user does not have permission to upload.
- Transfer content inside your home directory using SFTP. (
/home/username/website/
) - Start SSH Connection with your VM
- Sudo root user
sudo su -
- Copy content as root user inside
/var/www/html/
cp -r /username/website/* /var/www/html/
- Transfer content inside your home directory using SFTP. (
Security
With above setup your VM's IP address can easily be available to the outside world. Hence your website can become victim for DDOS attack. To mitigate this consider putting your website behind cloudflare. Also if your website is allowing any user input such as user login or contact form then use https connection instead of http.
Conclusion
By following above steps we have hosted static website on google vm using apache webserver. We have used basic compute engine f1 instance for this website however you can increase computation resources like memory, processor or disk space according to you need in future.