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:

  type: git

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

- type: git
- type: heroku

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 
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


  1. Make git bash support rsync (Windows)

    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

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


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:
- type: git
  repository: root@IP_address:/opt/git/hexo.git
  branch: master
  commit: Site updated
  message: hexo {{ now('YYYYMMDD') }}
- type: 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
  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/, 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/
server {
    listen 80;
    listen 443 ssl;


    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/;
    ssl_certificate_key /usr/local/nginx/ssl/;

    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

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