Connect, configure and test a web server

This article will show you how to connect to the VM built previously in the “Create a Linux VM in Azure” article, how to install and configure the server with the necessary software to server a web site and finally how to test it works.

Connecting to a Linux VM

Log in to Azure Portal and select Virtual machines from the list of resources on the left.

When the list of VMs has loaded, click the name of the VM created in the previous article ( “Create a Linux VM in Azure“).

When the details have loaded, check the status of the server, it is likely in a stopped state.

Click start if the VM is stopped.

After a few minutes, the VM will be started and the status will have changed to Running.

We can now connect to it by clicking the Connect button.

A connection pane will be shown on the right of the screen showing some server details. Click the copy icon next to the Login using the local VM account text box. We will use the string to establish an SSH connection to the VM via the Cloud Shell.

If your Cloud Shell is not open, then click the icon at the top of the screen to open it again. Paste the SSH connection string into the Cloud Shell and press enter.

When prompted, enter the passphrase used when the SSH key pair was created.

Install the web server

With Ubuntu, we use a package manager called APT to install the required components so that we can use the VM as a web server. The first thing we do is update the current packages, when you connected you may have noticed that there were updates available…

First thing to do is to update the distribution:

sudo apt-get dist-upgrade

If there are any packages to update, then they apt will take care of it now – it’s always a good idea to make sure you servers are up to date with the latest patches and updates.

The following command will obtain and install the package updates and then install the required packages to install what is called a LAMP stack. A LAMP stack consists of the required applications to run a web server, these are:

  • Linux – the operating system (we already have Ubuntu)
  • Apache – the web server
  • MySQL – a database server
  • PHP – a programming language

sudo apt-get update && sudo apt install lamp-server^

sudo runs the command with admin privileges and the double ampersand (&&) chains the commands so that the second command only runs if the first run succeeds.

This download and install process will take a moment or two and when done, will leave you back at the prompt.

Memory problem with MySQL

If the VM you created previously is insufficiently resourced in terms of memory then you will likely see an error like:

Errors  were encountered while processing:
mysql-server5.7
mysql-server

On checking the mysql logs, there will likely be some messages to indicate this:

more /var/log/mysql/error.log

[Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
[ERROR] InnoDB: mmap(137428992 bytes) failed; errno 12
[ERROR] InnoDB: Cannot allocate memory for the buffer pool
[ERROR] InnoDB: Plugin initialization aborted with error Generic error
[ERROR] Plugin ‘InnoDB’ init function returned error.
[ERROR] Plugin ‘InnoDB’ registration as a STORAGE ENGINE failed.
[ERROR] Failed to initialize builtin plugins.
[ERROR] Aborting

Verify the installations

The installations of the software can now be verified…

apache2 -v

Server version: Apache/2.4.29 (Ubuntu)
Server built:   2019-04-03T13:22:37

mysql -V

mysql  Ver 14.14 Distrib 5.7.25, for Linux (x86_64) using  EditLine wrapper

Note, the upper case V when checking the mysql version.

php -v

PHP 7.2.15-0ubuntu0.18.04.2 (cli) (built: Mar 22 2019 17:05:14) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.15-0ubuntu0.18.04.2, Copyright (c) 1999-2018, by Zend Technologies

Open port 80 for http connections and test

In Azure Portal, click Networking – this is found under Settings.

Click the Add inbound port rule.

Set the following configuration:

  • destination port to 80,
  • protocol to TCP
  • name to http

Click Add.

On the Outbound port rules there is already a rule in place for AllowInternetOutBound already so no new rule to allow traffic on port 80 to pass out from the server is required.

At the top of the Networking details screen, copy to the public IP.

Alternatively, the public IP address is also available from the VM overview page at the top-right corner.

Paste this into a web browser, press enter and you should see the Apache2 Ubuntu Default Page.

If you intend on continuing use of this VM as a web server, then you will need to do as the Apache2 default page says: “You should replace this file (located at /var/www/html/index.html) before continuing to operate your HTTP server.”

Well done on following the articles to this point, you have learnt how to build an Ubuntu VM, generate a SSH keypair, connect to the VM and configure it as a web server.


Posted

in

by

Comments

2 responses to “Connect, configure and test a web server”

  1. Illia Kyselov Avatar

    Thanks for the detailed and useful information. To be honest, I’ve been thinking for a long time to switch to Azur from my usual hosting, as it upsets me a lot))) Perhaps I will decide in the near future.
    Thanks!

    1. John Avatar

      You’re very welcome! Glad you found it useful.

      I’ve recently been thinking about moving my blogs across from various hosting companies to Azure but I need to work out the cost yet.

      Let me know if you do decide to move to Azure 👍

Leave a Reply

Your email address will not be published. Required fields are marked *