Can I edit a ASP.NET Core application in Visual Studio and deploy to Linux server (e.g., Ubuntu)?
To publish from Visual Studio, do the following: Change the solution configuration from Debug to Release on the toolbar to build a Release (rather than a Debug) version of your app. Right-click on the project (not the solution) in Solution Explorer and select Publish. In the Publish tab, select Publish.
With ASP.NET Core, you can build and run . NET applications not only on Windows but also macOS and Linux.
You can check this page in the ASP.NET CORE documentation - 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
I currently use my own batch script to deploy which follows these steps:
EDIT: I have added the version of my script which I used when I posted my original answer as requested by 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="[email protected] {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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With