This post was translated from the original with AI assistance. Read the original

Note

This post was written on 2020-10-25 and no longer matches the current setup; it is kept for the record. For the latest, see Reviving My Old Blog with Hugo.

Coding Pages deploys incredibly slowly and the site takes forever to update, which was really disappointing. In the end I decided to host my blog on an Alibaba Cloud server instead. This post walks through in detail how to host a Hexo static blog on an Alibaba Cloud server (Ubuntu 20.04).

About two days ago I was still saying things like “Coding is great, I’m going with it this time!” And yes, it really is easy to use, and it’s free, but it keeps acting up: slow deploys, slow page updates. I just couldn’t stand it anymore, so I decided to host the blog on an Alibaba Cloud server after all, which should be the most stable option.

Preparation#

If, like me, you want to access your blog via a domain name, you’ll need the following:

  1. A domain name and DNS resolution service
  2. A cloud server
  3. A shell terminal app (needed on Windows)

Domain Name#

You can buy a domain from services like Alibaba Cloud or Tencent Cloud, and once you buy one, you get the corresponding DNS service along with it.

Cloud Server#

I recommend Alibaba Cloud or Tencent Cloud; both have discounts for students.

Shell Terminal App#

On Windows, I recommend MobaXterm. It’s more powerful than XShell, and it’s free. Visit the MobaXterm website

Linux and macOS come with a terminal built in.

Setting Up Hexo Locally#

Setting up Hexo requires a Node.js environment. Note that this is done locally, not on the server.

Install Node.js#

Go to https://nodejs.org/zh-cn/download/ and download the installer for your OS. I recommend the LTS version.

Install cnpm#

npm’s default registry isn’t a mirror inside China, and switching registries is a hassle, so it’s easier to just install the “China-accelerated” npm: cnpm.

npm install -g cnpm --registry=https://registry.npm.taobao.org

Install Hexo and Its Dependencies#

cnpm install hexo-cli hexo-server hexo-browsersync hexo-renderer-pug hexo-renderer-sass hexo-renderer-ts --save

Initialize Hexo#

hexo init Hexo # Create a Hexo folder in the current directory and initialize Hexo in it
cd Hexo
cnpm install # Install any dependencies that may still be needed

Start Hexo to Test It#

hexo s

Once it starts successfully, open http://localhost:4000 in your browser and you’ll see Hexo’s default hello world page.

On the Cloud Server#

The following steps use Ubuntu 20.04 as an example; other systems are similar.

Connect to the Cloud Server via SSH#

This step depends on your terminal app. Using Linux’s built-in terminal as an example:

ssh username@cloud-server-public-ip

Enter the password of your cloud server instance and you’re connected.

Usually the username is root, and the password is either one you set yourself or one that was auto-generated.

Install git#

apt-get install git

Initialize a git Repository as the Remote for Hexo Deployment#

Put the repository somewhere that doesn’t require elevated permissions, such as /home.

cd /home
mkdir git
cd git
git init --bare MyBlog.git
mkdir MyBlog

Here, MyBlog.git is the git repository, and MyBlog holds the content.

Create a git Hook for Automatic Deployment#

vim /home/git/MyBlog.git/hooks/post-receive

Write the following:

git --work-tree=/home/git/MyBlog --git-dir=/home/git/MyBlog.git checkout -f

Make it executable:

chmod +x /home/git/MyBlog.git/hooks/post-receive

Configure Nginx#

First install Nginx

apt-get install nginx

Then start it to test

service nginx start

Visit your cloud server’s public IP and check whether you see the message saying Nginx was set up successfully.

Then edit Nginx’s config file:

vim /etc/nginx/sites-available/default

Change the root path from /var/www/html to /home/git/MyBlog

That is:

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;

        # root /var/www/html; 
        root /home/git/MyBlog;
        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }

Now reload the config and restart Nginx

service nginx reload
service nginx restart

If you visit the cloud server’s public IP now, you’ll get a 404, because the local Hexo site hasn’t been deployed to the server’s git repository yet, so the /home/git/MyBlog folder is empty and naturally there are no files to serve.

Deploying the Local Hexo Site to the Cloud Server#

Edit the Deployment section at the end of the _config.yml file in the Hexo root directory:

# Deployment
## Docs: https://hexo.io/docs/deployment.html
deploy:
  type: git
  repository: 
    myblog: username@your-cloud-server-ip:/home/git/MyBlog,master

Then deploy:

hexo clean
hexo g -d

When prompted for a password, type it in and press Enter.

If nothing goes wrong, Hexo is now deployed to the cloud server.

Visit the cloud server’s public IP and you should see the same hello world page you saw locally.

About DNS Resolution#

Use the following setup as a reference:

Host record: blog

Record type: A

Line type: Default

Record value: public IP

MX priority: -

TTL (seconds): 600

This resolves a subdomain. For example, my domain is zhangkexuan.cn, so once resolution succeeds, my blog’s home page is blog.zhangkexuan.cn.

If you want to access the blog via the top-level domain, e.g. zhangkexuan.cn, just change the host record to @.

Note: this uses port 80 on the cloud server with HTTP access. If you need HTTPS, you’ll also need an SSL certificate; search for tutorials on opening port 443 in Nginx and generating SSL certificates. If you use a port other than 80 or 443, you’ll also need to get the domain ICP-filed, change the record type to 显式URL (explicit URL), and set the record value to the form ip:port.

More#

For basic Hexo usage, switching themes, and changing config, see the Hexo website.


References:

The most complete tutorial on deploying a Hexo blog to a server

Comments

⬆︎TOP