欢迎来到【一个大西瓜】的博客

不曾为梦想奋斗,拿什么去燃烧青春。有梦之人亦终将老去,但少年心气如昨。
太阳每一个时刻都同时是夕阳和朝阳,每天她沉入西边,意味着她同时从另一面土地升起。
扩大
缩小

【ASP.NET Core快速入门】(四)在CentOS上安装.NET Core运行时、部署到CentOS

下载.NET Core SDK

下载地址:https://www.microsoft.com/net/download/windows

第一步:Add the dotnet product feed(添加dotnet产品)

官方安装说明:https://dotnet.microsoft.com/download

Before installing .NET, you'll need to register the Microsoft key, register the product repository, and install required dependencies. This only needs to be done once per machine.(在安装.NET之前,您需要注册Microsoft密钥,注册产品存储库并安装所需的依赖项。 这只需要每台机器完成一次。)

sudo rpm -Uvh https://packages.microsoft.com/config/rhel/7/packages-microsoft-prod.rpm

第二步:Install the .NET SDK(安装.NET SDK)

Update the products available for installation, then install the .NET SDK.(更新可用于安装的产品,然后安装.NET SDK。)

Update the products available for installation, then install the .NET Runtime.(更新可用于安装的产品,然后安装.NET Runtime。

sudo yum update
# 安裝SDK
sudo yum install dotnet-sdk-2.2
# 运行时
sudo yum install aspnetcore-runtime-2.2

安装完成之后我们可以执行dotnet控制台命令来创建dotnet项目了

创建core项目

 在命令行执行  dotnet new --help  命令查看创建帮助

 然后执行  dotnet new webapi -n helloCore  创建webapi项目

 

执行  cd helloCore 命令进入helloCore文件夹

执行  dotnet run  运行webapi,可以看到现在webapi运行在5000端口

这时候访问http://192.168.110.129:5000/无法访问,原因是端口没有打开

这时候程序已经运行了,不可能说停止服务之后再去打开,然后再重新启动

这里我们的做法是再重启一个PuTTy

然后执行  curl http://localhost:5000/api/values 查看api是否可以访问

 

将helloVS项目部署到CentOS

发布之前vs创建的helloVS项目到文件夹,然后我们需要借助一个 FileZilla 工具将文件上传到CentOS上面

 

在远程站点输入 \home 访问home文件夹,然后再home文件夹下创建netcore文件夹

 然后选择发布的文件夹,将文件夹内的文件全部上传上去

传输完成后我们继续使用PuTTy软件查看文件是否上传成功

 

这时候执行dotnet helloVS.dll运行我们发布的内容

遇到以上问题的解决方法:

 说明现在的平台上的dotnet SDK不是2.0.3的,执行以下命令即可:

sudo yum install dotnet-sdk-2.0.3

安装完成后,再次运行helloVS.dll

 

用PuTTy访问5000端口curl http://localhost:5000

这里可以看到网站已经可以访问了

Nginx映射端口

进入nginx安装目录 cd /etc/nginx

查看nginx.conf

可以看到默认是吧所有的*.conf文件引入进来的

刚开始有默认的80端口的配置,我们要把它注释掉

注释后

这时候进入conf.d文件夹发现里面并没有任何文件

 

这时候我们创建一个netcore.conf文件,将80端口对5000对口进行转发

 

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;
    proxy_set_header   X-Forwarded-For 
    proxy_add_x_forwarded_for;
    proxy_set_header   X-Forwarded-Proto $scheme;
    }
}

 配置完成之后执行  nginx -s reload  或者  systemctl restart nginx  进行重启nginx

然后访问80端口

这时候访问发现有可能报502,找了很久才找到解决办法

原因:SELinux配置问题。 
解决:关闭SELinux。 
输入:sestatus,如果SELinux status: enabled ,表示开启,输入 vi /etc/selinux/config  修改配置: SELINUX=disabled 。 

或者

CentOS: 将NGINX加到SELinux的允许名单

 

yum install policycoreutils-python 

cat /var/log/audit/audit.log | grep nginx | grep denied | audit2allow -M mynginx 

semodule -i mynginx.pp 

 然后重启CentOS

 

 

美滋滋~~~

 

posted on 2017-12-17 12:03  一个大西瓜咚咚咚  阅读(9300)  评论(21编辑  收藏  举报

导航