简体   繁体   English

将 ASP.NET Core 从 Visual Studio 发布到 Linux

[英]Publish ASP.NET Core from Visual Studio to Linux

Can I edit a ASP.NET Core application in Visual Studio and deploy to Linux server (eg, Ubuntu )?我可以在 Visual Studio 中编辑 ASP.NET Core 应用程序并部署到 Linux 服务器(例如, Ubuntu )吗?

Is there a tutorial for this problem?是否有针对此问题的教程?

You can check this page in the ASP.NET CORE documentation - https://docs.microsoft.com/en-us/aspnet/core/publishing/linuxproduction您可以在 ASP.NET CORE 文档中查看此页面 - https://docs.microsoft.com/en-us/aspnet/core/publishing/linuxproduction

A good example can also be found in this blog post from Scott Hanselman - https://www.hanselman.com/blog/PublishingAnASPNETCoreWebsiteToACheapLinuxVMHost.aspx一个很好的例子也可以在 Scott Hanselman 的这篇博文中找到 - https://www.hanselman.com/blog/PublishingAnASPNETCoreWebsiteToA CheapLinuxVMHost.aspx

I currently use my own batch script to deploy which follows these steps:我目前使用自己的批处理脚本进行部署,步骤如下:

  1. Publishes the app using dotnet publish command.使用 dotnet publish 命令发布应用程序。
  2. Zips everything using Powershell.使用 Powershell 压缩所有内容。
  3. Copies the zip to the Linux machine using pscp - https://the.earth.li/~sgtatham/putty/0.60/htmldoc/Chapter5.html使用 pscp 将 zip 复制到 Linux 机器 - https://the.earth.li/~sgtatham/putty/0.60/htmldoc/Chapter5.html
  4. Connects to the Linux machine using Windows Bash (you need Windows 10 Anniversary Update for this feature to be available).使用 Windows Bash 连接到 Linux 机器(您需要 Windows 10 周年更新才能使用此功能)。
  5. Calls the unzip command on the Linux machine - needs to be installed there first.在 Linux 机器上调用 unzip 命令 - 需要先安装在那里。
  6. Restarts the supervisor service on the Linux machine.重新启动 Linux 机器上的主管服务。

EDIT: I have added the version of my script which I used when I posted my original answer as requested by Andrew Basarab .编辑:我添加了我按照Andrew Basarab 的要求发布原始答案时使用的脚本版本。 I'm sure it needs some refactoring considering my poor scripting knowledge back then.考虑到我当时糟糕的脚本知识,我确信它需要一些重构。 Please use with caution:请谨慎使用:

@echo off

set PrivateKeyLocation="C:\fakepath\fakefile.ppk {private key for connecting to the remote Linux machine}"
set CertificateFileLocation="/mnt/c/fakepath/fakefile.pem {same key used to execute remote bash commands from my Windows machine}"

for %%* in (.) do set CurrentDirName=%%~nx*
set OutputFolder="%tmp%\%CurrentDirName%"
set OutputZipFile="%tmp%\%CurrentDirName%.zip"
set RemoteHost="ubuntu@54.142.181.122 {remote host address}"
set RemoteLocation="/home/ubuntu {the location to copy the output to}"

dotnet publish -o "%OutputFolder%"

powershell -command "& {&'Compress-Archive' -Path %OutputFolder% -DestinationPath %OutputZipFile%}"
rmdir /s /q %OutputFolder%

pscp -i %PrivateKeyLocation% -pw {private key password} %OutputZipFile% %RemoteHost%:%RemoteLocation%
del /q %OutputZipFile%

bash -c "ssh -i %CertificateFileLocation% %RemoteHost% 'sudo rm -rf %CurrentDirName% ; unzip %CurrentDirName%.zip ; rm -r %CurrentDirName%.zip ; sudo service supervisor restart'"

Some tools and services need to be installed on both machines.一些工具和服务需要在两台机器上安装。 Please refer to the aforementioned post by Scott Hanselman.请参阅 Scott Hanselman 的上述帖子。

我不知道如何Visual Studio 部署到 Ubuntu 服务器,但是如果您可以访问服务器(例如使用 SSH),您可以简单地从 Git 的存储库中提取代码,然后编译、发布和运行。

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

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