可以参考:https://segmentfault.com/a/1190000012428781
也可以看微软官方文档。
大致步骤如下:
1.在linux下安装.netcore,运行dotnet --version
命令检查是否安装成功.
2.新建一个模板网站:
sudo vim /etc/systemd/system/kestrel-firstapp.service
在/etc/systemd/system目录下新建一个 kestrel-firstapp.service文件,然后写上一段脚本如下:
[Unit]
Description=Example .NET Web API App running on Ubuntu
[Service]
WorkingDirectory=/home/birdhumen/www/firstapp
ExecStart=/usr/bin/dotnet /home/birdhumen/www/firstapp/test.dll
Restart=always
RestartSec=10 # Restart service after 10 seconds if dotnet service crashes
SyslogIdentifier=dotnet-example
User=root
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
[Install]
WantedBy=multi-user.target
红色标出来的很关键,因为我是在root下操作的,所以直接是home目录下的绝对路径/home/birdhumen/www/firstapp,
birdhumen是普通用户名,www和firstapp都是创建的文件夹,firstapp下放了发布好的asp.net core2.0的模板的dll,有7,8个文件。
3.然后执行:
sudo systemctl enable kestrel-mysite.service # 注册服务
sudo systemctl start kestrel-mysite.service # 启动服务
sudo systemctl status kestrel-mysite.service # 检查服务的运行状态 ,如果这句话出错,大概率是脚本写的有问题。
成功后可以看到类似
localhost:5000
的东西了,然后就说明已经成功启动服务。在linux下直接
wget http://localhost:5000
4.但是在外网依旧还是访问不了,需要借助nginx,看另一篇文章:https://blog.csdn.net/wcc27857285/article/details/80033364 安装nginx
5.装好nginx后,需要修改nginx的配置文件
找到nginx的目录,若找不到,就用命令:whereis nginx
目前是~/usr/local/nginx/conf/ 目录下面,有两份文件,nginx.conf和nginx.conf.default,default这份是默认的,但是实际加载的是nginx.conf。
vim nginx.conf
找到server节点,把location下面的替换成下面的红色文本:
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;
}
}
7.重启下服务:./nginx -s reload, 成功后,即可在外网访问linux的IP,即可访问到ASP.netcore2.0的模板网站了!