To set up a custom domain for your Azure VM, you'll need to configure Azure DNS and update your VM's settings. Here's a step-by-step guide to help you achieve this:
Prerequisites:
- You have an Azure subscription.
- You have created an Azure Virtual Machine (VM) with a public IP address.
- You have a FastAPI application running on the VM.
Step 1: Create an Azure DNS zone
- Go to the Azure portal and navigate to the DNS zones section.
- Click on + New DNS zone.
- Enter a name for your DNS zone (e.g.,
appname.azurewebsites.net).
- Select the subscription and resource group associated with your VM.
- Click Create.
Step 2: Create a DNS record
- In the DNS zone you created, click on + Record set.
- Select A as the record type.
- Enter the following values:
- Name:
@ (leave blank for the root domain)
- Type: A
- TTL: 1 hour (or any other value you prefer)
- Alias: No
- IP address: The public IP address of your Azure VM
- Click OK.
Step 3: Configure Azure VM's public IP address
- Go to the Azure portal and navigate to your VM's settings.
- Click on Networking.
- Click on Public IP address.
- Select the public IP address associated with your VM.
- Click on Configuration.
- Under DNS name label, enter the name of your DNS zone (e.g.,
appname).
- Click Save.
Step 4: Update your FastAPI application
- In your FastAPI application, update the
host parameter in the uvicorn configuration to use the custom domain:
import uvicorn
if __name__ == "__main__":
uvicorn.run("main:app", host="appname.azurewebsites.net", port=8000)
Replace main with the name of your FastAPI application module.
Step 5: Test your custom domain
- Go to
http://appname.azurewebsites.net:8000 in your web browser.
- You should see your FastAPI application running with the custom domain.
That's it! Your Azure VM should now be accessible using the custom domain appname.azurewebsites.net instead of the public IP address.