DEV Community

Cover image for Deploying a Complete ASP.NET Framework Web Application on Amazon EC2
Danh Hoàng Hiếu Nghị
Danh Hoàng Hiếu Nghị

Posted on

Deploying a Complete ASP.NET Framework Web Application on Amazon EC2

Deploying a web application can be a complex process, but using Amazon EC2 (Elastic Compute Cloud) to host an ASP.NET Framework application simplifies many aspects of deployment. In this comprehensive guide, we will cover the step-by-step process for deploying a traditional ASP.NET Framework web application on an EC2 instance. This includes setting up the EC2 instance, configuring IIS (Internet Information Services), deploying the application, and ensuring that it runs smoothly.

Table of Contents

  1. Introduction
  2. Prerequisites
  3. Creating an Amazon EC2 Instance
  4. Connecting to the EC2 Instance
  5. Installing the ASP.NET Framework
  6. Configuring IIS
  7. Deploying the ASP.NET Application
  8. Configuring Database Access
  9. Testing the Application
  10. Monitoring and Maintenance
  11. Conclusion
  12. References

Introduction

ASP.NET Framework applications are widely used for building dynamic web applications and services. While ASP.NET Core is gaining popularity, many organizations still use the traditional ASP.NET Framework. In this guide, we will detail how to deploy a complete ASP.NET Framework web application on an Amazon EC2 instance, providing insights into best practices and troubleshooting tips along the way.

Prerequisites

Before we start, make sure you have the following:

  • AWS Account: Sign up for an AWS account if you don't have one.
  • ASP.NET Framework Web Application: Have your ASP.NET Framework web application ready for deployment.
  • Familiarity with Windows Server: Basic knowledge of Windows Server and IIS will be helpful.
  • Remote Desktop Protocol (RDP): You will need an RDP client to connect to the EC2 instance.

Creating an Amazon EC2 Instance

Choosing the Right AMI

  1. Log in to the AWS Management Console: Go to the AWS Management Console.
  2. Navigate to EC2 Dashboard: Search for EC2 in the services and select it.
  3. Launch Instance:
    • Click on "Launch Instance."
    • Under the Amazon Machine Image (AMI) tab, select the Microsoft Windows Server AMI (for example, Windows Server 2019 Base).

Instance Type Selection

  1. Choose an Instance Type:
    • Select an instance type based on your application’s requirements. For small to medium applications, the t2.micro instance type is often suitable and is eligible for the free tier.
    • Click on "Next: Configure Instance Details."

Security Group Configuration

  1. Configure Security Group:
    • You can create a new security group or select an existing one.
    • Make sure to allow inbound traffic on the following ports:
      • RDP (port 3389): For remote access to your server.
      • HTTP (port 80): For web traffic.
      • HTTPS (port 443): If your application uses SSL.

Key Pair Creation

  1. Create a Key Pair:

    • You will need a key pair to securely connect to your instance.
    • Create a new key pair, download the .pem file, and save it securely, as it will not be available for download later.
  2. Review and Launch: After configuring the instance details and security group, review your settings and click on "Launch."

Connecting to the EC2 Instance

  1. Find Your Instance: Once the instance is running, navigate to the EC2 dashboard and select your instance.
  2. Get the Public DNS or IP Address: Copy the public DNS name or the public IP address of the instance.
  3. Connect via RDP:
    • Open the Remote Desktop Connection application on your local machine.
    • Click on "Connect" and enter the public DNS or IP address.
    • Use the downloaded key pair to retrieve the password:
      • Right-click on your instance in the EC2 dashboard, and select "Get Windows Password."
      • Upload your .pem file, decrypt the password, and use it to log in.

Installing the ASP.NET Framework

Once connected to your EC2 instance via RDP:

  1. Open Server Manager: The Server Manager should launch automatically upon login. If not, open it from the Start menu.
  2. Add Roles and Features:
    • Click on “Add roles and features.”
    • Click “Next” until you reach the Server Roles page.
  3. Select Web Server (IIS):

    • Check the box next to "Web Server (IIS)" and click “Next.”
    • Continue clicking “Next” until you reach the Features page. Here, you can add .NET Framework 4.5 (or higher) by checking the box.
  4. Install the Framework: Follow the prompts to install IIS and the .NET Framework.

Configuring IIS

Installing IIS

  1. Check IIS Installation: After installation, you can verify if IIS is installed by typing http://localhost in your browser on the EC2 instance. You should see the default IIS welcome page.

Setting Up a New Website

  1. Open IIS Manager:
    • Search for IIS Manager in the Start menu and open it.
  2. Add a New Website:

    • In the Connections pane, right-click on Sites and select Add Website.
    • Site Name: Enter a name for your website (e.g., MyAspNetApp).
    • Physical Path: Set the physical path to the directory where your ASP.NET application files will reside (you can create a folder in C:\inetpub\wwwroot).
    • Binding: For the binding, use http and leave the port as 80.
  3. Start the Website: Click on OK to create the website and ensure it is started.

Deploying the ASP.NET Application

Preparing the Application

  1. Build the Application: In Visual Studio, ensure your ASP.NET application is in the Release configuration.
    • Build the application by selecting Build > Build Solution.
  2. Publish the Application:
    • Right-click on your project in Visual Studio and select Publish.
    • Choose Folder as your publish target and specify a location (e.g., C:\PublishedApp).

Copying Files to the Server

  1. Copy Published Files:

    • Use RDP to access your EC2 instance, and navigate to the folder where you published your application (e.g., C:\PublishedApp).
    • Copy the published files to the physical path you configured for your website in IIS (e.g., C:\inetpub\wwwroot\MyAspNetApp).
  2. Adjust Permissions:

    • Right-click the folder where you copied your files, select Properties, and go to the Security tab.
    • Click Edit and add the user IIS_IUSRS with Read & Execute permissions.

Configuring Database Access

If your application uses a database, you will need to ensure that it is accessible from your EC2 instance.

Setting Up SQL Server

  1. Install SQL Server: If you don’t have a database server, you can install SQL Server Express on your EC2 instance.

    • Download the installer from the Microsoft SQL Server Express page.
    • Follow the installation instructions, selecting default settings where appropriate.
  2. Configure SQL Server:

    • Open SQL Server Management Studio (SSMS) and connect to your SQL Server instance.
    • Create a new database and import your existing data if necessary.

Configuring Connection Strings

  1. Modify Connection Strings:
    • In your ASP.NET application, locate the web.config file and update the connection string to point to your SQL Server instance.
    • For example:
   <connectionStrings>
       <add name="DefaultConnection" 
            connectionString="Server=localhost;Database=YourDatabaseName;User Id=YourUsername;Password=YourPassword;"
            providerName="System.Data.SqlClient" />
   </connectionStrings>
Enter fullscreen mode Exit fullscreen mode

Testing the Application

  1. Browse to Your Application: Open a

web browser and enter the public DNS or IP address of your EC2 instance. You should see your ASP.NET application running.

  1. Check Database Connectivity: Perform some operations that involve database access to ensure everything is functioning correctly.

Monitoring and Maintenance

Once your application is running, you should regularly monitor its performance and make necessary adjustments:

  1. Enable Logging: Implement logging within your application to capture errors and performance metrics.
  2. Monitor Resource Utilization: Use the CloudWatch service in AWS to monitor CPU, memory, and disk I/O of your EC2 instance.
  3. Regular Backups: Schedule regular backups of your database and application files to avoid data loss.

Conclusion

Deploying an ASP.NET Framework web application on Amazon EC2 can be straightforward if you follow the steps outlined in this guide. By leveraging AWS services, you can scale your application as needed and ensure high availability. Remember to keep your application updated and monitor its performance to ensure a seamless experience for your users.

References

Top comments (0)