搭建Halo博客

starlin 5,326 2021-08-27

最近买了个一个阿里云的服务器,于是乎想搭建一个博客,之前玩过hexo,wordpress,觉得还不是适合自己,直到发现了Halo这个博客,让我眼前一亮
搭建环境:Centos7
下面记录下搭建的过程(部分摘抄官方文档)

安装jdk11

sudo yum install java-11-openjdk -y

安装Halo

  1. 创建目录halo
mkdir halo
  1. 在Halo包下载至halo目录
curl -L https://github.com/halo-dev/halo/releases/download/v1.4.13/halo-1.4.13.jar --output halo.jar
  1. 创建工作目录Halo
mkdir ~/.halo && cd ~/.halo
  1. 下载示例配置文件到工作目录
wget https://dl.halo.run/config/application-template.yaml -O ./application.yaml 
  1. 编辑配置文件,配置数据库或者端口
    如有其他需求,比如更改数据库为mysql,请参考官方文档参考配置
    以下是我的最终配置(密码部分请自行修改):
server:
  port: 8090

  # Response data gzip.
  compression:
    enabled: false
spring:
  datasource:

    # H2 database configuration.
    driver-class-name: org.h2.Driver
    url: jdbc:h2:file:~/.halo/db/halo
    username: admin
    password: xxxx

    # MySQL database configuration.
#    driver-class-name: com.mysql.cj.jdbc.Driver
#    url: jdbc:mysql://127.0.0.1:3306/halodb?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
#    username: root
#    password: xxxxx

  # H2 database console configuration.
  h2:
    console:
      settings:
        web-allow-others: true
      path: /h2-console
      enabled: true

halo:

  # Your admin client path is https://your-domain/{admin-path}
  admin-path: admin

  # memory or level
  cache: level
  1. 回到下载jar包的目录halo,运行如下命令
java -jar halo.jar

如果出现以下提示,表示安装成功

2021-10-27 16:48:32.938  INFO 1590 --- [           main] run.halo.app.listener.StartedListener    : Halo admin started at   http://127.0.0.1:8090/admin
2021-10-27 16:48:32.938  INFO 1590 --- [           main] run.halo.app.listener.StartedListener    : Halo has started successfully!

打开 http://ip: 端口号 即可看到安装引导界面。

创建halo服务

  1. 下载 Halo 官方的 halo.service 模板
wget https://dl.halo.run/config/halo.service -O /etc/systemd/system/halo.service
  1. 修改halo.service
vim /etc/systemd/system/halo.service
  1. 修改配置
  • YOUR_JAR_PATH:Halo 运行包的绝对路径,例如 /root/halo/halo.jar,注意:此路径不支持 ~ 符号。
  • USER:运行 Halo 的系统用户,如果有按照官方教程创建新的用户来运行 Halo,修改为你创建的用户名称即可。反之请删除 User=USER
  1. 重新加载 systemd
systemctl daemon-reload
  1. 启动halo服务
systemctl start halo
  1. 在系统启动时启动服务
systemctl enable halo
  1. 查看服务日志检查启动状态
journalctl -n 20 -u halo

配置nginx

  1. 安装nginx
yum install nginx
  1. 修改配置文件
    因为要配置ssl,还请提前准备好证书,
    我这里是用的阿里云的免费CA证书
    最终的配置如下:
upstream halo {
  server 127.0.0.1:8090;
}
server {
  listen 80;
  listen [::]:80;
  server_name starlin.top www.starlin.top;
  client_max_body_size 1024m;
  location / {
    proxy_pass http://halo;
    proxy_set_header HOST $host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
}
server {
    listen       443 ssl http2;
    listen       [::]:443 ssl http2;
    server_name  starlin.top www.starlin.top;
    root         /usr/share/nginx/html;

    ssl_certificate "/root/halo/starlin.top.pem";
    ssl_certificate_key "/root/halo/starlin.top.key";
    ssl_session_cache shared:SSL:1m;
    ssl_session_timeout  10m;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;

# Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location / {
      proxy_pass http://halo;
      proxy_set_header HOST $host;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     }

    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }
}

博客主题

推荐这个主题
下载地址

以上,感谢阅读!!!


# blog