.NET Core + Angular Project Deployed to CentOS

.NET Core + Angular Project Deployed to CentOS

Recently, the company needed to develop a project that can run on Linux systems. The example development project uses .NET Core + Angular; theoretically, it fully supports cross-platform.

Last updated 5/11/2022 6:35 AM
chaney1992
7 min read
Category
.NET
Tags
.NET C# Web API

Preface

Recently, the company needed to develop a project that can run on Linux systems. The sample development project uses .NET Core + Angular for development; theoretically, it fully supports cross-platform.

But practice is the only criterion for testing truth; so let's verify and implement it. If any issues arise during the process, it will be considered accumulated experience.

1. Environment Preparation

Since this is mainly about verifying project deployment in a Linux environment, and I don't want to set up a new virtual machine environment, I will use the Linux subsystem in Win10 (What is WSL?)

  1. Steps to enable WSL:
  • Go to [Turn Windows features on or off] and enable WSL, as shown below

  • Enter Microsoft Store and select the appropriate version. This machine installs CentOS

  • After installation, upon startup (problem occurs)

Finally, the problem was identified: A Linux kernel update package is needed (update package download link: https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi)

After downloading and installing, the CentOS system environment is ready.

  1. .NET Core environment installation:
  • Update the system base software version (optional)
sudo yum update
  • Register Microsoft signing key:
sudo rpm -Uvh https://packages.microsoft.com/config/rhel/7/packages-microsoft-prod.rpm
  • Install .NET Core SDK. The sample program uses version 3.1 for development (Editor's note: as of May 11, 2022, .NET 7 Preview 4 has been released)
sudo yum install dotnet-sdk-3.1
  • Check if installation was successful. The image indicates success.

  1. Nginx environment installation: (for web project deployment)
  • Install dependencies
yum install gcc-c++
yum install pcre pcre-devel
yum install zlib zlib-devel
yum install openssl openssl--devel
  • Add Nginx yum repository
sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
  • Install Nginx
sudo yum install nginx
  • Nginx basic information

Directory structure:

Nginx Directory
Configuration path /etc/nginx/
Error log /var/log/nginx/error.log
Access log /var/log/nginx/access.log
Default site directory /usr/share/nginx/html

Basic commands:

  • nginx // start nginx
  • nginx -s quit // stop nginx
  • nginx -s reload // reload configuration file

2. Project Deployment

There are multiple ways to deploy a project to CentOS: direct execution, Docker deployment (already used multiple times in previous learning articles). This time we will use direct execution to deploy the application. Since the project uses a front-end and back-end separation architecture, they need to be deployed separately

  • Server-side deployment
  1. Copy the server-side project files to the CentOS directory. The deployment path this time: /home/www/publish

  1. Modify configuration file: The vi command is needed (requires separate installation)

Enter configuration file editing via command:

vim appsettings.json

After editing, exit using the following commands:

vim commands
:w save but not exit
:wq save and exit
:q exit
:q! force exit, do not save
:e! discard all modifications, edit command history starting from last saved file

  1. Start the service

Enter the project directory and execute the command:

[root@Coder supervisor]# cd /home/www/publish
[root@Coder publish]# dotnet ZLSoft.UnifiedDS.Web.Host.dll --urls http://*:8220

  • Front-end project deployment
  1. Copy the web project to: /home/www/web

  2. Add configuration file web.conf in the Nginx configuration directory

server {
    listen       8221;
    server_name  192.168.243.86:8220;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /home/www/web;
        index  index.html index.htm;
        try_files $uri $uri/ /index.html?$query_string;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

Note: Since the Angular project uses route redirection, the marked content is required.

  1. Update Nginx configuration:
/usr/sbin/nginx -s reload

3. Running Example

Server-side running:

Web project running effect:

At this point, the project is running successfully. However, when I terminate the CentOS command, the web server stops. Therefore, we need to create a service to implement a daemon process.

4. Creating a Daemon Process - supervisor

  1. Install supervisor
# Install Python extension
yum install python-setuptools
# Install supervisor via Python extension
easy_install supervisor

# Create configuration folder
mkdir /etc/supervisor
mkdir /etc/supervisor/conf.d
# Configuration file
echo_supervisord_conf > /etc/supervisor/supervisord.conf
# Modify configuration file (all .ini files in the supervisord.d folder are configuration files)
# At the end of /etc/supervisor/supervisord.conf
;files = relative/directory/*.ini change to files = conf.d/*.ini
  1. Create configuration file: Create publish.conf in the /etc/supervisor/conf.d directory
[program:publish]
command=dotnet ZLSoft.UnifiedDS.Web.Host.dll --urls http://192.168.243.86:8220  # run command
directory=/home/www/publish # program path
environment=ASPNETCORE__ENVIRONMENT=Production
user=root
stopsignal=INT
autostart=true # auto start
autorestart=true # auto restart after 3 seconds
startsecs=3
stderr_logfile=/var/log/ossoffical.err.log
stdout_logfile=/var/log/ossoffical.out.log
  1. Start the service
# Enter supervisor directory
cd /etc/supervisor
# Start supervisord service
supervisord -c supervisord.conf
  1. Enable the web management interface of the daemon process
# Modify configuration file:
vim /etc/supervisord.conf

# Uncomment the content
   [inet_http_server] ; inet (TCP) server disabled by default
   port=*:9001 ; ip_address:port specifier, *:port for all iface
   username=user ; default is no username (open server)
   password=123 ; default is no password (open server)

   # Reload configuration file

   supervisorctl reload
  1. Common supervisorctl commands
$ sudo service supervisor stop   # stop supervisor service
$ sudo service supervisor start  # start supervisor service
$ supervisorctl shutdown         # shutdown all tasks
$ supervisorctl stop|start program_name  # stop or start a service
$ supervisorctl status           # view all task status

5. Summary

Deploying and running projects in the CentOS system mainly depends on how familiar you are with Linux-related content: commands, permissions, software, etc. In any case, practice makes perfect; more practice is needed.

Moreover, the Linux system in WSL is not fully comprehensive; it temporarily lacks service and firewall related features. So, if you are in a real environment, you can set services to start on boot and handle firewall-related tasks.

Therefore, it is necessary to find a complete environment for learning and practicing.

Keep Exploring

Related Reading

More Articles
Same category / Same tag 1/19/2024

FluentValidation Validation Tutorial Based on .NET

FluentValidation is a validation framework based on .NET development. It is open-source, free, and elegant, supporting chained operations, easy to understand, feature-complete, and can be deeply integrated with MVC5, WebApi2, and ASP.NET Core. The component provides over a dozen commonly used validators, good scalability, support for custom validators, and support for localization and multilingual.

Continue Reading