• 欢迎访问db1024网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏db1024吧

Linux 部署.NET Core教程,教你怎样使用Linux CentOS部署.NET Core项目

主机教程 Liam 1年前 (2019-06-15) 573次浏览 20个评论 扫描二维码
文章目录[隐藏]

现在.NET Core这么火,但是很多朋友都只在window中使用过,不知道怎么在Linux中部署.NET Core,今天VPS234就手把手教大家在Linux 中部署.NET Core,大家照着示例做保证可以成功。下面我们就开始就讲解怎样在Linux 中部署.NET Core项目。

首先此次我们使用的Linux 版本是CentOS 7(注意CentOS 6不能运行.NET Core),使用的是Xshell工具连接Linux,下面我们就开始。

Linux 安装.NET Core运行环境

根据官方的说法,在安装DotNetCore环境之前需要注册Microsoft Key以及Product Repository,并且还需要安装一些依赖,这一步每台机器只需要运行一次,具体命令如下:

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

执行效果如下所示:

Linux 部署.NET Core教程,教你怎样使用Linux CentOS部署.NET Core项目

先用下面的命令更新系统:

sudo yum update

效果如下所示:

Linux 部署.NET Core教程,教你怎样使用Linux CentOS部署.NET Core项目

再用下面的命令安装 .NET Core 2.2

sudo yum install dotnet-sdk-2.2

效果如下所示:

Linux 部署.NET Core教程,教你怎样使用Linux CentOS部署.NET Core项目

然后测试是否安装成功,输入下面的命令:

dotnet –version

可以看到成功后效果如下所示:

Linux 部署.NET Core教程,教你怎样使用Linux CentOS部署.NET Core项目

新建.NET Core项目

.NET Core环境以及搭建好了,然后我们搭建一个.NET Core项目来测试一下。我们用Visual Studio搭建一个最简单的Web API项目,如下所示:

新建一个.NET Core Web项目

Linux 部署.NET Core教程,教你怎样使用Linux CentOS部署.NET Core项目

选择API项目,先不勾选Https选项,如下图所示:

Linux 部署.NET Core教程,教你怎样使用Linux CentOS部署.NET Core项目

可以看见,默认项目是已经创建好的Web API

Linux 部署.NET Core教程,教你怎样使用Linux CentOS部署.NET Core项目

然后我们就需要将项目发布到Linux服务器上,这里我们选择通过FTP发布(Linux安装FTP的方法可以参考我们之前的文章:Linux CentOS 7 搭建FTP新手教程)。

在项目上右键,然后点击“发布:

Linux 部署.NET Core教程,教你怎样使用Linux CentOS部署.NET Core项目

选择FPT发布方式:

Linux 部署.NET Core教程,教你怎样使用Linux CentOS部署.NET Core项目

设置好FTP连接信息

Linux 部署.NET Core教程,教你怎样使用Linux CentOS部署.NET Core项目

点击保存开始发布

Linux 部署.NET Core教程,教你怎样使用Linux CentOS部署.NET Core项目

发布完成如下所示:

Linux 部署.NET Core教程,教你怎样使用Linux CentOS部署.NET Core项目

然后我们再Linux服务器上,查看FTP的根目录,可以看到我们刚刚发布的项目文件

Linux 部署.NET Core教程,教你怎样使用Linux CentOS部署.NET Core项目

这时执行Dotnet Core命令,运行项目即可,使用下面的命令(命令格式为 dotnet+项目文件启动dll路径"):

dotnet VPS234-CoreTest.dll

运行效果如下图所示:

Linux 部署.NET Core教程,教你怎样使用Linux CentOS部署.NET Core项目

可以看到运行的端口是5000

但是这个时候我们还不能从外网访问到,Nginx来转发才能访问。

配置Nginx转发.NET Core 服务

先执行下载工具wget

yum install wget

下载nginx包:

wget -c https://nginx.org/download/nginx-1.11.6.tar.gz

输入下面的命令解压压缩包

tar -zxvf nginx-1.11.6.tar.gz

在执行命令前我们需要添加依赖库,继续输入下面的命令

安装gcc 环境

yum install gcc-c++

安装PCRE依赖库

yum install -y pcre pcre-devel

安装zlib 依赖库

yum install -y zlib zlib-devel

安装OpenSSL安全套接字层密码库

yum install -y openssl openssl-devel

进入Nginx安装目录(cd nginx-1.11.6 )执行配置命令

./configure

执行安装命令

make install

进入配置文件文件夹

cd /usr/local/nginx/conf

编辑nginx.conf配置文件

vi nginx.conf

编辑的内容如下所示,将server节点修改如下所示:

server {    
   listen 80;        #nginx服务器的代理端口    
   server_name _;    #这里也可以填写你的域名    
   location / {    
   proxy_pass http://localhost:5000;    #需要反向代理的IP地址+端口    
   proxy_http_version 1.1;    
   proxy_set_header Upgrade $http_upgrade;    
   proxy_set_header Connection "Upgrade";    
      }    
   }

修改如下所示:

Linux 部署.NET Core教程,教你怎样使用Linux CentOS部署.NET Core项目

然后进入Nginx目录

cd /usr/local/nginx/sbin/

然后执行下面的命令重新启动Nginx

./nginx -s reload

如果重启出现:nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory),可以通过执行下面的命令解决

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

然后再重新启动Nginx

./nginx -s reload

启动.NET Core 项目测试

这个我们再吧.NET Core项目启动起来,进入你的.NET Core项目目录下,执行.NET Core启动命令(上面我们已经讲了怎么启动.NET Core项目),然后这个时候通过你的服务器IP来访问,在浏览器中输入地址:http://你的服务器/values/5

然后可以看到出现"Value"的值,如下所示:

Linux 部署.NET Core教程,教你怎样使用Linux CentOS部署.NET Core项目

Linux服务器部署.NET Core项目总结

以上就是Linux服务器部署.NET Core的示例教程,我们使用的是Linux CentOS 7系统,其他Linux版本基本是一样的逻辑,希望通过本教程可以帮助到需要Linux搭建.NET Core项目的朋友。


db1024 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Linux 部署.NET Core教程,教你怎样使用Linux CentOS部署.NET Core项目
喜欢 (0)
关于作者:
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
(20)个小伙伴在吐槽
  1. Thanks for one's marvelous posting! I definitely enjoyed reading it, you will be a great author.I will make certain to bookmark your blog and definitely will come back someday. I want to encourage one to continue your great posts, have a nice day!
    web hosting sites2020-08-31 22:42 回复 未知操作系统 | Chrome 43.0.2357.125
  2. Pretty! This was an incredibly wonderful article. Thank you for supplying this information.
    cheap flights2020-08-28 11:25 回复 Linux | 未知浏览器
  3. Superb post however , I was wanting to know if you could write a litte more on this subject? I'd be very thankful if you could elaborate a little bit more. Kudos!
    cheap flights2020-08-28 06:24 回复 Windows 7 | Opera 9.80
  4. It is the best time to make some plans for the future and it's time to be happy. I have read this post and if I could I desire to suggest you some interesting things or suggestions. Maybe you can write next articles referring to this article. I wish to read even more things about it!
    cheap flights2020-08-27 19:04 回复 Mac OS X | Chrome 66.0.3359.181
  5. Thank you for your blog post.Really thank you! Awesome.
    Lana2020-08-27 16:27 回复 Windows XP | Internet Explorer 6.0
  6. Great work! That is the kind of information that are meant to be shared across the internet. Disgrace on Google for now not positioning this publish upper! Come on over and consult with my site . Thanks =)
    website hosting companies2020-08-26 07:39 回复 Mac OS X | Chrome 66.0.3359.181
  7. This is the right webpage for anybody who would like to understand this topic. You know so much its almost hard to argue with you (not that I personally would want to…HaHa). You certainly put a new spin on a subject that has been discussed for years. Great stuff, just great! cheap flights y2yxvvfw
    cheap flights2020-08-25 09:33 回复 Linux | Chrome 52.0.2777.35
  8. Hey! Would you mind if I share your blog with my myspace group? There's a lot of people that I think would really enjoy your content. Please let me know. Many thanks
    web hosting reviews2020-08-07 17:10 回复 Linux | Chrome 65.0.3325.109
  9. I absolutely love your blog and find many of your post's to be just what I'm looking for. Do you offer guest writers to write content available for you? I wouldn't mind creating a post or elaborating on some of the subjects you write related to here. Again, awesome web log!
    best website hosting2020-08-07 12:06 回复 Linux | 未知浏览器
  10. Keep on writing, great job!
    content hosting2020-08-06 01:55 回复 Linux | Firefox浏览器 2.0.0.6
  11. It's not my first time to pay a quick visit this web page, i am visiting this web page dailly and take pleasant data from here all the time.
    scotts cheap flights2020-07-31 11:37 回复 未知操作系统 | Chrome 66.0.3359.181
  12. Quality posts is the secret to interest the people to pay a visit the website, that's what this web page is providing.
    cheap flights to florida round trip2020-07-29 16:10 回复 Linux | Chrome 67.0.3396.62
  13. I'm extremely impressed together with your writing abilities as smartly as with the structure on your weblog. Is this a paid topic or did you modify it your self? Either way stay up the nice high quality writing, it is rare to peer a nice weblog like this one nowadays..
    jetcost cheap flights2020-07-27 21:33 回复 Linux | Chrome 67.0.3372.0
  14. I like what you guys tend to be up too. Such clever work and reporting! Keep up the excellent works guys I've you guys to blogroll.
    content hosting2020-07-26 03:39 回复 Windows 8.1 | Firefox浏览器 59.0
  15. My spouse and I stumbled over here from a different web page and thought I might check things out. I like what I see so now i'm following you. Look forward to looking over your web page yet again.
    web hosting company2020-07-23 10:08 回复 Windows XP | 360浏览器 JK
  16. This is a topic that's close to my heart... Thank you! Where are your contact details though?
    best web hosting2020-07-22 11:01 回复 Windows 7 | Internet Explorer 9.0
  17. You really make it seem so easy with your presentation but I find this topic to be actually something that I think I would never understand. It seems too complicated and very broad for me. I'm looking forward for your next post, I'll try to get the hang of it!
    best web hosting2020-07-18 08:25 回复 Linux | Firefox浏览器 52.9
  18. Amazing! This blog looks just like my old one! It's on a entirely different subject but it has pretty much the same page layout and design. Wonderful choice of colors!
    website hosting2020-07-16 20:41 回复 Linux | Chrome 67.0.3396.87
  19. It's very simple to find out any matter on web as compared to textbooks, as I found this article at this web site.
    cbd oil that works 20202020-06-27 18:14 回复 Linux | 未知浏览器
  20. When I originally left a comment I seem to have clicked on the -Notify me when new comments are added- checkbox and from now on whenever a comment is added I get four emails with the same comment. Is there an easy method you are able to remove me from that service? Kudos!
    rsacwgxy g2020-06-23 08:10 回复 Windows Vista | Firefox浏览器 3.1b3