如何 运行 AWS Linux 实例上的 .NET Core MVC 站点

How to Run a .NET Core MVC Site on AWS Linux Instance

我想 运行 来自 AWS Amazon Linux AMI 实例的 .NET Core MVC 网站。

以下是我目前采取的步骤:

  1. 创建模板 ASP.NET 核心 Web 应用程序 (.NET Core) - C# - MVC Web 应用程序 项目 Visual Studio 2015。编译和 运行 IIS Express 中的应用程序。没有对任何配置进行更改(web.confg、project.json 等)。
  2. 将整个 Web 应用程序解决方案上传到 GitHub。
  3. 启动 Amazon Linux AMI (2016.03.2) 实例。为简单起见,安全组现在已开放 "all traffic" 访问权限。
  4. 使用 PuTTY 通过 SSH 连接到 Linux 实例。使用 ec2-user.
  5. 登录
  6. 更新实例sudo yum update -y
  7. 安装libunwindsudo yum install libunwind -y
  8. 安装gettextsudo yum install gettext -y
  9. 安装.NET Corecurl -sSL https://raw.githubusercontent.com/dotnet/cli/rel/1.0.0-preview1/scripts/obtain/dotnet-install.sh | bash /dev/stdin --version 1.0.0-preview1-002702 --install-dir ~/dotnet
  10. Link sudo ln -s ~/dotnet/dotnet /usr/local/bin
  11. 安装 .NET 版本管理器 (DNVM) curl -sSL https://raw.githubusercontent.com/aspnet/Home/dev/dnvminstall.sh | DNX_BRANCH=dev sh && source ~/.dnx/dnvm/dnvm.sh
  12. 运行命令source /home/ec2-user/.dnx/dnvm/dnvm.sh
  13. 安装 .NET 执行环境 (DNX) dnvm upgrade -r coreclr
  14. 安装 libuvKestrel 使用 sudo yum install automake libtool wget -y wget http://dist.libuv.org/dist/v1.8.0/libuv-v1.8.0.tar.gz tar -zxf libuv-v1.8.0.tar.gz cd libuv-v1.8.0 sudo sh autogen.sh sudo ./configure sudo make sudo make check sudo make install sudo ln -s /usr/lib64/libdl.so.2 /usr/lib64/libdl sudo ln -s /usr/local/lib/libuv.so.1.0.0 /usr/lib64/libuv.so
  15. 安装Gitsudo yum install git -y
  16. 在“/home/ec2-user”目录中为应用程序创建目录。移至该目录。 mkdir director-name cd directory-name
  17. 使用 Git git config user.name "myUserName" git config user.email "myEmail" git clone https://github.com/username/repositoryname.git
  18. 克隆网络应用程序
  19. 移动到 'project' 目录 cd solution-name/src/web-project-name.
  20. 恢复包dotnet restore
  21. 构建应用程序dotnet build
  22. 运行申请dotnet run

此时我在终端中看到以下内容:

Now listening on: http ://localhost:5000

我尝试访问 AWS DNS/IP,最后标记了端口 5000 (http://aws-ip-or-dns:5000),但没有得到任何响应。

我知道 DockerMono 是我可以使用的工具,但我宁愿使用这种方法工作。

我用来安装 .NET Core、DNVM 和 DNX 的脚本是 CentOS 和来自这些链接的 Ubuntu 说明的一些组合:

  1. https://docs.asp.net/en/latest/getting-started.html
  2. https://www.microsoft.com/net/core#centos
  3. https://docs.asp.net/en/1.0.0-rc1/getting-started/installing-on-linux.html

免责声明 我对 Linux 不是很有经验。公平地说,我不理解我运行宁的一些命令。但是,我是来学习的!

问题: 我需要做什么才能在 AWS Amazon Linux 环境中获取模板 .NET Core Web 应用程序 运行?

(我的猜测是我在设置 HTTP 服务器时缺少一些东西)

我需要更多声誉才能 post 超过两个链接,所以如果有人想要编辑,我将不胜感激。

如果您 运行 您的 AWS 实例处于 VPC 模式并且您没有安全组表明它的入站规则具有 "All Traffic" 并且源是 "Anywhere" 或者如果它说 "Custom TCP rule",端口是 5000,源是 "Anywhere" 那么它将不允许你连接到那个端口。 在 VPC 模式下,所有传入端口默认关闭,您应该使用一些预定义的或您自己的安全组明确允许它们。

对于需要为 Microsoft.NETCore.App 1.0.1 2016 年 9 月更新更新上述内容的任何人,https://www.microsoft.com/net/core#centos 说明对我有用:

curl -sSL -o dotnet.tar.gz https://go.microsoft.com/fwlink/?LinkID=827529
sudo mkdir -p /opt/dotnet && sudo tar zxf dotnet.tar.gz -C /opt/dotnet
sudo rm /usr/local/bin/dotnet
sudo ln -s /opt/dotnet/dotnet /usr/local/bin

随后 运行 dotnet --info 给出:

.NET Command Line Tools (1.0.0-preview2-003131)

Product Information:
 Version:            1.0.0-preview2-003131
 Commit SHA-1 hash:  635cf40e58

Runtime Environment:
 OS Name:     amzn
 OS Version:  2016.09
 OS Platform: Linux
 RID:         amzn.2016.09-x64

之后我删除了我的 project.lock.json 和 运行 一个 dotnet restore

我无法让 dotnet run 直接工作,因为我的 RID 未知,但是使用 Dockerfilemicrosoft/dotnet:onbuild 以及我的 [=19= 中的这一部分] 有效:

"runtimes": {
    "debian.8-x64" : {}
},

@user326608 的回答差不多就到这里了,但我将在 .NET Core 1.0.0 发布后添加我现在使用的步骤。

  1. 创建模板 ASP.NET 核心 Web 应用程序 (.NET Core) - C# - MVC Web 应用程序 项目 Visual Studio 2015。编译和 运行 IIS Express 中的应用程序。没有对任何配置进行更改(web.confg、project.json 等)。
  2. 将整个 Web 应用程序解决方案上传到 GitHub。 不要在 Git 上传中包含 project.lock.json
  3. 启动 Amazon Linux AMI (2016.09.0) 实例。允许 SSH、HTTP 和 HTTPS 流量的安全组。
  4. 使用 PuTTY 通过 SSH 连接到 Linux 实例。使用 ec2-user.
  5. 登录
  6. 更新实例sudo yum update -y
  7. 安装libunwindsudo yum install libunwind -y
  8. 下载.NET Corecurl -sSL -o dotnet.tar.gz https://go.microsoft.com/fwlink/?LinkID=809131
  9. 安装.NET Coresudo mkdir -p /opt/dotnet && sudo tar zxf dotnet.tar.gz -C /opt/dotnet
  10. Link sudo ln -s /opt/dotnet/dotnet /usr/local/bin
  11. 配置 IP 表 sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 5000
  12. 安装Gitsudo yum install git -y
  13. 配置git config --global user.name "myUserName"
  14. 配置git config --global user.email "myGitEmail@something.com"
  15. 创建目录mkdir /var/coreapp
  16. 移动到目录cd /var/coreapp
  17. 从 Git git clone https://github.com/myUsername/myRepository.git
  18. 获取项目
  19. 更改所有者sudo chown -R ec2-user /var/coreapp
  20. 移动到目录 cd /var/coreapp/solution-name/src/web-project-name
  21. 在后台恢复 dotnet restore、构建 dotnet build 和 运行 nohup dotnet run > /dev/null 2>&1 &

这个解决方案现在对我来说效果很好。我有一个 related post 试图创建一个 用户数据 bootstrap 脚本来尝试使这更容易。

如果您使用 Opswork(或 Chef),您可以使用以下 Chef 配方 https://supermarket.chef.io/cookbooks/dotnetcore 在 AWS Linux 上安装一个 dotnet 核心,假设您已经使用 dotnet 上传了一个压缩文件将文件发布到 S3 存储桶。
json 层有 JSON 具有应用短名称,例如

{
  myapp {}
}

# The recipe to install
# 1) Figure which App needs to be installed using JSON in Opsworks layer
# 2) Get the Zip file from S3 and Unzip into /usr/bin/myapp/publish
# 3) Create bash file to start
# 4) Start the app 

    apps = search(:aws_opsworks_app)
    apps.sort_by { |v| v["shortname"] }.each do |app|
        appname = app["shortname"]
        app_source = app["app_source"]
        bucket, remote_path = OpsWorks::SCM::S3.parse_uri("#{app_source['url']}")
        filename = remote_path.split("/")[-1]
        extn = File.extname  filename
        if (!node["#{appname}"].nil? && extn == '.zip')
            apppath = "/usr/bin/#{appname}"
            dotnetapp = app["environment"]["dotnetapp"]
            aspnetcore_environment = app["environment"]["aspnetcore_environment"]
            Chef::Log.info("App dotnet Name:#{app[:environment][:dotnetapp]}")
            Chef::Log.info("Enviroment:#{aspnetcore_environment}")


            app_checkout = ::File.join(Chef::Config["file_cache_path"], app["shortname"])

            package = "#{app_checkout}/#{filename}"

# Use https://github.com/awslabs/opsworks-windows-demo-cookbooks or any s3_file recipe to download the zip file
# if you copy the opsworks-windows-cookbooks you will to modify the recipe a little to work on aws linux
            opsworks_scm_checkout app["shortname"] do
                destination      app_checkout
                repository       app_source["url"]
                user             app_source["user"]
                password         app_source["password"]
                type             app_source["type"]
                ssh_key          app_source["ssh_key"]
                revision         app_source["revision"]
            end


            directory "#{apppath}" do
            end

            execute 'unzip package' do
                command "unzip -o #{app_checkout}/#{filename} -d #{apppath}"

            end

            # create a sysvint sh file to manage dotnet service 
            initd_directory = "/etc/init.d"

            intd_file = File.join(initd_directory, app["shortname"])

            template intd_file do
                mode "744"
                source 'file.sh.erb'
                variables(
                    :service_name => app["shortname"],
                    :dotnetapp => "#{dotnetapp}",
                    :apppath => "#{apppath}/publish"
                )
            end
            execute "start service #{dotnetapp}" do
                command ".#{initd_directory}/#{appname} start"
                environment ({"ASPNETCORE_ENVIRONMENT" => "#{aspnetcore_environment}"})
            end
            Chef::Log.info("End Install #{appname}")
        end
    end

# The ERB Template:

#!/bin/bash
#
# description: <%= @service_name %>
#

# Get function from functions library
. /etc/init.d/functions
#
# Start the service <%= @service_name %>
#
start() {
        initlog -c "echo -n Starting dotnet <%= @service_name %> server: "
        cd <%= @apppath %>
        nohup dotnet <%= @dotnetapp %> /dev/null 2>&1 &
        ### Create the lock file ###
        touch /var/lock/subsys/<%= @service_name %>
        success $"<%= @service_name %> server startup"
        echo
}

# Restart the service <%= @service_name %>
stop() {
        initlog -c "echo -n Stopping dotnet <%= @service_name %> server: "
        killproc dotnet
        ### Now, delete the lock file ###
        rm -f /var/lock/subsys/<%= @service_name %>
        echo
}

### main logic ###
case "" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status dotnet
        ;;
  restart|reload|condrestart)
        stop
        start
        ;;
  *)
        echo $"Usage: [=10=] {start|stop|restart|reload|status}"
        exit 1
esac

exit 0