在CentOS 8 上 部署 .Net Core 應用程序

在Centos 8 上 部署 .Net Core 應用程序

                                                                          —— 記錄篇


 

1、更新dnf 源

1 dnf update

2、安裝 Asp.Net Core 運行時

1 dnf install aspnetcore-runtime-3.1

2.1、驗證是否安裝成功

1 dotnet --info

 

 

出現如上圖所示就說明安裝成功

3、安裝Nginx

dnf -y install nginx

3.1、查看nginx版本

nginx -v

3.2、設置開機自啓動

1 systemctl enable nginx

3.3、啓動 nginx 服務

service nginx start

3.4、其他 相關 指令

1 # 卸載 
2 dnf remove nginx
3 # 停止 服務
4 service nginx stop
5 # 重啓
6 service nginx restart
7 # 加載配置文件 
8 service nginx reload

4、MySql 安裝

4.1、下載

 wget https://repo.mysql.com//mysql80-community-release-el8-1.noarch.rpm

4.2、使用rpm 安裝 mysql 

 rpm -ivh mysql80-community-release-el8-1.noarch.rpm

4.3、dnf 安裝 mysql 服務

dnf -y install mysql-server

4.4、設置開機自啓動

systemctl enable mysqld.service

4.5、啓動mysql

systemctl start mysqld.service

4.6、設置遠程連接(可選)

因我時在某雲上,所以需要設置我本地連接,如果是在自己虛擬器可跳過此步驟

4.6.1、進入 mysql 命令行

 

 4.6.2、更新 系統表(user)
update mysql.user set host="%" where user="root";

 

 

4.6.3、設置 root 密碼
-- 切換數據庫
use mysql;
-- 執行語句
ALTER  USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123';
-- 刷新修改後的權限
flush privileges;
-- 退出
exit;
4.6.4、測試

 

 

5、將應用程序發佈後的包上傳

5.1、創建文件夾(用於存放應用程序包)

mkdir -p /var/www/web

5.2、ftp 上傳應用程序文件到 上一步創建的文件夾(/var/www/web/)中

6、Redis 安裝(可選)

如果項目中沒有用到 Redis 可以跳過此步驟

6.1、下載、解壓、編譯

 wget http://download.redis.io/releases/redis-6.0.6.tar.gz
 tar xzf redis-6.0.6.tar.gz
 cd redis-6.0.6
 dnf install tcl
 make

6.2、編譯測試

make test

6.3、遷移到指定的目錄(可選)

 1 mkdir -p /usr/local/soft/redis
 2 cd /usr/local/soft/redis/
 3 mkdir bin
 4 mkdir conf
 5 cd bin/
 6 cp /redis-6.0.6/src/redis-cli ./
 7 cp /redis-6.0.6/src/redis-server ./
 8 cd ../conf/
 9 cp /redis-6.0.6/redis.conf ./
10 # 配置 redis-server 的 配置文件爲 /usr/local/soft/conf/redis.conf
11 /usr/local/soft/redis/bin/redis-server /usr/local/soft/redis/conf/redis.conf
12 # 檢查端口是否在使用
13 netstat -anp | grep 6379

6.4、使用 systemd 方式守護 redis 進程

6.4.1、編輯 redis.service 文件
vim /lib/systemd/system/redis.service
  6.4.2、設置redis.service 內容

[Unit]
Description=Redis
After=network.target

[Service]
Type=forking
PIDFile=/var/run/redis_6379.pid
ExecStart=/usr/local/soft/redis/bin/redis-server /usr/local/soft/redis/conf/redis.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

6.4.3、重載系統
systemctl daemon-reload
6.4.4、設置開機啓動及其他指令
# 開機自啓
systemctl enable redis
# 啓動
systemctl start redis
# 查看狀態
systemctl status redis
# 停止
systemctl stop redis

7、配置.Net Core 應用程序的守護進程

7.1、編輯 aspnetCore.service 文件

文件名 自定義,這裏我起名爲 aspnetCore.service

vim /lib/systemd/system/aspnetCore.service

7.2、編輯內容

[Unit]
Description=AspnetCoreDemo running on Centos8 

[Service]
# 應用程序所在的文件目錄 WorkingDirectory
=/var/www/web/ ExecStart=/usr/bin/dotnet /var/www/web/Carefree.AspNetCoreDemo.dll Restart=always # 如果dotnet服務崩潰,10秒後重新啓動服務 RestartSec=10 KillSignal=SIGINT SyslogIdentifier=AspNetCoreDemo User=root #Production:生產環境 Development:開發環境 Environment=ASPNETCORE_ENVIRONMENT=Development Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false [Install] WantedBy=multi-user.target

7.3、重載系統及設置開機啓動

# 重載系統
systemctl daemon-reload
# 開機自啓動
systemctl enable aspnetCore.service

8、Nginx 代理

8.1、編輯配置文件

vim /etc/nginx/conf.d/web.conf

8.2、編輯內容

server
{
   listen       80;
   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;
   }
}

8.3、編輯 nginx.conf 文件

 

 

8.4、驗證配置文件是否正確及加載配置文件

# 驗證配置文件
nginx -t
# 加載配置文件
nginx -s reload

至此我們的應用程序可正常訪問了。如有何問題可與我聯繫,共同學習。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章