现在比较主流的是用 docker 部署,
本文是直接安装服务器环境,运行项目可执行文件 xxx.dll
先要准备的东西:
1、ubuntu 服务器,本人用的 ubuntu 20.04(虚拟机也行)
2、开发后发布好的项目
3、xshell 、xftp 用户远程连接服务器和文件传输
一、部署. NET 环境
1、在 Ubuntu 中打开一个 shell 并使用以下命令安装 Microsoft 软件包存储库。
$ wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
$ sudo dpkg -i packages-microsoft-prod.deb
2、接下来,安装最新的 .NET5 SDK。
sudo apt-get update; \
sudo apt-get install -y apt-transport-https && \
sudo apt-get update && \
sudo apt-get install -y dotnet-sdk-5.0
3、验证 .NET 5 是否安装成功
dotnet --version
4、可以使用 .NET CLI 检查在 Ubuntu 上运行的 .NET SDK 和运行时列表。
dotnet --list-sdks
dotnet --list-runtimes
5、创建项目文件夹并给完全控制权限
sudo mkdir -p /var/www/Mvc.Test
sudo chown -R vima91:www-data /var/www/Mvc.Test
二、部署 Nginx
1、安装-运行
sudo apt update
sudo apt install nginx
sudo service nginx start
systemctl status nginx -- 检查是否正常开启
-
配置完 Nginx 后我们可以访问域名看到
2、配置 Nginx
-
打开配置文件
sudo vim /etc/nginx/sites-available/default
-
将内容替换
-
监听本服务器的 80 端口
server {
listen 80;
server_name localhost;
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;
}
}
-
重启 Nginx 服务器
sudo systemctl restart nginx
-
如果 xftp 上传文件错误,一般是权限问题
sudo chmod 777 www
2、配置 Nginx
-
打开配置文件
sudo vim /etc/nginx/sites-available/default
-
将内容替换
-
监听本服务器的 80 端口
server {
listen 80;
server_name localhost;
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;
}
}
-
重启 Nginx 服务器
sudo systemctl restart nginx
-
如果 xftp 上传文件错误,一般是权限问题
sudo chmod 777 www
3、运行项目
dotnet /var/www/wechat/weichat.dll
-
这样就算配置成功了
4、将项目的运行配置成服务
sudo vim /etc/systemd/system/wechat.service
-
把下面的代码贴进去
[Unit]
Description=My application service
[Service]
WorkingDirectory=/var/www/wechat
ExecStart=/usr/bin/dotnet /var/www/wechat/wechat.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=wechat
User=vima91
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
[Install]
WantedBy=multi-user.target
5、启动上述的服务
sudo service wechat start
-
查看服务状态
service sshd status
service servicename status 是当前状态 hkconfig --list servicename 是查看启动状态
这样服务就配置成功,并启动了,端口为 5000