本页内容

  • 先决条件
  • 步骤 1 - 配置防火墙
  • 步骤 2 - 安装 PHP 及其扩展
  • 步骤 3 - 安装 Composer
  • 步骤 4 - 安装 MySQL
  • 步骤 5 - 配置 MySQL
  • 步骤 6 - 安装 Nginx
  • 步骤 7 - 安装 SSL
  • 步骤 8 - 安装 Wallabag
  • 步骤 9 - 配置 Nginx 和 PHP、配置 PHP-FPM、配置 Nginx
  • 步骤 10 - 访问 Wallabag
  • 结论
  • 配置 PHP-FPM
  • 配置 Nginx

Wallabag 是一种稍后读(read-it-later)类型的服务。它允许你保存网页,以便日后在空闲时阅读。市面上有很多类似的服务,例如 Pocket、Instapaper 等,但在你自己拥有的服务器上部署这样一项服务要好得多。首先,它不会倒闭并随之带走你的链接。

本教程将介绍如何在运行 Ubuntu 22.04 的服务器上安装并配置 Wallabag。同时也会介绍如何搭建 Nginx、MySQL、Composer 和 PHP,这些正是 Wallabag 运行所必需的组件。

先决条件

  • 一台运行 Ubuntu 22.04 的服务器。
  • 一个具有 sudo 权限的非 root 用户。
  • 一个完整域名(FQDN),例如 wallabag.example.com。
  • 确保所有内容都已更新。 $ sudo apt update $ sudo apt upgrade
  • 系统需要安装几个软件包。$ sudo apt install wget curl nano software-properties-common dirmngr apt-transport-https gnupg2 ca-certificates lsb-release ubuntu-keyring unzip -y 其中部分软件包可能已在你的系统中安装。

一台运行 Ubuntu 22.04 的服务器。

一个具有 sudo 权限的非 root 用户。

一个完整域名(FQDN),例如 wallabag.example.com。

确保所有内容都已更新。

$ sudo apt update
$ sudo apt upgrade

系统需要安装几个软件包。

$ sudo apt install wget curl nano software-properties-common dirmngr apt-transport-https gnupg2 ca-certificates lsb-release ubuntu-keyring unzip -y

其中部分软件包可能已在你的系统中安装。

步骤 1 - 配置防火墙

第一步是配置防火墙。Ubuntu 默认自带 ufw(Uncomplicated Firewall,简易防火墙)。

检查防火墙是否正在运行。

$ sudo ufw status

你应该会看到如下输出。

Status: inactive

放行 SSH 端口,以免启用防火墙时断开当前连接。

$ sudo ufw allow OpenSSH

同时放行 HTTP 和 HTTPS 端口。

$ sudo ufw allow http
$ sudo ufw allow https

启用防火墙

$ sudo ufw enable
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
Firewall is active and enabled on system startup

再次检查防火墙状态。

$ sudo ufw status

你应该会看到类似的输出。

Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
80/tcp                     ALLOW       Anywhere
443                        ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)
80/tcp (v6)                ALLOW       Anywhere (v6)
443 (v6)                   ALLOW       Anywhere (v6)

步骤 2 - 安装 PHP 及其扩展

Ubuntu 22.04 自带 PHP 8.1.2 版本,略显过时。我们将通过 Ondrej 的 PHP 软件源安装最新的 PHP 8.1 版本。

$ sudo add-apt-repository ppa:ondrej/php

接下来,安装 Wallabag 所需的 PHP 及其扩展。

$ sudo apt install php8.1-fpm php8.1-mysql php8.1-bcmath php8.1-xml php8.1-zip php8.1-curl php8.1-mbstring php8.1-gd php8.1-tidy php8.1-intl php8.1-cli 

验证安装。

$ php --version
PHP 8.1.16 (cli) (built: Feb 14 2023 18:35:37) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.16, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.16, Copyright (c), by Zend Technologies

步骤 3 - 安装 Composer

Composer 是 PHP 的依赖管理工具,安装 Wallabag 时需要使用它。

运行以下命令下载 Composer 二进制文件。Wallabag 仅兼容 Composer 2.2 LTS,因此我们相应地调整了命令。

$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
$ php composer-setup.php --2.2
$ php -r "unlink('composer-setup.php');"

将二进制文件移动到 /usr/local/bin 目录来完成 Composer 安装。

$ sudo mv composer.phar /usr/local/bin/composer

通过查看版本号来验证安装。

$ composer --version
Composer version 2.2.21 2023-02-15 13:07:40

步骤 4 - 安装 MySQL

Ubuntu 22.04 自带最新版本的 MySQL。你可以用一条命令完成安装。

$ sudo apt install mysql-server

查看 MySQL 版本。

$ mysql --version
mysql  Ver 8.0.32-0ubuntu0.22.04.2 for Linux on x86_64 ((Ubuntu))

对于 MySQL 8.0.28 及以上版本,此步骤是必要的。进入 MySQL Shell。

$ sudo mysql

运行以下命令为 root 用户设置密码。请确保密码包含数字、大写字母、小写字母和特殊字符的组合。

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YourPassword12!';

退出 Shell。

mysql> exit

运行 MySQL 安全安装脚本。

$ sudo mysql_secure_installation

首先,系统会要求你输入 root 密码,输入即可。接着会询问是否安装 Validate Password Component(密码强度校验组件),它会检查 MySQL 中所用密码的强度。按 Y 安装。随后会要求设置密码校验策略的级别,选择 2,因为它是最严格的一档。

Securing the MySQL server deployment.

Enter password for user root:

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: 2
Using existing password for root.

Estimated strength of the password: 100

接下来,输入 N 以拒绝修改 root 密码。同时,输入 Y 以删除匿名用户、禁止 root 远程登录、移除测试数据库并重新加载权限表。

Change the password for root ? ((Press y|Y for Yes, any other key for No) : N

 ... skipping.
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!

步骤 5 - 配置 MySQL

登录 MySQL shell。在提示时输入你的 root 密码。

$ sudo mysql -u root -p

创建一个示例数据库。

mysql> CREATE DATABASE wallabag;

创建一个 SQL 用户账户。

mysql> CREATE USER 'wallabaguser'@'localhost' IDENTIFIED BY 'Your_password2';

将该数据库的全部权限授予该用户。

mysql> GRANT ALL PRIVILEGES ON wallabag.* TO 'wallabaguser'@'localhost';

刷新用户权限。

mysql> FLUSH PRIVILEGES;

退出 Shell。

mysql> exit

步骤 6 - 安装 Nginx

Ubuntu 22.04 自带较旧版本的 Nginx。你需要添加 Nginx 官方软件源才能安装最新版本。

导入 Nginx 的签名密钥。

$ curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
| sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null

添加 Nginx 稳定版的软件源。

$ echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg arch=amd64] \
http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" \
| sudo tee /etc/apt/sources.list.d/nginx.list

更新系统软件源。

$ sudo apt update

安装 Nginx。

$ sudo apt install nginx

验证安装。

$ nginx -v
nginx version: nginx/1.22.1

启动 Nginx 服务。

$ sudo systemctl start nginx

步骤 7 - 安装 SSL

我们需要安装 Certbot 来生成 SSL 证书。你可以选择通过 Ubuntu 的软件源安装 Certbot,或使用 Snapd 工具获取最新版本。本教程将采用 Snapd 版本。

Ubuntu 22.04 默认已安装 Snapd。运行以下命令确保你的 Snapd 版本是最新的。

$ sudo snap install core && sudo snap refresh core

安装 Certbot。

$ sudo snap install --classic certbot

使用以下命令创建一个指向 /usr/bin 目录的符号链接,以确保可以直接运行 Certbot 命令。

$ sudo ln -s /snap/bin/certbot /usr/bin/certbot

运行以下命令生成 SSL 证书。

$ sudo certbot certonly --nginx --agree-tos --no-eff-email --staple-ocsp --preferred-challenges http -m 
[email protected]
 -d wallabag.example.com

上述命令会将证书下载到服务器上的 /etc/letsencrypt/live/wallabag.example.com 目录。

生成 Diffie-Hellman 组证书。

$ sudo openssl dhparam -dsaparam -out /etc/ssl/certs/dhparam.pem 4096

检查 Certbot 续期调度服务。

$ sudo systemctl list-timers

你会发现 snap.certbot.renew.service 是已排定运行的服务之一。

NEXT                        LEFT          LAST                        PASSED        UNIT                      ACTIVATES
.....
Sun 2023-02-26 06:32:00 UTC 9h left       Sat 2023-02-25 18:04:05 UTC 2h 59min ago  snap.certbot.renew.timer  snap.certbot.renew.service
Sun 2023-02-26 06:43:20 UTC 9h left       Sat 2023-02-25 10:49:23 UTC 10h ago       apt-daily-upgrade.timer   apt-daily-upgrade.service
Sun 2023-02-26 09:00:06 UTC 11h left      Sat 2023-02-25 20:58:06 UTC 5min ago      apt-daily.timer           apt-daily.service

对该流程执行一次演练(dry run),以检查 SSL 续期是否正常工作。

$ sudo certbot renew --dry-run

如果没有看到错误,说明一切就绪。你的证书将自动续期。

步骤 8 - 安装 Wallabag

创建 /var/www/wallabag/html 目录。

$ sudo mkdir /var/www/html/wallabag -p

下载最新版本的 Wallabag。

$ wget https://wllbg.org/latest-v2-package

解压归档文件。

$ tar xzf latest-v2-package

将解压目录中的文件移动到之前创建的目录。你可以从 GitHub releases 页面查看 Wallabag 的最新版本。撰写本教程时的最新版本是 2.5.4。

$ sudo mv wallabag-2.5.4/* /var/www/html/wallabag

创建资源(asset)目录。

$ sudo mkdir /var/www/html/wallabag/data/assets

将 /var/www/html/wallabag 目录的权限更改为当前登录用户。

$ sudo chown -R $USER:$USER /var/www/html/wallabag

切换到该目录。

$ cd /var/www/html/wallabag

通过复制示例文件来创建 parameters.yml 文件。

$ cp app/config/parameters.yml.dist app/config/parameters.yml

在开始配置 Wallabag 之前,先生成一个密钥(secret key)。请记下该密钥,稍后会用到。

$ openssl rand -base64 32
QLV/GpZwDobQbyQZQ15FkM1Hvt+ZFJZXw8GW9F4KR3o=

打开 parameters 文件进行编辑。

$ nano app/config/parameters.yml

找到以下部分并填写数据库凭据。MySQL 的数据库端口为 3306。

..........
    database_driver: pdo_mysql
    database_host: 127.0.0.1
    database_port: 3306
    database_name: wallabag
    database_user: wallabaguser
    database_password: Your_password2

填写服务器描述和域名。

    domain_name: https://wallabag.example.com
    server_name: "Howtoforge Wallabag"

填写你的 SMTP 详情。本教程中使用的是 Amazon SES 服务。

    mailer_transport:  smtp
    mailer_user:       YOUR_AES_USERNAME
    mailer_password:   YOUR_AES_PASSWORD
    mailer_host:       email-smtp.us-west-2.amazonaws.com
    mailer_port:       587
    mailer_encryption: tls

填写之前生成的密钥。如果想保留双因素认证,请确保应用以下设置。如果想关闭用户注册,请将 fouser_registration 的值设为 false。fouser_confirmation 变量默认设为 true,意味着每次用户注册都需要通过邮件确认。将 from_email 变量的值改为你自选的邮箱地址。

    # A secret key that's used to generate certain security-related tokens
    secret: QLV/GpZwDobQbyQZQ15FkM1Hvt+ZFJZXw8GW9F4KR3o=

    # two factor stuff
    twofactor_auth: true
    twofactor_sender: 
[email protected]


    # fosuser stuff
    fosuser_registration: true
    fosuser_confirmation: true
.....
    from_email: 
[email protected]

.....

还有更多与 Redis、RabbitMQ 和 Sentry 设置相关的条目(sentries)。安装相应软件包后,你可以按需进行配置。

按下 Ctrl + X,并在提示时输入 Y 以保存文件。

使用 Composer 下载并安装 Wallabag 所需的依赖。

$ SYMFONY_ENV=prod composer install --no-dev -o --prefer-dist

使用 Wallabag 命令行工具完成安装。

$ php bin/console wallabag:install --env=prod

系统会询问你是否要重置数据库及其 schema。两次都输入 no。接下来会问你是否创建管理员账户。输入 yes 继续,并填写该账户的用户名、密码和邮箱地址。

wallabag installer
==================

Step 1 of 4: Checking system requirements.
------------------------------------------

 ------------------------ -------- ---------------- 
  Checked                  Status   Recommendation  
 ------------------------ -------- ---------------- 
  PDO Driver (pdo_mysql)   OK!                      
  Database connection      OK!                      
  Database version         OK!                      
  curl_exec                OK!                      
  curl_multi_init          OK!                      
 ------------------------ -------- ---------------- 

 [OK] Success! Your system can run wallabag properly.                                                                   
                                                                                                                        

Step 2 of 4: Setting up database.
---------------------------------

 It appears that your database already exists. Would you like to reset it? (yes/no) [no]:
 > no

 Seems like your database contains schema. Do you want to reset it? (yes/no) [no]:
 > no

 Clearing the cache...

 Database successfully setup.

Step 3 of 4: Administration setup.
----------------------------------

 Would you like to create a new admin user (recommended)? (yes/no) [yes]:
 > yes

 Username [wallabag]:
 > navjot

 Password [wallabag]:
 > 

 Email [
[email protected]
]:
 > 
[email protected]


 Administration successfully setup.

Step 4 of 4: Config setup.
--------------------------

 Config successfully setup.
                                                                                                                        
 [OK] wallabag has been successfully installed.                                                                         
                                                                                                                     
 [OK] You can now configure your web server, see https://doc.wallabag.org

将目录权限重新交还 Nginx。

$ sudo chown -R nginx:nginx /var/www/html/wallabag

步骤 9 - 配置 Nginx 和 PHP

配置 PHP-FPM

打开 /etc/php/8.1/fpm/pool.d/www.conf 文件。

$ sudo nano /etc/php/8.1/fpm/pool.d/www.conf

我们需要将 PHP 进程的 Unix 用户/组设为 nginx。在文件中找到 user=www-data 和 group=www-data 这两行,并将它们改为 nginx。

...
; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
;       will be used.
; RPM: apache user chosen to provide access to the same directories as httpd
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx
...

在文件中找到 listen.owner = www-data 和 listen.group = www-data 这两行,并将它们改为 nginx。

; Set permissions for unix socket, if one is used. In Linux, read/write
; permissions must be set in order to allow connections from a web server. Many
; BSD-derived systems allow connections regardless of permissions. The owner
; and group can be specified either by name or by their numeric IDs.
; Default Values: user and group are set as the running user
;                 mode is set to 0660
listen.owner = nginx
listen.group = nginx

按下 Ctrl + X,并在提示时输入 Y 以保存文件。

将 PHP-FPM 和 PHP-CLI 的执行时间上限提高到 60 秒。

$ sudo sed -i 's/max_execution_time = 30/max_execution_time = 60/' /etc/php/8.1/fpm/php.ini
$ sudo sed -i 's/max_execution_time = 30/max_execution_time = 60/' /etc/php/8.1/cli/php.ini

将 PHP-FPM 的内存限制从 128MB 提高到 256MB。

$ sudo sed -i 's/memory_limit = 128M/memory_limit = 256M/' /etc/php/8.1/fpm/php.ini

重启 PHP-FPM 服务。

$ sudo systemctl restart php8.1-fpm

将 PHP sessions 目录的属组改为 Nginx。

$ sudo chgrp -R nginx /var/lib/php/sessions

配置 Nginx

创建并打开 /etc/nginx/conf.d/wallabag.conf 文件进行编辑。

$ sudo nano /etc/nginx/conf.d/wallabag.conf

在其中粘贴以下代码。

server {
    listen       443 ssl http2;
    listen       [::]:443 ssl http2;
    server_name  wallabag.example.com;

    access_log  /var/log/nginx/wallabag.access.log;
    error_log   /var/log/nginx/wallabag.error.log;

	# SSL
    ssl_certificate      /etc/letsencrypt/live/wallabag.example.com/fullchain.pem;
    ssl_certificate_key  /etc/letsencrypt/live/wallabag.example.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/wallabag.example.com/chain.pem;
    ssl_session_timeout  5m;
    ssl_session_cache shared:MozSSL:10m;
    ssl_session_tickets off;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers on;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
    ssl_ecdh_curve X25519:prime256v1:secp384r1:secp521r1;
    ssl_stapling on;
    ssl_stapling_verify on;
    ssl_dhparam /etc/ssl/certs/dhparam.pem;
    resolver 8.8.8.8;

    root /var/www/html/wallabag/web;

    location / {
        try_files $uri /app.php$is_args$args;
    }

    # Pass PHP Scripts To FastCGI Server
    location ~ ^/app\.php(/|$) {
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        fastcgi_pass unix:/run/php/php8.1-fpm.sock; # Depends On The PHP Version
        fastcgi_param SCRIPT_FILENAME  $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        include fastcgi_params;
        internal;
    }

    location ~ \.php$ {
        return 404;
    }
}

# enforce HTTPS
server {
    listen       80;
    listen       [::]:80;
    server_name  wallabag.example.com;
    return 301   https://$host$request_uri;
}

请注意,Nginx 配置中要使用的根目录是 /var/www/html/wallabag/public/。

完成后按下 Ctrl + X,并在提示时输入 Y 以保存文件。

打开 /etc/nginx/nginx.conf 文件进行编辑。

$ sudo nano /etc/nginx/nginx.conf

在 include /etc/nginx/conf.d/*.conf; 这一行之前添加以下一行。

server_names_hash_bucket_size  64;

按下 Ctrl + X,并在提示时输入 Y 以保存文件。

验证 Nginx 配置文件语法。

$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

重启 Nginx 服务。

$ sudo systemctl restart nginx

步骤 10 - 访问 Wallabag

在浏览器中打开 URL https://wallabag.example.com,你将看到如下登录界面。

在 Ubuntu 22.04 上安装 Wallabag 稍后读服务

输入安装期间创建的凭据,并点击 LOG IN 按钮继续。随后你将进入 Wallabag 仪表盘。

在 Ubuntu 22.04 上安装 Wallabag 稍后读服务

Wallabag 为各类浏览器、手机或电子书阅读器提供了大量配套应用,借助它们你可以添加链接。如果其他都不合心意,你甚至可以使用 Bookmarklet(书签小工具),其详细说明可在“How to”部分找到——点击仪表盘右上角的用户图标即可。

在 Ubuntu 22.04 上安装 Wallabag 稍后读服务

你会获得浏览器扩展、手机应用以及 Wallabag bookmarklet 的链接。

在 Ubuntu 22.04 上安装 Wallabag 稍后读服务

大功告成。现在你可以开始使用 Wallabag 保存文章,以便稍后阅读了。

结论

至此,关于在 Ubuntu 22.04 服务器上安装 Wallabag 的教程就结束了。如果你有任何疑问,请在下方评论区留言。