1. 安裝 .net core
可參見官網安裝教程。
- 選擇Linux發行版本為
Centos/Oracle
- 添加dotnet的yum倉庫配置
$ sudo rpm -Uvh https://packages.microsoft.com/config/rhel/7/packages-microsoft-prod.rpm
- 安裝.net SDK
#更新系統軟件
sudo yum update
#安裝dotnet sdk
sudo yum install dotnet-sdk-2.1
- 使用
dotnet --info
命令顯示 .net core 的信息。
2. 安裝nginx
可參見官網安裝配置教程。
- 配置yum倉庫,創建
/etc/yum.repos.d/nginx.repo
,並配置為以下內容:
[nginx]
name=nginx repo
#baseurl=http://nginx.org/packages/OS/OSRELEASE/$basearch/
#OS為rhel或centos
#OSRELEASE為centos版本,6或7
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
- 獲取公鑰文件
wget nginx.org/keys/nginx_signing.key
- 導入公鑰文件
rpm --import nginx_signing.key
- 安裝nginx
sudo yum -y install nginx
3. 將nginx設置為開機啟動
- 在系統服務目錄里創建nginx.service文件,並將文件內容設置如下:
touch /etc/systemd/system/nginx.service
#服務的說明
[Unit]
#描述服務
Description=nginx
#描述服務類別
After=network.target
#服務運行參數的設置
[Service]
#Type=forking是后台運行的形式
Type=forking
#服務的具體運行命令
ExecStart=/usr/sbin/nginx
#重啟命令
ExecReload=/usr/sbin/nginx -s reload
#停止命令
ExecStop=/usr/sbin/nginx -s quit
#PrivateTmp=True表示給服務分配獨立的臨時空間
PrivateTmp=true
#運行級別下服務安裝的相關設置,可設置為多用戶,即系統運行級別為3
[Install]
WantedBy=multi-user.target
- 設置為開機啟動
systemctl enable nginx.service
- 其他管理命令
#啟動nginx服務
systemctl start nginx.service
#停止開機自啟動
systemctl disable nginx.service
#查看服務當前狀態
systemctl status nginx.service
#重新啟動服務
systemctl restart nginx.service
#查看所有已啟動的服務
systemctl list-units --type=service
4. 配置nginx
- 增加Nginx配置文件myapp.conf,並做如下配置:
cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/myapp.conf
proxy_set_header Host $http_host
server {
#監聽端口號
listen 8081;
#監聽地址
server_name localhost 192.168.1.202;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
#設置代理
location / {
#Kestrel上運行的asp.net core mvc網站地址
proxy_pass http://localhost:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
}
#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;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
- 重新載入ngin
nginx -s reload
5. 發布網站,配置服務和防火牆
- 發布網站后將發布后的網站上傳到服務器。
- 使用
dotnet myapp.dll
啟動網站。 - 在瀏覽器中打開網站的地址,檢測網站正常啟動並可訪問。
- 如果需要外網訪問需要在防火牆上開放相應的端口。具體操作可參見:
#添加80端口:
firewall-cmd --zone=public --add-port=80/tcp --permanent
#刪除防火牆端口:
firewall-cmd --zone=public --remove-port=12345/tcp --permanent
#重新加載防火牆:
firewall-cmd --reload
#重啟防火牆:
systemctl stop firewalld systemctl start firewalld
- 將發布的網站配置為服務,可參見nginx配置為開機啟動。具體如下:
#新建配置文件
touch /etc/systemd/system/myapp.service
#編輯配置文件:
[Unit]
Description=myapp web
[Service]
WorkingDirectory=/home/myapp/web
ExecStart=/usr/bin/dotnet /home/myapp/web/WebPortals.dll
Restart=always
RestartSec=10 # Restart service after 10 seconds if dotnet service crashes
SyslogIdentifier=myapp
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
[Install]
WantedBy=multi-user.target
#服務開機啟動
systemctl enable myapp.service
#啟動服務
systemctl start myapp.service
#查看服務狀態
systemctl status myapp.service