Introduction
In the modern development landscape, versatility and efficiency in application delivery are critical. This is especially true when working with.NET Core, Microsoft's cross-platform framework, which has seen significant adoption for building a wide range of applications. A common scenario involves running multiple.NET Core applications on the same server. This guide provides a comprehensive overview of how to run multiple.NET Core applications side by side on a single Ubuntu instance without relying on Docker, covering all aspects from installation to configuration and deployment.
Docker and containerization offer powerful ways to isolate and deploy applications. However, there are scenarios where it is best to run applications directly on the host system, 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 knowledge of the command line interface
- .NET Core applications targeting different versions (for example, 3.1 and 6.0)
Step 1: Install the.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 allows you to develop and run applications for those versions on your Ubuntu server.
Update the package index and install prerequisite packages to ensure that your system can access the Microsoft 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 required by your applications. For example, to install the.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: Set up each application
To run multiple applications on the same server, you must ensure that they are configured to listen on different ports to avoid port conflicts.
In your first application, edit the appsettings.json or appsettings.Production.json file to specify a unique port:
{
"Kestrel": {
"Endpoints": {
"Http": {
"Url": "http://localhost:5001"
}
}
}
}
For the second application, change its configuration similarly to ensure it is listening on a different port, such as 5001:
{
"Kestrel": {
"Endpoints": {
"Http": {
"Url": "http://localhost:5000"
}
}
}
}
Step 3: Run the applications
Navigate to each application's directory and use the dotnet run command to start them, making sure they are running on the 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 stand-alone deployment:
Publish applications with the following commands, tailoring them to specific runtime and application paths:
dotnet publish -c Release -r ubuntu.20.04-x64 --output /path/to/YourApp
Run published applications directly from their publish directories.
Step 4: Using a Reverse Proxy (Optional)
In a production environment or to simplify network configurations, you might choose to use a reverse proxy such as Nginx or Apache. This configuration allows traffic to be routed to the correct application based on URL paths or domain names, even if the applications are listening on different ports locally.
Step 5: Configure System Services (Optional)
To ensure that your applications run as services and start automatically on startup, you can create systemd service files for each application. Here is an example for a.NET application:
Create a service file in /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, making sure to use unique service names and correct paths.
Conclusion
Running multiple.NET Core applications side by side on a single Ubuntu instance is a simple process that can be achieved without Docker or containerization. By carefully managing SDK installations, configuring application ports, and optionally configuring reverse proxies and system services, you can efficiently deploy and manage multiple.NET Core applications. This approach offers flexibility in development and deployment strategies, satisfying a variety of scenarios and performance requirements.