Deploy hexo to ECS in combination with nginx + git

preface

Before working on this system, my Hexo blog system was deployed in Github's hosting (the blog is not very written, but the tossing system is good 🤪), The great advantage is that it is easy and convenient, and there is no additional charge. Buying a domain name costs money for domain name resolution, but the biggest pain point is that it is not stable enough, the access speed is slow, and the content can not be deployed every three or five times, or the content is incomplete.

In order to solve the above problems, simply rent a server for filing and use nginx open port for external access;

about

I'm not very familiar with hexo, nginx and git;

hexo has been using a lightweight static page generation tool for a long time, but only a few articles have been written after deployment;

nginx is an easy-to-use server that can provide HTTP services and reverse proxy. The static resources generated by hexo can be deployed to access the Internet;

In this environment, the main function of git is to push the static pages generated by hexo to the set server, but in fact, git is very powerful. You can learn about it yourself;

text

Note: the following contents are carried out on the premise that hexo can be used normally;

Local and ECS

server setting

ssh connection server

Execute ssh root@ipaddress Where IPAddress is the extranet ip during the cloud service period, and the login password is the password set on the corresponding home page after purchasing the server;

Install git and nginx

Execute command
# apt-get install nginx
# apt-get install git 

Add git user

Execute command
# useradd -m git create a user name of GIT to facilitate the management of the user's role
# Passwd git sets the password for the user
# chmod 740 /etc/sudoers change the permissions of this file
# vim /etc/sudoers finds' root ALL=(ALL) ALL 'and adds' git ALL=(ALL) ALL' below it`
implement`chmod 400 /etc/sudoers` Change back to file permissions

Configure secret free ssh

On the local shell side

# SSH keygen - t RSA generate public key
# cat ~/. ssh/id_ rsa. The public key printed by pub cannot be copied without one letter

You can also use simple instructions to upload the public key directly to the server

ssh-copy-id -i ~/.ssh/id_rsa.pub git@ipadress

This command is to perform the next operation on the server. If this is successful, the following step is not required:

On the ECS side

# su git    #Switch to git user
# vim ~/.ssh/authorized_keys  #Create authorized_keys file
# chmod 600 ~/.ssh/authorzied_keys  #Is authorized_ The keys file gives the file owner read and write permissions
# chmod 700 ~/.ssh  #For ssh folders give folder owners read, write, and execute permissions
# vim ~/.ssh/authorized_keys    #Paste the ssh key in

Create git repository and set up git hooks for automatic deployment

# sudo mkdir /var/repo new git warehouse location
# sudo mkdir /var/hexo create a new directory for static resources
# cd /var/repo
# sudo git init --bare blog.git creates a repository called blog
# sudo vim /var/repo/blog. Git / hooks / post update configure hooks for automatic deployment
 Add the next two lines

#!/bin/bash
git --work-tree=/var/hexo --git-dir=/var/repo/blog.git checkout -f
 When we hexo Generated static resources push After connecting to the ECS, automatically execute the operation hooks Deploy the content to the corresponding location;

Permissions for the above files

# cd blog.git/hooks/
# sudo chown -R git:git /var/repo/      
# sudo chown -R git:git /var/hexo         
# Sudo Chmod + X post update gives it executable permission

Configure nginx
sudo nginx -t find the path to the nginx configuration file
sudo vim /etc/nginx/nginx.conf
Find the server {} block in the HTTP {} block. If not, you can add it manually. Remember that it is in http {};
Change (add) the following

 server {
      listen       80;
      server_name  localhost;  #If there is a domain name here, you can change it to a domain name

      #charset koi8-r;

      #access_log  logs/host.access.log  main;

      location / {
          root   /var/hexo;
          index  index.html index.htm;
      }

       error_page  404 = /404.html;

      # redirect server error pages to the static page /50x.html
      #
      error_page   500 502 503 504  /50x.html;
      location = /50x.html {
          root   html;
      }

Permission revocation to git
In order to prevent users from endangering the server through git, their data security is threatened, so it is necessary to change the bash of GIT;

# vim /etc/passwd
 find git User, the last /bin/bash Change to /bin/git-shell

Local settings

Hexo git settings

Enter to include Hexo config.yml In the directory of

#vim config.yml
 Turn to the last to find deploy option,Add the following;
type: git
repo: git@ipadress:/var/repo/blog.git  
branch: master

Tell hexo to find the location in the ECS every time you use hexo d to deploy content. Blog Git will deploy the content to / var / hexo according to the hooks configuration, and nginx will go back to the above path to find the static resources and deploy them in the server;
At the same time, this is also the workflow of the whole system I understand;

summary

The overall construction of this environment is still very simple. It can be completed in about ten minutes and can be used normally;

Reference documents

Document | Hexo

Know how to deploy Hexo blog to the server through git

Learn more

Personal blog: czheng.xyz

Keywords: git Nginx server

Added by random1 on Thu, 06 Jan 2022 09:06:37 +0200