在 RockyAlmaLinux 上安装 Lighttpd、PHP 和 MariaDB
介绍
RockyAlmaLinux 是一个强大且安全的 Linux 发行版,可以完美替代 CentOS。如果您想使用 Lighttpd、PHP 和 MariaDB 设置 Web 服务器环境,本文将指导您完成安装过程。我们将提供详细的说明,以及每个命令的示例和预期输出。
先决条件
在开始之前,请确保您拥有 RockyAlmaLinux 系统的管理员访问权限和活动的互联网连接。
步骤 1:更新系统
首先,让我们更新系统的软件包存储库并将已安装的软件包升级到最新版本。打开终端并运行以下命令:
示例
sudo dnf update
输出
$ sudo dnf update Last metadata expiration check: 0:20:47 ago on <date> Dependencies resolved. ================================================================================ Package Arch Version Repo Size ================================================================================ Upgrading: kernel x86_64 5.14.12-300.fc35 updates 10 M kernel-core x86_64 5.14.12-300.fc35 updates 21 M kernel-devel x86_64 5.14.12-300.fc35 updates 17 M kernel-headers x86_64 5.14.12-300.fc35 updates 5.2 M kernel-modules x86_64 5.14.12-300.fc35 updates 32 M kernel-modules-extra x86_64 5.14.12-300.fc35 updates 4.7 M ... Transaction Summary ================================================================================ Upgrade 21 Packages Total download size: 103 M Is this ok [y/N]: y ... Downloading Packages: [SKIPPED] kernel-5.14.12-300.fc35.x86_64.rpm: Already downloaded ... Running transaction check Transaction check succeeded. Running transaction test Transaction test succeeded. Running transaction Preparing : 1/1 Running scriptlet: kernel-core-5.14.12-300.fc35.x86_64 1/7 Upgrading : kernel-core-5.14.12-300.fc35.x86_64 1/7 Running scriptlet: kernel-5.14.12-300.fc35.x86_64 2/7 Upgrading : kernel-5.14.12-300.fc35.x86_64 2/7 Running scriptlet: kernel-headers-5.14.12-300.fc35.x86_64 3/7 Upgrading : kernel-headers-5.14.12-300.fc35.x86_64 3/7 Running scriptlet: kernel-modules-5.14.12-300.fc35.x86_64 4/7 Upgrading : kernel-modules-5.14.12-300.fc35.x86_64 4/7 ... Complete!
步骤 2:安装 Lighttpd
接下来,我们将安装 Lighttpd Web 服务器。在终端中执行以下命令:
示例
sudo dnf install lighttpd
输出
Last metadata expiration check: 0:15:01 ago on <date> Dependencies resolved. ================================================================================ Package Architecture Version Repo Size ================================================================================ Installing: lighttpd x86_64 1.4.59-1.fc34 updates 439 k Installing dependencies: libattr x86_64 2.4.48-7.fc34 fedora 29 k libev x86_64 4.33-1.fc34 fedora 70 k ... Transaction Summary ================================================================================ Install 6 Packages Total download size: 791 k Installed size: 2.1 M Is this ok [y/N]: y ... Downloading Packages: (1/6): libattr-2.4.48-7.fc34.x86_64.rpm 187 kB/s | 29 kB 00:00 (2/6): libev-4.33-1.fc34.x86_64.rpm 479 kB/s | 70 kB 00:00 ... Running transaction check Transaction check succeeded. Running transaction test Transaction test succeeded. Running transaction Preparing : 1/1 Installing : libev-4.33-1.fc34.x86_64 1/6 Installing : libattr-2.4.48-7.fc34.x86_64 2/6 Running scriptlet: lighttpd-1.4.59-1.fc34.x86_64 3/6 Installing : lighttpd-1.4.59-1.fc34.x86_64 3/6 Running scriptlet: lighttpd-1.4.59-1.fc34.x86_64 3/6 Running scriptlet: lighttpd-1.4.59-1.fc34.x86_64 4/6 Running scriptlet: lighttpd-1.4.59-1.fc34.x86_64 5/6 Running scriptlet: lighttpd-1.4.59-1.fc34.x86_64 6/6 Verifying : libattr-2.4.48-7.fc34.x86_64 1/6 Verifying : libev-4.33-1.fc34.x86_64 2/6 Verifying : lighttpd-1.4.59-1.fc34.x86_64 3/6 Installed products updated. ... Complete!
步骤 3:启动并启用 Lighttpd
安装 Lighttpd 后,启动服务并启用它在系统启动时自动启动。输入以下命令:
示例
sudo systemctl start lighttpd sudo systemctl enable lighttpd
输出
$ sudo systemctl enable lighttpd Created symlink /etc/systemd/system/multi-user.target.wants/lighttpd.service → /usr/lib/systemd/system/lighttpd.service.
步骤 4:安装 PHP
Lighttpd 需要 PHP 来处理动态内容。使用以下命令安装 PHP 及其必要的模块:
示例
sudo dnf install php php-fpm php-mysqlnd
输出
Last metadata expiration check: 0:15:01 ago on <date> Dependencies resolved. ================================================================================ Package Architecture Version Repo Size ================================================================================ Installing: php x86_64 7.4.26-1.fc35 updates 3.3 M php-fpm x86_64 7.4.26-1.fc35 updates 1.7 M php-mysqlnd x86_64 7.4.26-1.fc35 updates 195 k Installing dependencies: ... Transaction Summary ================================================================================ Install 3 Packages Total download size: 5.7 M Installed size: 23 M Is this ok [y/N]: y ... Downloading Packages: (1/3): php-7.4.26-1.fc35.x86_64.rpm 459 kB/s | 3.3 MB 00:07 (2/3): php-fpm-7.4.26-1.fc35.x86_64.rpm 178 kB/s | 1.7 MB 00:09 (3/3): php-mysqlnd-7.4.26-1.fc35.x86_64.rpm 150 kB/s | 195 kB 00:01 ... Running transaction check Transaction check succeeded. Running transaction test Transaction test succeeded. Running transaction Preparing : 1/1 Installing : php-7.4.26-1.fc35.x86_64 1/3 Installing : php-fpm-7.4.26-1.fc35.x86_64 2/3 Installing : php-mysqlnd-7.4.26-1.fc35.x86_64 3/3 Running scriptlet: php-mysqlnd-7.4.26-1.fc35.x86_64 3/3 Verifying : php-7.4.26-1.fc35.x86_64 1/3 Verifying : php-fpm-7.4.26-1.fc35.x86_64 2/3 Verifying : php-mysqlnd-7.4.26-1.fc35.x86_64 3/3 Installed products updated. ... Complete!
步骤 5:为 Lighttpd 配置 PHP
为了将 PHP 与 Lighttpd 集成,我们需要配置 FastCGI 进程管理器。使用文本编辑器打开 PHP-FPM 配置文件:
sudo nano /etc/php-fpm.d/www.conf
找到以 listen = 开头的行,并将其更改为以下内容:
listen = /var/run/php-fpm/php-fpm.sock
保存更改并退出文本编辑器。
步骤 6:启动并启用 PHP-FPM
通过执行以下命令启动 PHP-FPM 服务并启用它在系统启动时启动:
sudo systemctl start php-fpm sudo systemctl enable php-fpm
步骤 7:安装 MariaDB
MariaDB 是一个流行的开源关系数据库管理系统。使用以下命令安装它:
示例
sudo dnf install mariadb-server
输出
$ sudo dnf install mariadb-server Last metadata expiration check: 0:15:01 ago on <date> Dependencies resolved. ================================================================================ Package Architecture Version Repo Size ================================================================================ Installing: mariadb-server x86_64 10.6.5-1.fc35 updates 19 M Installing dependencies: mariadb-connector-c x86_64 3.1.14-1.fc35 updates 215 k mariadb-server-utils x86_64 10.6.5-1.fc35 updates 736 k perl-DBI x86_64 1.643-1.fc35 updates 803 k ... Transaction Summary ================================================================================ Install 5 Packages Total download size: 23 M Installed size: 116 M Is this ok [y/N]: y ... Downloading Packages: (1/5): mariadb-server-10.6.5-1.fc35.x86_64.rpm 485 kB/s | 19 MB 00:40 (2/5): mariadb-connector-c-3.1.14-1.fc35.x86_64.rpm 149 kB/s | 215 kB 00:01 ... Running transaction check Transaction check succeeded. Running transaction test Transaction test succeeded. Running transaction Preparing : 1/1 Installing : perl-DBI-1.643-1.fc35.x86_64 1/5 Running scriptlet: perl-DBI-1.643-1.fc35.x86_64 1/5 Installing : mariadb-connector-c-3.1.14-1.fc35.x86_64 2/5 Installing : mariadb-server-utils-10.6.5-1.fc35.x86_64 3/5 Installing : mariadb-server-10.6.5-1.fc35.x86_64 4/5 Running scriptlet: mariadb-server-10.6.5-1.fc35.x86_64 4/5 Running scriptlet: mariadb-server-10.6.5-1.fc35.x86_64 5/5 Verifying : mariadb-connector-c-3.1.14-1.fc35.x86_64 1/5 Verifying : mariadb-server-10.6.5-1.fc35.x86_64 2/5 Verifying : mariadb-server-utils-10.6.5-1.fc35.x86_64 3/5 Verifying : perl-DBI-1.643-1.fc35.x86_64 4/5 Installed products updated. ... Complete!
步骤 8:启动并启用 MariaDB
启动 MariaDB 服务并启用它在系统启动时运行。输入以下命令:
sudo systemctl start mariadb sudo systemctl enable mariadb
步骤 9:保护 MariaDB 安装
通过运行以下命令来保护您的 MariaDB 安装至关重要:
示例
sudo mysql_secure_installation
输出
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY! In order to log into MariaDB to secure it, we'll need the current password for the root user. If you've just installed MariaDB, and you haven't set the root password yet, the password will be blank, so you should just press enter here. Enter current password for root (enter for none): OK, successfully used password, moving on... Setting the root password ensures that nobody can log into the MariaDB root user without the proper authorization. Set root password? [Y/n] Y New password: Re-enter new password: Password updated successfully! Reloading privilege tables.. ... Success! By default, a MariaDB installation has an anonymous user, allowing anyone to log into MariaDB 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? [Y/n] 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? [Y/n] Y ... Success! By default, MariaDB 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? [Y/n] 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? [Y/n] Y ... Success! Cleaning up... All done! If you've completed all of the above steps, your MariaDB installation should now be secure. Thanks for using MariaDB!
步骤 10:测试设置
要验证 Lighttpd、PHP 和 MariaDB 是否正常工作,请创建一个包含一些代码的 PHP 文件,并通过 Web 浏览器访问它。
在默认 Web 目录中创建一个名为 info.php 的新文件:
sudo nano /var/www/html/info.php
向文件中添加以下几行:
<?php phpinfo(); ?>
保存文件并退出文本编辑器。
步骤 11:访问 PHP 信息页面
打开 Web 浏览器,输入服务器的 IP 地址或主机名,后跟 /info.php。例如,http://your_server_ip/info.php。您应该会看到一个页面,显示有关您的 PHP 安装的详细信息。
结论
恭喜!您已成功在 RockyAlmaLinux 上安装了 Lighttpd、PHP 和 MariaDB。此设置为您托管网站和 Web 应用程序提供了强大的基础。随意探索各种配置选项,并根据您的具体需求调整设置。祝您 Web 服务愉快!