General NetCore front-end separation project Linux system deployment steps

        Most of the projects I participated in recently are in the form of NetCore API plus SAP pages, and they are all deployed on the Linux system. This article records the deployment steps of Xinling's server. Using NetCore3.1, MySql database.

        The program name recorded in this article: myapp.dll

        Deployment file save directory:

        Background program: /usr/publish/myapp/server/

        Front-end page: /usr/publish/myapp/web_root

1. Install and configure MySQL

1.1. Install MySQL

        Use the following command to install mysql, please update the software source to get the latest version before installation:

sudo apt-get update  #更新软件源
sudo apt-get install mysql-server #安装mysql

        During the installation process, you will be prompted to set the password of the mysql root user. After the setting is complete, wait for the automatic installation.

        Start and stop mysql service instructions:

service mysql start
service mysql stop

         Use the command to confirm whether the startup is successful, and the mysql node is in the LISTEN state, indicating that the startup is successful

sudo netstat -tap | grep mysql

1.2, MySQL set root account password

        Some Linux versions do not automatically prompt to set the root password after installing mysql. In this case, you need to manually change the root password:

1.2.1, cd to etc/mysql directory, check debian. cnf file:

 

 1.2.2. Use the username and password recorded in the file to log in to mysql:

mysql -u debian-sys-maint -p

 1.2.3. Use sql statement in mysql to modify root account password:

use mysql;
update user set authentication_string=PASSWORD("自定义密码") where user='root';
# update user set authentication_string=PASSWORD("rootadmin") where user='root';
update user set plugin="mysql_native_password";
flush privileges;
quit;

1.2.4. Restart the mysql service after changing the root password

/etc/init.d/mysql restart #远程账户可能会需要输入系统root帐号密码

1.3, mysql basic configuration modification

        Common configurations that need to be modified include enabling remote login, modifying the connection port, ignoring uppercase and lowercase table names, etc.

        Go to the mysql configuration file directory and modify the configuration file mysqld.cnf

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

        To enable remote login, you only need to comment out the bind-address line under the [mysqld] node:

# bind-address = 127.0.0.1

        Modify the port line to change the connection port, the default connection port is 3306, the following configuration is modified to 3316:

port = 3316

        If you need to ignore the case of the database table name, you need to add configuration under [mysqld]:

lower_case_table_names = 1

1.4, configure the mysql account for the program

        Usually, a new account is created separately for the program to connect to mysql, and access the specified database, and use the root account to log in to the database:

mysql -u root -p

        Use the sql statement to create a database for the program first, then create an account for the program and authorize it:

CREATE DATABASE appdatabase;     --程序专用数据库  appdatabase
use mysql;
CREATE USER 'appaccount'@'%' IDENTIFIED BY 'app123456';  --程序专用帐号用户名 appaccount,密码 app123456
GRANT ALL PRIVILEGES ON appdatabase.* TO 'appaccount'@'%' IDENTIFIED BY 'app123456'; 
flush privileges;
quit;

        For the DB First program, you need to import the sql script to initialize the database. The database is automatically initialized when the Code First program starts.

1.5. Automatic database backup

1.5.1. Install Crontab scheduled task service:

sudo apt-get install cron

1.5.2. Create a database backup shell script

cd /usr/publish/myapp
mkdir dbback
chmod 777 dbback/
touch autodbbackupsql.sh
chmod 777 autodbbackupsql.sh
vim autodbbackupsql.sh

        Shell script instructions:

Mysqldump -uappaccount -papp123456 appdatabase> /usr/publish/myapp/dbback/appdatabase_$(date +%Y%m%d_%H%M%S).sql

1.5.3. Add task plan

        Configure the plan using the directive:

crontab -e                #选择编辑器进行编辑

        Add the following command to save and exit:

00 06 * * * bash /usr/publish/myapp/dbback/autodbbackupsql.sh
# m h  dom mon dow   command   
# m 表示分 0-59
# h表示小时 0-23
# dom表示一个月中的第几天 1-31
# mon表示月份 1-12
# dow表示星期中的第几天 0-6(星期天为0)
# * 表示参数未配置
# 以上完整命令即表示 每天早上6点执行命令 bash /usr/publish/myapp/dbback/autodbbackupsql.sh 进行备份

        Restart the scheduled task service after the configuration plan is completed:

service cron restart

2. Install the NetCore environment

2.1. Install related dependencies

        Different system versions have different download addresses for related dependent packages. For details, refer to the official instructions:

Install .NET on Ubuntu - .NET | Microsoft Learn

        Taking Ubuntu 16.04 as an example, the download and installation instructions for dependent packages are:

wget https://packages.microsoft.com/config/ubuntu/16.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
rm packages-microsoft-prod.deb

2.2. Install the SDK

        Use the following command to complete the SDK installation:

sudo apt-get update
sudo apt-get install -y apt-transport-https
sudo apt-get update
sudo apt-get install -y dotnet-sdk-3.1  #不同版本将3.1替换为对应版本号

2.3. Install graphics processing library

        Many programs require image verification codes. In the Linux environment, you need to install a graphics processing library that can be used by netcore, usually using libgdiplus:

sudo apt-get install libgdiplus
cd /usr/lib
sudo ln -s libgdiplus.so gdiplus.dll

2.4. Start the netcore program

        Start the program in background mode:

cd /usr/publish/myapp/server
nohup dotnet myapp.dll > log.log 2>&1 &

        Check the running status of the program:

 ps -ef | grep myapp.dll

2.5, the program starts automatically

        Install the process daemon supervisor and configure it to start on boot:

sudo apt-get install supervisor

        After the installation is complete, create a new node configuration file myapp.conf in the /ect/supervisor/conf.d/ directory:

cd /etc/supervisor/conf.d
touch myapp.conf
vi dotnetmedia.conf

        Modify the node configuration file:

[program:myapp]
command=dotnet myapp.dll
directory=/usr/publish/myapp/server
environment=ASPNETCORE__ENVIRONMENT=Production
user=root
stopsignal=INT
autostart=true
autorestart=true
startsecs=5
stderr_logfile=/var/log/myapp.err.log
stdout_logfile=/var/log/myapp.out.log

        Press esc and enter: wq to save and exit. Modify the supervisor configuration file:

vim /etc/supervisor/supervisord.conf

        Add the following configuration:

[inet_http_server]
port=*:8082
username=myusername
password=myapp123456

        Exit, save, and execute the following command to apply the configuration:

sudo supervisorctl shutdown && sudo supervisord -c /etc/supervisor/supervisord.conf

        Restart the supervisor service:

service supervisor restart

        After the supervisor service is started, the configuration process can be managed through the web page. The browser accesses the port 8082 configured above. After logging in with the username and password configured above, you can see the corresponding management page:

         The page can restart, stop, and monitor the status of the managed services.

3. Install Nginx environment

        Install Nginx using the command:

sudo apt-get install nginx

        Basic configuration modification. For most programs, nginx defaults some configuration items that need to be adjusted:

proxy_headers_hash_max_size 51200;
proxy_headers_hash_bucket_size 6400;

        The configuration node of the new version of the nginx site has been moved to a separate directory:

cd /etc/nginx/sites-enabled
vim default

        Add site configuration:

server {
        listen 8080;
        server_name myapp;
        root /usr/publish/myapp/web_root;
        index index.html;
        
        #配置接口数据跨域代理                                                                                                                               
        location /api{
                proxy_pass http://127.0.0.1:8081;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $host:$server_port;
                proxy_redirect off;
        }
        #图片及下载文件等后台资源路径代理
        location /Resource {
                proxy_pass http://127.0.0.1:8081;
                proxy_redirect off;
        }           
}

        After the configuration is complete, reload nginx:

nginx -s reload

        At this point, the entire project is deployed.

Guess you like

Origin blog.csdn.net/evanyanglibo/article/details/128727992