ASP.Net Core應用部署到Linux服務器

ASP.Net Core應用部署到Linux服務器

注:本文測試基於Deepin Linux系統

1、發佈

將項目文件發佈到windows系統的某個目錄底下,詳細操作可參考ASP.Net Core程序發佈

2、上傳服務器

將發佈好的項目文件打包上傳到Linux服務器,可參照SecureCRT上傳文件到服務器,爲了更好地理解,假定將文件AngularDemo01.tar上傳到服務器的/usr/publish/Angular路徑下;

3、運行

打開終端,進行上傳目錄

cd /usr/publish

對上傳的文件目錄進行賦權

sudo chmod -R 777 Angular

找到壓縮包若上傳的是壓縮包,則先進行解壓縮

cd Angular
sudo tar -xvf AngularDemo01.tar

進行解壓後的文件,運行項目

sudo dotnet AngularDemo01.dll

注:該AngularDemo01.dll必須是在發佈的目錄底下的dll文件,如發佈的路徑爲bin/Release/netcoreapp3.1/publish/,則爲publish文件夾底下的dll

打開瀏覽器,輸入訪問的地址

http://localhost:5000

若項目正常,則可繼續往下走,否則,請先處理異常

異常處理參考:

4、反向代理

現在在Linux系統上已經可以正常訪問了,如果需要在window下也可以訪問得到,那麼可以通過nginx進行反向代理。

安裝nginx

sudo apt-get install nginx

安裝成功後,啓動nginx

systemctl start nginx

啓動成功後,可以設置nginx爲開機自啓(Linux宕機、重啓會自動運行nginx而不需要手動輸入命令啓動)

systemctl enable nginx

可以在window系統中訪問Linux系統的IP,測試nginx是否可以訪問。若正常訪問,則可以修改nginx配置文件進行ASP.Net Core應用的轉發。

修改/etc/nginx/conf.d/default.conf文件

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;
    }
}

執行以下代碼重新加載nginx配置文件

nginx -s reload

再次運行ASP.Net Core應用程序,這時,就可以在window上直接訪問到應用了。

若出現以下錯誤:

img

可能是由於SELinux保護機制導致,將nginx添加到SELinux的白名單即可。

方案參考:

5、守護服務(Supervisor)

目前我們的項目是通過終端運行的,如果關閉終端,程序也將停止運行,從而導致無法訪問。可以通過配置守護服務來監聽應用的運行情況,當應用停止運行時立即進行重啓。

安裝SuperVisor

sudo apt-get python-setuptools
easy_install supervisor

配置SuperVisor

mkdir /etc/supervisor
echo_supervisord_conf > /etc/supervisor/supervisord.conf

修改supervisord.conf文件,在最後面添加一下代碼

[include]
files = conf.d/*.conf

然後重新加載配置文件使其生效

supervisorctl reload

6、配置ASP.Net Core程序的守護

在/etc/supervisor/conf.d目錄下創建conf文件,如DataMining.conf,在該文件中添加一下內容:

[program:DataMining]
command=sudo dotnet AngularDemo01.dll ;
directory=/usr/publish/DataMining/netcoreapp3.1/publish/ ;
autorestart=true ;
stderr_logfile=/var/log/DataMining.err.log ;
stdout_logfile=/var/log/DataMining.out.log ;
environment=ASPNETCORE_ENVIRONMENT=Production ;
user=root ; 
stopsignal=INT

保存並退出,然後在終端中運行以下代碼

supervisord -c /etc/supervisor/supervisord.conf
ps -ef | grep DataMining

如果存在dotnet AngularDemo01.dll進程則表示運行成功,這時就可以在瀏覽器上進行訪問了。

錯誤處理參考:

7、配置supervisor開機啓動

在/usr/lib/systemd/system目錄新增supervisord.service服務,在服務文件中增加以下代碼:

# dservice for systemd (Deepin)
# by ET-CS (https://github.com/ET-CS)
[Unit]
Description=Supervisor daemon

[Service]
Type=forking
ExecStart=supervisord -c /etc/supervisor/supervisord.conf
ExecStop=supervisorctl shutdown
ExecReload=supervisorctl reload
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target

然後執行以下命令設置supervisord.service服務自啓動

systemctl enable supervisord

可執行以下命令驗證supervisord.service是否爲開機啓動

systemctl is-enabled supervisord

[Install]
WantedBy=multi-user.target


 然後執行以下命令設置supervisord.service服務自啓動

systemctl enable supervisord


 可執行以下命令驗證supervisord.service是否爲開機啓動

systemctl is-enabled supervisord


![img](https://imgconvert.csdnimg.cn/aHR0cHM6Ly91c2VyLWdvbGQtY2RuLnhpdHUuaW8vMjAyMC80LzE3LzE3MTg3NWRmZTA5ZGY5YTM?x-oss-process=image/format,png)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章