在 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 服務愉快!