如何在 Rocky Linux 9 上安装 LOMP Stack(OpenLiteSpeed、MySQL 和 PHP)
OpenLiteSpeed 是 LiteSpeed Technologies 开发的 LiteSpeed Server 的轻量级开源版本。它支持 Apache Rewrite 规则、HTTP/2 和 HTTP/3、TLS v1.3 和 QUIC 协议。它配备了基于 WebGUI 的管理面板,使其与其他服务器不同并且更易于管理。
LOMP Stack 是 Linux、OpenLiteSpeed、MySQL/MariaDB 和 PHP 的缩写。 Litespeed 服务器以其速度而闻名,尤其是使用 LiteSpeed 服务器应用程序编程接口 (LSAPI) 集成的 PHP。 LiteSpeed PHP (LSPHP) 解释器通过 LSAPI 提供动态 PHP 页面。
本教程将逐步向您展示如何在 Rocky Linux 9 计算机上安装 LOMP 服务器。
先决条件
运行 Rocky Linux 9 的服务器。
具有 sudo 权限的非 root 用户。
完全限定的域名 (FQDN),例如指向服务器的 example.com
。
无需禁用或配置 SELinux 即可与 OpenLiteSpeed 一起使用。
确保一切都已更新。
$ sudo dnf update
您的系统需要的软件包很少。
$ 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
OpenLiteSpeed 的管理面板需要 7080 端口。
$ sudo firewall-cmd --permanent --add-port=7080/tcp
我们还需要 HTTP 和 HTTPS 端口才能运行。打开它们。
$ sudo firewall-cmd --permanent --add-service=http
$ sudo firewall-cmd --permanent --add-service=https
重新加载防火墙以应用更改。
$ sudo firewall-cmd --reload
再次列出所有服务。
$ sudo firewall-cmd --permanent --list-services
您应该得到以下输出。
cockpit dhcpv6-client http https ssh
第 2 步 - 安装 OpenLiteSpeed
下载 OpenLiteSpeed 二进制文件。您可以从 OpenLiteSpeed 官方下载页面获取最新二进制文件的链接。
$ wget https://openlitespeed.org/packages/openlitespeed-1.7.16.tgz
提取文件。
$ tar -zxf openlitespeed-*.tgz
切换到解压后的目录。
$ cd openlitespeed
运行安装程序。
$ sudo ./install.sh
等待安装程序完成一段时间。
Rocky Linux 9 计算机上的 OpenLiteSpeed 存在一些问题,因此您可能会看到一些有关缺少 PHP 包的错误。您现在可以忽略它们,因为服务器仍然可以使用各种解决方法运行。
安装程序脚本安装并启用 lshttpd
systemd 服务。
启动 OpenLiteSpeed 服务器。
$ sudo systemctl start lshttpd
检查安装的服务器版本。
$ /usr/local/lsws/bin/lshttpd -v
LiteSpeed/1.7.16 Open (BUILD built: Thu Nov 17 16:18:46 UTC 2022)
module versions:
lsquic 3.1.1
modgzip 1.1
cache 1.64
mod_security 1.4
创建管理员密码
运行密码重置脚本。
$ sudo /usr/local/lsws/admin/misc/admpass.sh
您将得到以下输出。将 admin
作为用户名并设置一个强密码。
Please specify the user name of administrator.
This is the user name required to login the administration Web interface.
User name [admin]: admin
Please specify the administrator's password.
This is the password required to login the administration Web interface.
Password:
Retype password:
Administrator's username/password is updated successfully!
您现在可以使用新的管理员密码。
打开 http://
以访问 OpenLiteSpeed 的管理面板。首次登录时,您的浏览器会警告您的连接不是私密的。单击“高级”,然后单击“接受风险并继续”(对于 Firefox)或“继续
”(对于基于 Chromium 的浏览器)。您将不会再看到该警告。
您应该看到登录页面。
输入您之前设置的admin
作为用户名和密码,然后按登录按钮继续。
您将看到以下屏幕。
第 3 步 - 安装 PHP
OpenLiteSpeed 安装程序通常默认安装 PHP 7.4。对于 Rocky Linux 9,它不会自动安装。此外,我们将安装最新版本的 PHP。在此之前,我们需要安装 LiteSpeed 存储库。
通过运行以下命令安装 OpenLiteSpeed 存储库。
$ sudo rpm -Uvh http://rpms.litespeedtech.com/centos/litespeed-repo-1.3-1.el8.noarch.rpm
您可以从 OpenLiteSpeed 存储库页面获取最新版本的 RPM 文件。
安装 PHP 8.2。
$ sudo dnf install lsphp82 lsphp82-mysqlnd lsphp82-process lsphp82-bcmath lsphp82-pdo lsphp82-common lsphp82-xml lsphp82-opcache lsphp82-soap
存储库中仍然缺少 lsphp-gd 和 lsphp-mbstring 包。所以如果你想要的话,还需要等待一段时间才能正式发布。
验证 PHP 安装。
$ /usr/local/lsws/lsphp82/bin/php -v
PHP 8.2.1 (cli) (built: Jan 3 2023 18:40:55) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.1, Copyright (c) Zend Technologies
with Zend OPcache v8.2.1, Copyright (c), by Zend Technologies
您可以检查已启用的 PHP 模块的列表。
$ /usr/local/lsws/lsphp82/bin/php --modules
[PHP Modules]
bcmath
bz2
calendar
Core
ctype
curl
date
dom
exif
fileinfo
filter
ftp
gettext
hash
iconv
json
libxml
mysqli
mysqlnd
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
random
readline
Reflection
session
shmop
SimpleXML
soap
sockets
SPL
sqlite3
standard
sysvmsg
sysvsem
sysvshm
tokenizer
xml
xmlreader
xmlwriter
xsl
Zend OPcache
zip
zlib
[Zend Modules]
Zend OPcache
稍后我们将配置 OpenLiteSpeed 以与 PHP 配合使用。
第 4 步 - 安装 MySQL
安装 MySQL 服务器。
$ sudo dnf install mysql-server
启用并启动 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 Fri 2023-02-17 07:31:00 UTC; 2s ago
Process: 14933 ExecStartPre=/usr/libexec/mysql-check-socket (code=exited, status=0/SUCCESS)
Process: 14955 ExecStartPre=/usr/libexec/mysql-prepare-db-dir mysqld.service (code=exited, status=0/SUCCESS)
Main PID: 15054 (mysqld)
Status: "Server is operational"
Tasks: 39 (limit: 5873)
Memory: 427.1M
CPU: 4.079s
CGroup: /system.slice/mysqld.service
??15054 /usr/libexec/mysqld --basedir=/usr
Feb 17 07:30:51 nspeaks.xyz systemd[1]: Starting MySQL 8.0 database server...
Feb 17 07:30:51 nspeaks.xyz mysql-prepare-db-dir[14955]: Initializing MySQL database
Feb 17 07:31:00 nspeaks.xyz systemd[1]: Started MySQL 8.0 database server.
运行 MySQL 安全脚本。
$ sudo mysql_secure_installation
您将收到几个提示。第一个提示将询问您是否要安装验证密码插件。按 Y 安装插件。选择 2 作为其安全级别,这将要求您的密码长度至少为 8 个字符,并且包含大写、小写、数字和特殊字符的组合。
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: 2
接下来,系统会要求您创建一个强 root 密码。确保您的密码符合验证插件的要求。
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。
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 shell。
$ mysql -u root -p
出现提示时输入您的 root 密码。
创建测试数据库和具有访问权限的用户。将 testdb
和 testuser
替换为适合您的设置的名称。将 password
替换为强密码。
mysql> CREATE DATABASE testdb;
mysql> CREATE USER 'testuser' IDENTIFIED BY 'Your_Password123';
mysql> GRANT ALL PRIVILEGES ON testdb.* TO 'testuser';
mysql> FLUSH PRIVILEGES;
退出 MySQL shell。
mysql> exit
第 5 步 - 配置 OpenLiteSpeed
将 HTTP 端口切换回 80
让我们将默认 HTTP 端口更改为 80。使用您刚刚创建的凭据登录到您的管理面板:http://
。
访问左侧的听众部分。您将看到端口 8080
的默认侦听器。
单击查看按钮查看详细配置。在下一页的监听器默认>常规页面下,单击编辑图标并将端口从8080
更改为80
.
单击“保存”并通过单击“正常重启”按钮重新启动服务器。
第 6 步 - 配置 PHP
在此步骤中,我们需要将 PHP 8.2 的副本与服务器关联。
点击左侧的服务器配置部分,然后点击外部应用选项卡。您将看到一个现有的 PHP LiteSpeed 应用程序。我们将对它进行一些编辑。
单击编辑按钮编辑 PHP 应用程序。
接下来,匹配如下所示的配置。将所有其他字段留空。
Name: lsphp
Address: uds://tmp/lshttpd/lsphp.sock
Max Connections: 35
Environment: PHP_LSAPI_MAX_REQUESTS=500
PHP_LSAPI_CHILDREN=35
LSAPI_AVOID_FORK=200M
Initial Request Timeout (secs): 60
Retry Timeout : 0
Persistent Connection: Yes
Response Buffering: no
Start By Server: Yes(Through CGI Daemon)
Command: lsphp82/bin/lsphp
Back Log: 100
Instances: 1
Priority: 0
Memory Soft Limit (bytes): 2047M
Memory Hard Limit (bytes): 2047M
Process Soft Limit: 1400
Process Hard Limit: 1500
完成后单击“保存”。
现在我们已经创建了自己的 PHP 8.2 应用程序,我们必须告诉服务器开始使用它。由于我们编辑了默认列表,因此它已经配置完毕。单击正常重启按钮重新启动服务器。
要测试您的 PHP 是否已正确切换,请在浏览器中访问 http://
。
第 7 步 - 创建虚拟主机
首先,我们需要为虚拟主机创建目录。
$ sudo mkdir /usr/local/lsws/example.com/{html,logs} -p
html
目录将保存公共文件,logs
目录将包含服务器日志。
接下来,打开管理控制台,从左侧访问“虚拟主机”部分,然后点击“添加”按钮。
填写指定的值
Virtual Host Name: example.com
Virtual Host Root: $SERVER_ROOT/example.com/
Config File: $SERVER_ROOT/conf/vhosts/$VH_NAME/vhconf.conf
Follow Symbolic Link: Yes
Enable Scripts/ExtApps: Yes
Restrained: Yes
External App Set UID Mode: Server UID
完成后点击保存按钮。您将收到以下错误,因为配置文件目前不存在。单击链接以创建配置文件。
再次单击保存按钮完成虚拟主机的创建。
创建虚拟主机后,转到虚拟主机 -> 选择虚拟主机(example.com) -> 常规并根据给定修改配置。
Document Root: $VH_ROOT/html/
Domain Name: example.com
Enable GZIP Compression: Yes
Enable Brotli Compression: Yes
完成后点击保存按钮。接下来,我们需要设置索引文件。单击常规部分下方索引文件的编辑按钮。设置以下选项。
Use Server Index Files: No
Index files: index.php, index.html, index.htm
Auto Index: No
完成后单击“保存”。接下来,我们需要选择日志文件。转到日志部分,点击针对虚拟主机日志进行编辑,然后填写以下值。保持其他设置不变。
Use Server’s Log: Yes
File Name: $VH_ROOT/logs/error.log
Log Level: ERROR
Rolling Size (bytes): 10M
Keep Days: 30
Compress Archive: Not Set
如果您使用的是生产/开发计算机,则可以将日志级别选择为DEBUG
。
单击“保存”,然后单击访问日志部分中的加号以添加新条目。填写以下值。
Log Control: Own Log File
File Name: $VH_ROOT/logs/access.log
Piped Logger: Not Set
Log Format: Not Set
Log Headers: Not Set
Rolling Size (bytes): 10M
Keep Days: 30
Bytes log: Not Set
Compress Archive: Not Set
完成后点击保存。接下来,我们需要在安全部分下配置访问控制。设置以下值。
Allowed List: *
Denied List: Not set
完成后点击保存。接下来,我们需要设置脚本处理程序定义。单击加号 (+) 添加新定义。设置以下值。
Suffixes: php
Handler Type: LiteSpeed SAPI
Handler Name: [Server Level]: lsphp
完成后点击保存。接下来,我们需要在重写部分下设置重写控制。设置以下值。
Enable Rewrite: Yes
Auto Load from .htaccess: Yes
Log Level: Not Set
完成后点击保存。最后,我们需要设置监听器。转到侦听器部分,然后单击默认侦听器旁边的查看按钮。然后,单击虚拟主机映射旁边的“添加”按钮添加新映射并设置以下值。
Virtual Host: example.com
Domains: example.com
完成后点击保存。现在,单击正常重启按钮以应用上述所有更改并重新启动服务器。
第 8 步 - 安装 SSL
在 OpenLiteSpeed 中设置 SSL 需要我们设置两个证书。整个服务器和 Let's Encrypt 站点特定服务器的自签名证书。
让我们首先创建自签名证书。
$ openssl req -x509 -days 365 -newkey rsa:4096 -keyout key.pem -out cert.pem -nodes
您将得到类似的输出。
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:
State or Province Name (full name) []:
Locality Name (eg, city) [Default City]:
Organization Name (eg, company) [Default Company Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:example.com
Email Address []:[email
您可以按 Enter 键浏览所有字段并将其留空。填写您的通用名域名和您的电子邮件地址。
证书现在存储在 /home/user
目录中。我们稍后将需要此信息。
要使用 Let's Encrypt,我们需要安装 Certbot 工具。为此,我们将使用 Snapd 软件包安装程序。由于 Rocky Linux 未附带,因此请安装 Snapd 安装程序。它需要 EPEL 存储库才能工作。
$ sudo dnf install -y epel-release
安装快照。
$ sudo dnf install -y snapd
启用并启动 Snap 服务。
$ sudo systemctl enable snapd --now
安装 Snap 核心包,并确保您的 Snapd 版本是最新的。
$ sudo snap install core && sudo snap refresh core
创建 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
发出以下命令来安装 Certbot。
$ sudo snap install --classic certbot
使用以下命令确保可以通过创建到 /usr/bin
目录的符号链接来运行 Certbot 命令。
$ sudo ln -s /snap/bin/certbot /usr/bin/certbot
验证安装。
$ certbot --version
certbot 2.3.0
运行以下命令生成 SSL 证书。
获取 SSL 证书。 webroot目录设置为之前配置的公共HTML目录。
$ sudo certbot certonly --webroot -w /usr/local/lsws/example.com/html/ --agree-tos --no-eff-email --staple-ocsp --preferred-challenges http -m [email -d example.com
生成 Diffie-Hellman 组证书。
$ sudo openssl dhparam -dsaparam -out /etc/ssl/certs/dhparam.pem 4096
要检查 SSL 续订是否正常工作,请试运行该过程。
$ sudo certbot renew --dry-run
如果没有看到任何错误,则一切都已准备就绪。您的证书将自动更新。
现在打开管理控制台并转到监听器>>添加新监听器并添加以下值。
Listener Name: SSL
IP Address: ANY
Port: 443
Secure: Yes
完成后点击保存。接下来,点击 SSL,然后点击“添加”按钮,然后填写以下值,转到 SSL 侦听器下的虚拟主机映射部分。
Virtual Host: example.com
Domains: example.com
完成后点击保存。接下来,转到侦听器>> SSL侦听器>> SSL选项卡>> SSL私钥和证书(编辑按钮)并为我们之前创建的自签名证书填写以下值。
Private Key File: /home/user/key.pem
Certificate File: /home/user/cert.pem
Chained Certificate: Yes
完成后点击保存。接下来,转到侦听器 >> SSL 侦听器 >> SSL 选项卡 >> SSL 协议(编辑按钮),并填写 SSL 协议和密码详细信息的以下值。
Protocol Version: TLS v1.2 TLS v1.3
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
Enable ECDH Key Exchange: Yes
Enable DH Key Exchange: Yes
DH Parameter: /etc/ssl/certs/dhparam.pem
完成后点击保存。接下来,转到虚拟主机 >> example.com >> SSL 选项卡 >> SSL 私钥和证书(编辑按钮)并使用 Let's Encrypt 证书填写以下值。
Private Key File: /etc/letsencrypt/live/example.com/privkey.pem
Certificate File: /etc/letsencrypt/live/example.com/fullchain.pem
Chained Certificate: Yes
完成后点击保存。接下来,转到虚拟主机 >> example.com >> SSL 选项卡 >> OCSP Stapling(编辑按钮)并填写以下值以启用 OCSP Stapling。
Enable OCSP Stapling: Yes
OCSP Response Max Age(Secs): 300
OCSP Responder: http://r3.o.lencr.org
完成后点击保存。接下来,转到虚拟主机>> example.com >> SSL选项卡>>安全(编辑按钮)并填写以下值以启用HTTP3/QUIC协议。
Enable HTTP3/QUIC: Yes
我们不需要启用其他选项,因为它们默认处于打开状态。
完成后点击保存。
单击“Graceful restart”按钮重新启动服务器。
第 9 步 - 测试站点
在 html
目录中创建一个测试文件。
$ sudo nano /usr/local/lsws/example.com/html/index.php
将以下代码粘贴到 Nano 编辑器中。
<html>
<head>
<h2>OpenLiteSpeed Server Install Test</h2>
</head>
<body>
<?php echo '<p>Hello,</p>';
// Define PHP variables for the MySQL connection.
$servername = "localhost";
$username = "testuser";
$password = "password";
// Create a MySQL connection.
$conn = mysqli_connect($servername, $username, $password);
// Report if the connection fails or is successful.
if (!$conn) {
exit('<p>Your connection has failed.<p>' . mysqli_connect_error());
}
echo '<p>You have connected successfully.</p>';
?>
</body>
</html>
在浏览器中打开 URL https://example.com
,您应该会看到以下页面。
测试站点功能齐全。您可以开始使用服务器来为动态 PHP 网站和应用程序提供服务。
结论
关于在 Rocky Linux 9 服务器上安装 LOMP Stack(OpenLiteSpeed、MySQL 和 PHP)的教程到此结束。如果您有任何疑问,请在下面的评论中发表。