简体   繁体   English

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

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

I would like to run a .NET Core MVC website from an AWS Amazon Linux AMI instance. 我想从AWS Amazon Linux AMI实例运行.NET Core MVC网站。

Here are the steps I have taken so far: 以下是我到目前为止采取的步骤:

  1. Create a template ASP.NET Core Web Application (.NET Core) - C# - MVC Web Application project in Visual Studio 2015. Compile and run application in IIS Express. 在Visual Studio 2015中创建模板ASP.NET核心Web应用程序(.NET核心) - C# - MVC Web应用程序项目。在IIS Express中编译并运行应用程序。 No changes made to any configuration (web.confg, project.json, etc). 没有对任何配置进行任何更改(web.confg,project.json等)。
  2. Upload entire web application solution to GitHub. 将整个Web应用程序解决方案上传到GitHub。
  3. Launch an Amazon Linux AMI (2016.03.2) instance. 启动Amazon Linux AMI(2016.03.2)实例。 Security Group has "all traffic" access open for now for simplicity. 为简单起见,安全组现在打开“所有流量”访问权限。
  4. Use PuTTY to SSH into Linux instance. 使用PuTTY SSH进入Linux实例。 Log in with ec2-user . 使用ec2-user登录。
  5. Update the instance sudo yum update -y 更新实例sudo yum update -y
  6. Install libunwind sudo yum install libunwind -y 安装libunwind sudo yum install libunwind -y
  7. Install gettext sudo yum install gettext -y 安装gettext sudo yum install gettext -y
  8. Install .NET Core curl -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 安装.NET Core curl -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 curl -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
  9. Link sudo ln -s ~/dotnet/dotnet /usr/local/bin 链接sudo ln -s ~/dotnet/dotnet /usr/local/bin
  10. Install .NET Version Manager (DNVM) curl -sSL https://raw.githubusercontent.com/aspnet/Home/dev/dnvminstall.sh | DNX_BRANCH=dev sh && source ~/.dnx/dnvm/dnvm.sh 安装.NET版本管理器(DNVM) curl -sSL https://raw.githubusercontent.com/aspnet/Home/dev/dnvminstall.sh | DNX_BRANCH=dev sh && source ~/.dnx/dnvm/dnvm.sh curl -sSL https://raw.githubusercontent.com/aspnet/Home/dev/dnvminstall.sh | DNX_BRANCH=dev sh && source ~/.dnx/dnvm/dnvm.sh
  11. Run command source /home/ec2-user/.dnx/dnvm/dnvm.sh 运行命令source /home/ec2-user/.dnx/dnvm/dnvm.sh
  12. Install .NET Execution Environment (DNX) dnvm upgrade -r coreclr 安装.NET执行环境(DNX) dnvm upgrade -r coreclr
  13. Install libuv to be used by Kestrel 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 安装libstv以供Kestrel使用 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
  14. Install Git sudo yum install git -y 安装Git sudo yum install git -y
  15. Create directory in '/home/ec2-user' directory for application. 在'/ home / ec2-user'目录中创建应用程序目录。 Move to that directory. 移动到该目录。 mkdir director-name cd directory-name
  16. Clone web app with Git git config user.name "myUserName" git config user.email "myEmail" git clone https://github.com/username/repositoryname.git 使用Git git config user.name "myUserName" git config user.email "myEmail" git clone https://github.com/username/repositoryname.git克隆Web应用程序git config user.name "myUserName" git config user.email "myEmail" git clone https://github.com/username/repositoryname.git
  17. Move to 'project' directory cd solution-name/src/web-project-name . 转到'project'目录cd solution-name/src/web-project-name
  18. Restore packages dotnet restore 还原包dotnet restore
  19. Build application dotnet build 构建应用程序dotnet build
  20. Run application dotnet run 运行应用程序dotnet run

At this point I see the following in the terminal: 此时我在终端中看到以下内容:

Now listening on: http ://localhost:5000 现在收听:http:// localhost:5000

I attempt to hit the AWS DNS/IP with port 5000 tagged at the end (http ://aws-ip-or-dns:5000), but get no response. 我尝试使用端口5000标记的AWS DNS / IP(http:// aws-ip-or-dns:5000),但没有得到响应。

I know that Docker and Mono are tools that I can use, but I would rather get this approach to work. 我知道DockerMono是我可以使用的工具,但我宁愿让这种方法起作用。

The scripts I used to install .NET Core, DNVM, and DNX are some combination of the CentOS and Ubuntu directions from these links: 我用来安装.NET Core,DNVM和DNX的脚本是这些链接中CentOS和Ubuntu方向的一些组合:

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

Disclaimer I am not that experienced with Linux. 免责声明我不熟悉Linux。 It is fair to say I don't understand some of the commands that I'm running. 可以说我不理解我正在运行的一些命令。 But, I'm here to learn! 但是,我在这里学习!

Question: What do I need to do to get a template .NET Core web application running an an AWS Amazon Linux environment? 问题:如何获取运行AWS Amazon Linux环境的模板.NET Core Web应用程序需要做什么?

(My guess is I have something missing with setting up the HTTP server) (我的猜测是我在设置HTTP服务器时缺少一些东西)

I need more reputation to post more than two links, so if someone wants to EDIT, I'd appreciate it. 我需要更多的声誉来发布两个以上的链接,所以如果有人想编辑,我会很感激。

The answer from @user326608 almost gets it there, but I'm going to add the steps I am using now after the release of .NET Core 1.0.0. 来自@ user326608的答案几乎可以实现,但我将在.NET Core 1.0.0发布后添加我现在使用的步骤。

  1. Create a template ASP.NET Core Web Application (.NET Core) - C# - MVC Web Application project in Visual Studio 2015. Compile and run application in IIS Express. 在Visual Studio 2015中创建模板ASP.NET核心Web应用程序(.NET核心) - C# - MVC Web应用程序项目。在IIS Express中编译并运行应用程序。 No changes made to any configuration (web.confg, project.json, etc). 没有对任何配置进行任何更改(web.confg,project.json等)。
  2. Upload entire web application solution to GitHub. 将整个Web应用程序解决方案上传到GitHub。 Do not include project.lock.json in the Git upload. 不要在Git上传中包含project.lock.json
  3. Launch an Amazon Linux AMI (2016.09.0) instance. 启动Amazon Linux AMI(2016.09.0)实例。 Security Group with SSH, HTTP, and HTTPS traffic allowed. 允许使用SSH,HTTP和HTTPS流量的安全组。
  4. Use PuTTY to SSH into Linux instance. 使用PuTTY SSH进入Linux实例。 Log in with ec2-user . 使用ec2-user登录。
  5. Update the instance sudo yum update -y 更新实例sudo yum update -y
  6. Install libunwind sudo yum install libunwind -y 安装libunwind sudo yum install libunwind -y
  7. Download .NET Core curl -sSL -o dotnet.tar.gz https://go.microsoft.com/fwlink/?LinkID=809131 下载.NET Core curl -sSL -o dotnet.tar.gz https://go.microsoft.com/fwlink/?LinkID=809131
  8. Install .NET Core sudo mkdir -p /opt/dotnet && sudo tar zxf dotnet.tar.gz -C /opt/dotnet 安装.NET Core sudo mkdir -p /opt/dotnet && sudo tar zxf dotnet.tar.gz -C /opt/dotnet
  9. Link sudo ln -s /opt/dotnet/dotnet /usr/local/bin 链接sudo ln -s /opt/dotnet/dotnet /usr/local/bin
  10. Configure IP tables sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 5000 配置IP表 sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 5000
  11. Install Git sudo yum install git -y 安装Git sudo yum install git -y
  12. Configure git config --global user.name "myUserName" 配置git config --global user.name "myUserName"
  13. Configure git config --global user.email "myGitEmail@something.com" 配置git config --global user.email "myGitEmail@something.com"
  14. Make directory mkdir /var/coreapp 制作目录mkdir /var/coreapp
  15. Move to directory cd /var/coreapp 移至目录cd /var/coreapp
  16. Get project from Git git clone https://github.com/myUsername/myRepository.git 从Git git clone https://github.com/myUsername/myRepository.git获取项目
  17. Change owner sudo chown -R ec2-user /var/coreapp 更改所有者sudo chown -R ec2-user /var/coreapp
  18. Move to direcotry cd /var/coreapp/solution-name/src/web-project-name 转到direcotry cd /var/coreapp/solution-name/src/web-project-name
  19. Restore dotnet restore , build dotnet build , and run in background nohup dotnet run > /dev/null 2>&1 & 恢复dotnet restore ,构建dotnet build ,并在后台nohup dotnet run > /dev/null 2>&1 &

This solution is working well for me now. 这个解决方案现在对我很有用。 I have a related post trying to create a User Data bootstrap script to try and make this even easier. 我有一个相关的帖子试图创建一个用户数据引导脚本,试图让这更容易。

For anyone needing to update the above for the Microsoft.NETCore.App 1.0.1 September 2016 update, the https://www.microsoft.com/net/core#centos instructions worked for me: 对于需要更新以上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

Subsequently running dotnet --info gives: 随后运行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

After that I deleted my project.lock.json and ran a dotnet restore . 之后我删除了我的project.lock.json并运行了一个dotnet restore

I couldn't get a dotnet run to work directly as my RID wasn't known, but using a Dockerfile with microsoft/dotnet:onbuild and this section in my project.json worked: 我不能让一个dotnet run ,直接为我的RID工作是不知道的,但使用Dockerfilemicrosoft/dotnet:onbuild ,在我的这部分project.json工作:

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

If you are running your AWS instance in VPC mode and you don't have security group that says that it's inbound rule has "All Traffic" and Source is "Anywhere" or if it says that "Custom TCP rule", Port is 5000 and Source is "Anywhere" then it will not allow you to connect to that port. 如果您在VPC模式下运行AWS实例,并且您没有安全组表明其入站规则具有“所有流量”且Source为“Anywhere”或者如果它表示“自定义TCP规则”,则端口为5000并且源是“任何地方”,然后它将不允许您连接到该端口。 In VPC mode all incoming ports are closed by default and you should allow them explicitly with some predefined or your own security groups. 在VPC模式下,默认情况下关闭所有传入端口,您应该明确允许它们使用某些预定义或您自己的安全组。

If you use Opswork (or Chef) you can use the following Chef recipe https://supermarket.chef.io/cookbooks/dotnetcore to install a dotnet core on AWS Linux, assumption are that you have uploaded the a zipped file using dotnet publish file to a S3 bucket. 如果您使用Opswork(或Chef),您可以使用以下Chef配方https://supermarket.chef.io/cookbooks/dotnetcore在AWS Linux上安装dotnet核心,假设您已使用dotnet发布上传了压缩文件文件到S3存储桶。
The layer json has JSON that has the app short name eg 图层json具有JSON,其具有app短名称,例如

{
  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 "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status dotnet
        ;;
  restart|reload|condrestart)
        stop
        start
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart|reload|status}"
        exit 1
esac

exit 0 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM