Hosting a .NET 8 web application on a CentOS 7 VPS requires setting up the necessary environment, installing dependencies, and configuring a web server. Below are the detailed steps to achieve this:
using System; namespace HelloWorld { class Program { static void Main(string[] args) { Console.WriteLine("Hello World!"); } } }
Ensure your CentOS 7 system is up to date to avoid compatibility issues.
bash
CollapseWrapCopy
sudo yum update -y
Install essential tools and libraries required for building and running .NET applications.
sudo yum groupinstall "Development Tools" -y sudo yum install gcc-c++ glibc-devel zlib-devel -y
.NET 8 is not natively available in the default CentOS 7 repositories, so you need to add the Microsoft package repository.
sudo rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm
sudo yum install dotnet-sdk-8.0 -y
dotnet --version
Publish Your Application: If your application is not already published, publish it on your development machine or directly on the VPS. Use the following command to create a self-contained deployment (recommended for VPS to avoid dependency issues):
dotnet publish -c Release -r linux-x64 --self-contained true
The published files will be in the bin/Release/net8.0/linux-x64/publish/ directory of your project.
Transfer Files to VPS: If you published the application on a different machine, use scp or an FTP client to transfer the contents of the publish folder to your VPS. For example:
scp -r /path/to/publish user@your-vps-ip:/path/to/destination
To serve your .NET 8 web application, you need a web server. The most common choices are Nginx (as a reverse proxy) or running the app directly with Kestrel (not recommended for production). Here, we'll use Nginx.
Install Nginx:
sudo yum install nginx -y
Start and Enable Nginx:
sudo systemctl start nginx sudo systemctl enable nginx
Configure Nginx as a Reverse Proxy: Create a new Nginx configuration file for your application:
sudo nano /etc/nginx/conf.d/yourapp.conf
Add the following configuration (replace yourapp and your-vps-ip as needed):
nginx
server { listen 80; server_name your-vps-ip; location / { proxy_pass http://localhost:5000; # Default Kestrel port proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection keep-alive; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } }
Save and exit the file (Ctrl+O, Enter, Ctrl+X).
Test Nginx Configuration: Ensure the configuration is valid:
sudo nginx -t
Restart Nginx: Apply the changes:
sudo systemctl restart nginx
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: