Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deploy Hexo blog on Ubuntu server

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "how to deploy a Hexo blog on a Ubuntu server". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to deploy Hexo blogs on Ubuntu servers.

Hexo is a static site generator (Static Site Generator) developed with Node.js, which supports Markdown syntax writing, has a powerful plug-in system, and has excellent performance. When reading many articles shared by the technology community, I saw that many students in China are using this engine, and it seems that the "market share" is not low.

This article describes how to quickly deploy a Hexo blog site on a CVM CVM with Ubuntu 14.04, and how to quickly publish a blog post and deploy it to the Web server directory through a private Git repository on the CVM.

prerequisite

If you want to follow this tutorial to successfully build a Hexo blog, you need to have the following conditions:

A CVM CVM with Ubuntu 14.04 installed. General personal blog traffic is relatively low, the initial choice of 1-core 1GB memory type of server, and there are a lot of free trial time.

Install Node.js and npm on your local computer, and Google is advised to know the specific steps for the corresponding keywords.

In addition, two necessary software packages, Git and Nginx, should be installed on the cloud server. Git is used for version management and deployment, and Nginx is used for static blog hosting.

Sudo apt-get updatesudo apt-get install git nginx-y

In addition, this article takes Windows as an example to demonstrate the operation on the local side, and the operation on Mac will be easier. There is no iTerm2 for Mac on Windows, so we use a terminal consisting of ConEmu and Git Bash instead. If readers are interested, follow-up articles will introduce the specific configuration methods.

1. Local Hexo installation and initialization

There are many Hexo-related packages on NPM, but once the two core components, hexo-cli and hexo-server, are installed, you can get the blog running.

We use Node.js 's package manager npm to install hexo-cli and hexo-server.

Npm install hexo-cli hexo-server-g

Hexo-cli is a command-line tool for Hexo that can be used to quickly create, publish, and deploy blogs; hexo-server is a built-in server for Hexo that can be used for previewing and testing before deployment. The-g option, which indicates a global installation.

Next, do some basic configuration for the Hexo blog, including creating the base file. This step is simple. Hexo provides a shortcut command that only needs to provide a directory address where the files are stored.

Hexo init ~ / hexo_blog

It will be a bit slow to execute the command in the domestic environment. Because it needs to be cloned from Hexo's warehouse on Github; after the repository is cloned successfully, a series of npm commands will be executed automatically to install the dependent module.

At this time, we already have an environment for writing and managing blogs.

two。 Cloud server configuration

After completing the operation on the local side, temporarily return to the configuration of the server. Before doing the following, make sure that you have purchased a CVM and can log in as a ubuntu user.

In this section, you need to accomplish the following things:

Configure a remote repository for deploying static files for the local hexo_blog.

Many tutorials use Github as a transit platform, but it makes the whole process more complex and is affected by the network between the server and the Github. If Github goes down or is blocked, you will not be able to update your blog.

Configure the Nginx managed blog file directory.

Configure the remote repository to automatically update the hook to the blog file directory.

2.1 create a private Git repository

Under / var/repo/, create a naked warehouse called hexo_static (bare repo).

If you do not have a / var/repo directory, you need to create it first; then modify the ownership and user rights of the directory, and then the ubuntu user has permissions for all newly generated directories and files under the / var/repo directory.

Sudo mkdir / var/repo/sudo chown-R $USER:$USER / var/repo/sudo chmod-R 755 / var/repo/

Then, execute the following command:

Cd / var/repo/git init-- bare hexo_static.git2.2 configuration Nginx managed file directory

Next, create the / var/www/hexo directory for Nginx hosting.

Sudo mkdir-p / var/www/hexo

Similar to the previous step, you also need to modify the ownership and permissions of the directory.

Sudo chown-R $USER:$USER / var/www/hexosudo chmod-R 755 / var/www/hexo

Then, modify the default settings for Nginx:

Sudo vim / etc/nginx/sites-available/default

Point the root instruction to the / var/www/hexo directory.

.. server {listen 80 default_server; listen [:]: 80 default_server ipv6only=on; root / var/www/hexo; # part of index index.html index.htm;... that needs to be modified

Save and exit the file. If you purchase and file the domain name in the future, you can change the default_server in the configuration to your domain name.

Finally, restart the Nginx service so that the changes take effect.

Sudo service nginx restart

After this step, you should still get an error when you visit the server's IP, because the / var/www/hexo directory is empty.

2.3 create Git hooks

Next, create a hook in the bare repository hexo_static on the server to transfer the static HTML file to the directory of the Web server, / var/www/hexo, when certain conditions are met.

Create a new hook file under the automatically generated hooks directory:

Vim / var/repo/hexo_static.git/hooks/post-receive

Add two lines of code to the file, specifying the working tree (source code) and Git directory (configuration file, etc.) for Git.

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

Save and exit the file and make it executable.

Chmod + x / var/repo/hexo_static.git/hooks/post-receive

At this point, the configuration of the server is basically finished.

3. Complete the local Hexo configuration

In part 3 of the operation, we will complete the following tasks:

Modify URL and default article layout in Hexo configuration

Create a new blog draft and publish it

Configure hexo_static naked repositories that are automatically deployed to the server

3.1 modify some default configuration of Hexo

After entering the hexo_blog directory, there are mainly the following files.

-rw-r--r-- 1 benjisong 1049089 1619 Feb 24 14:45 _ config.yml-rw-r--r-- 1 benjisong 1049089 1049089 Feb 24 13:51 db.jsondrwxr-xr-x 1 benjisong 1049089 0 Feb 24 12:16 node_modules-rw-r--r-- 1 benjisong 1049089 484 Feb 24 12:16 package.jsondrwxr-xr-x 1 benjisong 1049089 0 Feb 24 13:50 publicdrwxr-xr-x 1 benjisong 1049089 0 Feb 24 12:08 scaffoldsdrwxr-xr-x 1 benjisong 1049089 0 Feb 24 12:13 sourcedrwxr-xr-x 1 benjisong 1049089 0 Feb 24 12:08 themes

Where _ config.yml is the main configuration file for Hexo. Let's first modify the URL address of the blog.

# URL## If your site is put in a subdirectory, set url as' http://yoursite.com/child' and root as'/ child/'url: http://server-ip # enter the actual IP address of the server if no domain name is bound. Root: / permalink:: year/:month/:day/:title/permalink_defaults:

Next, modify the default_layout, which is located in the Writing section. Changing it from post to draft means that every blog post is a draft by default and must be published before it can be accessed on the blog site.

# Writingnew_post_name:: title.md # File name of new postsdefault_layout: draft # the original value is posttitlecase: false # Transform title into titlecase

Temporarily save and exit the file. Continue with the configuration in section 3.3.

3.2. Create a new blog draft and publish it

Here is a brief demonstration of the process of creating a draft blog post and publishing it through Hexo.

Execute the following command to create the first blog post.

Hexo new first-post

You will see output similar to the following:

INFO Created: ~\ Workspace\ Git\ hexo_blog\ source\ _ drafts\ first-post.md

Edit blog posts locally through your familiar editor. Here, we write the content of this article into the first blog.

Title: deploy the Hexo blog on the Ubuntu 14.04 server tags:-Ubuntu-Hexocategories:-Hexocomments: truedate: 2017-02-24 15V 31Rose 31Rose Murray Markdown # the following is the text of the article.

Then, publish the blog with the following command:

Hexo publish first-post

The output looks like this:

INFO Published: ~\ Workspace\ Git\ hexo_blog\ source\ _ posts\ first-post.md

After the blog is pushed to the server, it can be accessed on the website.

3.3 deployment via Git

At this point, it can be said that everything is ready, except for the east wind. This Dongfeng is to push the static content generated by Hexo to the server through Git.

Continue editing the _ config.yml file, find the Deployment section, and modify it as follows:

Deploy: type: git repo: ubuntu@CVM IP address of CVM: / var/repo/hexo_static branch: master

Save and exit the file.

After that, you need to install a Hexo package that is responsible for sending the static content needed by the blog to the set up Git repository.

Npm install hexo-deployer-git-save

After installation, you can test the deployment:

Hexo generate & & hexo deploy

During this period, you may be prompted for the login password of the ubuntu user (if the SSH login is not set). The output after success is roughly as follows:

... Create mode 100644 tags/Node-js/index.html create mode 100644 tags/Ubuntu/index.htmlBranch master set up to track remote branch master from ubuntu@139.199.170.173:/var/repo/hexo_static.To 139.199.170.173:/var/repo/hexo_static * [new branch] HEAD-> masterINFO Deploy done: git

Now we can open 139.199.170.173 in our browser and see our blog site.

We found that Hexo's style of blockquote tags is not very beautiful and needs to be tuned later.

The packaged image can also be uploaded to the official service market for use by all users, and can also be shared directly with other users.

If you want to use the image, you can leave your Tencent Cloud account (the QQ number or mailbox you used when logging in) in the comments section at the bottom of this article.

4.1 use of mirrors

The server has been set up in the image. After starting the CVM CVM through the image, readers only need to set the deployment address and server URL and other parameters of the local Hexo writing environment according to the steps in "completing Hexo Local configuration" in the third part of this article.

It is important to note that when starting the CVM through the image, be sure to reset the password or key, otherwise the maker of the image may easily log in to your server.

Do not choose the installation method shown in the figure above.

Summary

This paper completely introduces the installation and initialization of Hexo blog, how to configure the server and deploy through Git and so on. Unlike other tutorials, instead of using public third-party services such as Github, we created private repositories directly on the server. Then, through the Git hook, the blog static files generated by Hexo are quickly pushed to the managed directory of the Web service.

Because Hexo is developed in Node.js, it may not be very attractive to learners of other languages, because it is more difficult to develop independently. Therefore, the author will also introduce blog engines in other languages, such as Lektor written in Python and Wordpress written in PHP.

At this point, I believe you have a deeper understanding of "how to deploy Hexo blogs on Ubuntu servers". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report