Setting up a public-facing web server in Azure using a virtual machine offers flexibility and control over your web hosting environment. This tutorial walks you through creating an Azure VM with Apache installed, explaining not just how but why each step is essential in configuring a static web content server accessible from the internet.
Prerequisites
Before we begin, ensure you have:
- An Azure account – This gives you access to Azure’s cloud services.
- Basic familiarity with the Azure Portal – Understanding the interface will help you navigate and manage your resources efficiently.
- Basic Linux command line skills – We’ll use a Linux-based VM, so familiarity with common commands is crucial.
Creating the Azure Virtual Machine
Virtual Machines in Azure provide a flexible, scalable environment for hosting web applications. Here’s how to set one up:
1. Log into the Azure portal – This is your central hub for managing all Azure resources.
2. Navigate to Virtual Machines and click Create – This starts the VM creation wizard.
3. Configure the VM:
- Select or create a Resource Group – This helps organize related Azure resources for easier management.
- Provide a VM name – Choose a descriptive name to quickly identify this VM later.
- Choose your region – Select a data center close to your target audience for better performance.
- Select an image (e.g. Ubuntu Server) – This determines the operating system. Ubuntu is popular for web servers due to its stability and extensive package repository.
- Choose an appropriate VM size – Balance cost with performance needs. A basic tier is often sufficient for a simple web server.
- Provide a username and password for SSH – This creates your admin account for securely accessing the VM.
- Open port 22 for SSH access allows you to remotely connect to and manage your VM.
5. Review and create the VM – Double-check all settings before finalizing the creation.
Installing the Web Server
We’ll use Apache, a popular and robust web server, to host our content. Here’s how to set it up:
1. SSH into the VM:
ssh username@vm-ip-address
This establishes a secure connection to your VM, allowing you to run commands as if you were physically at the machine.
2. Update the system and install Apache:
sudo apt update -y
sudo apt upgrade -y
sudo apt install apache2 -y
These commands update your system’s package list, upgrade all installed packages to their latest versions, and install Apache. Keeping your system updated is crucial for security and performance.
Configuring Network Access
By default, Azure VMs are locked down for security. We need to explicitly allow web traffic:
1. In the Azure portal, search for “Network security groups” – These act as a firewall for your VM.
2. Find the NSG associated with your VM – Each VM has an associated NSG that controls its network access.
3. Add a new inbound security rule:
- Set service to HTTP – This allows web traffic.
- Set port to 80 – The default port for HTTP traffic.
- Set action to Allow – This permits the traffic to reach your VM.
4. Save the new rule – This applies the changes and opens your web server to the internet.
Customizing Your Web Content
Now that your web server is accessible let’s personalize the content:
1. SSH back into the VM – We must access the file system to modify the web content.
2. Navigate to the web root:
cd /var/www/html
This directory is where Apache looks for files to serve. Any files placed here will be accessible via the web.
3. Edit the index.html file:
sudo nano index.html
We use ‘sudo’ because the root user owns this file. ‘ nano is a simple text editor.
4. Add your custom HTML content – This is where you craft the actual web page visitors will see.
5. Save the file and exit – In nano, you can do this by pressing CTRL+X, Y, then Enter.
6. Open a browser and navigate to the public IP of your VM to ensure the web server is running. You will see that the web server is now working.
Conclusion
You now have a public-facing web server on Azure using a virtual machine. This setup gives you full control over your hosting environment, allowing for extensive customization and scalability. Remember to properly secure your VM and web server in a production environment by implementing HTTPS, keeping software updated, and following security best practices.
For more straightforward static site hosting, consider Azure Web Apps, which abstracts away much of the server management. For high-traffic sites, explore Azure CDN to improve the performance of your static content by caching it closer to your users.