Linux CentOS7上 .NET Core 简单安装 及管理

安装运行时

首先要看你项目中用的.NET Core版本是多少,版本要对,不然运行起来会报错。虽然.NET Core 3.0都出来了,但我的 vs2017中用的最高版本还是2.1,所以装个2.1的。要装2.2就把下面的runtime-2.1改成runtime-2.2即可。

这边在Linux发行版 CentOS 7 64bit下操作:

#1.升级
rpm -Uvh https://packages.microsoft.com/config/rhel/7/packages-microsoft-prod.rpm

#2.要托管程序,只安装runtime就可以了,没必要安装sdk,runtime比较小,这样也比较快。
yum install aspnetcore-runtime-2.1 -y

 

通过上以两条命令就安装好了。查看下版本信息

dotnet --info

 


 

部署一个.NET Core Web站点

创建目录

mkdir dotnetcoresite


把编译完的代码传到dotnetcoresite/目录下

#添加权限,为了方便我直接给目录所有权限
chmod 777 -R dotnetcoresite/

#进入目录
cd dotnetcoresite/

#运行起来
dotnet ./WebAppDotNetCore.dll

WebAppDotNetCore.dll改为你项目的那个dll

 

#看下能不能访问
curl http://localhost:5000

注:一定要进入到刚才建的dotnetcoresite目录再运行,不然站点能打开,但可能找不到css,js那些静态文件

 

修改监听方式,使局域网或外网能直接访问
.NET Core在linux上装完自带了Kestrel web服务器。测试的话不需要用nginx反向代理什么的。

默认使用了5000端口;默认只监听localhost,需要修改下项目中的Program.cs,才能在外网或局域网中访问

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseKestrel().UseUrls("http://*:5000") //加这行,无需反向代理;直接访问 http://ip:5000
                .UseStartup();

访问http://服务器ip:5000即可  

 

按ctrl+c结束进程

加&使进程在后台运行 

dotnet ./WebAppDotNetCore.dll &

 


 

写了个shell,管理进程

每次部署完去进程关闭开启还挺麻烦,于是写了个脚本

#
#dotnet manager
#https://blog.cozof.com
#e.g.
#./dotnet.sh start
#!/bin/bash
#set -e #on error exit
FLAG=$1
dll='WebAppNetCore.dll'

if [[ $FLAG = 'start' ]]; then
  dotnet $dll&
  exit
fi

webid=$(pgrep dotnet)

if [[ $FLAG = 'stop' ]]; then
  kill -9 $webid
elif [[ $FLAG = 'restart' ]]; then
  kill -9 $webid
  dotnet $dll&
elif [[ $FLAG = 'status' ]]; then
   if [[ $webid > 0 ]]; then
     echo -e "\033[42m active \033[0m"
     echo -e $(ls -l /proc/$webid | grep cwd)
   fi
else
  echo -e ' \033[41merror:\033[0m \n \033[31mmissing params start|stop|restart|status\033[0m'
fi

保存为dotnet.sh,放到dotnetcoresite目录下。最好在linux下用vi操作,不然可能有编码问题。

 

#添加执行权限
chmod +x dotnet.sh

#开启
./dotnet.sh start
#重启
./dotnet.sh restart
#停止
./dotnet.sh stop
#查看是否运行
./dotnet.sh status

shell中的WebAppNetCore.dll改成你的dll名字。其实可以不写死用参数的形式,但我觉得不用参数更方便。

类别:OperationMaintenance   阅读(0)   评论(0)    发表时间:2019-06-22 18:13  星期六

评论区

发表评论

        姓名:
邮箱|网站:
        内容:

  (可按Ctrl+Enter提交)