Linux下安装Nginx并部署Net Core Web API程序

尝试在CentOS和Ubuntu二个Linux的版本下安装Nginx并部署Net Core程序,整理安装过程作为给团队的部署人员参考。

0. 查看Linux系统版本

$lsb_release -a #Ubuntu下执行
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.2 LTS
Release:    16.04
Codename:   xenial
$cat /etc/redhat-release  #CentOS下执行
CentOS Linux release 7.3.1611 (Core) 

1. 下载安装Net Core

详细的安装步骤参考微软Net Core网址,不同的Linux选择不同的版本。
CentOS下第一次安装先得执行这二句注册一下,然后安装sdk。


$sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
$sudo sh -c 'echo -e "[packages-microsoft-com-prod]\nname=packages-microsoft-com-prod \nbaseurl= https://packages.microsoft.com/yumrepos/microsoft-rhel7.3-prod\nenabled=1\ngpgcheck=1\ngpgkey=https://packages.microsoft.com/keys/microsoft.asc" > /etc/yum.repos.d/dotnetdev.repo'

$sudo yum install libunwind libicu
$sudo yum install dotnet-sdk-2.1.4$sudo yum install aspnetcore-runtime-2.1

$dotnet --version
2.1.4

Ubuntu下第一次安装也得注册,执行语句根据不同的Ubuntu的版本还有些许差异,注册后执行安装sdk。


$curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg
$sudo mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg
$sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-ubuntu-xenial-prod xenial main" > /etc/apt/sources.list.d/dotnetdev.list'

$sudo apt-get install apt-transport-https
$sudo apt-get update
$sudo apt-get install dotnet-sdk-2.1.4

$dotnet --version
2.1.4

Nginx等软件可以通过宝塔(www.bt.cn)进行安装,不多赘述。

 

3. 配置Nginx

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;

    }

 

 

 

4.监视应用程序

创建配置文件:

sudo nano /etc/systemd/system/kestrel-hellomvc.service

 

写入以下配置:

 

[Unit]
Description=simpleWeixin .NET Web API App running on centos7
[Service]
WorkingDirectory=/www/wwwroot/xxx
ExecStart=/usr/bin/dotnet /www/wwwroot/xxx/xxx.Web.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

 

保存和开启服务:

 

systemctl enable kestrel-hellomvc.service

 

启动并查看状态:

 

systemctl start kestrel-hellomvc.service

systemctl status kestrel-hellomvc.service

 

你可能感兴趣的:(.net)