CentOS7 deploys Node project

Update system and install git, vim, curl
yum update -y
yum install curl git -y
Install Node.js via nvm

Install nvm

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.5/install.sh | bash

Verify installation was successful

source ~/.bashrc
nvm --version

Seeing the output version information 0.33.5 means the installation is successful.

Install MySQL 5.7

Download and install

yum install https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm -y
yum install mysql-community-server -y

Start mysql

systemctl start mysqld
systemctl enable mysqld

Find root's initial password

cat /var/log/mysqld.log | grep password

change password

mysql -uroot -p 

Enter the found password after pressing Enter, and the login is successful.
Insert image description here

Deploy Node project

Download the source code of the project

mkdir /var/www
cd /var/www
git clone https://gitee.com/icjs-cc

Globally install ThinkJS command

npm install -g think-cli
thinkjs -V

Install dependencies

cd /var/www/test
npm install 

Create a database and import data

 mysql -uroot -p -e "create database test character set utf8mb4"
 mysql -uroot -p nideshop < /var/www/test/test.sql

Modify project database configuration

vim src/common/config/database.js

After modification

const mysql = require('think-model-mysql');

module.exports = {
    handle: mysql,
    database: 'test',
    prefix: 'test_',
    encoding: 'utf8mb4',
    host: '127.0.0.1',
    port: '3306',
    user: 'root',
    password: '你的密码',
    dateStrings: true
};

Pay attention to the values ​​of encoding and prefix

Compile project

npm run compile
node production.js

Open another terminal to verify whether the startup is successful

curl -I http://127.0.0.1:8080/

Output HTTP/1.1 200 OK, indicating success, Ctrl + C to stop running

Use PM2 management services

Install and configure pm2

npm install -g pm2

Modify pm2.json in the project root directory to:

vim pm2.json

Modified content:

{
  "apps": [{
    "name": "test",
    "script": "production.js",
    "cwd": "/var/www/test",
    "exec_mode": "fork",
    "max_memory_restart": "256M",
    "autorestart": true,
    "node_args": [],
    "args": [],
    "env": {

    }
  }]
}

If the server configuration is high, the values ​​of max_memory_restart and instances can be adjusted appropriately.

Start pm2

pm2 start pm2.json

Verify access

curl -I http://127.0.0.1:8080/
Use nginx as a reverse proxy
yum install nginx -y
systemctl start nginx.service
systemctl enable nginx.service

Test whether the local area can be accessed normally

curl -I localhost 

Modify nginx configuration

cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
vim /etc/nginx/nginx.conf

The content is as follows (just change the content in server)

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen 80;
        server_name nideshop.com www.test.com; # 改成你自己的域名
        root /var/www/test/www;
        set $node_port 8080;

        index index.js index.html index.htm;
        if ( -f $request_filename/index.html ){
            rewrite (.*) $1/index.html break;
        }
        if ( !-f $request_filename ){
            rewrite (.*) /index.js;
        }
        location = /index.js {
            proxy_http_version 1.1;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-NginX-Proxy true;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_pass http://127.0.0.1:$node_port$request_uri;
            proxy_redirect off;
        }

        location ~ /static/ {
            etag         on;
            expires      max;
        }
    }
}

Restart nginx and verify whether nginx can still be accessed normally

nginx -t 
systemctl restart nginx.service
curl  http://127.0.0.1/

If the interface data is returned, it means the operation is successful.

Note: Alibaba Cloud cannot access port 80/443 from the external network by default. Please change the security group configuration of the instance.Configuration Tutorial

Configure https access

Install certbot

yum install epel-release -y
yum install certbot-nginx -y
certbot --nginx

If an error occurs in this step of certbot -nginx, execute

pip install --upgrade --force-reinstall 'requests==2.6.0' urllib3

Re-execute certbot --nginx,Details document

certbot renew --dry-run

Test whether the browser access using https is successful

Guess you like

Origin blog.csdn.net/icjs_cc/article/details/134887265