如何在 Rocky Linux 9 上部署 Ghost CMS
在此页
- 先决条件
- 第 1 步 - 配置防火墙
- 第 2 步 - 安装 Nginx
- 第 3 步 - 安装 Node.js
- 第 4 步 - 安装 MySQL
- 第 5 步 - 安装 Ghost
- 安装 Ghost-CLI
- 准备 Ghost 目录
- 安装幽灵
Ghost 是一个开源博客平台,可帮助您创建具有专业外观的博客。它于 2013 年作为 WordPress 的替代品推出,因为它变得过于复杂。它是用 JavaScript 编写的,由 Node.js 库提供支持。
本教程将探讨如何在由 Rocky Linux 9 提供支持的服务器上使用 Nginx 和 MySQL 安装 Ghost CMS。我们将使用 Nginx 服务器来托管博客,并使用 Lets Encrypt SSL 证书来保护我们的安装。
先决条件
-
A server running Rocky Linux 9.
-
A non-root user with sudo privileges.
-
A fully qualified domain name (FQDN) pointing to your server. For our purposes, we will use
ghost.example.com
as the domain name. -
Make sure everything is updated.
$ sudo dnf update
-
Install basic utility packages. Some of them may already be installed.
$ sudo dnf install wget curl nano unzip yum-utils -y
第 1 步 - 配置防火墙
第一步是配置防火墙。 Rocky Linux 使用 Firewalld 防火墙。检查防火墙状态。
$ sudo firewall-cmd --state running
防火墙适用于不同的区域,公共区域是我们将使用的默认区域。列出防火墙上所有活动的服务和端口。
$ sudo firewall-cmd --permanent --list-services
它应该显示以下输出。
cockpit dhcpv6-client ssh
Wiki.js 需要 HTTP 和 HTTPS 端口才能运行。打开它们。
$ sudo firewall-cmd --add-service=http --permanent $ sudo firewall-cmd --add-service=https --permanent
重新加载防火墙以应用更改。
$ sudo firewall-cmd --reload
第 2 步 - 安装 Nginx
Rocky Linux 9 附带了旧版本的 Nginx。您需要下载官方 Nginx 存储库以安装最新版本。
创建并打开
/etc/yum.repos.d/nginx.repo
文件以创建官方 Nginx 存储库。$ sudo nano /etc/yum.repos.d/nginx.repo
将以下代码粘贴到其中。
[nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true [nginx-mainline] name=nginx mainline repo baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/ gpgcheck=1 enabled=0 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true
通过按 Ctrl + X 并在出现提示时输入 Y 来保存文件。
安装 Nginx 服务器。
$ sudo dnf install nginx -y
验证安装。
$ nginx -v nginx version: nginx/1.22.1
启用并启动 Nginx 服务器。
$ sudo systemctl enable nginx --now
检查服务器的状态。
$ sudo systemctl status nginx ? nginx.service - nginx - high performance web server Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled) Active: active (running) since Tue 2022-10-25 08:27:47 UTC; 2s ago Docs: http://nginx.org/en/docs/ Process: 1650 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS) Main PID: 1651 (nginx) Tasks: 2 (limit: 5912) Memory: 1.9M CPU: 7ms CGroup: /system.slice/nginx.service ??1651 "nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf" ??1652 "nginx: worker process" Oct 25 08:27:47 ghost.example.com systemd[1]: Starting nginx - high performance web server...
第 3 步 - 安装 Node.js
Ghost Installer 需要 Nodejs 才能工作。运行以下命令来安装节点 16。
$ curl -fsSL https://rpm.nodesource.com/setup_16.x | sudo bash -
安装节点。
$ sudo dnf install -y nodejs
验证节点安装。
$ node --version v16.18.0
在编写本教程时,Node 18 已经发布,并且是 LTS 版本。 Ghost 尚未添加对它的支持。请检查 Ghost 文档中关于 Node 版本的最新支持的 Node 版本。当 Ghost 添加对 Node 18 的支持时,请改用以下命令安装 Node 18。
$ curl -fsSL https://rpm.nodesource.com/setup_18.x | sudo bash - $ sudo dnf install -y nodejs
第 4 步 - 安装 MySQL
Rocky Linux 9 附带最新版本的 MySQL。您可以使用单个命令安装它。
$ sudo dnf install mysql-server
检查MySQL的版本。
$ mysql --version mysql Ver 8.0.30 for Linux on x86_64 (Source distribution)
启用并启动 MySQL 服务。
$ sudo systemctl enable mysqld --now
检查服务状态。
$ sudo systemctl status mysqld ? mysqld.service - MySQL 8.0 database server Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled) Active: active (running) since Tue 2022-10-25 09:00:26 UTC; 3s ago Process: 2920 ExecStartPre=/usr/libexec/mysql-check-socket (code=exited, status=0/SUCCESS) Process: 2942 ExecStartPre=/usr/libexec/mysql-prepare-db-dir mysqld.service (code=exited, status=0/SUCCESS) Main PID: 3021 (mysqld) Status: "Server is operational" Tasks: 39 (limit: 5912) Memory: 404.4M CPU: 4.686s CGroup: /system.slice/mysqld.service ??3021 /usr/libexec/mysqld --basedir=/usr Oct 25 09:00:18 ghost.example.com systemd[1]: Starting MySQL 8.0 database server... Oct 25 09:00:18 ghost.example.com mysql-prepare-db-dir[2942]: Initializing MySQL database Oct 25 09:00:26 ghost.example.com systemd[1]: Started MySQL 8.0 database server.
默认情况下,您可以使用 Unix 身份验证登录 MySQL shell。但是您不能运行安全设置向导。因此,对于MySQL 8.0.28及以上版本,以下步骤是必须的。进入 MySQL 外壳。
$ sudo mysql
运行以下命令为您的 root 用户设置密码。确保它混合了数字、大写字母、小写字母和特殊字符。
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YourPassword12!';
退出外壳。此步骤的注意事项是您不能再使用 Unix 身份验证登录 MySQL shell。
mysql> exit
运行 MySQL 安全安装脚本。
$ sudo mysql_secure_installation
系统将要求您安装验证密码组件。它检查 MySQL 中使用的密码强度。按 Y 进行安装。
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
接下来,系统将要求您设置密码验证策略的级别。选择 2,因为它是最强的。然后,系统会要求您创建一个 root 密码。输入具有给定要求的密码。当系统询问您是否要继续输入密码时,请按 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 Please set the password for root here. New password: Re-enter new password: Estimated strength of the password: 100 Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y
最后,按 Y 删除匿名用户,禁止远程 root 登录,删除测试数据库,并重新加载权限表。
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!
这样就完成了安装和保护 MySQL 的过程。
第 5 步 - 安装 Ghost
Ghost 安装将包含三个组件 - Ghost-CLI 命令行工具,用于安装和管理对 Ghost 博客和博客包本身的更新。
安装 Ghost-CLI
运行以下命令安装Ghost-CLI工具。忽略在此过程中收到的任何警告。
$ sudo npm install -g
准备 Ghost 目录
创建 Ghost 根目录。
$ sudo mkdir -p /var/www/html/ghost
将目录的所有权设置为当前用户。
$ sudo chown $USER:$USER /var/www/html/ghost
设置正确的目录权限。
$ sudo chmod 755 /var/www/html/ghost
切换到 Ghost 目录。
$ cd /var/www/html/ghost
安装幽灵
安装 Ghost 是一个单一的命令过程。
$ ghost install
在安装过程中,CLI 工具会询问一些问题来配置博客。
- 在编写本教程时,Ghost-CLI 不支持除 Ubuntu 之外的任何其他操作系统。它将询问您是否仍要继续安装。按 Y 继续。
- 博客 URL:输入完整的博客 URL 以及 https 协议。 (
https://ghost.example.com
) - MySQL 主机名:按 Enter 键使用默认值
localhost
因为我们的 Ghost 安装和 MySQL 在同一台服务器上。 - MySQL 用户名:输入
root
作为您的 MySQL 用户名。 - MySQL 密码:输入您之前创建的根密码。
- Ghost 数据库名称:为您的 Ghost 数据库命名。 (<代码类=系统>ghostdb)
- Sudo 密码:输入您的系统 sudo 密码以执行提升的命令。
- 设置一个 ghost MySQL 用户?:安装程序会询问您是否要为 Ghost 创建一个单独的 MySQL 用户。按 Y 继续。
- 设置 Nginx?:通常,Ghost-CLI 会检测您的 Nginx 安装并自动为您的博客配置它。但截至目前,它无法检测到我们的 Nginx 安装。因此,安装程序将自动跳过此步骤。我们将手动配置 Nginx。
- 设置 SSL?:由于它跳过了 Nginx 配置,CLI 工具也将跳过设置 SSL。
- 设置 systemd?:Ghost 会询问您是否要为 Ghost 设置系统服务。按 Y 继续。
- 启动 Ghost?:按 Y 开始安装 Ghost。该服务将启动,但我们需要配置 Nginx 和 SSL 才能使其正常工作。
第 6 步 - 安装 SSL
在继续之前,我们需要为我们的域安装 Certbot 工具和 SSL 证书。
要安装 Certbot,我们将使用 Snapd 包安装程序。 Snapd 始终带有最新稳定版本的 Certbot,您应该使用它。
Snapd 工具需要 Epel 存储库才能工作。
$ sudo dnf install epel-release -y
我们将使用 Snapd 安装 Certbot。安装快照。
$ sudo dnf install snapd -y
启用并启动 Snap 服务。
$ sudo systemctl enable snapd.socket --now
为 Snapd 工作创建必要的链接。
$ sudo ln -s /var/lib/snapd/snap /snap $ echo 'export PATH=$PATH:/var/lib/snapd/snap/bin' | sudo tee -a /etc/profile.d/snapd.sh
确保您的 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 -d ghost.example.com
上述命令会将证书下载到服务器上的
/etc/letsencrypt/live/ghost.example.com
目录。生成 Diffie-Hellman 组证书。
$ sudo openssl dhparam -dsaparam -out /etc/ssl/certs/dhparam.pem 4096
试运行该过程以检查 SSL 续订是否正常工作。
$ sudo certbot renew --dry-run
如果您没有看到任何错误,则一切就绪。您的证书将自动更新。
第 7 步 - 配置 Nginx
创建并打开文件
/etc/nginx/conf.d/ghost.conf
进行编辑。$ sudo nano /etc/nginx/conf.d/ghost.conf
将以下代码粘贴到
ghost.conf
文件中。将ghost.example.com
的所有实例替换为您的域。## enforce HTTPS server { listen 80; listen [::]:80; server_name ghost.example.com; return 301 https://$server_name$request_uri; } server { listen 443 ssl http2; listen [::]:443 ssl http2; server_name ghost.example.com; access_log /var/log/nginx/ghost.access.log; error_log /var/log/nginx/ghost.error.log; client_max_body_size 20m; http2_push_preload on; # Enable HTTP/2 Server Push ssl_certificate /etc/letsencrypt/live/ghost.example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/ghost.example.com/privkey.pem; ssl_trusted_certificate /etc/letsencrypt/live/ghost.example.com/chain.pem; ssl_session_timeout 1d; # Enable TLS versions (TLSv1.3 is required upcoming HTTP/3 QUIC). ssl_protocols TLSv1.2 TLSv1.3; # Enable TLSv1.3's 0-RTT. Use $ssl_early_data when reverse proxying to # prevent replay attacks. # # @see: https://nginx.org/en/docs/http/ngx_http_ssl_module.html#ssl_early_data ssl_early_data on; ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384'; ssl_prefer_server_ciphers on; ssl_ecdh_curve X25519:prime256v1:secp384r1:secp521r1; ssl_session_cache shared:SSL:50m; ssl_session_tickets off; # OCSP Stapling --- # fetch OCSP records from URL in ssl_certificate and cache them ssl_stapling on; ssl_stapling_verify on; ssl_dhparam /etc/ssl/certs/dhparam.pem; add_header X-Early-Data $tls1_3_early_data; location / { proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-Proto https; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://localhost:2368; } } # This block is useful for debugging TLS v1.3. Please feel free to remove this # and use the `$ssl_early_data` variable exposed by NGINX directly should you # wish to do so. map $ssl_early_data $tls1_3_early_data { "~." $ssl_early_data; default ""; }
上述配置会将所有 HTTP 请求重定向到 HTTPS,并将作为 Ghost 服务的代理通过您的域为其提供服务。
通过按 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 服务器。
$ sudo systemctl reload nginx
第 8 步 - 配置 SELinux
允许 Nginx 连接到网络。
$ sudo setsebool -P httpd_can_network_connect 1
第 9 步 - 运行站点
现在,您可以通过在 Web 浏览器中打开
https://ghost.example.com
来验证您的安装。您将看到以下页面,表明安装成功。第 10 步 - 完成设置
要完成 Ghost 博客的设置,请在浏览器中访问
https://ghost.example.com/ghost
。博客域末尾的额外/ghost
会将您重定向到 Ghosts 管理面板,或者在本例中为设置,因为您是第一次访问它。您将需要创建您的管理员帐户并选择一个博客标题。
输入您的详细信息,然后单击“创建帐户并开始发布”按钮继续。
接下来,您将被带到以下屏幕,在这里您可以选择撰写您的第一篇文章、自定义您的站点和导入成员。
我们将选择 Explore Ghost 管理员进行探索并直接转到仪表板。在设置结束时,您将看到 Ghosts 管理面板。
如果你想切换到黑暗模式,你可以通过点击设置页面底部设置齿轮按钮旁边的切换开关来实现。
切换到帖子页面,您将看到默认帖子。您可以取消发布或删除它并开始发布。
第 11 步 - 配置邮件程序
Ghost 不仅充当博客平台,还充当时事通讯管理器。对于日常操作,您可以使用任何事务性邮件服务与 Ghost 一起发送邮件。但是如果你想通过 Ghost 发送时事通讯,唯一支持的官方群发邮件程序是 Mailgun。您也可以使用不同的时事通讯服务,但为此,您需要使用 Ghost 的 Zapier 集成功能。
让我们首先为交易电子邮件配置 SMTP 服务。为此,打开文件
/var/www/html/ghost/config.production.json
文件进行编辑。$ nano /var/www/html/ghost/config.production.json
找到以下几行。
"mail": { "transport": "Direct" },
用以下代码替换它们。
"mail": { "from": "'Acme Support' ", "transport": "SMTP", "options": { "host": "YOUR-SES-SERVER-NAME", "port": 465, "service": "SES", "auth": { "user": "YOUR-SES-SMTP-ACCESS-KEY-ID", "pass": "YOUR-SES-SMTP-SECRET-ACCESS-KEY" } } },
我们在这里使用 Amazon SES 邮件服务,因为如果您使用他们的 EC2 服务,它非常便宜并且终身免费。
如果您想通过 Mailgun 使用他们的时事通讯服务,请输入以下代码。
"mail": { "from": "'Acme Support' ", "transport": "SMTP", "options": { "service": "Mailgun", "host": "smtp.mailgun.org", "port": 587, "secure": true, "auth": { "user": "", "pass": "1234567890" } } },
通过按 Ctrl + X 并在出现提示时输入 Y 来保存文件。
完成后,重新启动 Ghost 应用程序以使更改生效。
$ ghost restart
要配置时事通讯设置,请访问设置 >> 电子邮件时事通讯部分。
单击 Mailgun 配置链接以展开。
填写您的 Mailgun 区域、域和 API 密钥。
单击右上角的保存按钮以保存设置。
要测试时事通讯的发送,请打开任何帖子,打开其设置并单击电子邮件时事通讯选项。接下来,发送一封测试电子邮件以检查它是否有效。如果您没有收到任何错误,则表示您的时事通讯发送正常。
第 12 步 - 更新 Ghost
有两种类型的 Ghost 更新 - 次要更新和主要更新。
首先,如果要运行次要更新,请进行完整备份。
$ cd /var/www/html/ghost $ ghost backup
运行update命令进行次要更新。
$ ghost update
要执行重大更新,您应该遵循 Ghost 上的官方详细更新指南。根据您当前使用的版本和要更新到的主要版本,步骤会有所不同。
结论
我们关于如何使用 Nginx 在 Rocky Linux 9 服务器上设置 Ghost CMS 的教程到此结束。如果您有任何问题或反馈,请在下面的评论中分享。