How to build a Hexo blog?

I believe that most people who use Hexo to build personal blogs will deploy to some free code hosting platforms, but these free platforms are always unsatisfactory. For example, although the foreign GitHub platform is completely free, the loading speed in China is very slow, or Many functions of the free version of the code cloud platform in China have been castrated. For example, the domain name cannot be customized, and the submitted code cannot be automatically refreshed every time. It needs to be refreshed manually on the code cloud platform, which is very cumbersome.

  In order to effectively solve some of the appealed issues, if possible, you may wish to set up a Hexo blog on your own cloud server.

Effect demonstration


This is the speed test effect of the Hexo blog deployed on GitHub

Demo1

This is the website speed test effect after the Hexo blog is deployed to the Alibaba Cloud server

Demo2

Environmental preparation


  • Local environment: Windows 10
  • Cloud server environment: Alibaba Cloud ECS (CentOS7.x)

Start deployment


Local environment construction

1. Install Git

Go to the official git website to download the .exe file, Download git, the installation options are still all default, and select when adding the path in the last step Use Git from the Windows Command Prompt.

2. Install Nodejs

To Node.js net official download .exefiles, install all the default options. After the installation is complete, press to Win+Ropen the cmd command prompt, enter node -vand npm -v, if the version number appears, the installation is successful.

Use npm Ali's domestic mirror to accelerate download

bash

npm config set registry https://registry.npm.taobao.org

4. Install Hexo

First create a folder MyBlogto store your blog files, and then go cdto this folder (or right-click to git bash hereopen it directly under this folder ).

Navigate to this directory and enter npm install -g hexo-cliinstallation Hexo. There may be several errors, just ignore it.

bash

npm install -g hexo-cli

Enter after installation hexo -v, if the version number appears, the installation is successful.

Next initialize it hexo, that is, initialize our blog, enter the hexo initinitialization folder

bash

hexo init MyBlog

After the creation is complete, MyBlogthere are:

  • node_modules: Dependent package
  • public: Store the generated page
  • scaffolds: Some templates for generating articles
  • source: Used to store your articles
  • themes:theme**
  • _config.yml: Blog configuration file**

Enter to hexo ggenerate a static web page, and then enter to hexo sopen the local server preview

bash

hexo g
hexo s

 

Hexo

Generate ssh public key

Right-click on the local desktop to Git Bash Hereopen the Git terminal, execute the following command `, press enter all the way

bash

ssh-keygen -t rsa

At this time it will tell you the .sshfolders that have been generated . In git bashthe input

bash

cat ~/.ssh/id_rsa.pub

The output content is the public key information

Alibaba Cloud server environment setup

installationGit

bash

yum install git

Create Gitaccount

bash

adduser git

Add account permissions

bash

chmod 740 /etc/sudoers
vim /etc/sudoers

turn up

bash

## Allow root to run any commands anywhere
root    ALL=(ALL)     ALL

Add the following

bash

git   ALL=(ALL)     ALL

Save and exit and change back to permissions

bash

chmod 400 /etc/sudoers

Set gitaccount password

bash

sudo passwd git

Switch to gituser, create  ~/.ssh folders and  ~/.ssh/authorized_keys files, and assign corresponding permissions

bash

su git
mkdir ~/.ssh
vim ~/.ssh/authorized_keys
# 然后将win10中生成的id_rsa.pub文件中的公钥复制到authorized_keys
chmod 600 /home/git/.ssh/authorized_keys
chmod 700 /home/git/.ssh

In the local Gitterminal, test whether you can log in without password git, which SERVERis to fill in your own cloud host IP. yesAfter you enter it , it will be explained without a password.

bash

ssh -v git@SERVER

Create a directory

bash

#repo作为为Git仓库目录
mkdir /var/repo
chown -R git:git /var/repo
chmod -R 755 /var/repo
#hexo作为网站根目录
mkdir /var/www/hexo
chown -R git:git /var/www/hexo
chmod -R 755 /var/www/hexo

Then create a bare  Git warehouse

bash

cd var/repo
git init --bare hexoBlog.git

Create a new  Git hook for automatic deployment in  /var/repo/hexoBlog.git the next, there is an auto-generated  hooks folders. We need to create a new hook file inside  post-receive.

bash

vim /var/repo/hexoBlog.git/hooks/post-receive

Press  i enter edit mode file, add two lines of code (paste into lower code) in the file, specified  Git in the working tree (source) and a  Git directory (configuration file, etc.)

bash

#!/bin/bash
git --work-tree=/var/www/hexo --git-dir=/var/repo/hexoBlog.git checkout -f

Then, press the  Esc key to exit edit mode, enter :wq" " save and exit.

Modify file permissions to make it executable

bash

chown -R git:git /var/repo/hexoBlog.git/hooks/post-receive
chmod +x /var/repo/hexoBlog.git/hooks/post-receive

So far, the  Git warehouse has been built.

Alibaba Cloud server configuration Nginx

Use the pagoda panel to deploy the Nginx  Linuxpanel 6.0 installation command (for the time being only compatible Centos7.x, please install the stable version 5.9 for other system versions):

bash

yum install -y wget && wget -O install.sh http://download.bt.cn/install/install_6.0.sh && bash install.sh

LinuxPanel 6.0 Professional Edition

bash

curl http://download.bt.cn/install/update6.sh|bash

After the installation is complete, the backend address, account number and password of the panel will be displayed. Open the panel backstage address login panel, select Nginxthe deployment plan, and wait for deployment quietly.

After the deployment is complete, click on the website-add site-enter the domain name (enter your own IPaddress if you don't have a domain name )-the PHPversion at the bottom select "pure static"-submit.

After the website is created, click Settings-Profile

bash

server
{
    listen 80;
  # server_name 填写自己的域名
    server_name luckyzmj.cn blog.luckyzmj.cn;
    index index.php index.html index.htm default.php default.htm default.html;
  # 这里root填写自己的网站根目录,修改为/var/www/hexo
    root /var/www/hexo;

-Save

Click Settings-Site Directory, modify as /var/www/hexo , save

Restart the pagoda panel service

bash

service bt restart

Local Hexo deployment to Alibaba Cloud server

Go to the Hexofolder of the local blog MyBlog, right click Git Bash Hereand enter the command

bash

#定义邮箱(更换为你的邮箱地址就行)
git config --global user.email "[email protected]"
#定义名称(更换自定义一个名称就行)
git config --global user.name "Your Name"

Configure _config.yml, complete automated deployment

Open the folder under the folder of the local Hexoblog and findMyBlog_config.ymldeploy

bash

deploy:
  type: git
  #server改为你的服务IP地址或解析后的域名
  #例如我改为repo: [email protected]:/var/repo/blog.git
  repo: git@server:/var/repo/blog.git
  branch: master

After saving, you can test the deployment

Then enter the local Hexoblog folder MyBlog, right-click Git Bash Here, and enter the command

bash

hexo clean 
hexo g -d

No error description is complete, open the browser and enter your domain name or ipaddress to see your deployed Hexoblog.

So far, we have successfully completed the department, and accessing our server side is much faster than accessing Github.

Tips

During the deployment process, executing hexo d found that the deployment is always wrong, what permissions are not allowed, etc. Here we need to check gitwhether we use gituser operations in the above operation deployment , if not, we need to change the user group for the corresponding directory.

bash

chown -R git:git /var/repo/

This command recursively sets the repouser group of the directory and its subdirectories to git. use simultaneously

bash

chown -R git:git /var/www/hexo

This can solve such problems.

Another problem is that the domain name cannot be accessed after binding. The reason is that as long as any domain name is bound to a domestic server host, it must be filed with the Ministry of Industry and Information Technology and the Ministry of Public Security before it can be used normally. If it is a server from Hong Kong, Macau, Taiwan or a foreign server, you do not need to file.

Guess you like

Origin blog.csdn.net/wx_15323880413/article/details/108266242