To Create A Virtual Machine - With Web Page
To Create A Virtual Machine - With Web Page
To Create A Virtual Machine - With Web Page
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.
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
Here's the Azure CLI command that uses the above user data to configure the VM:
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>
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:
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.
bash
Copy code
az vm show --resource-group test --name myWebVM --show-details --query
[publicIps] --output tsv
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).
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
```
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
```
This command opens the file in the `nano` text editor with **sudo** privileges, allowing you to make
changes to the HTML content.
```html
<html>
<body>
</body>
</html>
```
- After editing the file, press **Ctrl+O** to write (save) the changes.
If needed, you can revert the ownership of the file back to `www-data` (the default Apache user) after
editing:
```bash
```
If the web page doesn't immediately reflect the changes, you might want to restart the Apache service:
```bash
```
Now, when you navigate to the webpage (e.g., `http://<VM_Public_IP>`), you should see the updated
content.