Scaled VM: Part 3. Web hosting

Valentyn Shybanov
3 min readSep 23, 2017

--

How to setup and configure web hosting in “Scaled VM” scenario

In previous posts I’ve described the need of simple auto-scaling solution with shared file system and how to create that setup in Google Cloud Platform.

In this post I’ll describe how to install Apache2 and PHP in this setup and configure Load Balancer to handle requests to our VMs.

[Part 1: introduction] [Part 2: FS/VM] [Part 3: Web] [Part 4: Load Balancer] [Quick walkthrough]

Getting Apache2 running

Now as we have VMs with shared FS, it would be good to have web server running and serving files. I’ve decided to use Apache2, but you can easily use nginx or anything else. So, let’s start.

Unfortunately, the process of installing additional software is not the same, as you got used with “normal VMs”. Just because instead of VM with own persistent storage, we have VMs that can be created or removed by need. That is why we need to provide the way, how we can have new VM in the shape we need.

Easiest way is to use “Management” script in the same way how we added NFS support to our VMs. So instead of logging into VM, you need to open “Instance Templates” in console, select the last one you’re using, and press “Copy” (so you’ll have all settings, including management script in place). Expend “Management, disks, networking, SSH keys” and add following lines to “Startup script”:

sudo DEBIAN_FRONTEND=noninteractive apt-get -y install mc apache2 mysql-client 
sudo DEBIAN_FRONTEND=noninteractive apt-get -y install php7.0 php7.0-mysql apache2-mod-php7.0

(Note: I am using Debian-style of installing packages. If you used another VM, please change script)

As our new VMs will expose HTTP, don’t forget to check “Allow HTTP traffic”. (Small Note: probably, you’ll have also modify “default” network and add firewall rule to allow http traffic. For me, it’s kind of bug that checking option above does not create firewall rule. To add that rule, go to “Firewall rules” in the console and add new rule for HTTP — take other rules as example)

Save the new template.

Now, to apply new changes, you need to perform “Rolling update”: Go to “instance groups”, select your group and click “Rolling update”, where you can find new template you’ve just created.

Now wait until old VMs are shut down and new one is spawned (you can see, that new one is using new template).

Next step is quite tricky but important: as configuration will be shared, you need to copy just installed Apache2 configuration to shared FS. To do so, SSH into Instance Group’s VM (not filer VM — there we don’t have Apache installed) and run:

$ mkdir /mnt/etc
$ cp -r /etc/apache2 /mnt/etc

So you’ll hare a copy of default Apache2 setting on your shared FS. For testing I’ve created www folder in /mnt and created some testing file there:

$ mkdir /mnt/wwww
$ echo "Hello! <?php phpinfo(); ?>" > /mnt/www/index.php

And changed in/mnt/etc/apache2/sites-available/000-default.conf location from where Apache serve files:

DocumentRoot /mnt/www

And the last step: is to modify “Startup script” (Do you remember? Copy last instance template into new one and navigate to “Management, disks, …”) by appending these lines:

sudo mv /etc/apache2 /etc/apache2.bak && sudo ln -s /mnt/etc/apache2 /etc/apache2
sudo service apache2 restart

Save template and perform “Rolling update” of instance group to use new template. After that, if you’ve done right, you should be able to click on “External IP” link of VM Instance under instance group and see “Hello! …” with information about your PHP installation.

In next part I’ll describe how to configure Load Balancer so you can configure DNS to access your auto scaling configuration.

--

--