Practical Deployment of ASP.NET Core on Linux: A Beginner’s Advancement

In today’s software development field, ASP.NET Core has become one of the preferred frameworks for many developers to build web applications due to its cross-platform features and powerful capabilities. Linux, with its open-source, stable, and efficient advantages, occupies an important position in the server-side market. Deploying ASP.NET Core applications on Linux servers not only leverages the flexibility of ASP.NET Core but also takes advantage of Linux’s performance to achieve optimal application runtime results. This article will provide a detailed introduction to the deployment process of ASP.NET Core on Linux for beginners. 1. Preliminary Preparation (1) Install .NET SDK To deploy ASP.NET Core applications on Linux systems, the first step is to install the .NET SDK. Taking Ubuntu as an example, open the terminal and execute the following command to add the Microsoft software source:

wget https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb

Then install the .NET SDK:

sudo apt-get update; sudo apt-get install -y dotnet-sdk-3.1

Here, 3.1 is the version number, which can be selected based on actual needs. (2) Prepare ASP.NET Core Project Ensure that you have a complete ASP.NET Core project locally. If you are obtaining the project from a code repository, use the git clone command to clone the project to your local directory. In the project directory, compile the project using the dotnet build command to ensure there are no compilation errors. 2. Project Publishing In the local development environment, open the terminal and switch to the root directory of the project, then execute the following command to publish the project:

dotnet publish -c Release -o out

Here, -c Release indicates that the compilation is done in release mode, and -o out specifies that the published files will be output to a directory named out. After publishing, the out directory will contain all the files required to run the application, including the compiled DLL, configuration files, and dependencies. 3. Upload to Linux Server You can use the scp command to upload the published directory (such as the out directory) to the Linux server. For example, assuming the server’s IP address is 192.168.1.100 and the username is user, to upload the local out directory to the server’s /var/www/myapp directory, you can execute the following command:

scp -r out [email protected]:/var/www/myapp

After the upload is complete, connect to the Linux server via SSH to check if the uploaded files are complete. 4. Configure Server Environment (1) Set Firewall Rules If the server has a firewall enabled, you need to open the ports required by the application. For example, ASP.NET Core applications use port 5000 by default; you can execute the following command to open this port (using Ubuntu’s UFW firewall as an example):

sudo ufw allow 5000

(2) Install Web Server (using Nginx as an example) Nginx is a high-performance web server and reverse proxy server. To install Nginx on the Linux server:

sudo apt-get update
sudo apt-get install nginx

After installation, edit the Nginx configuration file (usually located at /etc/nginx/sites-available/default), and add the reverse proxy configuration to forward requests to http://yourdomain.com (replace with the actual domain or IP address) to the locally running ASP.NET Core application:

server {    listen 80;    server_name yourdomain.com;    location / {        proxy_pass http://localhost:5000;        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;    }}

After saving the configuration file, reload the Nginx configuration:

sudo systemctl reload nginx

5. Run ASP.NET Core Application On the Linux server, switch to the uploaded project directory (such as /var/www/myapp) and execute the following command to run the application:

dotnet myapp.dll

Here, myapp.dll is the main DLL file of the application, replace it with the actual project name. At this point, the ASP.NET Core application should be successfully running on the Linux server, and you can access the application by visiting the configured domain or IP address in a browser. 6. Set Auto-Start on Boot To ensure that the application can run automatically after the server restarts, you can use systemd to set up the service. Create a new service file, for example, /etc/systemd/system/myapp.service, with the following content:

[Unit]Description=My ASP.NET Core ApplicationAfter=network.target[Service]WorkingDirectory=/var/www/myappExecStart=/usr/bin/dotnet /var/www/myapp/myapp.dllRestart=alwaysRestartSec=10SyslogIdentifier=myappUser=www-data[Install]WantedBy=multi-user.target

After saving the file, execute the following commands to enable and start the service:

sudo systemctl enable myapp
sudo systemctl start myapp

Thus, the deployment of the ASP.NET Core application on the Linux server is basically complete. Through the above steps, beginners can successfully deploy their ASP.NET Core projects to the Linux environment, opening a new chapter in cross-platform development and deployment. In practical applications, further optimizations and expansions can be made, such as configuring HTTPS and performance monitoring, to meet different business needs.

Leave a Comment