I have been rewriting my blog in my spare time. I used hexo-generated blog and then ran it on git page. This rewrite is based on the KOA2 framework. Today I will write about the koA2 website deployment and HTTPS configuration. The current rough structure of the site is that koA2 applications run on port 1113, then Nginx reverse-proxies to port 443 (HTTPS), while Nginx forwards port 80 (HTTP) to port 443 (HTTPS).

Pm2 configuration

Pm2 is an open source nodeJs-based process manager, including a complete set of functions of daemon, monitoring, logging, basic Nodejs application daemon choice, in fact, it can not only start Nodejs programs, as long as the general script program it can also be capable of.

Pm2 is a process manager for Node applications with load balancing function. Using PM2 keeps our Node application running in the background. Pm2 also has a very powerful deploy feature that allows you to deploy online sites directly from the local site. Now LET me talk about the specific operation.

  1. Install software on the server. Install Git, Node, pM2, and nginx on the server. After installing these, you need to generate SSH Keys on the server and add the generated PUB key to the Deploy Keys in the setting of the appropriate Git repository.

  2. Local installation Software You need to install Git, Node, and PM2 locally.

  3. The pM2 deploy configuration uses the PM2 ecosystem to generate a configuration sample file, and then opens the configuration generated configuration file. The generated sample configuration file is shown below.

    module.exports = { /** * Application configuration section * http://pm2.keymetrics.io/docs/usage/application-declaration/ */ apps: [ // First application { name: 'API', script: 'app.js', env: { COMMON_VARIABLE: 'true' }, env_production: { NODE_ENV: 'production' } }, // Second application { name: 'WEB', script: 'web.js' } ], /** * Deployment section * http://pm2.keymetrics.io/docs/usage/deployment/ */ deploy: Production: {user: 'node', host: '212.83.163.1', ref: 'origin/master', repo: '[email protected]:repo.git', path: '/var/www/production', 'post-deploy': 'npm install && pm2 reload ecosystem.config.js --env production' }, dev: { user: 'node', host: '212.83.163.1', ref: 'origin/master', repo: '[email protected]:repo.git', path: '/var/www/development', 'post-deploy': 'npm install && pm2 reload ecosystem.config.js --env dev', env: { NODE_ENV: 'dev' } } } };Copy the code

    You can configure multiple applications in apps, and deploy is the configuration to configure different environments. Once you have the examples, you can modify them accordingly. After you have the configuration file, run the pm2 deploy file.config. js production setup command to initialize it. Then run the pm2 deploy file.config. js production command to update the deployed application. (To make it easier to configure these two commands into scripts in package.json). After the command is executed, if Success is displayed, the deployment is successful. Enter the corresponding server IP and port number in the browser to open your deployed website. On the server, you can view the running applications in the Pm2 list.

Nginx configuration

  1. After installing nginx on the server, open the /etc/nginx/sites-enabled directory (the default configuration file directory) and create a new configuration file such as blog.conf. Then open the configuration file for configuration. Example Reverse proxy the running Node service to port 80. My configuration is as follows:

    server { listen 80; server_name blog.zhaofinger.com zhaofinger.com www.zhaofinger.com; access_log /var/www/log/blog-access.log; error_log /var/www/log/blog-error.log; location / { 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_pass http://127.0.0.1:1113/; proxy_redirect off; }}Copy the code

    Proxy_pass corresponds to the port on which the Node service is running. After the file is written, save the file and exit. Then restart the nginx service, service nginx restart, restart the service successfully (if there is an error, you can open the nginx log to check the corresponding error).

  2. To configure HTTPS, first you need to purchase an HTTPS certificate. I bought a free certificate on Aliyun. Open the website and click “Buy certificate”, then select “free DV SSL” and click “Buy now”. After purchasing the certificate, open the certificate console to complete the information. Download the two certificate files as prompted and upload them to the server. Then open the blog.conf file for HTTPS configuration. My configuration is as follows:

    server { listen 443; server_name blog.zhaofinger.com zhaofinger.com www.zhaofinger.com; access_log /var/www/log/blog-access.log; error_log /var/www/log/blog-error.log; ssl on; ssl_certificate /etc/nginx/cert/214259593710649.pem; ssl_certificate_key /etc/nginx/cert/214259593710649.key; ssl_session_timeout 5m; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:! NULL:! aNULL:! MD5:! ADH:! RC4; Ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; location / { 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_pass http://127.0.0.1:1113/; proxy_redirect off; }}Copy the code

    Ssl_certificate and SSL_Certificate_key are the locations of the files just uploaded. After the configuration is complete, save the configuration file and exit and restart the Nginx service. Then open xxx.com to visit the website. After configuring HTTPS, you need to transfer the HTTP service corresponding to port 80 to HTTPS, and add the following configuration:

    server { listen 80; server_name www.zhaofinger.com; rewrite ^/(.*) https://$server_name$1 permanent; # jump to Https}Copy the code

Pm2 + nginx pm2 + nginx

Finally, the source of this blog is hosted on Github, welcome to use, welcome to star.

Any errors in this article are welcome to correct.