Administrator
Published on 2024-11-25 / 16 Visits
0
0

搭建匿名提问箱网站

参考:NekoBox

安装环境

GO

  1. 下载go,请参考[官网](All releases - The Go Programming Language)下载最新版本(本文为1.20.6版本)
curl -O https://dl.google.com/go/go1.20.6.linux-amd64.tar.gz
  1. 安装go

    完全参考[官方指南](Download and install - The Go Programming Language)即可

    1. Remove any previous Go installation

      by deleting the /usr/local/go folder (if it exists), then extract the archive you just downloaded into /usr/local, creating a fresh Go tree in /usr/local/go:

      sudo rm -rf /usr/local/go && sudo tar -C /usr/local -xzf go1.20.6.linux-amd64.tar.gz
      

      (You may need to run the command as root or through sudo).

      Do not untar the archive into an existing /usr/local/go tree. This is known to produce broken Go installations.

    2. Add /usr/local/go/bin to the PATH environment variable.

      You can do this by adding the following line to your $HOME/.profile or /etc/profile (for a system-wide installation):

      export PATH=$PATH:/usr/local/go/bin
      

      注意,可能将路径设置在.profile在重启后仍无效

      建议将

      source ~/.profile
      

      加入.zshrc文件末尾(如果你使用zsh的话),bash就是.bashrc

      Note: Changes made to a profile file may not apply until the next time you log into your computer. To apply the changes immediately, just run the shell commands directly or execute them from the profile using a command such as source $HOME/.profile.

    3. Verify that you've installed Go by opening a command prompt and typing the following command:

      go version
      
    4. Confirm that the command prints the installed version of Go.

  2. go设置代理

    go env -w GO111MODULE=on
    go env -w GOPROXY=https://goproxy.cn,direct
    

MySQL

主要参考Ubuntu上安装MySQL

以及遇见报错提示的解决方法

  1. 安装MySQL

    sudo apt update
    sudo apt upgrade
    sudo apt install mysql-server
    
  2. 启动MySQL

    sudo systemctl start mysql
    
  3. MySQL 安全配置

    执行以下命令调整 MySQL 服务器的安全性:

    sudo mysql_secure_installation
    

    这将会输出:

    Securing the MySQL server deployment.
    
    Connecting to MySQL using a blank password.
    
    VALIDATE PASSWORD COMPONENT can be used to test passwords
    and improve security. It checks the strength of password
    and allows the users to set only those passwords which are
    secure enough. Would you like to setup VALIDATE PASSWORD component?
    
    Press y|Y for Yes, any other key for No:
    

    这里时问你是否使用密码验证组件。输入 ‘Y’ 并按下回车键。

    There are three levels of password validation policy:
    
    LOW    Length >= 8
    MEDIUM Length >= 8, numeric, mixed case, and special characters
    STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file
    
    Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG:
    

    这是是设置密码验证策略的级别。他提供了 3 个级别:

    • LOW: 密码长度至少 8 位
    • MEDIUM: 密码长度至少 8 位, 由数字、混合大小写的字母和特殊字符组成
    • STRONG: 密码长度至少 8 位, 由数字、混合大小写的字母、特殊字符和字典文件组成

    请选择适合你的密码级别。在这里由于是用来开发和测试,我选择 1.

    Please set the password for root here.
    
    New password:
    
    Re-enter new password:
    

    在这里,输入两次密码。

    Estimated strength of the password: 25
    Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y
    

    如果你对密码强度满意,输入 Y 和回车键后继续。

    理论过程如上所述,但实际由于版本问题,在输入密码的时候会遇到一个报错

     ... Failed! Error: SET PASSWORD has no significance for user 'root'@'localhost' as the authentication method used doesn't store authentication data in the MySQL server. Please consider using ALTER USER instead if you want to change authentication parameters.
    

    此时关闭这个ssh链接或者窗口(如果你是实机)

    重新链接,使用如下命令使得root可以使用密码登录

    1. 结束后台程序(如果还在运行的话)

      sudo killall -9 mysql_secure_installation
      
    2. 运行MySQL语句

      sudo mysql
      ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'SetRootPasswordHere';
      exit
      

      逐句执行,注意将SetRootPasswordHere设置为你的密码

    3. 重新运行

      sudo mysql_secure_installation
      
    By default, a MySQL installation has an anonymous user,
    allowing anyone to log into MySQL 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? (Press y|Y for Yes, any other key for No) : Y
    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? (Press y|Y for Yes, any other key for No) : Y
    Success.
    
    By default, MySQL 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? (Press y|Y for Yes, any other key for No) : Y
     - 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? (Press y|Y for Yes, any other key for No) : Y
    Success.
    
    All done!
    

    在这里,上面所有问题都输入 Y 。然后完成整个配置过程。

  4. 检查MySQL版本

    mysql -u root -p
    
    SELECT VERSION();
    

Redis

参考官方链接

  1. 安装前置

    sudo apt install lsb-release curl gpg
    
  2. 安装redis

    curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg
    
    echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list
    
    sudo apt-get update
    sudo apt-get install redis
    
  3. 检查redis版本

    redis-cli --version
    

安装NekoBox

  1. 下载源码

    cd ~
    git clone https://github.com/NekoWheel/NekoBox.git
    
  2. 安装编译

    cd NekoBox
    ls
    cd cmd
    go build -o NekoBox
    
  3. 配置

    cp NekoBox ~/NekoBox
    cd ~/NekoBox
    cp conf/app.sample.ini conf/app.ini
    

配置文件

配置MySQL

  1. 配置MySQL

    mysql -u root -p
    
  2. 创建名为‘RigelBox’的数据库

    CREATE DATABASE RigelBox;
    
  3. 创建新的用户:执行以下 SQL 查询来创建一个名为 "rigel" 的用户,并设置密码

    CREATE USER 'rigel'@'localhost' IDENTIFIED BY 'your_password_here';
    

    'your_password_here' 替换为你想要设置的实际密码。

  4. 赋予用户权限:现在,我们需要为用户 "rigel" 授予对 "RigelBox" 数据库的权限。执行以下 SQL 查询来赋予用户权限

    GRANT ALL PRIVILEGES ON RigelBox.* TO 'rigel'@'localhost';
    
  5. 刷新权限:在 MySQL 中,授予权限后需要刷新权限才能生效。执行以下命令刷新权限

    FLUSH PRIVILEGES;
    

设置redis密码

  1. 进入 Redis 配置文件:

    Redis 的配置文件通常位于 /etc/redis/redis.conf。使用文本编辑器(如 nanovim)打开该文件。

    sudo vim /etc/redis/redis.conf
    
  2. 在配置文件中找到 requirepass 配置项:

    在 Redis 配置文件中,搜索或找到名为 requirepass 的配置项。默认情况下,该配置项通常是被注释掉的(以 # 开头)。(在vim中,使用/后接搜索内容,回车后按n切换到下一个)

    requirepass your_redis_password_here
    

    保存并关闭文件,:wq

  3. 重启 Redis 服务器:重新启动 Redis 服务器,使密码设置生效。在大多数 Linux 系统中,可以使用以下命令重启 Redis

    sudo service redis-server restart
    

修改配置文件

sudo vim ~/NekoBox/conf/app.ini

修改后的配置文件如下

涉及个人信息暂不放出

相应的解释如下(由chatgpt生产)

#当在 conf/app.ini 文件中填写这些配置项时,每个配置项表示不同的设置,用于配置应用程序的行为和功能。以下是每个配置项的含义和作用:

[app] 部分:

production: 一个布尔值,指示应用程序是否处于生产环境。在生产环境中,可能会启用一些优化和安全措施。如果是生产环境,通常设置为 true。
title: 应用程序的标题或名称,可以是你想要的任何字符串。
icp: 用于备案的网站 ICP(Internet Content Provider)号,如果有的话。
uptrace_dsn: Uptrace(分布式跟踪系统)的 DSN(Data Source Name),用于应用程序性能监控和调试。
qiniu_access_key 和 qiniu_access_secret :是七牛云的 Access Key 和 Secret Key,用于访问七牛云对象存储服务(Qiniu Cloud Object Storage)。这对凭证是用于上传、下载、管理存储在七牛云上的文件和资源。
aliyun_access_key 和 aliyun_access_key_secret:是阿里云的 Access Key ID 和 Access Key Secret,用于访问阿里云的各种云服务。这些凭证通常用于对象存储、云服务器、数据库等服务的操作。

[security] 部分:

enable_text_censor: 一个布尔值,指示是否启用文本审核功能。如果设置为 true,应用程序可能会对用户提交的文本进行敏感信息过滤或审核。
[server] 部分:

port: 应用程序监听的端口号,用户可以通过该端口访问应用程序。
salt: 密码散列的盐值,用于增加密码安全性的一种方式。
xsrf_key: XSRF(跨站请求伪造)的密钥,用于防范跨站请求伪造攻击。
xsrf_expire: XSRF 令牌的过期时间,单位为秒。
[database] 部分:

user: 数据库用户名,用于连接数据库。
password: 数据库密码,用于连接数据库。
address: 数据库的地址和端口号,通常采用 tcp(IP:Port) 的格式。
name: 数据库名称,要连接的具体数据库。
[redis] 部分:

addr: Redis 服务器的地址和端口号,通常采用 IP:Port 的格式。
password: Redis 服务器的密码,如果有密码保护的话。
[recaptcha] 部分:

domain: ReCAPTCHA 服务的域名,通常是 "https://www.recaptcha.net"。
site_key: ReCAPTCHA 网站密钥,用于在前端显示 ReCAPTCHA 验证。
server_key: ReCAPTCHA 服务器密钥,用于在后端进行 ReCAPTCHA 验证。
[upload] 部分:

default_avatar: 默认头像的路径或URL,用于在用户未设置头像时显示默认头像。
default_background: 默认背景的路径或URL,用于在用户未设置背景时显示默认背景。
aliyun_endpoint: 阿里云对象存储服务的 Endpoint 地址。
aliyun_access_id: 阿里云对象存储服务的 Access Key ID。
aliyun_access_secret: 阿里云对象存储服务的 Access Key Secret。
aliyun_bucket: 阿里云对象存储服务的存储桶(Bucket)名称。
aliyun_bucket_cdn_host: 阿里云对象存储服务的 CDN 主机地址,用于访问存储桶中的资源。
[mail] 部分:

account: 发送电子邮件所用的邮箱账号。
password: 发送电子邮件所用的邮箱密码。
port: 邮件服务器的端口号。
smtp: 邮件服务器的地址。
#以上配置项中,有些涉及到敏感信息,比如密码、密钥等,务必确保在实际使用时将其正确填写,并妥善保管,避免泄露给未授权的人员。
  1. 生成salt 和 xsrf_key

    salt和xsrf_key 本质为了安全和加密,在此我们只要使用随机字符串即可,不过这个字符串不要外泄

    salt 是一个随机的字符串,用于加密和混淆用户密码等敏感数据。它在密码哈希算法中被称为“盐”,是在对用户密码进行哈希运算之前,与密码进行连接以增加安全性。可以使用任意的随机字符串作为 salt,长度应该足够长,通常建议使用 16 字符以上的随机字符串。

    xsrf_key 是用于生成和验证 CSRF 令牌的密钥。CSRF 令牌是一种用于防范跨站请求伪造攻击的安全机制。在每次向服务器发送请求时,都需要将 CSRF 令牌包含在请求中,服务器会验证令牌的合法性,以确保请求来自于合法的来源。xsrf_key 可以是一个随机字符串,长度通常也应足够长,建议使用 32 字符以上的随机字符串。

    可以使用python程序为我们生成随机字符串

    import random
    import string
    
    def generate_random_string(length):
        letters_and_digits = string.ascii_letters + string.digits
        return ''.join(random.choice(letters_and_digits) for i in range(length))
    
    # 生成一个长度为 16 的随机字符串
    random_string = generate_random_string(16)
    print(random_string)
    
  2. 获取 reCAPTCHA 密钥

    1. 访问 Google reCAPTCHA 网站:打开你的网页浏览器,然后前往 https://www.google.com/recaptcha
    2. 注册你的网站:点击 "Get reCAPTCHA" 按钮,进入 reCAPTCHA 网站注册页面。在这里,你需要登录你的 Google 账号,如果没有账号,你需要先注册一个。
    3. 添加一个新的 reCAPTCHA 网站:在注册页面中,你需要提供一些关于你的网站的信息,包括一个标识你的网站的名称,域名,以及选择 reCAPTCHA 的版本。通常,选择 reCAPTCHA V3 是最常见的选择,因为它不需要用户进行人机验证。
    4. 获取 reCAPTCHA 密钥:完成网站信息的填写后,你将会得到两个关键的 reCAPTCHA 密钥:Site Key 和 Secret Key。这些密钥用于在你的网站上集成 reCAPTCHA 服务。Site Key 将用于在你的网站上渲染 reCAPTCHA,而 Secret Key 则用于与 Google reCAPTCHA 服务器进行通信,以验证用户的响应。
    5. 在你的应用程序中使用密钥:将获得的 Site Key 和 Secret Key 填写到你的应用程序配置文件中,以便在应用程序中启用 reCAPTCHA 功能。

    请注意,reCAPTCHA 密钥是敏感信息,需要妥善保管,避免泄露给未授权的人员。确保将 Secret Key 隐私保存,并不要将它直接暴露在客户端代码中。在集成 reCAPTCHA 时,通常只需要在服务器端使用 Secret Key 进行验证。

  3. 获取七牛云 Access Key 和 Secret Key

  4. 获取阿里云 Access Key ID 和 Access Key Secret

    1. 注册阿里云账号
    2. 购买OSS服务,可以选择最便宜的40GB/年的套餐,只要9元
    3. 创建一个bucket
    4. 创建一个子账号,赋予仅允许访问OSS的权限
    5. 获取子账号的Access Key ID 和 Access Key Secret填入
    6. 查询aliyun_endpoint,即地域服务器,形如oss-cn-hangzhou.aliyuncs.com,看你所填的服务器位置
    7. aliyun_bucket_cdn_host需要购买CDN加速,没有就先不填
  5. 上传默认头像,背景

    1. 可以去阿里云的矢量素材库下载自己喜欢的素材
    2. 上传到github图床
    3. 注意需要在下载页面点击右键,在新标签页打开,获取到

改变html页面

需要自己一点点修改templates中各个html页面

持久化运行

  1. 编译

    #删除原有
    cd  ~/NekoBox/
    rm NekoBox
    #重新编译生成
    cd cmd
    go build -o NekoBox
    mv NekoBox ../
    cd ..
    
  2. 试运行

    sudo ./NekoBox web
    
    1. 登录后走完注册,登录的流程
    2. 注意观察设定的默认头像和默认背景有没有显示
    3. 上传自己的头像和背景,观察能否成功(阿里云OSS)
    4. 退出登陆后,匿名评论自己的主页,然后登录,观察能否看到他人留言
    5. 查看邮箱,观察有无新邮件
    6. 尝试重置密码组件能否正常工作
    7. 全部完成后,进入持久化运行
  3. 持久化运行

    sudo nohup ./NekoBox web > nekobox.log 2>&1 &
    
  4. sudo nohup ./RigelBox web > rigelbox.log 2>&1 &
    

Comment