ASP.net Core Linux deployment and use Nginx reverse proxy https detailed flow

This article directory is as follows

  1. Operating Environment
  2. Ready to work
  3. MySQL data import and install
  4. Install Nginx
  5. Install .net Core Runtime
  6. Copy .net Core program tries to run
  7. Registration Service
  8. Nginx Reverse Proxy https

First, the operating environment

System Ubuntu 16.04, the deployment of ASP.net Core 2.1 + MySQL. Use Nginx as a reverse proxy https.
There may be differences in some of the steps for this article other Linux systems, but the process is the same.
Other systems installed .net Core Runtime tutorial can refer to Microsoft's documentation
https://dotnet.microsoft.com/learn/dotnet/hello-world-tutorial/install

Second, prepare for work

Tools software need an FTP transfer software and remote access tools. This selection of WinSCP and Putty. Recommend Quguan network download, Baidu search can find.
You also need a domain name and domain name of the private key file and certificate file, I use the secondary domain name certificate and private key applications Tencent cloud.

Three, MySQL data import and install

Linux, MySQL installation and deployment can easily find tutorials on the Internet, not described in detail here. Do not use MySQL readers can skip this section.
(1) install MySQL

sudo su					//转到管理员模式
apt-get install mysql-server		//安装mysql

After (2) After installing MySQL MySQL to configure the
general case of MySQL default encoding is latin1, entered Chinese will be garbled.
Since the project will need a timed event database, so the way the event is also scheduled to open the switch.

cd /etc/mysql/mysql.conf.d/
vi mysqld.cnf

Here a little pits, under the windows MySQL configuration file my.ini only one, and Linux my.cnf here it is by reference to a combination of multiple ini files together, so we need to [mysqld] node in the my.ini to modify this file corresponds to mysqld.cnf. Add in the [mysqld] section

[mysqld]
character-set-server=utf8		#设置默认编码为utf8
event_scheduler=ON			#打开事件调度支持定时任务

The basic use of vi: Move the cursor to be modified where i press enter the edit mode, press Esc to exit after editing the edit mode, press Shift + Q exhaled command line, type exit and return to save wq +, w = storage, q = Exit , q! Exit without saving.
After the configuration file editing is complete restart MySQL

service mysql restart		//重启MySQL

(3) into the database
in advance good export file to the database server using the FTP tool.

mysql -u root -p
输入密码登录后
create database XXX;
use XXX;
set names utf8;
source /home/ubuntu/XXX.sql

XXX is the name of the database, the database file transfers before the source path is followed.

Fourth, install Nginx

apt-get install nginx

After installation may be used: dpkg -L nginx view the installation position

Fifth, install .net Core runtime

Here independent deployment deployment mode selection, only .net Core runtime mounted on it; if you choose to deploy dependent frame, you need to install dotnet-sdk.

It should be Microsoft's first apt-get path to trust and then apt-get install command as follows (the root user has no need to add sudo):

curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg
sudo mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg
sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-ubuntu-xenial-prod xenial main" > /etc/apt/sources.list.d/dotnetdev.list'
sudo apt-get install apt-transport-https
sudo apt-get update
sudo apt-get install dotnet-runtime-2.0.9

After the installation using the command: dotnet where see something like the installation was successful. Here Insert Picture Description
Other systems can refer to Microsoft's documentation: https: //dotnet.microsoft.com/learn/dotnet/hello-world-tutorial/install

Sixth, try to run a copy of ASP.net Core

The release of ASP.net Core files uploaded to the server via FTP, remember publishing options selected Linux x64 independent release, do not rely on the framework of the election.
Once uploaded using the command: dotnet XXX.dll try to run the program, see the following display shows run successfully, look through extranet access port to try ip +, if access to a description has been close to success.
Here Insert Picture Description

VII Registration Service

Now run the program, but also to visit, but this is not enough, because we are unsure whether the program is running all the time, such as some bug causes the program to quit unexpectedly and so on. So we need a registration under Linux Service to run our ASP.net Core. It can be understood as to tell the system has been running in the background of this program, if the program quits unexpectedly have to restart it. What services can be compared at the windows.
Create a service file named: xxx.service. Follows

[Unit]
Description=xxxService_config

[Service]
WorkingDirectory=/home/ubuntu/WebApps/PSDReminder
ExecStart=/usr/bin/dotnet /home/ubuntu/WebApps/PSDReminder/PasswordReminder.dll
Restart=always
# Restart service after 30 seconds if the dotnet service crashes:
RestartSec=30
KillSignal=SIGINT
SyslogIdentifier=PasswordReminder
User=ubuntu
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target

Description fill in a description of the service, you can easily fill
WorkingDirectory fill ASP.net Core Programs folder path
ExecStart run your fill of ASP.net Core's command, in fact, run the command dot net programs in Section VI.
SyslogIdentifier fill log identifies the
User fill in user name, it is best to fill the larger user permissions.

After service files do copy it to the following path
/etc/systemd/system/xxx.service

Execute the following command to enable the service

systemctl enable xxx.service
systemctl start xxx.service

After performing successfully use the following command to view the service running status

systemctl status xxx

Here Insert Picture Description
Press q to exit Review

Eight, the use Nginx reverse proxy https

Reverse proxy forwarded to another address will be appreciated that processing to the user's request. Because .net Core code is not binding domain, so there need to use https Nginx will forward the request to the domain name of our .net Core.
I set .net Core program is listening on port 8888, so it is necessary to forward the request to the default https port 443 to port 8888. Specifically, the following:
modifying /etc/nginx/nginx.conf file, add the following in the http {}

http{
...
...
   server{
      listen 443;
      server_name xxx.xxx.com;#可用域名
      
      ssl on;
      ssl_certificate ca.crt; #证书路径
      ssl_certificate_key privateKey.key; #私钥路径
      ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 
      ssl_prefer_server_ciphers on;
      ssl_ciphers "ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE";
      
      ssl_ecdh_curve secp384r1;
      ssl_session_cache shared:SSL:10m;
      ssl_session_tickets off;
      ssl_stapling on; #ensure your cert is capable
      ssl_stapling_verify on; #ensure your cert is capable
      
      location / {
            proxy_pass http://127.0.0.1:8888;#请求转发地址
            proxy_set_header Host $host;
      }
   }
}

It should be noted that the private key and certificate path path must write a relative path, my approach is a direct right certificate and private key files are placed in the / etc / nginx / folder.

After editing execution command to restart nginx

systemctl restart nginx

If it fails to start nginx conf file is likely to be wrong.

Done

Published 15 original articles · won praise 2 · Views 4865

Guess you like

Origin blog.csdn.net/weixin_38138153/article/details/85942336