Deployment: Deploying a NestJS Application
Deploying a NestJS application to a cloud platform (e.g., AWS, Google Cloud, Azure) or a containerized environment (e.g., Docker, Kubernetes).
Deploying NestJS to Azure
Explanation: Deploying to Azure (Example)
Azure offers a variety of services for deploying applications, each with its own strengths and weaknesses. This example outlines the general process and provides a starting point. Choosing the right service depends on your application's needs, budget, and desired level of control. Here are a few common options:
- Azure App Service: A Platform-as-a-Service (PaaS) offering. Easy to use and manage, ideal for simpler deployments and scaling. Azure handles the underlying infrastructure, so you focus on your code.
- Azure Virtual Machines (VMs): Infrastructure-as-a-Service (IaaS). Provides complete control over the operating system and environment. Suitable for complex applications or when specific configurations are required. Requires more management on your part.
- Azure Kubernetes Service (AKS): A managed Kubernetes service. For containerized applications that require orchestration and scaling. Offers high availability and scalability but has a steeper learning curve.
For example, a simple API might be best suited for App Service. A more complex application requiring specific OS configurations might use a VM. And a microservices architecture might leverage AKS. The steps below will focus on deploying to Azure App Service, due to its ease of use.
Step-by-Step Guide: Deploying a NestJS Application to Azure App Service
Prerequisites:
- An Azure Account with an active subscription.
- Node.js and npm installed locally.
- Azure CLI installed and configured.
- A NestJS application (created using
nest new project-name
). - Git (optional, but recommended for source control and deployment).
Step 1: Create a NestJS Application (If you don't have one)
nest new my-nestjs-app
cd my-nestjs-app
Run your app locally to make sure it works:
npm run start:dev
Step 2: Prepare your NestJS Application for Deployment
Ensure your application is configured to listen on the port specified by the PORT
environment variable. Azure App Service will set this variable.
In your src/main.ts
file, update the bootstrap
function:
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
const port = process.env.PORT || 3000; // Use environment variable or default to 3000
await app.listen(port);
console.log(`Application is running on: ${await app.getUrl()}`);
}
bootstrap();
Also, you will need to build the application for production. Add a build script in package.json
"scripts": {
"build": "nest build"
}
Step 3: Create an Azure App Service
You can create an App Service using the Azure portal or the Azure CLI. Here, we'll use the CLI:
- Login to Azure:
- Set your active subscription (if you have multiple):
- Create a resource group (if you don't have one):
- Create an App Service Plan (defines the pricing tier and region):
- Create the App Service:
az login
az account set --subscription "Your Subscription Name"
az group create --name myResourceGroup --location eastus
az appservice plan create --name myAppServicePlan --resource-group myResourceGroup --sku B1 --is-linux
Note: B1
is a basic tier and may not be suitable for production environments. Consider higher tiers for better performance and features. --is-linux
indicates that we'll deploy on a Linux environment.
az webapp create --resource-group myResourceGroup --plan myAppServicePlan --name my-nestjs-app
Step 4: Configure Deployment Settings
Azure App Service supports several deployment methods, including Git, ZIP deploy, and Azure Pipelines. Here, we'll use ZIP deploy, which is a simple way to deploy your application.
- Set the Node.js version for your App Service (important for runtime compatibility):
- Configure the startup command (to start the NestJS application):
az webapp config appsettings set --resource-group myResourceGroup --name my-nestjs-app --settings WEBSITES_NODE_DEFAULT_VERSION="18.LTS"
Note: Replace 18.LTS
with a supported Node.js version. Check Azure documentation for the latest supported versions.
az webapp config appsettings set --resource-group myResourceGroup --name my-nestjs-app --settings PM2_STARTUP_FILE="dist/main.js"
This tells App Service to use PM2 to start the dist/main.js
file, which is the compiled entry point of your NestJS application.
Step 5: Deploy your NestJS Application
- Build your NestJS application:
- Zip the contents of the
dist
folder: - Deploy the ZIP file to Azure App Service:
npm run build
zip -r dist.zip dist
az webapp deployment source config-zip --resource-group myResourceGroup --name my-nestjs-app --src dist.zip
Step 6: Verify the Deployment
- Open your web browser and navigate to the URL of your App Service (you can find this in the Azure portal or using the CLI):
- Access your application's endpoints to ensure everything is working correctly.
- Check the application logs in the Azure portal or using the CLI to troubleshoot any issues.
az webapp show --resource-group myResourceGroup --name my-nestjs-app --query defaultHostName --output tsv
Troubleshooting
- Deployment errors: Check the deployment logs in the Azure portal or using the CLI for error messages.
- Application not starting: Ensure the startup command is correct and that the necessary dependencies are installed.
- 500 errors: Check the application logs for runtime errors. Enable logging in your NestJS application for more detailed information.
Next Steps
- Configure CI/CD pipelines for automated deployments using Azure DevOps or GitHub Actions.
- Implement logging and monitoring using Azure Monitor.
- Configure custom domains and SSL certificates.
- Scale your App Service to handle increased traffic.