In the modern development landscape, versatility and efficiency in deploying applications are paramount. This is particularly true when working with .NET Core, Microsoft’s cross-platform framework, which has seen significant adoption for building a wide range of applications. One common scenario involves running multiple .NET Core applications on the same server. This guide provides a comprehensive walkthrough on how to run multiple .NET Core applications side-by-side on a single Ubuntu instance without relying on Docker, covering everything from installation to configuration and deployment.
Introduction
Docker and containerization offer powerful ways to isolate and deploy applications. However, there are scenarios where running applications directly on the host system is preferred, whether for simplicity, resource conservation, or specific performance requirements. This guide focuses on Ubuntu as the host system, given its popularity and support within the .NET Core ecosystem.
Prerequisites
- Ubuntu server (18.04 LTS or later recommended)
- Basic command-line interface knowledge
- .NET Core applications targeting different versions (e.g., 3.1 and 6.0)
Step 1: Install .NET SDKs
.NET Core supports side-by-side installation of different runtime versions, the SDKs for the specific versions of .NET Core that your applications target. This enables the development and execution of applications for those versions on your Ubuntu server.
- Update the package index and install prerequisite packages to ensure your system can access Microsoft’s package repository:
wget https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
sudo apt update
sudo apt install -y apt-transport-https
sudo apt update
- Install the .NET Core SDKs for the versions your applications require. For instance, to install .NET Core 3.1, .NET 6 and .NET 8 SDKs:
sudo apt install -y dotnet-sdk-3.1
sudo apt install -y dotnet-sdk-6.0
sudo apt install -y dotnet-sdk-8.0
Step 2: Configure Each Application
To run multiple applications on the same server, you must ensure they are configured to listen on different ports to avoid port conflicts.
- On the first application, edit the appsettings.json or appsettings.Production.json file to specify a unique port:
{ "Kestrel": { "Endpoints": { "Http": { "Url": "http://localhost:5000" } } } }
- For the second application, adjust its configuration similarly to ensure it listens on a different port, such as 5001:
{ "Kestrel": { "Endpoints": { "Http": { "Url": "http://localhost:5001" } } } }
Step 3: Run the Applications
Navigate to the directory of each application and use the dotnet run command to start them, ensuring they’re running on their configured ports.
dotnet run
Alternatively, for production environments or if you prefer not to use the dotnet run command directly, publish your applications to create a self-contained deployment:
Publish the applications with the following commands, adjusting for your specific runtime and application paths:
dotnet publish -c Release -r ubuntu.20.04-x64 --output /path/to/YourApp
Execute the published applications directly from their publish directories.
Step 4: Using a Reverse Proxy (Optional)
In a production setting or to simplify network configurations, you might opt to use a reverse proxy like Nginx or Apache. This setup allows you to route traffic to the correct application based on URL paths or domain names, even if the applications are listening on different ports locally.
- Reference: Setting Up Apache Reverse Proxy
Step 5: Set Up System Services (Optional)
To ensure your applications run as services and start automatically at boot, you can create systemd service files for each application. Here’s an example for a .NET application:
- Create a service file at /etc/systemd/system/yourapp.service:
[Unit] Description=.NET Web Application [Service] WorkingDirectory=/path/to/app ExecStart=/usr/bin/dotnet /path/to/app/YourApp.dll Restart=always RestartSec=10 SyslogIdentifier=dotnet-yourapp User=www-data Environment=ASPNETCORE_ENVIRONMENT=Production [Install] WantedBy=multi-user.target
- Reload the system daemon service:
sudo systemctl daemon-reload
- Enable and start the service:
sudo systemctl enable yourapp.service
sudo systemctl start yourapp.service
- Repeat the process for each application, ensuring to use unique service names and correct paths.
Conclusion
Running multiple .NET Core applications side-by-side on a single Ubuntu instance is a straightforward process that can be achieved without Docker or containerization. By carefully managing SDK installations, configuring application ports, and optionally setting up reverse proxies and system services, you can efficiently deploy and manage multiple .NET Core applications. This approach offers flexibility in development and deployment strategies, catering to a variety of scenarios and performance requirements.