在 Ubuntu 16.04 LTS 上使用 PHP 7 (PHP-FPM) 和 MySQL 5.7 安装 Lighttpd
本教程适用于这些操作系统版本
- Ubuntu 10.04 (Lucid Lynx)
在此页
- 1 条初步说明
- 2 安装 MySQL 5.7
- 3 安装Lighttpd
- 4 安装 PHP 7.0
- 5 配置 Lighttpd 和 PHP 7.0
- 6 测试 PHP 7.0/获取有关 PHP 安装的详细信息
- 7 在 PHP 中获得 MySQL 支持
- 8 phpMyAdmin
- 9 使 PHP-FPM 使用 TCP 连接(可选)
- 10 个链接
Lighttpd 是一种安全、快速、符合标准的 Web 服务器,专为速度关键型环境而设计。本教程展示了如何在支持 PHP 7(通过 PHP-FPM)和 MySQL 5.7 的 Ubuntu 16.04 服务器上安装 Lighttpd。 PHP-FPM(FastCGI 进程管理器)是一种替代的 PHP FastCGI 实现,具有一些对任何规模的站点都非常有用的附加功能,尤其是繁忙的站点。我在本教程中使用 PHP-FPM 而不是 Lighttpds spawn-fcgi。
1 初步说明
在本教程中,我使用 IP 地址为 192.168.1.100 的主机名 server1.example.com。这些设置可能因您而异,因此您必须在适当的地方替换它们。
我以 root 权限运行本教程中的所有步骤,因此请确保您以 root 身份登录:
sudo -s
2 安装MySQL 5.7
首先,我们像这样安装MySQL:
apt-get -y install mysql-server mysql-client
系统将要求您提供 MySQL root 用户的密码 - 此密码对用户 [email 有效,因此我们以后不必手动指定 MySQL root 密码:
MySQL \root\ 用户的新密码:<-- yourrootsqlpassword
MySQL \root\ 用户的重复密码:<-- yourrootsqlpassword

安装程序已经设置了 MySQL root 密码,但是为了安全的 MySQL 安装,还有一些设置需要更改。这可以通过 mysql_secure_installation 命令来完成。
mysql_secure_installation
该命令是交互式的:
:~# mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root: <-- Enter the MySQL root password
VALIDATE PASSWORD PLUGIN 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 plugin?
Press y|Y for Yes, any other key for No: <-- Press y if you want this function or press Enter otherwise.
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) : <-- Press enter
... skipping.
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) : <-- y
Success.
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) : <-- 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? (Press y|Y for Yes, any other key for No) : <-- y
Success.
All done!
3 安装Lighttpd
Lighttpd 可作为 Ubuntu 软件包使用。因此,我们可以直接使用 Ubuntu Xenial Xerus 包存储库中的 apt 安装它:
apt-get -y install lighttpd
现在将您的浏览器指向 http://192.168.1.100/,您应该会看到 Lighttpd 占位符页面:

Lighttpd默认文档根目录在Ubuntu上是/var/www/html,配置文件是/etc/lighttpd/lighttpd.conf。其他配置存储在 /etc/lighttpd/conf-available 目录中的文件中 - 这些配置可以使用 lighttpd-enable-mod 命令启用,该命令创建从 /etc/lighttpd/conf-enabled 目录到适当配置的符号链接/etc/lighttpd/conf-available 中的文件。您可以使用 lighttpd-disable-mod 命令禁用配置。
4 安装 PHP 7.0
我们可以通过像这样安装的 PHP-FPM 让 PHP 在 Lighttpd 中工作:
apt-get -y install php7.0-fpm php7.0
PHP-FPM 是一个守护进程(带有初始化脚本 php5-fpm),它在套接字 /var/run/php/php7.0-fpm.sock 上运行 FastCGI 服务器。
5 配置Lighttpd和PHP 7.0
要在 Lighttpd 中启用 PHP,我们必须修改 /etc/php/7.0/fpm/php.ini 并取消注释 cgi.fix_pathinfo=1 行:
nano /etc/php/7.0/fpm/php.ini
[...]
; 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
[...]
PHP 的 Lighttpd 配置文件 /etc/lighttpd/conf-available/15-fastcgi-php.conf 适合与 spawn-fcgi 一起使用,但是,我们想使用 PHP-FPM,因此我们创建文件的备份(命名为15-fastcgi-php.conf.bak),修改15-fastcgi-php.conf如下:
cd /etc/lighttpd/conf-available/
cp 15-fastcgi-php.conf 15-fastcgi-php.conf.bak
nano 15-fastcgi-php.conf
# /usr/share/doc/lighttpd-doc/fastcgi.txt.gz
# http://redmine.lighttpd.net/projects/lighttpd/wiki/Docs:ConfigurationOptions#mod_fastcgi-fastcgi
## Start an FastCGI server for php (needs the php7.0-cgi package)
fastcgi.server += ( ".php" =>
((
"socket" => "/var/run/php/php7.0-fpm.sock",
"broken-scriptfilename" => "enable"
))
)
要启用 fastcgi 配置,请运行以下命令:
lighttpd-enable-mod fastcgi
lighttpd-enable-mod fastcgi-php
这将创建指向 /etc/lighttpd/conf-available/10-fastcgi.conf 和 /etc/lighttpd/conf-enabled/15-fastcgi-php 的符号链接 /etc/lighttpd/conf-enabled/10-fastcgi.conf .conf 指向 /etc/lighttpd/conf-available/15-fastcgi-php.conf:
ls -l /etc/lighttpd/conf-enabled
:/etc/lighttpd/conf-available#
然后我们重新加载Lighttpd:
service lighttpd force-reload
注意:如果您收到区域设置错误,则可以使用以下方法删除错误
apt-get -y install language-pack-en-base
dpkg-reconfigure locales
6 测试 PHP 7.0/获取有关 PHP 安装的详细信息
默认网站的文档根目录是/var/www/html。我们现在将在该目录中创建一个小的 PHP 文件 (info.php) 并在浏览器中调用它。该文件将显示有关我们的 PHP 安装的许多有用的详细信息,例如已安装的 PHP 版本。
nano /var/www/html/info.php
<?php
phpinfo();
?>
现在我们在浏览器中调用该文件(例如 http://192.168.1.100/info.php):

如您所见,PHP 7.0 正在运行,并且它通过 FPM/FastCGI 运行,如服务器 API 行所示。如果进一步向下滚动,您将看到所有已在 PHP5 中启用的模块。那里没有列出 MySQL,这意味着我们还没有在 PHP 中支持 MySQL。
7 在 PHP 中获得 MySQL 支持
要在 PHP 中获得 MySQL 支持,我们可以安装 php7.0-mysql 包。最好安装一些其他 PHP 模块,因为您的应用程序可能需要它们。您可以像这样搜索可用的 PHP 模块:
apt-cache search php7.0
选择你需要的并像这样安装它们:
apt-get -y install php7.0-mysql php7.0-curl php7.0-gd php7.0-intl php-pear php-imagick php7.0-imap php7.0-mcrypt php-memcache php7.0-pspell php7.0-recode php7.0-sqlite3 php7.0-tidy php7.0-xmlrpc php7.0-xsl php7.0-mbstring php-gettext
APCu 是 PHP 7 附带的 PHP Opcache 模块的扩展,它为支持 APC 缓存的软件(例如 WordPress 缓存插件)添加了一些兼容性功能。
APCu 可以按如下方式安装:
apt-get -y install php-apcu
现在重新加载 PHP-FPM:
service php7.0-fpm reload
现在在浏览器中重新加载 http://192.168.1.100/info.php 并再次向下滚动到模块部分。你现在应该在那里找到很多新模块,包括 mysqli 和 mysqlnd 模块:

8 php我的管理员
phpMyAdmin 是一个 Web 界面,您可以通过它管理 MySQL 数据库。安装它是个好主意:
apt-get -y install phpmyadmin
您将看到以下问题:
Web server to reconfigure automatically: <-- lighttpd
Configure database for phpmyadmin with dbconfig-common? <-- Yes
MySQL application password for phpmyadmin: <-- Press Enter
如果您收到以下错误:
Run /etc/init.d/lighttpd force-reload to enable changes
dpkg: error processing package phpmyadmin (--configure):
subprocess installed post-installation script returned error exit status 2
E: Sub-process /usr/bin/dpkg returned an error code (1)
然后运行这些命令:
/etc/init.d/lighttpd force-reload
apt-get -y install phpmyadmin
之后就可以在http://192.168.1.100/phpmyadmin/下访问phpMyAdmin了:

9 让 PHP-FPM 使用 TCP 连接(可选)
默认情况下,PHP-FPM 监听套接字 /var/run/php/php7.0-fpm.sock。也可以使 PHP-FPM 使用 TCP 连接。为此,打开 /etc/php/7.0/fpm/pool.d/www.conf...
nano /etc/php/7.0/fpm/pool.d/www.conf
...并使 listen 行如下所示:
[...]
;listen = /var/run/php/php7.0-fpm.sock
listen = 127.0.0.1:9000
[...]
这将使 PHP-FPM 侦听 IP 127.0.0.1(本地主机)上的端口 9000。确保您使用的端口未在您的系统上使用。
然后重新加载 PHP-FPM:
service php7.0-fpm reload
接下来打开 Lighttpd PHP 配置文件 /etc/lighttpd/conf-available/15-fastcgi-php.conf 并将套接字行替换为主机和端口行:
nano /etc/lighttpd/conf-available/15-fastcgi-php.conf
# /usr/share/doc/lighttpd-doc/fastcgi.txt.gz
# http://redmine.lighttpd.net/projects/lighttpd/wiki/Docs:ConfigurationOptions#mod_fastcgi-fastcgi
## Start an FastCGI server for php (needs the php7.0-cgi package)
fastcgi.server += ( ".php" =>
((
"host" => "127.0.0.1",
"port" => "9000",
"broken-scriptfilename" => "enable"
))
)
最后重新加载Lighttpd:
service lighttpd force-reload
10 个链接
- Lighttpd:http://www.lighttpd.net/
- PHP:http://www.php.net/
- PHP-FPM:http://php-fpm.org/
- MySQL:http://www.mysql.com/
- Ubuntu:http://www.ubuntu.com/
- phpMyAdmin:http://www.phpmyadmin.net/