Publish .Net Core Web API to Linux Host
Publish .Net Core Web API to Linux Host
In this post below, I am going to detail out the step of publishing a .NET Core Web API to Linux Host
SSH the Linux host
- Open any SSH client like gitbash
- Go to the location where the .pem file exists
- ssh -i <pemfilename> -v <username>@<host_IP_Address>
- use sudo su for super admin access to run further command
Create project folder
- Go to /var/www folder (cd /var/www)
- create a new folder for the project. For example SampleWebAPI (mkdir SampleWebAPI )
Publish project in release mode
- Open command prompt as administrator
- Go to Project folder - cd <foldername>
- dotnet publish --configuration release
Copy latest publish code to project folder
- Open file transfer tool like FileZilla
- Connect to linux host with right credentials
- Copy the file from local published folder to /var/www/SampleWebAPI
Change the Nginx configuration file
- Go to cd /etc/nginx/sites-available/
- Edit default config file: vim default
- Add a new configuration (Example given below)
- Save the file and exit editor
- Listen 90 - means that the application will be publicly accessible on port 90 of the public IP
- proxy_pass http://localhost:9000 - means that the SampleWebAPI will be running locally on port 9000
- sudo nginx -s reload
- sudo service nginx start
Run the core web API application
- Go to project folder (cd /var/www/SampleWebAPI )
- dotnet assemblyname.dll run --urls=http://localhost:9000/
- Launch the url <publicIP>:90/ and you should see your application running
Problem with above publish approach
- The app will stop running if the user exits the command prompt, because the dotnet command executed above will terminate
- To keep it running always, a service file needs to be created and put in auto run mode
Create Service file
- The app will stop running if the user exits the command prompt, because the dotnet command executed above will terminate
- To keep it running always, a service file needs to be created.
- Go to /etc/systemd/system/
- Create a new file, name it anything
- vim samplewebapi.service and enter the below command
- Run systemctl daemon-reload to load the new service file
- sudo systemctl enable samplewebapi.service (This command ensures that if the VM is restarted, the service will auto run)
- sudo systemctl stop samplewebapi.service (Stop the Service)
- sudo systemctl start samplewebapi.service (Start the Service)
- Launch the url <publicIP>:90/ and you should see your application running, and it won't stop even if the command prompt is closed
Comments
Post a Comment