Nano Server速记

时间:2023-07-16 21:04:38

!!!!NanoServer已死,烧香!!!donnetcore 只支持1.x

 

入门参考https://docs.microsoft.com/zh-cn/windows-server/get-started/nano-server-quick-start

1、创建VHD

Import-module .\NanoServerImageGenerator.psm1 -Verbose

New-NanoServerImage -Edition Standard -DeploymentType Guest -MediaPath j:\ -BasePath d:\vm\nano -TargetPath d:\vm\nano\nano.vhd -ComputerName Nano -Package Microsoft-NanoServer-IIS-Package –EnableRemoteManagementPort

               
 

2、使用VHD创建虚拟机(在其它操作系统上也可以应用此镜像,如WIN8.1)

 

 

3、远程管理

A、连接

@@@code
Set-Item WSMan:\localhost\Client\TrustedHosts "10.168.1.125"
Enter-PSSession -ComputerName "10.168.1.125" -Credential "administrator"
@@#

               

B、共享文件夹

参考https://www.cnblogs.com/dotNETCoreSG/p/aspnetcore-2_5-nano_server_on_core.html

 

以SCSI方式挂载其它虚拟磁盘 参考

@@@code
Get-Dis
Get-Partition
Set-Disk -Number 1 -IsOffline $false
Set-Disk -Number 1 -IsReadOnly $false  Remove-Partition -DriveLetter D  Set-Partition -DriveLetter E -NewDriveLetter D  
 mkdir c:\temp
net share AspNetCoreSampleForNano=c:\PublishedApps\AspNetCoreSampleForNano /GRANT:EVERYONE`,FULL  (启用文件共享没什么效果)
netsh advfirewall firewall set rule group="File and Printer Sharing" new enable=yes
netsh advfirewall firewall set rule group="@FirewallAPI.dll,-28502" new enable=yes  net share ShareTemp=c:\temp /GRANT:EVERYONE`,FULL
@@#

               
 

C、开端口

参考http://www.pstips.net/manage-firewall-using-powershell.html

               
 

New-NetFirewallRule -Name "MyService" -DisplayName "我的服务" -Protocol TCP -LocalPort 80,8080,3413-3420 -Action Allow -Enabled True

               
 

               
 

D、安装NET CORE

参考https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-install-script

./dotnet-install.ps1 -Channel 2.0 -InstallDir C:\cli

如果提示错误

Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass

             
 

IIS

参考 https://www.cnblogs.com/dotNETCoreSG/p/aspnetcore-2_5-nano_server_on_core.html

下载安装脚本,执行命令

             
 

             
 

自服务

参考https://docs.microsoft.com/zh-cn/aspnet/core/host-and-deploy/windows-service?view=aspnetcore-2.1

Cmd /c sc create MyService binPath= "c:\temp\publish\WebApplication2.exe" start= auto

Cmd /c sc start MyService

(使用独立发布,安装了2.0框架,在CENTOS中安装了2.1,独立发布出现DLL的版本签名不一致,最终使用框架依赖发布)

public
class
Program

{

public
static
void Main(string[] args)

{

// BuildWebHost(args).Run();

CreateWebHostBuilder(args).RunAsService();

}

             
 

public
static IWebHost BuildWebHost(string[] args)

//WebHost.CreateDefaultBuilder(args)

// .UseStartup<Startup>()

// .Build();

{

//在IIS启动

var config = new ConfigurationBuilder()

.SetBasePath(Directory.GetCurrentDirectory())

.AddJsonFile("hosting.json", optional: true)

.Build();

return
new WebHostBuilder()

.UseConfiguration(config)

.UseKestrel()

.UseContentRoot(Directory.GetCurrentDirectory())

.UseIISIntegration()

.UseStartup<Startup>()

.Build();

}

             
 

public
static IWebHost CreateWebHostBuilder(string[] args)

{

//以服务形式启动

var pathToExe = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;

var pathToContentRoot = Path.GetDirectoryName(pathToExe);

             
 

var config = new ConfigurationBuilder()

.SetBasePath(pathToContentRoot)

.AddJsonFile("hosting.json", optional: true)

.Build();

             
 

return
new WebHostBuilder()

.UseConfiguration(config)

.UseKestrel()

.UseContentRoot(pathToContentRoot)

.UseStartup<Startup>().Build();

}

             
 

}

             
 

         
 

Linux下安装NETCORE

#netcor 安装脚本

#vi /etc/sysconfig/network-scripts/ifcfg-eth0 (可能不是这个名字),将onboot=no修改为yes

ip addr

service network restart

ip addr

     
 

#使用CRT登录方便复制

su

     
 

yum -y install net-tools

ifconfig

mkdir /opt/dotnet

cd /opt/dotnet

     
 

yum -y install wget

yum -y install icu

     
 

wget -c https://dot.net/v1/dotnet-install.sh

     
 

chmod +x dotnet-install.sh

./dotnet-install.sh -Channel 2.1 -InstallDir /opt/dotnet

export PATH=$PATH:/opt/dotnet

./dotnet --info

     
 

yum -y install zip unzip

yum -y install lrzsz

     
 

#部署网站

mkdir /app

cd /app

mkdir BandServer

     
 

#使用rz指令上传网站压缩包,使用unzip解压,注意压缩包的相对目录

#unzip publish.zip

#添加防火墙

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

firewall-cmd --reload

     
 

#启动网站观察是否工作正常

dotnet BandServer.dll

     
 

     
 

#配置守护进程

     
 

yum -y install python-setuptools

easy_install supervisor

supervisord --version

     
 

echo_supervisord_conf > /etc/supervisord.conf

     
 

#编辑supervisord.conf在末尾添加应用(如下,记得去除#)

# [program:bandserver]

# directory=/app/BandServer

# command=/opt/dotnet/dotnet BandServer.dll

# autostart=true

# autorestart=true

# stderr_logfile=/var/log/bandserver.err.log

# stdout_logfile=/var/log/bandserver.out.log

# user=root

# stopsignal=INT

# redirect_stderr=true

     
 

     
 

#设置为开机执行

# vi /etc/rc.local

#添加 supervisord -c /etc/supervisord.conf

#chmod +x /etc/rc.local

systemctl enable rc-local

#手动启动守护

supervisord -c /etc/supervisord.conf

supervisorctl start all

supervisorctl status

     
 

#重启

# reboot