LAMP使一个在托管静态和动态web程序的开发环境中使用的流行栈。它是Linux, Apache, MySQL(或MariaDB)和PHP的缩写。它由Apache web服务程序,MYSQL或MariaDB数据程序和PHP组成。我们在这里经历在Rocky Linux 8上安装LAMP。
前提条件
在你开始前,确保你有一下去:
- 一个Rocky Linux实例
- 一个sudo配置的用户
第一步:在Rocky Linux上安装Apache
我们将要开始安装的第一个组件是Apache web服务程序。由httpd软件包提供了这个服务程序。在安装时,httpd守护进程在后台运行,等待来自客户端设备进入的HTTP请求。
要按照Apache,启动你的终端并且运行这个命令。
[root@rockygu ~]# dnf install httpd -y
这一起安装了httpd包和其它依赖包。
之后,使得这个web服务器在系统启动时启动。
[root@rockygu ~]# systemctl enable httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
启动这个服务程序并且验证Apache在Rocky Linux 8上运行了:
[root@rockygu ~]# systemctl start httpd
[root@rockygu ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor prese>
Active: active (running) since Tue 2022-07-26 22:46:49 CST; 1 day 23h ago
Docs: man:httpd.service(8)
Main PID: 589823 (httpd)
Status: "Total requests: 197; Idle/Busy workers 100/0;Requests/sec: 0.00115;>
Tasks: 278 (limit: 11402)
Memory: 33.1M
CGroup: /system.slice/httpd.service
├─589823 /usr/sbin/httpd -DFOREGROUND
├─589824 /usr/sbin/httpd -DFOREGROUND
├─589825 /usr/sbin/httpd -DFOREGROUND
├─589826 /usr/sbin/httpd -DFOREGROUND
├─589827 /usr/sbin/httpd -DFOREGROUND
└─590053 /usr/sbin/httpd -DFOREGROUND
验证Apache有效并且运行的另一种方法是通过浏览服务器的IP地址或者完全合格域名(FQDN)。
http://server-IP
OR
http://domain.com
这应该给出Apache HTTP测试页:
如果当尝试访问这个页面时,在你浏览器上得到一个错误,则可能防火墙阻塞了HTTP流量。运行以下命令来运行HTTP流量并且重载这个防火墙。
[root@rockygu ~]# firewall-cmd --add-service=http --permanent
[root@rockygu ~]# firewall-cmd --add-service=http
[root@rockygu ~]# firewall-cmd --reload
第二步:在Rocky Linux行安装MySQL
接着我们需要安装一个数据库服务软软件MySQL。
要按照MySQL,运行以下命令:
[root@rockygu blctrl]# dnf install mysql-server mysql -y
当按照完成时,使MySQL能在系统重新启动时自启动。
[root@rockygu blctrl]# systemctl enable --now mysqld
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
最终,启动MySQL服务。
[root@rockygu blctrl]# systemctl start mysqld
用以下命令验证MySQL运行了。
[root@rockygu blctrl]# systemctl status mysqld
● mysqld.service - MySQL 8.0 database server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor pres>
Active: active (running) since Sat 2022-07-30 16:46:13 CST; 1min 43s ago
Process: 622362 ExecStartPost=/usr/libexec/mysql-check-upgrade (code=exited, >
Process: 622238 ExecStartPre=/usr/libexec/mysql-prepare-db-dir mysqld.service>
Process: 622214 ExecStartPre=/usr/libexec/mysql-check-socket (code=exited, st>
Main PID: 622318 (mysqld)
Status: "Server is operational"
Tasks: 37 (limit: 11402)
Memory: 436.7M
CGroup: /system.slice/mysqld.service
└─622318 /usr/libexec/mysqld --basedir=/usr
MySQL的默认设置是脆弱并且造成一些可以被黑客利用入侵数据库服务器的漏洞。因为如此,我们需要采取补充步骤来加固这个数据库服务器。要实现这个目标,运行以下显示的脚本。
为余下配置输入'Y'。这将有效地净化或移除任何义名用户,防止远程root登录以及移除在一个生产环境中不需要地Test数据库。
[root@rockygu blctrl]# mysql_secure_installation
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: 8
Invalid option provided.
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
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) : N
... skipping.
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) : N
... skipping.
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!
数据库服务器现在完全配置并且安全了。
第三步:在Rocky Linux上按照PHP
最后,要安装地最后组件是PHP,PHP是一个在动态web页面开发中使用的脚本语言。Rocky Linux AppStream提供了多个PHP版本。要检查可用版本,运行命令:
[root@rockygu blctrl]# dnf module list php
Last metadata expiration check: 2:05:58 ago on Sat 30 Jul 2022 02:57:37 PM CST.
Rocky Linux 8 - AppStream
Name Stream Profiles Summary
php 7.2 [d] common [d], devel, minimal PHP scripting language
php 7.3 common [d], devel, minimal PHP scripting language
php 7.4 common [d], devel, minimal PHP scripting language
php 8.0 common [d], devel, minimal PHP scripting language
Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled
这提供了一个PHP模块和Streams列表。默认PHP stream是PHP 7.2。要从仓库安装最新模块Stream,重置PHP stream。
[root@rockygu blctrl]# dnf module reset php
Last metadata expiration check: 2:09:38 ago on Sat 30 Jul 2022 02:57:37 PM CST.
Dependencies resolved.
Nothing to do.
Complete!
[root@rockygu blctrl]#
接着使能首选的PHP stream。例如,要使能PHP 7.4,执行:
[root@rockygu blctrl]# dnf module install php:7.4
你可以安装补充的PHP扩展。我们现在安装php-curl和php-zip扩展。
[root@rockygu blctrl]# dnf install php-curl php-zip -y
安装结束后,确认安装的PHP版本。
[root@rockygu blctrl]# php -v
PHP 7.4.19 (cli) (built: May 4 2021 11:06:37) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
测试安装的PHP版本的另一种方法是在/var/www/html/路径创建一个测试PHP文件。
[root@rockygu blctrl]# cat /var/www/html/info.php
<?
phpinfo();
?>
重启服web服务器。
[root@rockygu blctrl]# systemctl restart httpd
接着用你的浏览器并且输入以下URL:
http://server-ip/info.php
一个显示PHP版本以及诸如启动的PHP扩展细节的其它参数的页面将被显示。
现在删除以上测试的PHP文件。
[root@rockygu blctrl]# rm -rf /var/www/html/info.php