Deploy Hexo to ECS

preface

See a lot of people in the group ask how to deploy Hexo to the cloud server. According to my thought, if there is a server, who still uses Hexo? But there are people. Take advantage of Tencent cloud's machine that went whoring last month has not expired, and quickly reinstall a system to talk about how to deploy Hexo to the cloud server.

Start operation

This article is not a tutorial that starts with installation. This tutorial is built after you have passed the http://localhost:4000 / visit your blog based tutorial. Therefore, Node installation and configuration, Hexo download, etc. are not involved. If you haven 't finished it, you can do Baidu by yourself or read my previous hydrology article: Building Hexo blog from scratch

1, SSH key generation via Git

// If you have not set the global information of git, you need to set it first. It can be ignored
git config --global user.name "yourname"
git config --global user.email youremail@example.com
// Generate SSH key
ssh-keygen -t rsa -C "youremail@example.com"
// Disable automatic conversion, which will cause a warning when uploading later
git config --global core.autocrlf false  

Then the key generated by the students of Windows is under the path of C:\Users\yourname\.ssh, but I don't know the little partners of Linux or MAC. After all, I haven't used these two operating systems.

2, Configure GIT server

To connect to the server, you can use tools such as Xshell or FinalShell. Here I have Xshell as an example, another tool is the same.

  1. logon server

    Since it's a newly reinstalled system, it's easy to log in. See the white characters on black as before.

  2. Check if git exists, if not, install it

    Use the git --version command to check whether there is a git command. I will display not found here. It means there is no GIT and needs to be installed.

  3. Install Git

    yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel -y
    yum install -y git
    

    The installation will be finished soon!

  4. Create users and configure their warehouses

    // Create user
    useradd git
    // Set password
    passwd git 
    // Switch users
    su git 
    // Enter git user's home directory
    cd /home/git/
    // Real directory where the project exists (you can create it elsewhere, of course)
    mkdir -p projects/blog 
    // create folder
    mkdir repos && cd repos
    // Create a bare warehouse
    git init --bare blog.git
    

    cd blog.git/hooks
    // Create the hook function. The input is as follows
    vi post-receive 
    //-------------Split lines do not need to be copied-------------
    #!/bin/sh
    git --work-tree=/home/git/projects/blog --git-dir=/home/git/repos/blog.git checkout -f
    //-------------Split lines do not need to be copied-------------
    

    [the external link image transfer failed. The source station may have anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-adabrtin-1592708755684)( https://cdn.jsdelivr.net/gh/blogimg/HexoStaticFile2@latest/2020/05/20/38a4628161b8cc3619c17bc6c66eca8e.png ]

    Press and hold ESC, then output: wq can save to exit!

    // Add executable permissions for the file you just created
    chmod +x post-receive
    // Log out to root
    exit 
    // add permission
    chown -R git:git /home/git/repos/blog.git 
    

  5. Test whether it can be clone

    Create a new folder anywhere on your computer, and switch bash or other command-line tools here. Start executing the following command (note to replace it with your server ip) to test whether the warehouse can be clone down.

    git clone git@server_ip:/home/git/repos/blog.git
    

  6. Establish SSH trust relationship (password free login)

    Enter the following command on your own computer (note replace with your file path)

    ssh-copy-id -i C:/Users/yourname/.ssh/id_rsa.pub git@server_ip
    // Test whether you can log in
    ssh git@server_ip 
    

    It's over!?

    Well, I don't have the SSH copy ID command, but it doesn't matter. Since I can't use the command, I will manually. If you have no problem with the above two commands, you can skip the process of manually establishing trust relationship.

    Manual settings:

    1. Execute the following commands in turn

      // Switch to git user
      su git
      //Enter the home directory. If there is no. ssh directory, it needs to be created. If there is direct access
      cd ~
      //Create. ssh directory
      mkdir .ssh
      // Grant authority
      chmod 700 .ssh/
      // Enter the. ssh directory
      cd .ssh
      // Edit an authorized_keys and write the public key(id_rsa.pub)content
      vi authorized_keys
      // Give permission after adding
      chmod 600 authorized_keys
      

      If the command in the picture is different from the command given by the code, please refer to the given code. The picture is only for reference.

    2. Test whether the connection is successful on your own computer.

      ssh git@server_ip 
      

      A password is required to log in.

    Pass SSH after completing the above steps git@server_ IP connection to the server does not require a password. If you still need a password, please check if you missed any step

  7. For security, disable the GIT user's shell login rights. So you can only log in with git clone, git push, etc

    Enter the following command on the server side:

    // If not root, switch to root
    su root
    // Check whether git shell is in login mode
    cat /etc/shells 
    // Check to see if it is installed
    which git-shell 
    vi /etc/shells
    // Add the Lu Jin shown in the previous 2 steps, usually in / usr / bin / git shell
    

    Modify permissions in / etc/passwd

    // Modify passwd file
    vi /etc/passwd
    
    // Change the original
    git:x:1000:1000::/home/git:/bin/bash
    
    // Change to
    git:x:1000:1000::/home/git:/bin/git-shell
    

3, Configure NGINX server

This step is mainly used to open the 80 port server. You don't think you're going to use hexo s, do you?

  1. Install NGINX server

    // Installation dependency
    sudo yum install -y yum-utils
    // Install nginx service
    sudo yum install -y nginx
    
  2. Check for successful installation.

    See the output version number and represents success.

  3. Configure NGINX server

    //Running nginx directly
    nginx
     //Stop nginx first
    nginx -s stop 
    //Switch to the configuration file directory of nginx
    cd /etc/nginx
     //Edit file
    vi nginx.conf
    
    // 
    At the same time, otherwise nginx cannot access 
    
    

    Change user to root as shown in the figure below

    Modify the root resolution path (/ home/git/projects/blog /), as shown below

    nginx -s reload
    

    At this time, you can see a prompt - 404 page by directly visiting your server ip address in the browser

4, Configure Hexo Publishing

  1. Configure the_ config.yml file

    deploy:
      type: git #user name
      repo: git@server_ip:/home/git/repos/blog.git #Git warehouse
      branch: master #branch
    

  2. At package.json Add npm script to

    "scripts": {
      "deploy": "hexo clean && hexo g -d",
      "start": "hexo clean && hexo g && hexo s"
    },
    

  3. In this case, npm start is used for local debugging. After debugging, it will be uploaded to the server, and then it can be accessed through the IP of the server!

Bind domain name

Modify the file of NGINX directly. Then the domain name is resolved to the server with A record.

Configure HTTPS (SSL)

Please refer to Baidu!

It's not easy to be original. I can get all the words and pictures by myself. Please respect the author, and indicate the source of reprint.

Tags: git ssh Nginx yum

Posted on Sat, 20 Jun 2020 23:36:50 -0400 by Billett