Explain asp.net core deployment project to Ubuntu server

Time:2021-10-6

Based on the successful installation of Ubuntu server 16.10 in the previous article, let’s continue our deployment journey of asp.net core project!

It’s just that I’ve been dealing with windows all these years. It’s really a little hard to use Linux for the first time.

However, for this important cross platform feature of. Net core, even if there are more holes, we still have to bite the bullet.

Otherwise, someone will ask you with surprised eyes: is your. Net core project still deployed on windows?

No more nonsense. I wish you a successful deployment within ten steps< ( ̄︶ ̄) ↗ [GO!]

1、 Installing the. Net core SDK

Enter the following commands in sequence to complete the installation. If there are failures in the process, please try several more times.


sudo sh -c 'echo "deb [arch=amd64] https://apt-mo.trafficmanager.net/repos/dotnet-release/ yakkety main" > /etc/apt/sources.list.d/dotnetdev.list'

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 417A0893

sudo apt-get update

sudo apt-get install dotnet-dev-1.0.1

After installation, try to execute the dotnet command. The following content appears, indicating that you have successfully installed it. Congratulations!

2、 Small trial ox knife

1. Create a folder, name it test, and then switch to it


mkdir test

cd test

2. Initialize the CLI configuration


dotnet new

3. Create an MVC project


dotnet new mvc

4. Build the project and run it


dotnet restore

dotnet run

5. If the monitoring is successful, it means there is no problem. Next, enter the program.cs file and modify it to the following content to listen to all network adapters on port 80

1) Vi. program. CS / / open the program. CS file

2) . I / / enter editing mode

3) Press ESC to exit editing mode

4) ,: WQ / / save and exit

6. Install the reverse agent nginx and start it. Of course, Apache is also a must.


sudo apt-get install nginx

sudo service ngnix start

Then visit the server address. If the following page appears, it indicates that the installation has been successful

7. After installation, enter the following directory, open the default file and modify it to the following content


cd /etc/nginx/sites-available

sudo vim default

By the way, record several common editing commands

VIM program. CS // open or create a new program.cs file

I // enter editing mode

ESC // exit editing mode

YY // copy the line of the cursor

5yy // 5 lines after the copy cursor

P // paste

DD // delete the line of the cursor

5dd // delete the 5 lines after the cursor

U // undo the operation

:q! // Exit without saving

: WQ // save and exit

8. Restart nginx to apply the configuration


sudo nginx -t

sudo nginx -s reload

9. Next is the moment to witness miracles. Re run the test project we just mentioned, then open the local browser and enter: http://192.168.1.104 (if you don’t know your Ubuntu server address, enter ifconfig to view it.) if the following screen appears, the configuration has been successful. The server is no longer IIS, but nginx on Ubuntu, version 1.10.1

Tips: if you don’t succeed, don’t lose heart! Try to update the system with the following command, or uninstall nginx and try again. I believe the dawn is coming!


sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade
sudo reboot

3、 Configure daemon Supervisor

So far, the application is still running manually by ourselves, and we can’t close and exit with Ctrl + C, otherwise your website will be inaccessible! Therefore, we need a monitor like role to ensure the normal operation of asp.net core process, and supervisor will be our first choice.

1. Install Supervisor


sudo apt-get install supervisor

2. Release the MVC test project above and record your release path


dotnet publish

3. After the release is successful, remember to switch to the release directory, execute the dotnet run command, and then repeat step 9 above to see if it can be accessed normally. If there is no problem, let’s go to the next step

4. Switch to the VaR path, create a NETCORE folder, create a test publish folder under it, and then copy all the files published above here


cd /var

sudo mkdir netcore

cd netcore

sudo mkdir test-publish

cd test-publish

sudo cp -a /home/jack/test/bin/Debug/netcoreapp1.1/publish/* /var/netcore/test-publish

ls

5. Create the supervisor configuration file of test.conf and add the following contents (be careful not to make mistakes)


cd /etc/supervisor/conf.d

sudo vim test.conf

6. Restart supervisor to apply configuration! Then try to use this machine to access the IP of Ubuntu server and see if the website is displayed in front of you as you want! If it is consistent with step 9 above, the whole deployment process has come to an end temporarily, so that even if you restart the server, the website can still be accessed. I’m finally relieved to be here.


sudo service supervisor restart

4、 Advanced

Next, I will deploy my previous project on IIS: “asp.net core: enriching your system framework with dapper and swaggerui” to Ubuntu server. A remote login tool needs to be prepared:Putty, which is used to transfer our native release files to the server. At the same time, you can also perform some command operations like in Ubuntu.

1. Install openssh server on Ubuntu server and start the SSH service (the default port is 22)


sudo apt-get install openssh-server

sudo /etc/init.d/ssh start

2. Enable firewall configuration UFW (standby by default and for security), and then open port 22 of SSH service.


sudo ufw default deny

sudo ufw enable

sudo ufw allow ssh

Note: the first command means that all ports are closed by default unless it is specified to be open. As for the ones in the red box, I don’t need to explain too much (laugh but don’t speak ~).

3. Open putty and enter the IP address of Ubuntu server. The port is 22 and the connection type is SSH. You can save this configuration and name it Ubuntu. Next time, double-click to open it. It’s very convenient.

4. When the following prompt appears, select “yes”

5. Then enter the user name and password of the server to log in. If there is no problem, you will see the following screen

5. Small test bull knife: open the command prompt window of windows and enter the following command to copy a test.txt (under disk E) file to the test folder on the Ubuntu server


pscp test.txt [email protected]:test

Then switch to putty for viewing

6. Republish our project and specify the runtime as ubuntu.16.10-x64. For more information, please clickhereView.

Copy codeThe code is as follows:
dotnet publish –framework netcoreapp1.1 –runtime ubuntu.16.10-x64 –output “E:\Publish\Light.Api-Ubuntu” –configuration Release

Unfortunately, there is a big error: “project. Assets. JSON don’t have a target for ‘. Netcoreapp, version = v1.1 / Ubuntu. 16.10-x64′”. After checking on the Internet, it is found that there is no such dependency in the project file csproj. After adding it, run the release command again. If the last screen appears, you will succeed!


<RuntimeIdentifiers>ubuntu.16.10-x64</RuntimeIdentifiers>

At the same time, set our listening port to 6000, otherwise it will not start, because port 5000 has been occupied by the test project test. The useiisintegration method can be removed because we want to publish to the Ubuntu server without IIS, but in this case, your local machine will not be accessible.

7. The most important step is to use psftp. The main commands are open and put. Copy the light.api project we just released to light.api-publish, and you will see a lot of copy records

8. After the replication is completed, switch to the directory of the server. If it can be started, it means that there is no problem

9. Next, needless to say, Ctrl + Art + F2 switches to the second management page, changes the listening port of nginx to 6000, and restarts nginx

10. Hold your breath and enter the server address in the local browser:“ http://192.168.1.105/swagger/ui ”, if you can access it normally and request data, you have succeeded! If the server is found to be unavailable, it is mostly caused by the firewall on the machine and the inability to connect to the database. At this time, you can’t Ping the host. Just turn off the firewall in the control panel! The final rendering, nginx on Ubuntu, is a powerful proof

4、 Write at the end

Maybe careful people have found that I actually have the last step. The daemon of light. API is not configured, so I’ll leave it to you. I won’t tell you, just refer to step 5 above! I hope this article can help those students who have just come into contact with the deployment of asp.net core in Linux. If you have any questions or unclear about this article, welcome to discuss it. I also hope you can support developpaer.