To Create A Virtual Machine - With Web Page

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

To create a virtual machine (VM) in Azure and automatically set up a simple web page using

cloud-init (user data), you can include a custom script that runs when the VM boots for the first
time. This can install a web server (like Apache), create a simple HTML page, and start the web
server.

Here's the bash command to create a VM with the necessary user data to set up and publish a
simple web page.

Step 1: Prepare the cloud-init (user data) script

This cloud-init script installs Apache and creates a simple "Hello World" webpage.

bash
Copy code
#!/bin/bash
# Install Apache
sudo apt update
sudo apt install -y apache2
# Create a simple HTML file
echo "<html><body><h1>Hello from ISMT College!</h1></body></html>" | sudo
tee /var/www/html/index.html
# Start Apache
sudo systemctl enable apache2
sudo systemctl start apache2

Step 2: Create the VM with the cloud-init script

Here's the Azure CLI command that uses the above user data to configure the VM:

#create resource group

az group create --name test --location uksouth

#list all running/ created resource

az resource list --output table

# list resource of specific resource group

az resource list --resource-group <resource-group-name> --output table

az resource list --resource-group test --output table

1. Using Azure CLI

You can use the following command to create a Network Security Group:

bash
Copy code
az network nsg create --resource-group <resource-group-name>
--name <nsg-name> --location <location>

 Replace <resource-group-name> with the name of your resource group.


 Replace <nsg-name> with the desired name for your Network Security Group.
 Replace <location> with the Azure region where you want the NSG to be created (e.g.,
eastus, westus, etc.).

Example:
bash
Copy code
az network nsg create --resource-group MyResourceGroup --name
MyNSG --location eastus

This command creates a Network Security Group named MyNSG in the MyResourceGroup
within the East US region.

bash
Copy code
az vm create \
--resource-group test \
--name myWebVM \
--location uksouth \
--size Standard_B1s \
--image Ubuntu2204 \
--admin-username azureuser \
--admin-password Niraj98452#### \
--authentication-type password \
--enable-secure-boot true \
--enable-vtpm true \
--nsg sg_test \
--custom-data user_data.txt \
--no-wait

Command Breakdown:

 --resource-group test: Uses the existing test resource group.


 --name myWebVM: The VM name is myWebVM.
 --location uksouth: The region is UK South.
 --size Standard_B1s: Specifies the VM size.
 --image UbuntuLTS: Uses the latest Ubuntu LTS image.
 --admin-username azureuser: Sets azureuser as the admin username.
 --authentication-type ssh: Uses SSH for authentication.
 --generate-ssh-keys: Automatically generates SSH keys if you don’t have them.
 --custom-data cloud-init-web.txt: Uses the cloud-init-web.txt file for the user
data.
Step 3: Save and Use the Cloud-init Script

1. Create a file named cloud-init-web.txt.


2. Paste the following user data script into the file:

bash
Copy code
#!/bin/bash
# Install Apache
sudo apt update
sudo apt install -y apache2
# Create a simple HTML file
echo "<html><body><h1>Hello from Azure VM!</h1></body></html>" | sudo tee
/var/www/html/index.html
# Start Apache
sudo systemctl enable apache2
sudo systemctl start apache2

3. Run the az vm create command, specifying the path to your cloud-init-web.txt file.

Verify the Setup:

Once the VM is created:

1. Retrieve the public IP address of the VM:

bash
Copy code
az vm show --resource-group test --name myWebVM --show-details --query
[publicIps] --output tsv

2. Open a web browser and go to http://<Public_IP>.

You should see the message: "Hello from Azure VM!"


Command to Check File Ownership
bash
Copy code
ls -l /path/to/file

Example

If you want to check the ownership of the file /var/www/html/index.html, you would use:

bash
Copy code
ls -l /var/www/html/index.html

To modify an HTML file with write privileges in Linux, you'll first need to ensure that the user has the
correct permissions. Here's a basic approach using **sudo** to ensure you can edit the HTML file
(typically located in `/var/www/html/` on an Apache server).

### Step 1: Change the File Ownership (Optional)

If you're logged in as a non-root user (like `azureuser`), you might need to modify the permissions or
change the file ownership to gain write access.

```bash

sudo chown azureuser /var/www/html/index.html

```

This command changes the ownership of the `index.html` file to your user (`azureuser`), allowing you to
edit the file directly.

### Step 2: Modify the File Using a Text Editor (e.g., `nano`)

```bash

sudo nano /var/www/html/index.html

```
This command opens the file in the `nano` text editor with **sudo** privileges, allowing you to make
changes to the HTML content.

For example, you can change the content to:

```html

<html>

<body>

<h1>Updated content from Azure VM!</h1>

</body>

</html>

```

### Step 3: Save the Changes

- After editing the file, press **Ctrl+O** to write (save) the changes.

- Then press **Ctrl+X** to exit `nano`.

### Step 4: Verify Ownership (Optional)

If needed, you can revert the ownership of the file back to `www-data` (the default Apache user) after
editing:

```bash

sudo chown www-data /var/www/html/index.html

```

### Step 5: Restart Apache (Optional)

If the web page doesn't immediately reflect the changes, you might want to restart the Apache service:
```bash

sudo systemctl restart apache2

```

Now, when you navigate to the webpage (e.g., `http://<VM_Public_IP>`), you should see the updated
content.

az group delete --name <resource-group-name> --yes --no-wait


az group delete --name MyResourceGroup --yes --no-wait
az group delete --name SubnetNAT --yes --no-wait
az group list --output table

You might also like