在CentOS部署AspNetCore網站


 

  前段時間某雲服務器大促,就買了一台打算折騰一下,買了幾個月,卻啥也沒做,就改了個初始密碼。最近快到雙十一了,另一家廠商相同配置的服務器價格又便宜了一大截,看來又得剁手了。從今年開始,搜索一下雲服務器,發現好多大廠都開始涉足雲服務器市場了,之前做這塊和沒做這塊業務的大大小小的廠商今年都在大力推廣自己的雲服務器產品,市場競爭起來了,價格就友好多了呀。😁 2019年已經不適合在地上走了,咱也到雲里面去看一看。開始折騰吧。

 

安裝dotnet 運行時

  首先注冊微軟產品倉庫,這樣就可以通過yum安裝.NET Rumtime了。

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

  目前dotnetcore3.0已經發布,運行時版本最好選擇和本地開發的sdk版本一致。我本地已經更新到.NET SDK3.0了,所以在服務器安裝aspnetcore-runtime-3.0

sudo yum install aspnetcore-runtime-3.0

 

aspnetcore網站

  創建aspnetcore Web程序,目標框架選擇.NET Core 3.0,啥也不做,直接發布。powershell或者cmd進入到publish目錄,使用dotnet命令運行一下程序,默認訪問5000端口,可以看到網站正常運行。

 

上傳到服務器

  遠程連接工具使用WinSCP和PuTTY。首先用WinSCP連接到服務器,在home目錄創建目錄wwwroot專門用來存放網站資源,再創建一個目錄firstnetcorev3對應剛剛發布的網站,將publish目錄下的所有文件上傳到firstnetcorev3這個目錄。在服務器網站目錄中使用dotnet命令啟動網站,不出意外應該會提示現在監聽5000端口。此時在本地瀏覽器訪問服務器5000端口是不會有響應的,因為在服務器上還沒有開放端口。在服務器上的瀏覽器訪問5000端口,應該可以看到網站運行的頁面,不過我的服務器是最小化安裝,沒有圖形化界面。 ̄□ ̄||

 

安裝nginx

  添加yum源,根據自己的服務器系統架構選擇。http://nginx.org/packages/

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

  安裝

sudo yum install -y nginx

  啟動

sudo systemctl start nginx.service

  現在本地訪問服務器ip就可以看到nginx的歡迎頁面了。

  開機啟動

sudo systemctl enable nginx.service

  nginx全局配置文件在這里:/etc/nginx/nginx.conf

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}
View Code

  可以看到它包含了這個目錄下的所有配置文件:/etc/nginx/conf.d/,目錄下有一個default.conf文件,這是nginx的默認配置,修改一下,或者新增一個conf文件。如果是新增的conf,監聽端口不可以是80,因為默認配置中已經使用了80端口。我這里新增了一個firstnetcorev3.conf文件。

server {
# 監聽外網端口 listen 500
0; 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

 

開放端口

  查看防火牆狀態

systemctl status firewalld

  啟動防火牆

systemctl start firewalld

  開放80端口,我同時開放了5000端口。

firewall-cmd --zone=public --add-port=80/tcp --permanent

  重啟防火牆

firewall-cmd --reload

 

外網訪問服務器網站

  在本地瀏覽器直接輸入服務器ip,正常打開nginx歡迎頁面。我之前是新增了一個nginx配置文件,監聽外部5000端口轉發服務器內部5000端口,在本地訪問服務器5000端口,報500錯誤。80端口正常,說明nginx運行正常,5000端口是剛剛部署的網站端口,500錯誤應該是網站沒有啟動。

  進入網站發布路徑,使用dotnet命令啟動網站,然而報錯了。

   提示5000端口已經被使用了,原來是之前開放了防火牆5000端口,那就換另一個端口,先刪除5000端口,再開放5001端口,重啟防火牆。

firewall-cmd --zone=public --remove-port=5000/tcp --permanent
firewall-cmd --zone=public --add-port=5001/tcp --permanent
firewall-cmd --reload

  修改firstnetcorev3.conf,將開放端口改成5001。進入網站發布路徑,再次啟動網站。

  網站部署成功了,先小結一段。服務器多站點部署情況下,nginx可以使用多個配置文件,nginx監聽的防火牆端口不可以和web應用程序運行的端口沖突。但是這種部署方式需要手動啟動網站,雖然已經設置開機啟動nginx服務,但是網站本身不會自動啟動,重啟服務器的話,需要手動啟動網站。這個時候就需要一個程序自動啟動網站了。

 

使用守護進程supervisor

  安裝supervisor

yum install supervisor

  安裝成功后,在/etc/目錄下會產生一個supervisord.conf文件,打開文件查看,這一行:files = supervisord.d/*.ini,可以看到配置文件都在supervisord.d目錄下,並且是ini格式。進入到supervisord.d目錄,創建一個firstnetcorev3.ini文件。

[program:firstnetcorev3]
command=dotnet publish2centos.dll
directory=/home/wwwroot/firstnetcorev3/
autostart=true
autorestart=true
stderr_logfile=/var/log/firstnetcorev3.err.log
stdout_logfile=/var/log/firstnetcorev3.out.log
environment=ASPNETCORE_ENVIRONMENT=Production
user=root
startsecs=1
stopsignal=INT

  啟動supervisor,並設置為開機啟動。

systemctl enable supervisord
systemctl start supervisord
systemctl is-enabled supervisord

   重啟服務器試試,在PuTTY里面用命令reboot彈出了一個彈窗,但是應該已經重啟成功了,不確定的話,在雲服務器控制面板重啟服務器,完成重啟后,再次訪問服務器5001端口,網站運行正常。這樣,AspNetCore網站部署在CentOS服務器就成功了。

 


免責聲明!

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



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