3.1. Create a Web Server#
Note
In Cloud Shell using the vscode terminal you have to set your project every time. Run this command:
$ gcloud config set project your-project-id
Change your-project-id to the actual ID of your GCP project.
Step 1: Create a VM#
Run these two commands to create a new VM and configure the firewall to let browsers connect over the internet:
$ gcloud compute instances create my-debian-vm \
--machine-type=e2-small \
--image-family=debian-12 \
--image-project=debian-cloud \
--zone=us-central1-a \
--tags=http-server
$ gcloud compute firewall-rules create allow-http-https-on-tagged-servers \
--network=default \
--allow=tcp:80,tcp:443 \
--target-tags=http-server \
--source-ranges=0.0.0.0/0 \
--description="Allow incoming HTTP and HTTPS traffic to servers with the http-server tag" \
--direction=INGRESS
Note
Make a note of the external IP address of your vm.
Step 3: Install Apache2#
On your new VM install the Apache web server. To do that you have to first SSH into the machine:
$ gcloud compute ssh my-debian-vm --zone=us-central1-a
On the machine run the following commands to install Apache using the apt
package manager:
user@my-debian-vm:~$ sudo apt update
user@my-debian-vm:~$ sudo apt install apache2
Step 4: View and Change Your Page#
Using the browser on your desktop visit the eternal IP address of your VM. You might need to type the complete URL like this:
http://xx.xx.xx.x/
Replace the `xx.xx.xx.xx with your VM’s external IP address. The page you are looking at is written in HTML and CSS. You can view its source code using the context menu on your browser (Right Click -> View Source).
The HTML is in a file on your VM. You can view and edit the file using nano:
user@my-debian-vm:~$ sudo nano /var/www/html/index.html
Notice that if you save changes to the file that they are present when you reload the page on your browser. If you create a file, that file becomes available by it’s URL:
user@my-debian-vm:~$ echo 'Hello World' | sudo tee /var/www/html/mypage.html
The new file is located at:
http://xx.xx.xx.x/mypage.html
URL’s are like paths, they can have arbitrary depth. Create a subdirectory in
the /var/www/html directory and put a file in it like this:
user@my-debian-vm:~$ sudo mkdir /var/www/html/cis-92
user@my-debian-vm:~$ echo 'Hello CIS-92' | sudo tee /var/www/html/cis-92/index.html
The new file is located at:
http://xx.xx.xx.x/cis-92/index.html
When there’s no file part of a path the filename index.html is the default,
so your new file is also located in:
http://xx.xx.xx.x/cis-92/
It’s important so see how the URL how the client tells the server what resource it wants to receive. URLs are like file paths, but there’s a bit more to the story. You’ll see that in the next lab.
Step 5: Use curl#
The curl command is a simplified web browser that’s used to retrieve a single
page. It’s incredibly handy when you want to debug a web page or application
because it’s simpler than the browser. By default curl prints the content of
a web page to the screen. Use curl to browse your site:
$ curl http://xx.xx.xx.xx/
Replace the xx.xx.xx.xx with your VM’s external IP address.
Cleanup#
These commands delete your VM and the firewall rules you created in the first part of this lab:
$ gcloud compute instances delete my-debian-vm --zone=us-central1-a
$ gcloud compute firewall-rules delete allow-http-https-on-tagged-servers
Warning
Failure to delete the VM after this lab will cost you money!