mycpen

Mycpen

记录学习历程与受益知识
github
telegram
bilibili

04_Hexo-Multi-platform Deployment-Cloud Server-GitHub-Gitee

1. Introduction#

1.1 Preface#

  • The official documentation introduces that Hexo can be deployed to multiple platforms with just one click by using multiple deployers simultaneously.

Official Documentation

Hexo provides a quick and convenient one-click deployment function, allowing you to deploy your website to a server with just one command.

$ hexo deploy

Before you start, you need to modify the parameters in _config.yml. A correct deployment configuration should have at least the type parameter, for example:

deploy:
  type: git

You can use multiple deployers at the same time, and Hexo will execute each deployer in order.

deploy:
- type: git
  repo:
- type: heroku
  repo:

1.2 Explanation#

  • Use Git Hooks to synchronize the static files generated by Hexo. (Later, I abandoned git and switched to rsync.)

2. Steps#

2.1 Create a Git Repository on the Cloud Server#

$ cd /opt/git/

$ git init --bare hexo.git			# Create a remote repository for Hexo	git init --bare repository_name.git

$ cd /opt/git/hexo.git/hooks/ 

$ vim post-receive 
#!/bin/bash
git --work-tree=/opt/www/hexo --git-dir=/opt/git/hexo.git checkout -f

# --work-tree=/opt/www/hexo   Set the path to the working tree
# --git-dir=/opt/git/hexo.git Set the path to the repository

$ chmod +x post-receive				# Add execute permission to the hook file

$ mkdir /opt/www/hexo				# Create the working tree path

$ chmod -R 777 /opt/www/hexo		# Authorize it, otherwise it will return 403 later

2.1.2 Use rsync as the deployer#

Note: Later, I found that uploading to the cloud server using git was too slow (not sure why), so I switched to rsync.

The upload speed of git is as shown in the following figure

image-20220906115955982

  1. Make git bash support rsync (Windows)

    Reference:
    https://blog.csdn.net/m0_48613893/article/details/124104757
    https://blog.csdn.net/qq_38689395/article/details/125758842
    If the file is duplicated, skip it.
    If there is an error, modify the file name: .\usr\bin\msys-xxhash-0.8.0.dll → msys-xxhash-0.dll

  2. ./_config.yml file

    deploy:
    - type: rsync
      host: IP_address
      user: username
      root: root_directory_of_remote_host
      port: 22
      delete: true
      verbose: true
      ignore_errors: false
    

    Reference: https://hexo.io/zh-cn/docs/one-command-deployment#Rsync

Even when transferring large files (about 10M), it is still slow, but faster than before.

2.2 Configure the Local Hexo _config.yml#

  • ./_config.yml file, deploy to GitHub, Gitee, and the cloud server simultaneously
# Deployment
## Docs: https://hexo.io/zh-cn/docs/one-command-deployment.html
deploy:
- type: git
  repository: root@IP_address:/opt/git/hexo.git
  branch: master
  commit: Site updated
  message: hexo {{ now('YYYYMMDD') }}
- type: git
  repository: git@github.com:mycpen/mycpen.github.io.git
  branch: main
  commit: Site updated
  message: hexo {{ now('YYYYMMDD') }}       # The message is a custom commit message, the default is Site updated: YYYY-MM-DD HH:mm:ss
- type: git
  repository: git@gitee.com:mycpen/mycpen.gitee.io.git
  branch: master
  commit: Site updated
  message: hexo {{ now('YYYYMMDD') }}

2.3 Configure SSH Passwordless Login#

  • Add the content of the local SSH public key, such as ~/.ssh/id_rsa.pub, to ~/.ssh/authorized_keys on the cloud server.

2.4 Deployment#

  • In the local Hexo source file project, run the following command:
hexo clean ; hexo g ; hexo d

2.5 Use NGINX as the Web Server#

  • Add a virtual host configuration
$ cat /usr/local/nginx/conf/conf.d/blog.cpen.top.conf
server {
    listen 80;
    listen 443 ssl;

    server_name blog.cpen.top;

    error_page  404  /404.html;

    # Force HTTPS redirection
    if ($scheme = "http") {
        return  307 https://$host$request_uri;
    }

    root /opt/www/hexo;

    ssl_certificate     /usr/local/nginx/ssl/blog.cpen.top.pem;
    ssl_certificate_key /usr/local/nginx/ssl/blog.cpen.top.key;

    access_log /data/service_logs/nginx/blog.cpen.top_access.log misc;
    error_log  /data/service_logs/nginx/blog.cpen.top_error.log;
}

3. Points to Note#

  1. Configure SSH passwordless login.

  2. Grant permissions to the storage directory.
    Grant 777 permission to the working tree path /opt/www/hexo, I didn't grant permission at the beginning, and got a 403 error when accessing. The environment is CentOS 8.2. The reason has been found, see point 5.

  3. Open the ports. Pay attention to whether you need to add a new security group policy on the cloud server. Ports 80 and 443 need to be opened.

  4. Due to SELinux and other reasons.

  5. 403 permission issue
    After querying later, I found the reason for 403. When compiling and installing Nginx, I specified the startup user as a regular user (default is nobody), which caused the working process to have insufficient permissions. The new static files uploaded by Hexo did not have 777 permissions.

    So I changed the startup user of Nginx to root.

    $ vim /usr/local/nginx/conf/nginx.conf
    user    root;
    
    $ nginx -s reload
    

4. References#

Official Documentation One-Command Deployment

* Deploy to Cloud Server Using Git Hooks

Deploy to Cloud Server Using Git Hooks

Deploy to Cloud Server Using Git Hooks Add git User and Authorize Directory

* 403 Access Modify Nginx Startup User to root

* 403 Access Authorize 777 Directory

403 Access CentOS 8 Environment

403 Access SELinux Not Disabled


Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.