网站搜索

如何在 CentOS/RHEL 8/7 上使用 PHP 和 MariaDB 安装 Lighttpd


Lighttpd 是一款开源、安全、快速、灵活且更优化的 Web 服务器,专为速度关键型环境而设计,与其他 Web 服务器相比,内存利用率更低。

它可以在一台服务器上并行处理多达10,000个连接,并具有有效的 CPU 负载管理,并配备高级功能集,例如 FastCGI、SCGI、Auth、输出压缩、URL 重写等。

Lighttpd 对于每台 Linux 服务器来说都是一个出色的解决方案,因为它的高速 io 基础设施使我们能够使用相同的硬件实现比其他替代 Web 服务器好几倍的性能。

在本教程中,我们将解释如何在 PHPPHP-FPM、MariaDB 上安装 Lighttpd主机名howtoing.com和IP地址192.168.0.103的strong>CentOS/RHEL 8/7发行版。

第 1 步:安装 Lighttpd Web 服务器

1. 要安装Lighttpd,首先,您需要使用以下 yum 命令更新系统软件包和可用存储库。

yum -y update

2. 接下来,您需要在系统上启用 EPEL 存储库并使用以下命令更新软件包。

yum -y install epel-release
yum -y update

3.启用 EPEL 存储库后,您现在可以通过运行以下命令来安装 Lighttpd

yum install lighttpd

4.安装Lighttpd软件包后,您可以启动该服务并使其在启动时自动启动,并确保使用以下命令验证状态。

systemctl start lighttpd
systemctl enable lighttpd
systemctl status lighttpd

5. 现在使用以下命令验证系统上安装的 Lighttpd 版本。

lighttpd -v

lighttpd/1.4.55 (ssl) - a light and fast webserver

6. 现在您需要在防火墙上允许 HTTPHTTPS 流量。

firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload

7. 现在打开浏览器并指向以下 URL 以查看您的 Lighttpd 网络服务器是否正常工作。

http://Your-Domain.com
OR
http://Your-IP-addr

Lighttpd的默认文档根目录是/var/www/lighttpd/,主要配置文件位于/etc/lighttpd/lighttpd.conf下。

步骤 2:在 CentOS 7 中安装 MariaDB 作为 MySQL

8. 接下来,使用以下命令安装对 LighttpdMySQL 支持。

yum -y install mariadb mariadb-server

9. 安装完成后,使用以下命令启动、启用 MariaDB 并验证其状态。

systemctl start mariadb.service
systemctl enable mariadb.service
systemctl status mariadb.service

10.最后,您需要通过发出以下命令来保护 MariaDB 安装的安全。

mysql_secure_installation

系统将提示您一些有关 MariaDB 安装以及您希望如何保护它的不同问题。您可以更改数据库root用户密码、禁用测试数据库、禁用匿名用户、禁用root远程登录。

样本输出
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): Enter 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!

11. 尝试连接到 MySQL 服务器并在终端上使用以下命令查看数据库服务器上的现有数据库。

mysql -u root -p
样本输出
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

MariaDB [(none)]>

步骤 3:在 CentOS 7 上使用 FastCGI 安装 PHP 和 PHP-FPM

12.要启用PHP-FPMFastCGI支持,首先,您需要安装PHP以及必要的扩展使用以下命令。

yum -y install php php-mysqlnd php-pdo php-gd php-mbstring

13.安装PHP后,立即启用PHP-FPMFastCGILighttpd的支持>,为此您还需要安装这些软件包。

yum -y install php-fpm lighttpd-fastcgi

14. 现在打开一个名为 /etc/php-fpm.d/www.conf 的文件。

vi /etc/php-fpm.d/www.conf

将用户和组设置为 Lighttpd

; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
;       will be used.
; RPM: apache Choosed to be able to access some dir as httpd
user = lighttpd
; RPM: Keep a group allowed to write in log dir.
group = lighttpd

另外,PHP-FPM默认使用/var/run/php/php7.0-fpm.sock套接字,您需要制作PHP-FPM 使用 TCP 连接。使 listen 行如下所示:

;listen = /var/run/php/php7.0-fpm.sock
listen = 127.0.0.1:9000 

15. 现在启动 PHP-FPM 服务,并确保使用以下命令使其能够在启动时自动启动。

systemctl start php-fpm.service
systemctl enable php-fpm.service

步骤 4:在 Lighttpd 中使用 FastCGI 启用 PHP 和 PHP-FPM

16.这里,我们需要修改三个文件/etc/php.ini/etc/lighttpd/modules.conf /etc/lighttpd/conf.d/fastcgi.conf 启用 PHPLighttpd 的支持。

打开第一个文件/etc/php.ini

vi /etc/php.ini

取消注释以下行 line cgi.fix_pathinfo=1

; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI.  PHP's
; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok
; what PATH_INFO is.  For more information on PATH_INFO, see the cgi specs.  Setting
; this to 1 will cause PHP CGI to fix its paths to conform to the spec.  A setting
; of zero causes PHP to behave as before.  Default is 1.  You should fix your scripts
; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
; http://php.net/cgi.fix-pathinfo
cgi.fix_pathinfo=1

然后打开第二个文件/etc/lighttpd/modules.conf

vi /etc/lighttpd/modules.conf

取消注释以下行 include “conf.d/fastcgi.conf”

##
## FastCGI (mod_fastcgi)
##
include "conf.d/fastcgi.conf"

接下来,打开名为 /etc/lighttpd/conf.d/fastcgi.conf 的第三个文件。

vi /etc/lighttpd/conf.d/fastcgi.conf

现在在文件底部添加以下容器并保存。

fastcgi.server += ( ".php" =>
        ((
                "host" => "127.0.0.1",
                "port" => "9000",
                "broken-scriptfilename" => "enable"
        ))
)

重新启动 Lighttpd 服务以反映更改并启用 PHP 支持。

systemctl restart lighttpd

步骤 5:在 Lighttpd 中使用 FastCGI 支持测试 PHP 和 PHP-FPM

17.正如我们上面所说,Lighttpd的默认文档根目录是/var/www/lighttpd/。因此,我们将在该目录中创建一个 phpinfo.php 文件。

vi /var/www/lighttpd/info.php

将以下行添加到其中。这段小代码将显示有关 PHP 安装及其版本的信息。

<?php
phpinfo();
?>

18. 打开浏览器并导航至以下链接。

http://Your-Domain.com/info.php
OR
http://Your-IP-addr/info.php

您将看到PHPPHP-FPMMySQL的工作信息以及许多已启用的其他模块。