ASP. Web Core Step-by-step personal website (7) _linux system porting

Source: Internet
Author: User
Tags dotnet free ssl free ssl certificate ssl certificate symantec ssl nginx reverse proxy


Summary


Consider why we choose. NET Core?
Because it is for high-performance server development, the bloated components of the AspNet, the very lightweight, and Microsoft's cross-platform strategy, the affinity for Docker, the developer is also very friendly, so the overall environment is healthy development, future technology decision-making, the. Net Core also has a great advantage. Well, since. NET Core itself has cross-platform (Windows, Mac OSX, Linux) features, and our site has been deployed on a Windows Server server, this chapter is a great place to learn in production environments How to deploy our website application on a Linux system.


Environment description
    • centos/7.1 (64bit) (Linux operating system)
    • MySQL5.7 (Web application database)
    • . NET Core SDK 2.0.0 (Web application environment)
    • Nginx (reverse proxy server)
    • Supervisor (Manage Web site app daemon)
    • Symantec SSL (domain name certificate, providing HTTPS protocol service)


Here the database is changed from SQL Server to MySQL, because SQL Server 2017 has a high configuration for the running environment, I can't hurt the low-end cloud server of 1G memory, O (╥﹏╥) o


Tools Introduction
    • Xshell 5 (terminal emulation software)
    • WinSCP (graphical SFTP client)
Installing Centos


First the cloud server, reinstall the operating system, select centos/7.1 x86_64 (64bit) version of the image, and set the Access user name and password, the system will help us install the CentOS system automatically. Next we configure the Xshell and WINSCP, so that these 2 tools can normally access and manage the operating system, the relevant configuration and use of the method, please do their own Baidu.
Once you've done this, check the hard drive first:


fdisk -l



found that our second disk cloud disk (/DEV/VDB) was not loaded.
Before mounting, you must first format the hard disk. We chose the EXT4 format:


mkfs.ext4 /dev/vdb


After the hard disk is formatted, create a Mount target directory and mount the disk on the directory:


mkdir /datamount /dev/vdb /data


At this point, we look at the system disk condition:


df -h



As you can see, the cloud disks can be mounted properly. However, if the system is restarted, the disk will still be lost and we must mount the disk each time it is started.
To modify the/etc/fstab file, you need to add a line at the end of the file, as follows:

At this point, we restart the system and found that the disk can be used properly mounted.


Install the. NET Core


Reference Official Document: Prerequisites for. NET Core on Linux
1th, sign up for the Microsoft signing key, and then add the Microsoft product feed:


sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
sudo sh -c 'echo -e "[packages-microsoft-com-prod]\nname=packages-microsoft-com-prod \nbaseurl=https://packages.microsoft.com/yumrepos/microsoft-rhel7.3-prod\nenabled=1\ngpgcheck=1\ngpgkey=https://packages.microsoft.com/keys/microsoft.asc" > /etc/yum.repos.d/dotnetdev.repo'


2nd, update the list of products available for installation, install the components required for. NET core, and then install the. NET Core SDK:


sudo yum update
sudo yum install libunwind libicu
sudo yum install dotnet-sdk-2.0.0


3rd step, set the environment variables:


export PATH=$PATH:$HOME/dotnet


Above, the. NET core environment is configured to complete, you can create a new project HelloWorld, execute the command: dotnet run, normal operation, and use localhost:5000 normal access, there is no more.


Install MySQL


1th step, download the RPM installation package for the MySQL Yum repository:
Download the Yum source RPM installation package on the MySQL website: http://dev.mysql.com/downloads/repo/yum/
2nd step, install the MySQL RPM installation package:


yum localinstall mysql57-community-release-el7-11.noarch.rpm


3rd step, install MySQL server:


yum install mysql-community-server


After the installation is complete, we start MySQL and view the running status and set the boot up:


service mysqld start
service mysqld status
systemctl enable mysqld



After the MySQL installation is complete, a default password is generated for root in the/var/log/mysqld.log file. Find the root default password in the following way:


grep 'temporary password' /var/log/mysqld.log


We get the default password, we can make some security settings, enter the command:


mysql_secure_installation


Finally set remote access permissions:


mysql -u root -p mysql
mysql> grant all privileges on *.* to 'root'@'%' identified by 'password' with grant option;


By default, the MySQL character set is Lanti, consider our use of Chinese habits, modify the MySQL configuration file/etc/my.cnf, add the following line, the default character set to UTF8:


[mysqld] character-set-server=utf8
Program porting


Because. NET core itself is support cross-platform, the program logic does not have to make any adjustments, but we have switched the database to MySQL, so for this piece, tweak it slightly.
First, modify the database connection string in Appsettings.json to a new connection;
The Startup.cs file startup configuration is then modified as follows:


services.AddDbContext<ApplicationDbContext>(options =>
    options.UseMySQL(Configuration.GetConnectionString("DefaultConnection")));


Recompile publish to directory:/data/wwwroot/mywebsite
Enter the directory and execute:


dotnet MyWebSite.dll



Now our project is in normal deployment and running on localhost:5000.


Installing Nginx


Nginx is a high-performance HTTP and reverse proxy server that is easy to install and configure in CentOS, followed by the following:


yum install nginx


After the installation is complete, we start the nginx and set it to boot:


service nginx start
systemctl enable nginx.service


To make Nginx proxy outside the network 80 port, access our internal 5000-Port monitoring website program, you need to modify Nginx configuration file/etc/nginx/nginx.conf:


location / {
  proxy_pass http://localhost:5000;
  proxy_http_version 1.1;
  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection keep-alive;
  proxy_set_header Host $host;
  proxy_cache_bypass $http_upgrade;
}


Finally, we restarted restart Nginx.service and, through the extranet link, found that we could visit our website normally.


Installing Supervisor


In fact, after the above steps, has completed the basic porting and deployment, site access is no problem. However, the. NET core application runs in the shell, and if the shell shuts down, the. NET core application shuts down, causing the app to be inaccessible. And if the. NET core process terminates unexpectedly or the server goes down or restarts, it will cause the service to be unavailable, which we do not want to see.
To solve this problem, we need to have a daemon to listen to the status of an ASP. NET Core application and restart it immediately when the application stops running.
Here, we use the Supervisor tool to help us create such daemons, first to install:


easy_install supervisor


After the installation is complete, the following actions


mkdir /etc/supervisor
echo_supervisord_conf > /etc/supervisor/supervisord.conf


Modify the supervisord.conf file to modify the configuration of the tail of the file:


[include]
files = conf.d/*.conf


Then create a/etc/supervisor/conf.d/mywebsite.conf file with the following content:


[program:MyWebSite]
Command=dotnet .dll ; command to run the program
Directory=/data/wwwroot/MyWebSite/ ; directory where the command is executed
Autorestart=true ; Whether the program quits unexpectedly and automatically restarts
Stderr_logfile=/var/log/MyWebSite.err.log ; error log file
Stdout_logfile=/var/log/MyWebSite.out.log ; output log file
Environment=ASPNETCORE_ENVIRONMENT=Production ; process environment variable
User=root ; the user identity of the process execution
Stopsignal=INT


Run Supervisord to see if it takes effect:


supervisord -c /etc/supervisor/supervisord.conf
ps -ef | grep MyWebSite



Finally, we want to configure Supervisor boot, create a new "/usr/lib/systemd/system/supervisord.service" file


# dservice for systemd (CentOS 7.0+)
# by ET-CS (https://github.com/ET-CS)
[Unit]
Description=Supervisor daemon
[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisor/supervisord.conf
ExecStop=/usr/bin/supervisorctl shutdown
ExecReload=/usr/bin/supervisorctl reload
KillMode=process
Restart=on-failure
RestartSec=42s
[Install]
WantedBy=multi-user.target


Execute the command to enable the service to boot:


systemctl enable supervisord




Configuring the HTTPS protocol


In order to realize the encrypted transmission between the client and the server, to prevent the leakage of data information, to ensure the security of the information transmitted by both parties, we need to add SSL layer under HTTP, I have already applied for Symantec free SSL certificate, so I will briefly explain how to configure it.
In front of our website application, has been through the Nginx reverse proxy to access, it is only by configuring Nginx to manage the certificate file we requested. First download the certificate file, including the. CRT and. Key two files, and then copy to the directory:/etc/nginx/ssl, then edit the nginx configuration file, find the relevant place to make the following changes:


Listen 80 default_server;
Listen [::]:80 default_server;
Listen 443 ssl; #If the hard requirement is to go all the https protocol, remove ssl here
Server_name _;
Root /usr/share/nginx/html;

#ssl on; #If the hard requirements all go https protocol, here open ssl on
Ssl_certificate /etc/nginx/ssl/www.lancel0t.cn.crt;
Ssl_certificate_key /etc/nginx/ssl/www.lancel0t.cn.key;
        
Ssl_session_cache shared: SSL: 1m;
Ssl_session_timeout 5m;
        
Ssl_protocols SSLv2 SSLv3 TLSv1.2;
        
Ssl_ciphers HIGH:!aNULL:!MD5;
Ssl_prefer_server_ciphers on;

#Load configuration files for the default server block.
Include /etc/nginx/default.d/*.conf;  


Finally, we restart Nginx, enter our website address: https://www.lancel0t.cn/, see can be normal access.

At this point, the website application is ported to the Linux environment for perfect completion! Of course, the implementation of the process of the author also stepped on a lot of pits, here hope to give a discussion, what questions Welcome to my blog comments, together.



ASP. Web Core Step-by-step personal website (7) _linux system porting


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.