Since they have servers, always want to do their own blog; But I’m not too keen on hexo and Vuepress. Read a number of articles finally completed last year, this article will be a complete story about a small white from 0 to build a complete process of a blog. In a hurry, the source code has not been optimized, will not mean absolutely in place.

This is my blog address www.xz1024.top/

My background management system admin.xz1024. Top /

The source code

See the old iron hope to go to the message board brush a message support, of course, if really help you, you can also give the blogger brush 50 cents sponsorship oh page

Description of Technology Stack

  • The front end

Nextjs, ANTd, axios write a server.js to start with Pm2

  • Background management system

React, ANTD, and Axios are directly deployed on Nginx

  • The service side

Koa2, mysql, Redis, PM2

First, you need to understand the server

Because the ariyun I bought before expires, I can choose centOS and Ubuntu for the cloud I bought. After buying, I can go to the console to set the user name and password.

Install Node, PM2, nginx, mysql for Linux

nextjs

This direct to the Chinese official website to see the documentation can be no difficulty, mainly the deployment of the problem, need to write a startup script, server.js so more flexible, you can see my source server.js

Koa2 and mysql

Here is the key point, before this content is we often contact, so I did not have too much narrative. Mysql is too difficult to operate directly, so I chose sequelize to write JS directly to the database. The official website documentation is very clear, I won’t go into more details.

Write interface, remember two points, the request and response are on CTX, just write CTX. Body value, the interface automatically return, so easy. You can see my source code my-blog

Hotspot data must be cached in Redis or Node. If you don’t want to check your cuka every time, you can install Redis on the server and start it. Here I use ioredis.js

Nodemailer implements the emailing function

var nodemailer = require('nodemailer')
var smtpTransport = require('nodemailer-smtp-transport');
var mailConfig = require('@/config/index.js').email

smtpTransport = nodemailer.createTransport(smtpTransport({
    host: 'smtp.qq.com'.port: 465.secureConnection: true.// SSL is used
    auth: {
        user: mailConfig.user,
        pass: mailConfig.pass
    }
}));
smtpTransport.sendMail({
		from:"[email protected]".to: "[email protected]".subject: "Title".html: "Hello"
})
Copy the code

Do HTTPS

Tencent cloud bought a domain name after a year of free certificate, directly put the certificate and public key on the server, using nginx HTTPS module under the two configuration point to these two files, if you do not understand HTTPS I strongly you look at the perspective of HTTP protocol

 server {
     listen       443 ssl;
     server_name  www.xz1024.top;
     ssl_certificate      cert/1_xz1024.top_bundle.crt;
     ssl_certificate_key  cert/2_xz1024.top.key;
 }
Copy the code

Software used

Navicat for MySQL and RedisDesktopManager here does not provide download address, the database must be regularly backup, because one day not to play all for nothing, database and redis must remember to set password; Otherwise you’re vulnerable. It’s like running naked

The last

All right; So with that said, let me summarize

① : Node service memory keeps increasing. Finally, it is found that there is a problem with the pM2 configuration. The watch cannot be true

module.exports = {
    "apps": [{name: "koa-app".script: "./index.js".cwd: '/'.// Current working path
            watch: false.instances: 2.// Start two instances
            exec_mode: "cluster".max_memory_restart: "100M".env: {
                NODE_ENV: 'development'.PORT: 4000
            }, 
            env_production: {
                NODE_ENV: 'production'.PORT: 4000
            },
            out_file: './logs/out.log'.// Common log path
            error_file: './logs/err.log'.// Error log path
            merge_logs: true.log_date_format: 'YYYY-MM-DD HH:mm:ss' // Set the date format for the log}}]Copy the code

② : The header of the front-end HTTP request is automatically converted to lowercase after koA2 receives it

If necessary, static resources can be used as CDN, I directly put in oss (small note below),next. Config. js has an assetPrefix configuration. This takes the pressure off the server (of course my site is not stressed at all ~😄)

④ : Node is the most important memory problem, need to do memory monitoring, as well as performance test, test I have not had time to do

const easyMonitor = require('easy-monitor');
easyMonitor('koa-app');
Copy the code