跳转到主要内容

Windows 安装

:information_source: 本教程已在 Windows 10 和 11 上测试过。

要在 Windows 上为 Discourse 设置开发环境,可以使用适用于 Linux 的 Windows 子系统功能。

此设置需要安装 WSL 2。它仅适用于 Windows 10 内部版本 18917 或更高版本。 我们假定你已经在 Windows 10 系统上安装了适用于 Linux 的 Windows 子系统 2(Ubuntu)。警告: 请安装 Ubuntu 18.04,而不是 20.04,因为某些安装步骤在 20.04 上会失败。更多信息请查看本文底部的 2020 年 6 月 30 日说明。

让我们开始吧!

安装 Discourse

  1. 首先按照话题在 Ubuntu 上安装 Discourse 进行开发的初学者指南中的步骤操作,直到 克隆 Discourse 步骤。

[quote="Arpit Jalan, post:1, topic:14727, username:techAPJ"]

安装 Discourse 依赖

你的系统上需要以下软件包:

** 可选

现在我们已经安装了 Discourse 的依赖,接下来安装 Discourse 本身。 [/quote]

  1. 在设置数据库之前,你必须使用以下命令手动启动 PostgreSQL 服务和 Redis 服务器

    sudo service postgresql start
    redis-server --daemonize yes
    
  2. 然后完成 Ubuntu 指南的所有剩余步骤。

[quote="Arpit Jalan, post:1, topic:14727, username:techAPJ"]

克隆 Discourse

在 ~/discourse 文件夹中克隆 Discourse 仓库:

git clone https://github.com/discourse/discourse.git ~/discourse

~ 表示主文件夹,因此 Discourse 源代码将位于你的主文件夹中。

设置数据库

创建角色,名称与你的 ubuntu 系统用户名相同:

sudo -u postgres createuser -s "$USER"

引导 Discourse

切换到你的 Discourse 文件夹:

cd ~/discourse

安装所需的 gems

source ~/.bashrc
bundle install

现在你已经成功安装了 gems,运行以下命令:

bundle exec rake db:create
bundle exec rake db:migrate
RAILS_ENV=test bundle exec rake db:create db:migrate

尝试运行 specs:

bundle exec rake autospec

所有测试都应当通过。

启动 rails 服务器:

bundle exec rails server

现在你应该可以在 http://localhost:3000 上连接你的 Discourse 应用了 - 试试看!

在另一个终端实例中,进入你的 discourse 文件夹(cd ~/discourse)并运行:

bin/dev --only ember

这会启动前端打包器;应用仍然从 http://localhost:3000 提供服务。 [/quote]

创建启动 Discourse 的命令

现在你的开发环境已基本就绪。 唯一的问题是,每次在 Windows 上打开 Ubuntu 时,你都必须手动启动 PostgreSQL 服务和 Redis 服务器。别担心,我们可以通过创建一个自定义命令来解决这个问题 :wink:

cd ~

使用命令 nano start-discourse 创建一个新文件,粘贴以下内容,然后保存并退出。

#!/bin/bash

# to start PostgreSQL
sudo service postgresql start

# to start Redis server
redis-server --daemonize yes

现在使用以下命令修改 CHMOD

chmod +x start-discourse

然后将该文件复制到你的 bin 文件夹

sudo cp start-discourse /usr/bin/

完成了。 现在,每当你打开 Ubuntu bash 时,只需运行以下命令即可开始开发 :+1:

start-discourse

或者,如果你使用的是 Windows 10 企业版、专业版或教育版,则可以在 hyper-v 中创建 Linux 虚拟机来设置 Discourse 开发环境。

关于 Windows 环境的说明

[details=截至 2020 年 6 月 30 日:] [quote="Andrea Habib, post:56, topic:75149, username:AndreaHabib"] 随着 Windows 10 build 2004 和 WSL 2 的推出,你们中很多人可能会遇到一些问题,因为 Discourse 现在需要 WSL 2 才能实际运行。 **首先:**如果还没有更新,你需要将系统更新到 Windows 2004 (19041) 注意:不要通过系统设置中的 Windows 更新来安装,否则安装过程中可能会出错。同时确保你的系统是 Windows 1903 (18362) 或 1909 (18363),可以通过搜索 winver 来确认。

  • 前往:下载 Windows 10
  • 点击 Windows 10 May 2020 Update 下方的“立即更新”按钮,下载更新助手,让它完成安装
  • 如果遇到任何错误,下载媒体创建工具并运行,创建一个可启动的闪存盘(至少 8GB),然后用它安装 Windows。

Windows 安装完成后,你需要手动安装 WSL 2 但首先,我们需要确保两个功能已启用:

  • 虚拟机平台,可以直接在 Windows 中启用:前往“程序和功能”,然后在启用或关闭 Windows 功能中启用可选功能
  • 另一个是虚拟化(Virtualization),只能在 BIOS 中启用。不过该选项因主板而异。例如:在 AMD 上叫 SVM。请查阅你的具体主板。

现在两个功能都已启用:

  • 安装 Ubuntu 18.04,而不是 20.04,因为某些安装在 20.04 上会失败。
  • 前往这里更新 Linux 内核:链接在回复中
  • 前往此链接并按步骤操作:在 Windows 10 上安装适用于 Linux 的 Windows 子系统 (WSL) | Microsoft Docs
  • 打开 powershell(以管理员身份运行),运行 wsl --set-version Ubuntu-18.04 2 或 wsl --set-version Ubuntu 2,取决于 wsl -l -v 中显示的是哪个发行版
  • 转换需要一些时间,几分钟
  • 运行 wsl -l -v 确认 Ubuntu 正在 WSL 2 上运行

现在你可以运行 Ubuntu-18.04 或带有 Ubuntu 的 Windows Terminal,开始安装依赖项和工具

注意:如果在 bundle install 期间安装某个 gem 时出错:请确保你使用的是 Ubuntu-18.04 而不是 20.04

不过,如果你已经安装了 postgreSQL 12 并运行在端口 5432 上,可能会遇到一些问题,因为依赖项安装的是 postgreSQL 10,端口可能会递增到 5433。 要解决这个问题:

  • 在文本编辑器中打开你的文件夹(例如 VS Code)
  • 前往 config/database.yml
  • 在 development 部分中、adapter 字段下方,另起一行添加:port: 5433
  • 确保对 test 和 profile 做同样的操作。

如果上述方法无效,请通过 pg_isready 检查 PostgreSQL 是否在运行以及运行在哪个端口。然后按照上面相同的说明操作,将端口设置为数据库实际运行的端口。

关于 LINUX 内核:更新 WSL 2 Linux 内核 | Microsoft Docs [/quote]

[quote="Ricky Chon, post:61, topic:75149, username:RickyC0626"] 一种无需在 database.yml 中显式配置端口的解决方案

数据库创建和迁移过程中的一些错误:

Redis 连接特定端口无效 / 连接被拒绝

bundle exec rake db:create
rake aborted!
Redis::CannotConnectError: Error connecting to Redis on localhost:6379 (Errno::ECONNREFUSED)
...

Caused by:
Errno::ECONNREFUSED: Connection refused - connect(2) for 127.0.0.1:6379
...

Caused by:
IO::EINPROGRESSWaitWritable: Operation now in progress - connect(2) would block
...

Tasks: TOP => db:create => db:load_config => environment
(See full trace by running task with --trace)

从这个错误可以看出 Redis 使用的端口是 6379,我们可以通过该端口启动 Redis 服务器来解决这个问题。

默认情况下,redis-server --daemonize yes 应该可以工作,但如果不行,使用:

redis-server --daemonize yes --port 6379

检查 Redis 实例的状态:

redis-cli

127.0.0.1:6379> ping
PONG

连接 psql 端口无效

bundle exec rake db:migrate
PG::ConnectionBad: could not connect to server: No such file or directory
       Is the server running locally and accepting
       connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

从这个错误可以看出 PostgreSQL 连接端口 5432 时遇到了问题。通常启动该服务即可解决

sudo service postgresql start

如果这样仍然无效且错误依旧,尝试检查该服务正在监听哪个端口,如有必要,在 postgresql.conf 中将端口改为你想要的:

pg_isready
/var/run/postgresql:5432 - no response
or
/var/run/postgresql:5432 - accepting connections

结果

完成上述操作或执行 start-discourse 命令后,两个实例应该会在各自默认/指定的端口上运行。要通过默认的 Windows cmd 终端检查它们的状态,我们可以运行:

netstat -anop tcp

输出类似这样的内容:

Proto    Local Address        Foreign Address        State            PID
TCP      127.0.0.1:5432       0.0.0.0:0              LISTENING        17768
TCP      127.0.0.1:6379       0.0.0.0:0              LISTENING        17768

现在我们已确认 postgresql 和 redis-server 两个实例都在运行。

我们也可以在 Ubuntu 或 WSL 上用以下命令检查实例的状态:

lsof -i
COMMAND     PID        USER  FD  TYPE  DEVICE  SIZE/OFF  NODE  NAME
mailcatch  3244  rickyc0626  7u  IPv4   36127       0t0   TCP  localhost:1025 (LISTEN)
mailcatch  3244  rickyc0626  8u  IPv4   36128       0t0   TCP  *:socks (LISTEN)
redis-ser  3287  rickyc0626  6u  IPv6   29352       0t0   TCP  *:6379 (LISTEN)
redis-ser  3287  rickyc0626  7u  IPv6   29353       0t0   TCP  *:6379 (LISTEN)

sudo ss -plunt | grep postgres
tcp  LISTEN  0  128  127.0.0.1:5432  0.0.0.0:*  users:(("postgres",pid=3070,fd=7))

pg_lsclusters
Ver Cluster Port Status Owner    Data directory              Log file
10  main    5432 online postgres /var/lib/postgresql/10/main /var/log/postgresql/postgresql-10-main.log

从这里开始,这些命令应该可以正常运行,无需修改 database.yml 文件:

bundle exec rake db:create
bundle exec rake db:migrate
RAILS_ENV=test bundle exec rake db:create db:migrate

后续出现的其他问题可以留待以后处理。 [/quote]

[/details]

[details=截至 2020 年 7 月 1 日]

[quote="Ricky Chon, post:61, topic:75149, username:RickyC0626"] 一种无需在 database.yml 中显式配置端口的解决方案

数据库创建和迁移过程中的一些错误:

到特定端口的 Redis 连接无效 / 连接被拒绝

bundle exec rake db:create
rake aborted!
Redis::CannotConnectError: Error connecting to Redis on localhost:6379 (Errno::ECONNREFUSED)
...

Caused by:
Errno::ECONNREFUSED: Connection refused - connect(2) for 127.0.0.1:6379
...

Caused by:
IO::EINPROGRESSWaitWritable: Operation now in progress - connect(2) would block
...

Tasks: TOP => db:create => db:load_config => environment
(See full trace by running task with --trace)

从这个错误可以看出 Redis 使用的端口是 6379,我们可以通过该端口启动 Redis 服务器来解决这个问题。

默认情况下,redis-server --daemonize yes 应该可以工作,但如果不行,请使用:

redis-server --daemonize yes --port 6379

检查 Redis 实例的状态:

redis-cli

127.0.0.1:6379> ping
PONG

psql 端口连接无效

bundle exec rake db:migrate
PG::ConnectionBad: could not connect to server: No such file or directory
       Is the server running locally and accepting
       connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

从这个错误可以看出 PostgreSQL 在连接端口 5432 时遇到问题。通常启动该服务即可解决

sudo service postgresql start

如果这样仍然无效且错误依旧,请检查服务正在监听哪个端口,如有必要,在 postgresql.conf 中将端口改为你想要的:

pg_isready
/var/run/postgresql:5432 - no response
or
/var/run/postgresql:5432 - accepting connections

结果

完成上述步骤或执行 start-discourse 命令后,这两个实例应该会在其默认/指定端口上运行。要通过默认的 Windows cmd 终端检查它们的状态,我们可以运行:

netstat -anop tcp

将会显示类似这样的内容:

Proto    Local Address        Foreign Address        State            PID
TCP      127.0.0.1:5432       0.0.0.0:0              LISTENING        17768
TCP      127.0.0.1:6379       0.0.0.0:0              LISTENING        17768

现在我们已确认 postgresql 和 redis-server 实例都在运行。

我们也可以使用以下命令在 Ubuntu 或 WSL 上检查这些实例的状态:

lsof -i
COMMAND     PID        USER  FD  TYPE  DEVICE  SIZE/OFF  NODE  NAME
mailcatch  3244  rickyc0626  7u  IPv4   36127       0t0   TCP  localhost:1025 (LISTEN)
mailcatch  3244  rickyc0626  8u  IPv4   36128       0t0   TCP  *:socks (LISTEN)
redis-ser  3287  rickyc0626  6u  IPv6   29352       0t0   TCP  *:6379 (LISTEN)
redis-ser  3287  rickyc0626  7u  IPv6   29353       0t0   TCP  *:6379 (LISTEN)

sudo ss -plunt | grep postgres
tcp  LISTEN  0  128  127.0.0.1:5432  0.0.0.0:*  users:(("postgres",pid=3070,fd=7))

pg_lsclusters
Ver Cluster Port Status Owner    Data directory              Log file
10  main    5432 online postgres /var/lib/postgresql/10/main /var/log/postgresql/postgresql-10-main.log

从这里开始,这些命令应该可以正常运行,没有大的问题,无需修改 database.yml 文件:

bundle exec rake db:create
bundle exec rake db:migrate
RAILS_ENV=test bundle exec rake db:create db:migrate

之后出现的任何问题都可以在将来处理。 [/quote]

[/details]


最后由 @SaraDev 于 [date=2022-06-15 time=19:00:00 timezone="America/Los_Angeles"] 审核