三步把asp.net core 3.1應用部署到centos7



一、編譯發布Asp.net core 應用

直接使用vs2019編譯發布后,通過ftp上傳到centos的 /www/ 目錄下,不再贅述。

二、centos安裝asp.net core runtime和nginx

1、安裝asp.net core runtime

#注冊 Microsoft 密鑰。注冊產品存儲庫。安裝必需的依賴項。

sudo rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm

#安裝 .NET Core 運行時
sudo yum install aspnetcore-runtime-3.1

2、安裝nginx

添加源:

sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

安裝 nginx

$ sudo yum -y install nginx

Nginx常用命令


# 卸載 nginx
$ sudo yum remove nginx  

# 設置開機啟動 
$ sudo systemctl enable nginx 

# 啟動 nginx 服務
$ sudo service nginx start 

# 停止 nginx 服務
$ sudo service nginx stop 

# 重啟 nginx 服務
$ sudo service nginx restart 

# 重新加載配置,一般是在修改過 nginx 配置文件時使用。
$ sudo service nginx reload 

#查看nginx版本
$ nginx -v  

3、使用nginx反向代理

在 /etc/nginx/ 目錄下新建AspnetCoreDemo.conf,內容如下

server {
    listen        80;
    server_name   example.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;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto $scheme;
    }
}

4、重新加載nginx配置

# 驗證配置文件的語法
$ sudo nginx -t 
# 強制 Nginx 選取更改。
$ sudo nginx -s reload  

三、添加Systemd守護

1、Systemd service內容如下

路徑 /etc/systemd/system/AspnetCoreDemo.service 新建文件

[Unit]
Description=AspnetCoreDemo running on Centos

[Service]
WorkingDirectory=/www
ExecStart=/usr/bin/dotnet /www/AspnetCoreDemo.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=AspnetCoreDemo
User=www-data
#Production:生產環境 Development:開發環境
Environment=ASPNETCORE_ENVIRONMENT=Development 
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target

2、Systemd基本操作命令

#啟用
systemctl enable AspnetCoreDemo.service
#啟動
systemctl start AspnetCoreDemo.service
#狀態
systemctl status AspnetCoreDemo.service
#重啟
systemctl restart AspnetCoreDemo.service
#關閉
systemctl stop AspnetCoreDemo.service

四、防火牆設置(不需要端口訪問,可忽略此步)

1、開放端口

firewall-cmd --zone=public --add-port=5000/tcp --permanent   # 開放5000端口
firewall-cmd --zone=public --remove-port=5000/tcp --permanent  #關閉5000端口
firewall-cmd --reload   # 配置立即生效 

2、查看防火牆所有開放的端口
firewall-cmd --zone=public --list-ports 

3.、關閉防火牆

如果要開放的端口太多,嫌麻煩,可以關閉防火牆,安全性自行評估
systemctl stop firewalld.service 

4、查看防火牆狀態
 firewall-cmd --state


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM