环境要求
- 512 MB 及以上内存
- Mysql 5.7 及以上版本
- jdk 1.8
(jdk 1.8的安装方法参考这篇文章:使用sdkman快速安装和管理多版本Java sdk)
开始安装:
首先创建一个供Halo使用的数据库,编码选择utf8mb4
创建halo目录并进入halo目录
root@www:~# mkdir halo
root@www:~# cd halo
root@www:~/halo# ls
root@www:~/halo#
下载最新的可执行jar包:
wget https://github.com/halo-dev/halo/releases/download/v1.4.2/halo-1.4.2.jar -O halo-latest.jar
或者:
curl -L https://github.com/halo-dev/halo/releases/download/v1.4.2/halo-1.4.2.jar --output halo-latest.jar
下载配置文件:
示例代码:
curl -o ~/.halo/application.yaml --create-dirs https://dl.halo.run/config/application-template.yaml
显示结果如下,说明没有问题。
root@www:~/halo# wget https://github.com/halo-dev/halo/releases/download/v1.4.2/halo-1.4.2.jar -O halo-latest.jar
--2021-01-17 11:50:19-- https://github.com/halo-dev/halo/releases/download/v1.4.2/halo-1.4.2.jar
Resolving github.com (github.com)... 140.82.113.3
Connecting to github.com (github.com)|140.82.113.3|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://github-production-release-asset-2e65be.s3.amazonaws.com/126178683/728f2280-1d7e-11eb-8baa-f2770d1064c9?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20210117%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20210117T035020Z&X-Amz-Expires=300&X-Amz-Signature=5800010653ddc9a2794704608ce9680d84042ca22f922e366d761735a6426299&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=126178683&response-content-disposition=attachment%3B%20filename%3Dhalo-1.4.2.jar&response-content-type=application%2Foctet-stream [following]
--2021-01-17 11:50:20-- https://github-production-release-asset-2e65be.s3.amazonaws.com/126178683/728f2280-1d7e-11eb-8baa-f2770d1064c9?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20210117%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20210117T035020Z&X-Amz-Expires=300&X-Amz-Signature=5800010653ddc9a2794704608ce9680d84042ca22f922e366d761735a6426299&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=126178683&response-content-disposition=attachment%3B%20filename%3Dhalo-1.4.2.jar&response-content-type=application%2Foctet-stream
Resolving github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)... 52.217.84.188
Connecting to github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)|52.217.84.188|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 79170304 (76M) [application/octet-stream]
Saving to: ‘halo-latest.jar’
halo-latest.jar 100%[===========================================================================>] 75.50M 24.1MB/s in 3.1s
2021-01-17 11:50:23 (24.1 MB/s) - ‘halo-latest.jar’ saved [79170304/79170304]
root@www:~/halo# ls
halo-latest.jar
root@anchis:~/halo# curl -o ~/.halo/application.yaml --create-dirs https://dl.halo.run/config/application-template.yaml
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 798 100 798 0 0 634 0 0:00:01 0:00:01 --:--:-- 636
root@anchis:~/halo#
修改配置文件:
比如我现在登录用户是root,那么刚才下好的配置文件就在/root/.halo目录,找到这个文件,下载到本地,使用vs code或者其它代码编辑器修改配置
注意事项:
- 1.如果需要自定义端口,修改 server 节点下的 port 即可。
- 2.默认使用的是 H2 Database 数据库,这是一种嵌入式的数据库,使用起来非常方便。需要注意的是,默认的用户名和密码为 admin 和 123456,这个是自定义的,最好将其修改,并妥善保存。
- 3.如果需要使用 MySQL 数据库,需要将 H2 Database 的所有相关配置都注释掉,并取消 MySQL 的相关配置。另外,MySQL 的默认数据库名为 halodb,请自行配置 MySQL 并创建数据库,以及修改配置文件中的用户名和密码。
- 4.h2 节点为 H2 Database 的控制台配置,默认是关闭的,如需使用请将 h2.console.settings.web-allow-others 和 h2.console.enabled 设置为 true。控制台地址即为 域名/h2-console。注意:非紧急情况,不建议开启该配置。
- 5.server.compression.enabled 为 Gzip 功能配置,如有需要请设置为 true,需要注意的是,如果你使用 Nginx 或者 Caddy 进行反向代理的话,默认是有开启 Gzip 的,所以这里可以保持默认。
- 6.halo.admin-path 为后台管理的根路径,默认为 admin,如果你害怕别人猜出来默认的 admin(就算猜出来,对方什么都做不了),请自行设置。仅支持一级,且前后不带 /。
- 7.halo.cache 为系统缓存形式的配置,可选 memory 和 level,默认为 memory,将数据缓存到内存,使用该方式的话,重启应用会导致缓存清空。如果选择 level,则会将数据缓存到磁盘,重启不会清空缓存。如不知道如何选择,建议默认。
代码:
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: 123456
# 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: halodb
password: Mysql数据库密码
# H2 database console configuration.
# h2:
# console:
# settings:
# web-allow-others: false
# path: /h2-console
# enabled: false
halo:
# Your admin client path is https://your-domain/{admin-path}
admin-path: admin
# memory or level
cache: memory
测试运行:
修改完成后将application.yaml配置文件上传到服务器/root/.halo目录,替换原来旧的配置文件,然后放行8090端口,接下来测试运行一下。
java -jar halo-latest.jar
输出如下:
root@www:~/halo# java -jar halo-latest.jar
__ __ __
/ / / /___ _/ /___
/ /_/ / __ `/ / __ \
/ __ / /_/ / / /_/ /
/_/ /_/\__,_/_/\____/
Version: 1.4.2
2021-01-17 12:36:26.454 INFO 21914 --- [ main] run.halo.app.Application : Starting Application v1.4.2 on anchis with PID 21914 (/root/halo/halo-latest.jar started by root in /root/halo)
2021-01-17 12:36:26.460 INFO 21914 --- [ main] run.halo.app.Application : No active profile set, falling back to default profiles: default
2021-01-17 12:36:31.742 INFO 21914 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2021-01-17 12:36:32.602 INFO 21914 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 816ms. Found 22 JPA repository interfaces.
2021-01-17 12:36:36.948 INFO 21914 --- [ main] org.eclipse.jetty.util.log : Logging initialized @15005ms to org.eclipse.jetty.util.log.Slf4jLog
2021-01-17 12:36:37.792 INFO 21914 --- [ main] o.s.b.w.e.j.JettyServletWebServerFactory : Server initialized with port: 8090
2021-01-17 12:36:37.856 INFO 21914 --- [ main] org.eclipse.jetty.server.Server : jetty-9.4.31.v20200723; built: 2020-07-23T17:57:36.812Z; git: 450ba27947e13e66baa8cd1ce7e85a4461cacc1d; jvm 1.8.0_275-b01
2021-01-17 12:36:38.100 INFO 21914 --- [ main] o.e.j.s.h.ContextHandler.application : Initializing Spring embedded WebApplicationContext
2021-01-17 12:36:38.101 INFO 21914 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 11389 ms
2021-01-17 12:36:40.515 INFO 21914 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2021-01-17 12:36:40.973 INFO 21914 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.4.21.Final
2021-01-17 12:36:41.988 INFO 21914 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2021-01-17 12:36:42.605 INFO 21914 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2021-01-17 12:36:43.653 INFO 21914 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2021-01-17 12:36:43.828 INFO 21914 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL57Dialect
2021-01-17 12:36:53.680 INFO 21914 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2021-01-17 12:36:53.747 INFO 21914 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2021-01-17 12:36:55.788 INFO 21914 --- [ main] run.halo.app.config.HaloConfiguration : Halo cache store load impl : [class run.halo.app.cache.InMemoryCacheStore]
2021-01-17 12:36:58.340 INFO 21914 --- [ main] org.eclipse.jetty.server.session : DefaultSessionIdManager workerName=node0
2021-01-17 12:36:58.341 INFO 21914 --- [ main] org.eclipse.jetty.server.session : No SessionScavenger set, using defaults
2021-01-17 12:36:58.343 INFO 21914 --- [ main] org.eclipse.jetty.server.session : node0 Scavenging every 660000ms
2021-01-17 12:36:58.381 INFO 21914 --- [ main] o.e.jetty.server.handler.ContextHandler : Started o.s.b.w.e.j.JettyEmbeddedWebAppContext@6cff61fc{application,/,[file:///tmp/jetty-docbase.2368474148557073677.8090/, jar:file:/root/halo/halo-latest.jar!/BOOT-INF/lib/springfox-swagger-ui-2.9.2.jar!/META-INF/resources],AVAILABLE}
2021-01-17 12:36:58.384 INFO 21914 --- [ main] org.eclipse.jetty.server.Server : Started @36440ms
2021-01-17 12:37:07.582 INFO 21914 --- [ main] run.halo.app.handler.file.FileHandlers : Registered 9 file handler(s)
2021-01-17 12:37:13.035 INFO 21914 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 4 endpoint(s) beneath base path '/api/admin/actuator'
2021-01-17 12:37:13.688 INFO 21914 --- [ main] pertySourcedRequestMappingHandlerMapping : Mapped URL path [/v2/api-docs] onto method [springfox.documentation.swagger2.web.Swagger2Controller#getDocumentation(String, HttpServletRequest)]
2021-01-17 12:37:15.842 INFO 21914 --- [ main] o.s.s.c.ThreadPoolTaskScheduler : Initializing ExecutorService 'taskScheduler'
2021-01-17 12:37:16.116 INFO 21914 --- [ main] o.e.j.s.h.ContextHandler.application : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-01-17 12:37:16.117 INFO 21914 --- [ main] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2021-01-17 12:37:16.160 INFO 21914 --- [ main] o.s.web.servlet.DispatcherServlet : Completed initialization in 42 ms
2021-01-17 12:37:16.272 INFO 21914 --- [ main] o.e.jetty.server.AbstractConnector : Started ServerConnector@345f69f3{HTTP/1.1, (http/1.1)}{0.0.0.0:8090}
2021-01-17 12:37:16.275 INFO 21914 --- [ main] o.s.b.web.embedded.jetty.JettyWebServer : Jetty started on port(s) 8090 (http/1.1) with context path '/'
2021-01-17 12:37:16.279 INFO 21914 --- [ main] d.s.w.p.DocumentationPluginsBootstrapper : Context refreshed
2021-01-17 12:37:16.331 INFO 21914 --- [ main] d.s.w.p.DocumentationPluginsBootstrapper : Found 2 custom documentation plugin(s)
2021-01-17 12:37:16.335 INFO 21914 --- [ main] d.s.w.p.DocumentationPluginsBootstrapper : Skipping initializing disabled plugin bean swagger v2.0
2021-01-17 12:37:16.336 INFO 21914 --- [ main] d.s.w.p.DocumentationPluginsBootstrapper : Skipping initializing disabled plugin bean swagger v2.0
2021-01-17 12:37:16.394 INFO 21914 --- [ main] run.halo.app.Application : Started Application in 52.426 seconds (JVM running for 54.451)
2021-01-17 12:37:16.398 INFO 21914 --- [ main] run.halo.app.listener.StartedListener : Starting migrate database...
2021-01-17 12:37:16.457 INFO 21914 --- [ main] o.f.c.internal.license.VersionPrinter : Flyway Community Edition 6.5.0 by Redgate
2021-01-17 12:37:17.106 INFO 21914 --- [ main] o.f.c.internal.database.DatabaseFactory : Database: jdbc:mysql://127.0.0.1:3306/halodb (MySQL 5.7)
2021-01-17 12:37:17.498 INFO 21914 --- [ main] o.f.c.i.s.JdbcTableSchemaHistory : Repair of failed migration in Schema History table halodb
.flyway_schema_history
not necessary as table doesn't exist.
2021-01-17 12:37:17.548 INFO 21914 --- [ main] o.f.core.internal.command.DbRepair : Successfully repaired schema history table halodb
.flyway_schema_history
(execution time 00:00.061s).
2021-01-17 12:37:17.842 INFO 21914 --- [ main] o.f.core.internal.command.DbValidate : Successfully validated 4 migrations (execution time 00:00.059s)
2021-01-17 12:37:18.537 INFO 21914 --- [ main] o.f.c.i.s.JdbcTableSchemaHistory : Creating Schema History table halodb
.flyway_schema_history
with baseline ...
2021-01-17 12:37:18.875 INFO 21914 --- [ main] o.f.core.internal.command.DbBaseline : Successfully baselined schema with version: 1
2021-01-17 12:37:18.924 INFO 21914 --- [ main] o.f.core.internal.command.DbMigrate : Current version of schema halodb
: 1
2021-01-17 12:37:18.937 INFO 21914 --- [ main] o.f.core.internal.command.DbMigrate : Migrating schema halodb
to version 2 - migrate 1.2.0-beta.1 to 1.2.0-beta.2
2021-01-17 12:37:19.892 INFO 21914 --- [ main] o.f.core.internal.command.DbMigrate : Migrating schema halodb
to version 3 - migrate 1.3.0-beta.1 to 1.3.0-beta.2
2021-01-17 12:37:20.571 INFO 21914 --- [ main] o.f.core.internal.command.DbMigrate : Migrating schema halodb
to version 4 - migrate 1.3.0-beta.2 to 1.3.0-beta.3
2021-01-17 12:37:20.625 INFO 21914 --- [ main] o.f.core.internal.command.DbMigrate : Successfully applied 3 migrations to schema halodb
(execution time 00:01.743s)
2021-01-17 12:37:20.828 INFO 21914 --- [ main] run.halo.app.listener.StartedListener : Migrate database succeed.
2021-01-17 12:37:21.156 INFO 21914 --- [ main] run.halo.app.listener.StartedListener : Created backup directory: [/tmp/run.halo.app/halo-backup]
2021-01-17 12:37:21.159 INFO 21914 --- [ main] run.halo.app.listener.StartedListener : Created data export directory: [/tmp/run.halo.app/halo-data-export]
2021-01-17 12:37:21.253 INFO 21914 --- [ main] run.halo.app.listener.StartedListener : Halo started at http://127.0.0.1:8090
2021-01-17 12:37:21.254 INFO 21914 --- [ main] run.halo.app.listener.StartedListener : Halo admin started at http://127.0.0.1:8090/admin
2021-01-17 12:37:21.257 INFO 21914 --- [ main] run.halo.app.listener.StartedListener : Halo has started successfully!
这里就代表启动成功,打开浏览器输入http://ip:8090,会自动跳转至设置界面,输入用户名,用户昵称,邮箱,密码,博客信息即可完成安装,(这里支持导入数据,由于我是第一次安装,所以不需要导入数据,直接完成安装即可),安装完成后打开 http://ip:8090/admin/ 输入用户名(或邮箱),输入密码,即可登录管理后台发布文章。
持久化运行
注意:测试运行的进程在关闭ssh后就没有了,生产条件肯定是要在关闭ssh后还能继续运行,有三种方式可以选择,我自己用的是screen,大家可以自行选择适合自己的方法(1和2在关闭ssh后都可以在后台运行,但在重启系统后会失效,需要重新执行1或2的代码,而法3可以重启后自启动。
关闭ssh连接后再重新连接:
1.使用screen
代码:
screen -S halo
cd halo
java -jar halo-latest.jar
2.setsid
cd halo
setsid java -jar halo-latest.jar
3.开机自启
参考:进阶配置
配置反向代理和域名访问并添加https
配置反向代理和域名、https均可通过nginx实现,这里不在赘述。
Halo 官网
https://github.com/halo-dev/halo
https://halo.run/
https://halo.run/p/themes.html