Deploy ASP. NET Core to Ubuntu Server, asp. netubuntu

Source: Internet
Author: User
Tags openssh server

Deploy ASP. NET Core to Ubuntu Server, asp. netubuntu

Based on the previous successful installation of Ubuntu Server 16.10, we will continue to deploy the ASP. NET Core project!

It's just that I have been dealing with Windows all these years, and it's really hard to use Linux for the first time.

However, for the important cross-platform feature of. NET Core, even if there are more pitfalls, it is still necessary to stick to the scalp.

Otherwise, someone may ask you with a strange look: Is your. NET Core project still deployed on Windows?

I wish you a successful deployment in 10 steps! <( ̄) [GO!]

1. Install. NET Core SDK

Run the following command in sequence to complete the installation. If any of the installation fails, try the installation several 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 dotnet command, the following content appears, indicating that you have successfully installed, congratulations!

Second, a small test cattle knife

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

mkdir test

cd test
2. Initialize the CLI configuration

dotnet new
3. Create an MVC project

dotnet new mvc
4. Build and run the project

dotnet restore

dotnet run
5. If you can monitor successfully, it means no problem. Next, enter the Program.cs file and modify it to the following content, listening to all network adapters on port 80

1), vi Program.cs // Open Program.cs file
2), i // Enter edit mode
3) Esc key to exit edit mode
4) ,: wq // Save and exit

6. Install the reverse proxy 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 means that the installation has been successful

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

cd / etc / nginx / sites-available

sudo vim default
By the way, record several commonly used editing commands

vim Program.cs // Open or create a new Program.cs file

i // Enter edit mode

Esc key // Exit edit mode

yy // Copy the line where the cursor is

5yy // copy 5 lines after the cursor

p // Paste

dd // Delete the line where the cursor is

5dd // Delete 5 lines after the cursor

u // Undo operation

: q! // Do not save and exit

: wq // Save and exit
8. Restart Nginx to apply the configuration

sudo nginx -t

sudo nginx -s reload
9. The next step is to witness the miracle. Re-run our test project just now, then open the local browser, enter: http://192.168.1.104 (if you do not know your ubuntu server address, enter the ifconfig command to view) access, if the following screen appears, indicating that the configuration success. The server is no longer IIS, replaced by nginx on ubuntu, version 1.10.1

Tips: Do n’t be discouraged if you do n’t succeed! 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
Three, configure the daemon Supervisor

Up to the current location, the application is still running manually by ourselves, and it cannot be closed and exited by Ctrl + C, otherwise your website will be inaccessible! So we need to have a role like a monitor to ensure the normal operation of the ASP.NET Core process, then Supervisor will be our first choice.

1. Install supervisor

sudo apt-get install supervisor
2. Publish the above mvc test project and record your release path

dotnet publish
3. After the release is successful, remember to switch to the release directory and execute the dotnet run command, and then repeat step 9 above to see whether it can be accessed normally.

4. Switch to the var path, create a netcore folder, and then create a test-publish folder under it, and then copy all the files published above to 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 content (be careful not to write the wrong hand shake)

cd /etc/supervisor/conf.d

sudo vim test.conf
6. Restart the supervisor to apply the configuration! Then try to access the IP of the ubuntu server with this machine and see if the website is displayed in front of you as you wish! If it is the same as step 9 above, then the entire deployment process has come to an end, so that even if you restart the server, the website can still be accessed. At this point, I was finally relieved.

sudo service supervisor restart
Fourth, advanced

Next, I will deploy my previous project on IIS: "ASP.NET Core: Use Dapper and SwaggerUI to enrich your system framework" and deploy it to Ubuntu Server, which needs to prepare a remote login tool: Putty, use To transfer our local release files to the server, and 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 the firewall configuration ufw (default is standby, but also for security), and then open the 22 port of the 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 they are specified to be opened. As for the ones in the red box, I do n’t need to explain them too much (laughs without words ~).

3. Open Putty, enter the Ubuntu Server's IP address, port 22, and connection type as SSH. You can save this configuration and name it: ubuntu. Just double-click to open it next time, which is 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. A small test: open the command prompt window of windows and enter the following command to copy a test.txt (under E disk) file to the test folder on the ubuntu server

pscp test.txt jack@192.168.1.105: test
Then switch to Putty to view

6. Republish our project and specify the runtime as ubuntu.16.10-x64. For more information, please click here to view.

Copy the code The code is as follows: dotnet publish --framework netcoreapp1.1 --runtime ubuntu.16.10-x64 --output "E: \ Publish \ Light.Api-Ubuntu" --configuration Release

But unfortunately, there was a big error: "project.assets.json don't have a target for '.NETCoreApp, Version = v1.1 / ubuntu.16.10-x64'", after checking online, it was found that it was a project file There is no such dependency in csproj. After adding it, re-run the release command. If the last screen appears, you are successful!

<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. And the UseIISIntegration method can be removed, because we are going to publish to the Ubuntu server, without IIS, but then your local machine will be inaccessible.

7. To the most important part: use PSFTP, the main commands are open and put, copy the Light.Api project we just released to Light.Api-Publish, you will see a lot of copy records

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

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

10. Hold your breath and enter the server address in the browser of the machine: "http://192.168.1.105/swagger/ui". If you can access it normally and you can request data, it means you have succeeded! If you find that the server is unavailable, it is mostly because the firewall is turned on and the database cannot be connected, because you cannot ping the host at this time, just go to the control panel to turn off the firewall! Finally, nginx on ubuntu is a strong proof

4. Write at the end

Maybe the attentive person has discovered that I still have the last step of the Light.Api daemon not configured, so I will leave it to everyone in the end. I will not tell you that you only need to refer to step 5 above! I hope this article can help those who are new to the deployment of ASP.NET Core in Linux. If you have any questions or unclear articles, please feel free to discuss them. I also hope that everyone will support the home of helpers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.