如何在 CentOS 7 上使用 PHP-FPM 和 MariaDB 安装 Lighttpd
本教程适用于这些操作系统版本
- 中央操作系统 5.6
在此页
- 1 条初步说明
- 2 安装 MariaDB 作为 MySQL 的替代品
- 3 安装Lighttpd
- 4 安装 PHP
Lighttpd 是一种安全、快速、符合标准的 Web 服务器,专为速度关键型环境而设计。本教程展示了如何在支持 PHP(通过 PHP-FPM)和 MySQL 支持的 Centos 7 服务器上安装 Lighttpd。 PHP-FPM(FastCGI 进程管理器)是一种替代的 PHP FastCGI 实现,具有一些对任何规模的站点都非常有用的附加功能,尤其是繁忙的站点。我在本教程中使用 PHP-FPM 而不是 Lighttpds spawn-fcgi。
1 初步说明
在本教程中,我使用 IP 地址为 192.168.1.100 的主机名 server1.example.com。这些设置可能因您而异,因此您必须在适当的地方替换它们。
2 安装 MariaDB 作为 MySQL 的替代品
首先,我们像这样安装MySQL:
yum -y install mariadb mariadb-server
然后我们为 MySQL 创建系统启动链接(以便 MySQL 在系统启动时自动启动)并启动 MySQL 服务器:
systemctl enable mariadb.service
systemctl start mariadb.service
为 MarisDB root 帐户设置密码:
mysql_secure_installation
[ ~]# mysql_secure_installation
/usr/bin/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): <-- press enter
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] <-- y
New password: <-- enter new password
Re-enter new password: <-- 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!
3 安装Lighttpd
因为 Lighttpd 和 PHP-FPM 无法从官方 CentOS 存储库获得,我们需要启用 EPEL 存储库:
yum -y install epel-release
导入 EPEL GPG 密钥:
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
然后运行:
yum update
之后,我们可以像这样安装 Lighttpd:
yum -y install lighttpd
然后我们为 Lighttpd 创建系统启动链接(以便 Lighttpd 在系统启动时自动启动)并启动它:
systemctl enable lighttpd.service
systemctl start lighttpd.service
如果 Lighttpd 无法启动并显示以下错误消息...
(network.c.203) socket failed: Address family not supported by protocol
... 打开 /etc/lighttpd/lighttpd.conf...
nano /etc/lighttpd/lighttpd.conf
...并将 server.use-ipv6 从启用更改为禁用:
[...]
##
## Use IPv6?
##
server.use-ipv6 = "disable"
[...]
然后尝试再次启动 Lighttpd - 它现在应该可以正常工作了:
systemctl start lighttpd.service
Lighttpd 的文档根目录位于 /var/www/htdocs(基本目录 /var/www 加上 htdocs 作为子目录,根据 lighttpd.conf 文件),但它会安装默认文件到 /var/www/lighttpd。那是不一致的,所以我们必须像这样重命名目录。
mv /var/www/lighttpd /var/www/htdocs
现在将您的浏览器指向 http://192.168.1.100,您应该会看到以下页面:

CentOS 7上Lighttpds默认文档根目录为/var/www/htdocs/,配置文件为/etc/lighttpd/lighttpd.conf。
4 安装PHP
我们可以通过像这样安装的 PHP-FPM 让 PHP 在 Lighttpd 中工作:
yum -y install php-fpm lighttpd-fastcgi
PHP-FPM 是一个守护进程,它在端口 9000 上运行 FastCGI 服务器。
打开 /etc/php-fpm.d/www.conf...
nano /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 创建系统启动链接并启动它:
systemctl enable php-fpm.service
systemctl start php-fpm.service