在Linux服务器部署 .NET-Core 项目

一、文章概要

 这篇文章是讲述一个Linux 新手将 .NET-Core 项目部署在 Linux 服务器上的一个记录,以及在部署期间遇到的问题以及解决办法。有不恰当的地方。欢迎大神指正。

二、前期准备

 如果你要部署在Linux服务器上,首先你需要连接到Linux服务器。那么用什么工具连呢?1. 如果你是在Windows的系统环境下,我是采用 PuTTY来进行SSH 连接的。可以参考:使用Putty连接SSH管理linux图文教程 。2.如果你的系统是Mac 的,那直接用Terminal 就可以啦。输入:

ssh root@hostname

然后就连接上Linux 服务器啦。

   2.1 项目环境

  • .NET-Core 项目的版本为 .NET Core 1.1
  • Linux 服务器的版本为Ubuntu 14.04

   2.2 所需工具

  • PuTTY

三、文章内容

  在上面的步骤都做好了之后呢,我们登陆到Linux 服务器上,进行如下操作:

  1. 可以先用mkdir /data 创建一个发布用的文件夹。
  2. 然后进入cd data
  3. 操作 git ,先把你的项目clone 到 data文件夹 git clone 项目地址
  4. 进行dotnet 的命令操作。 dotnet restore 进行包的还原,有些因为是有些包是自己公司的,所以要加上自己公司包的地址
dotnet restore -s http://nuget.222.om/nuget/core -s http://nuget.565646.com/nuget/Default
  1. dotnet publish -c release 操作,进行发布。发布成功,得到发布后的路径为:/data/MyProject/MyProject.Web/bin/release/netcoreapp1.1/ubuntu.14.04-x64/publish
  2. supervisor 来管理
  3. 先安装 supervisor 通过 用apt-get install supervisor命令安装supervisor
  4. 在 /etc/supervisor/conf.d 目录中添加 q.conf 配置文件,其中的配置如下:
[program:q]
directory=/data/MyProject/MyProject.Web/bin/release/netcoreapp1.1/ubuntu.14.04-x64/publish
command=/data/MyProject/MyProject.Web/bin/release/netcoreapp1.1/ubuntu.14.04-x64/publish/MyProject.Web
autostart=true
autorestart=true
stderr_logfile=/var/log/q.err.log
stdout_logfile=/var/log/q.out.log
environment=Hosting__Environment=Production
user=root
stopsignal=INT
  1. 运行命令service supervisor restart重启supervisor,站点就会自动启动
  2. 在浏览器输入地址,成功运行。

四、问题解决

   4.1 错误:

Err https://apt-mo.trafficmanager.net trusty/main amd64 Packages
Bad header line

   解决:

https://www.idaima.com/a/6735.html

   4.2 错误:

Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package dotnet-dev-1.0.0-preview2.1-003177
E: Couldn't find any package by regex 'dotnet-dev-1.0.0-preview2.1-003177'

   解决:

由于上一个问题没解决导致

   4.3 错误:

在Linux上还原包失败

   解决:

有些因为是有些包是自己公司的,所以要加上自己公司包的地址```C#
dotnet restore -s http://nuget.222.om/nuget/core -s http://nuget.565646.com/nuget/Default

####   4.4 问题:

> A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name correct and that SQL Server is configured to allow remote connections.(provider: TCP Provider, error: 35 - An internal exception was caught) ---> System.AggregateException: One or more errors occurred. (No such device or address) ---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException: No such device or address

####   相关问题:
> https://github.com/dotnet/corefx/issues/8086

###五、参考文献

* 1.这篇是讲如何在Linux 上配置`.NET-Core`的环境的。[.NET - Powerful Open Source Development](https://www.microsoft.com/net/core#linuxubuntu)
* 2.这篇是讲如何使用Putty连接SSH管理linux图文教程。[使用Putty连接SSH管理linux图文教程](http://down.chinaz.com/server/201109/1169_1.htm)
* 3.这篇是讲sudo 出现unable to resolve host 解决方法。[sudo 出现unable to resolve host 解决方法](http://blog.csdn.net/ichuzhen/article/details/8241847)
* 4.这篇是讲Vim常用的命令,因为我刚刚接触 Linux ,对那些命令有些陌生,大神可以跳过。[Vim常用的命令](http://pizn.github.io/2012/03/03/vim-commonly-used-command.html)
* 5.记一次阿里云Linux服务器安装.net core sdk的问题以及解决方法.[记一次阿里云Linux服务器安装.net core sdk的问题以及解决方法](https://www.idaima.com/a/6735.html)
* 6.System.Net.Internals.InternalSocketException on OSX/Linux (Debian).[System.Net.Internals.InternalSocketException on OSX/Linux (Debian)](https://github.com/dotnet/corefx/issues/8086)
* 7.ASP.NET Core Web API on Ubuntu 14.04 LTS doesn't get SQL connection to MSSQL 2008 server. [ASP.NET Core Web API on Ubuntu 14.04 LTS doesn't get SQL connection to MSSQL 2008 server](http://stackoverflow.com/questions/40150357/asp-net-core-web-api-on-ubuntu-14-04-lts-doesnt-get-sql-connection-to-mssql-200)
* 8.Linux sudo 命令。[Linux sudo 命令](http://www.cnblogs.com/sparkdev/p/6189196.html)
* 9.Linux kill 强制关闭 进程。(不建议使用)[Linux kill 强制关闭 进程](http://blog.csdn.net/lechengyuyuan/article/details/16337233)
* 10.Linux认证辅导:Linux下杀僵尸进程办法。 [Linux认证辅导:Linux下杀僵尸进程办法](http://6244685.blog.51cto.com/6234685/1316234)
原文地址:https://www.cnblogs.com/xiyin/p/6401305.html