前言

我的第一个网站,也就是本站,在经历一系列折腾后,终于建成了✿ヽ(°▽°)ノ✿

这第一篇文章便用来记录我搭建本网站的全过程,如果能给看到这篇文章的你带来一些帮助那就更好了。

本站采用halo作为建站工具,并使用dream作为主题。

先感谢开源大佬们的无私奉献。

halo

开源地址 https://github.com/halo-dev/halo

官方网站 https://halo.run/

halo-theme-dream

开源地址 https://github.com/nineya/halo-theme-dream

作者博客 https://blog.nineya.com

目前halo最新版本已更新至2.x,但本站仍然采用1.6.1。

稍后我还会更新一篇关于从源码构建halo 2.xjar包的文章,因为halo从2.x开始仅提供docker部署方式。

可恶,什么时候才能更新啊啊啊啊啊啊

虽然到时候迁移数据又要折腾就是了

而且目前从源码构建的jar包使用起来还有些问题,但貌似影响不大

2023.5.25 无所谓了,1.6.1养老了

准备工作

以下为我所使用的部分软件以及版本

  • debian 11
  • curl
  • vim
  • openjdk 17
  • nginx
  • mariadb (非必须)
  • screen (非必须)
  • socat (非必须)
  • halo 1.6.1
  • halo-theme-dream 2.0.9

当然,你还需要一台云服务器和一个域名。服务器及域名的选购不是本文的重点,因此略过。

如果你想将服务器设置在大陆地区,那么备案可能花费半个月甚至更久。在此期间可以通过ip直接访问网站,并调试好网站,等待备案通过。

ps:目前已更换为ubuntu22.04LTS,但流程基本相同

环境搭建

购买服务器后,等待系统安装完成,再通过ssh连接至服务器。windows可以使用Xshell,PuTTY等软件。此外,win10和win11自带有ssh工具,可以通过终端使用。mac和linux则直接在终端使用即可。ssh的使用方法同样不是本文的重点,略过。默认用户应该为root。

以下命令均为debian的命令,其他linux发行版的命令以及软件包名请自行查阅资料。

添加用户

连接至服务器后,首先创建一个新用户,不推荐直接使用root用户。

执行useradd -m -s /bin/bash your_username添加用户,注意将your_username换成 你自己的用户名。

执行passwd your_username为你新建的账户设置密码。linux在输入密码时不会显示,输入完成后直接按回车即可。

执行usermod -aG sudo your_username将你的用户添加到sudo组里。或者直接编辑/etc/sudoers文件,在其中添加如下字段your_username ALL=(ALL:ALL) ALL。注意,编辑前需要执行chmod 660 /etc/sudoers更改文件的读写权限。编辑完成后,执行chmod 440 /etc/sudoers恢复其原本的读写权限。

随后断开ssh连接,使用你刚刚创建的用户名重新连接。

更新并安装软件

执行sudo apt update更新软件列表,随后执行sudo apt upgrade更新所有软件。在命令前加上sudo表示以管理员身份运行,需要输入账户密码确认。输入一次后,在后续一段时间内反复使用sudo不需要再次输入密码。

更新期间可能会有类似这样的情况出现,选择第一项安装新版配置文件即可。

待更新完成后,重启服务器,并再次连接服务器。

重连服务器后,执行命令sudo apt install curl vim nginx mariadb-server openjdk-17-jre,等待安装完成即可。

mariadb

如果你使用halo内置的h2数据库,那么该部分可以直接跳过。

执行sudo systemctl enable mariadb --now将mariadb设置为开机启动,并马上启动。随后执行systemctl status mariadb查看mariadb的运行状态。

mariadb正常运行后,执行sudo mysql_secure_installation对mariadb进行一些设置。
如无以外,应有类似如下输出:

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we’ll need the current
password for the root user. If you’ve just installed MariaDB, and
haven’t set the root password yet, you should just press enter here.

Enter current password for root (enter for none):

由于是第一次安装,暂未设置mariadb的root密码,此处直接回车即可。

Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.

You already have your root account protected, so you can safely answer ‘n’.

Switch to unix_socket authentication [Y/n]

我们的linux系统的root账户已经有密码了,此处不用切换到unix套接字身份验证,输入n并回车即可。

You already have your root account protected, so you can safely answer ‘n’.

Change the root password? [Y/n]

这里直接回车,更改一下mariadb的root账户密码。

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n]
… Success!

Normally, root should only be allowed to connect from ‘localhost’. This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n]
… Success!

By default, MariaDB comes with a database named ‘test’ that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n]
– Dropping test database…
… Success!
–Removing privileges on test database…
… Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n]
… Success!

Cleaning up…

All done! If you’ve completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

接下来的选项,一路回车即可。分别是移除匿名账户,拒绝远程root登录,移除test数据库,刷新权限。至此mariadb初始化完毕。

接下来执行mariadb -u root -p,并输入刚刚设置的root密码。

随后创建halo数据库create database halodb character set utf8mb4 collate utf8mb4_bin;

创建数据库用户create user 'your_username'@'localhost' identified by 'your_password';注意将your_usernameyour_password换成你自己的用户名与密码。

执行grant all on halodb.* to 'your_username'@'localhost' identified by 'your_password';为你的数据库账号赋予数据库的权限。

执行flush privileges;刷新权限。

最后执行exit退出mariadb

ssl证书

这里假定你已经购买了域名并且已经将域名解析到了服务器上。

首先安装acme.sh来获取一个免费的ssl证书。

克隆整个acme.sh项目(推荐)或者执行curl https://get.acme.sh | sh安装acme.sh

随后执行source ~/.bashrc使环境变量生效。

执行acme.sh --upgrade --auto-upgrade开启acme.sh的自动更新

执行acme.sh --set-default-ca --server letsencryptacme.sh的默认ca从ZeroSSL改为Let's Encrypt,主要是ZeroSSL还要去注册账号,而Let's Encrypt什么都不用做。

我个人使用过两种模式申请过ssl证书。一种使用DNS验证,一种使用standalone模式验证。

DNS验证

DNS模式验证根据你购入域名的服务商不同而有所不同,但大体上都是相似的,这里以腾讯云DNSPod举例。

如果你在其它服务商处购买域名,请查看https://github.com/acmesh-official/acme.sh/wiki/dnsapi来获取用到的指令。

登录后,点击右上角头像,点击API密钥。

进入后,选择DNSPod Token,点击创建密钥即可。

记录弹出窗口中ID与Token。由于Token只会在创建时显示一次,请务必保管好。

回到服务器,执行export DP_Id="your_ID" && export DP_Key="your_Token"导入ID与Token。注意替换自己的ID与Token。

执行acme.sh --issue --dns dns_dp -d your_domain -k ec-256

-k指定签发证书种类,此处签发的是256位ecc证书。

注意,出现如下错误,大概率是acme.sh没安装完整,建议克隆整个项目https://github.com/acmesh-official/acme.sh

如果证书签发不成功,出现类似的信息,则需要手动为域名添加一条txt解析记录。主机记录填写acme.sh报错中给出的Domain,文本内容则填写TXT value。

等待解析生效后,执行acme.sh --issue --dns dns_dp -d your_domain -k ec-256 --renew

如果出现如下类似信息,则证书签发完成。

证书有效期为3个月,acme.sh会帮助我们在证书到期前自动续期。

standalone模式

在此模式下,acme.sh需要额外安装socat,且申请过程中不能有程序占用80端口。执行sudo apt install socat安装socat。

安装完成后执行acme.sh --issue -d your_domain --standalone -k ec-256

成功后也会输出与DNS模式成功后相似的信息。

此外,acme.sh还有其他模式,由于我没有尝试过,本文略过,有时间的话可以去自行研究_(:з」∠)_

最后,执行如下命令将安装证书到指定路径
acme.sh --installcert -d your_domain --fullchainpath /your_fullchainpath/your_filename.crt --keypath /your_keypath/your_filename.key --ecc

nginx

执行sudo rm /etc/nginx/sites-enabled/default删除nginx默认配置的软连接。

编辑网站配置,此处以vim为例sudo vim /etc/nginx/sites-available/your_filename

配置示例如下,记得修改其中的路径等信息

upstream halo {
 	server 127.0.0.1:8090;
}

server {
	listen 80;
	listen [::]:80;
	server_name your_domain;
	return 301 https://$server_name$request_uri;
}

server {
	listen 443 ssl;
	listen [::]:443 ssl;
	server_name your_domain;

	client_max_body_size 1024m;

	ssl_certificate /your_fullchainpath/your_filename.crt;
	ssl_certificate_key /your_keypath/your_filename.key;
	
	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;
  	}
}

保存后退出。

执行sudo ln -s /etc/nginx/sites-available/your_filename /etc/nginx/sites-enabled/创建软连接。

执行sudo nginx -t来检查配置文件是否有误。

如无误,执行sudo systemctl enable nginx --now将nginx设为开机自启动,并立即运行nginx。随后执行systemctl status nginx查看nginx的运行状态。

配置halo

首先下载halo的jar包,直接执行curl -L https://github.com/halo-dev/halo/releases/download/v1.6.0/halo-1.6.0.jar --output halo.jar。或从官方网站下载

在此假定下载文件保存为halo.jar,保存在/home/your_username/halo/目录下。请根据你的实际情况修改。

创建halo工作目录,halo运行时产生的文件均存放在此处mkdir ~/.halo

配置文件

创建halo配置文件vim ~/.halo/application.yaml

以下是配置示例,记得根据你的情况修改其中的信息

server:
  port: 8090

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

    # H2 database configuration.
    # 如果你使用h2数据库,以下部分请取消注释
    #driver-class-name: org.h2.Driver
    #url: jdbc:h2:file:~/.halo/db/halo
    #username: admin #记得更改用户名和密码
    #password: 123456 #halo初始化完毕后,请勿修改

    # MySQL database configuration.
    # 如果你使用mariadb/mysql,以下部分请取消注释
#    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: 123456

  # H2 database console configuration.
  # h2数据库控制台配置,通常不需要取消注释
#  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

保存后退出。

以下是h2数据库的注意事项

以下是mysql/mariadb的注意事项

持久化运行

断开ssh连接后,我们运行的程序会一并被终止,因此我们需要以下方法保持halo运行

使用screen

执行sudo apt install screen安装。

执行screen -R halo创建一个窗口并连接。

此时我们进入到了新窗口中,在这个窗口中执行的程序会一直运行,无论ssh是否连接,直到我们手动终止。

执行java -jar ~/halo/halo.jar

如无意外,在halo输出的信息最下方看到Halo has started successfully!既代表halo运行成功。

按下ctrl + a + d退出当前窗口。

执行screen -r halo重连已退出窗口。

使用systemd

编辑文件sudo vim /etc/systemd/system/halo.service

参考配置如下

[Unit]
Description=Halo Service
Documentation=https://halo.run
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
User=your_username #记得更改为halo工作目录所属用户的用户名,并删除该注释
ExecStart=/usr/bin/java -server -Xms256m -Xmx256m -jar your_jar_path
ExecStop=/bin/kill -s QUIT $MAINPID
Restart=always
StandOutput=syslog

StandError=inherit

[Install]
WantedBy=multi-user.target

保存后退出

执行sudo systemctl daemon-reload重新加载重新加载 systemd。

执行sudo systemctl enable halo --now将halo设为开机自启动并立即运行halo。随后执行systemctl status halo查看halo的运行状态。

初始化

至此,halo搭建工作基本完成,你现在可以暂时使用ip访问了。等待备案完成后,你便可以使用域名访问。当然,如果你将服务器设置在非大陆地区,现在就能使用域名访问了。

浏览器访问https://your-domain/{admin-path},开始初始化站点。

全部按照提示填写即可。

成功初始化后,进入halo后台。

安装主题

halo支持本地上传和网络下载两种方式,根据自身情况选择即可。

上传完毕后,点击启用主题即可。

至此,网站安装全部完成,剩下就是一些设置了,慢慢探索即可。