在本教程中,我们将向您展示如何在 AlmaLinux 9 上安装 LAMP 堆栈。对于那些不知道的人,LAMP 堆栈是一组开源软件,通常安装在一起以使服务器能够托管动态网站和网络应用程序。 它包括 Apache Web 服务器、MariaDB 数据库服务器和 PHP,这是一种用于开发动态网页的后端脚本语言。
本文假设您至少具备 Linux 的基本知识,知道如何使用 shell,最重要的是,您将网站托管在自己的 VPS 上。 安装非常简单,假设您在 root 帐户下运行,如果不是,您可能需要添加 ‘sudo
‘ 到命令以获取 root 权限。 我将向您展示在 AlmaLinux 9 上逐步安装 LAMP 堆栈。对于 CentOS 和 Rocky Linux,您可以按照相同的说明进行操作。
先决条件
- 运行以下操作系统之一的服务器:AlmaLinux 9。
- 建议您使用全新的操作系统安装来防止任何潜在问题。
- 对服务器的 SSH 访问(或者如果您在桌面上,则只需打开终端)。
- 一个
non-root sudo user
或访问root user
. 我们建议充当non-root sudo user
,但是,如果您在充当 root 时不小心,可能会损害您的系统。
在 AlmaLinux 9 上安装 LAMP 堆栈
第 1 步。首先,让我们首先确保您的系统是最新的。
sudo dnf clean all sudo dnf install epel-release sudo dnf update
步骤 2. 安装 Apache AlmaLinux 9 上的网络服务器。
默认, Apache 在 AlmaLinux 9 基础存储库中不可用。 现在我们安装最新版本的 Apache 使用 dnf
命令:
sudo dnf install httpd httpd-tools
你可以启动 httpd
服务并通过输入以下命令将其配置为在启动时运行:
sudo systemctl start httpd sudo systemctl enable httpd sudo systemctl status httpd
要使您的页面可供公众使用,您必须使用以下命令编辑防火墙规则以允许 Web 服务器上的 HTTP 和 HTTPS 请求:
sudo firewall-cmd --permanent --zone=public --add-service=http sudo firewall-cmd --permanent --zone=public --add-service=https sudo firewall-cmd --reload
您可以通过导航到以下位置进行测试以确保一切正常:
成功安装后,您可以通过访问服务器的 IP 地址来验证 Web 服务器是否正在运行并可访问:
https://your-IP-address
步骤 3. 在 AlmaLinux 9 上安装 MariaDB。
默认情况下,MariaDB 在 AlmaLinux 9 基础存储库中可用。 只需使用以下命令安装 MariaDB 包 dnf
命令:
sudo dnf install mariadb-server mariadb
安装完成后,启动数据库服务器的服务,然后启用,这样系统重启后它可以自动启动:
sudo systemctl restart mariadb sudo systemctl status mariadb sudo systemctl enable mariadb
默认情况下,MariaDB 未加固。 您可以使用 mysql_secure_installation
脚本。 您应该仔细阅读以下每个步骤,这些步骤将设置 root 密码、删除匿名用户、禁止远程 root 登录、删除测试数据库和访问安全 MariaDB:
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 haven't set the root password yet, you should just press enter here. Enter current password for root (enter for none): OK, successfully used password, moving on... Setting the root password or using the unix_socket ensures that nobody can log into the MariaDB root user without the proper authorisation. You already have your root account protected, so you can safely answer 'n'. Switch to unix_socket authentication [Y/n] Y <---- Type Y then press the ENTER KEY. Enabled successfully! Reloading privilege tables.. ... Success! You already have your root account protected, so you can safely answer 'n'. Change the root password? [Y/n] Y <---- Type Y then press the ENTER KEY. 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 <---- Type Y then press the ENTER KEY. ... 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 <---- Type Y then press the ENTER KEY. ... 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 <---- Type Y then press the ENTER KEY. - 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 <---- Type Y then press the ENTER KEY. ... 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!
要登录 MariaDB,请使用以下命令(请注意,它与登录 MariaDB 数据库的命令相同):
mysql -u root -p
步骤 4. 在 AlmaLinux 9 上安装 PHP 8.1。
PHP 是一种流行的脚本语言,可为数百万个网站和应用程序的动态内容提供支持。 现在我们运行以下命令来安装 PHP:
sudo dnf install php php-curl php-bcmath php-gd php-soap php-zip php-curl php-mbstring php-mysqlnd php-gd php-xml php-intl php-zip
检查并验证安装的版本:
php -v
输出:
[[email protected] ~]# php -v PHP 8.1.6 (cli) (built: May 20 2022 16:36:33) ( NTS gcc x86_64 ) Copyright (c) The PHP Group Zend Engine v4.0.16, Copyright (c) Zend Technologies with Zend OPcache v8.0.16 Copyright (c), by Zend Technologies
为了确认我们的 Web 服务器可以访问并且 PHP 正在按预期工作,我们可以创建一个名为 info.php
在 – 的里面 /var/www/html
目录:
sudo nano /var/www/html/info.php
添加以下文件:
<?php phpinfo (); ?>
Save 和 close 该文件,然后重新启动您的网络服务器,以便 Apache 知道它也会为 PHP 请求提供服务:
sudo systemctl restart httpd
现在我们访问 https://localhost/info.php
或者 https://your-ip-address/info.php
.
恭喜! 您已成功安装 LAMP。 感谢您使用本教程安装 LAMP (Apache, 玛丽亚数据库, 和 PHP) 堆栈在您的 AlmaLinux 9 系统上。 如需更多帮助或有用信息,我们建议您查看官方 LAMP 网站。