> Ubuntu >

Ubuntu 16.04上安装Lighttpd+PHP 7(PHP-FPM)+MySQL 5.7

Lighttpd 是一款安全、快速、符合标准的Web服务器。本教程介绍了如何通过PHP7(PHP-FPM)和MySQL5.7在Ubuntu16.04服务器上安装Lighttpd服务。 PHP-FPM(FastCGI进程管理器)是一种替代PHP的FastCGI适用于各种规模的网站,尤其是繁忙的网站有用的一些额外的功能。

1、安装初步说明

在本教程中,使用的IP地址为192.168.1.100,主机名server1.example.com。这些设置可能与你的服务器不同,所以你不得不在适当情况下更换他们。
运行的所有步骤在本教程中使用的root权限,所以一定要确保你以root身份登录:

sudo -s

2、安装MySQL 5.7

安装命令:

apt-get -y install mysql-server mysql-client

安装过程中要求设置 root 账户密码,如图:

mysql_password

MySQL 安装更改设置命令:

mysql_secure_installation

如下进行设置:

root@server1:~# 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:

安装命令:

apt-get -y install lighttpd

浏览器输入主机地址,看看安装是否成功:

ubuntu_lighttpd_default_page

另外说明一下:

Lighttpd 默认根目录: /var/www/html
配置文件:/etc/lighttpd/lighttpd.conf、
其他配置:/etc/lighttpd/conf-available
/etc/lighttpd/conf-enabled
/etc/lighttpd/conf-available

4、安装PHP 7.0
安装命令:

apt-get -y install php7.0-fpm php7.0

5、配置 Lighttpd 与 PHP 7.0

打开 php.ini:

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
[...]

httpd配置文件,我们需要将 /etc/lighttpd/conf-available/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-enabled/10-fastcgi.conf 指向/etc/lighttpd/conf-available/10-fastcgi.conf
/etc/lighttpd/conf-enabled/15-fastcgi-php.conf 指向 /etc/lighttpd/conf-available/15-fastcgi-php.conf

ls -l /etc/lighttpd/conf-enabled

执行情况:

root@server1:/etc/lighttpd/conf-available# ls -l /etc/lighttpd/conf-enabled
total 0
lrwxrwxrwx 1 root root 33 Apr 27 11:26 10-fastcgi.conf -> ../conf-available/10-fastcgi.conf
lrwxrwxrwx 1 root root 37 Apr 27 11:26 15-fastcgi-php.conf -> ../conf-available/15-fastcgi-php.conf
lrwxrwxrwx 1 root root 42 Apr 21 11:10 90-javascript-alias.conf -> ../conf-available/90-javascript-alias.conf
root@server1:/etc/lighttpd/conf-available#

重新加载Lighttpd:

service lighttpd force-reload

注意:如果提示你的语言环境错误,那么你可以下面的命令删除错误:

apt-get -y install language-pack-en-base
dpkg-reconfigure locales

6、建立一个探针文件看看安装情况:

nano /var/www/html/info.php

内容:

<?php
phpinfo();
?>

好了,浏览器访问这个探针文件:http://192.168.1.100/info.php

7、PHP 获得 MySQL 支持

产看模块:

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

apt-get -y install php-apcu

重新加载php7.0-fpm:

service php7.0-fpm reload

8、安装phpmyadmin

命令:

apt-get -y install phpmyadmin

安装过程中询问设置:

Web server to reconfigure automatically: Configure database for phpmyadmin with dbconfig-common? MySQL application password for phpmyadmin:

如果出现错误:
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

然后访问 phpMyAdmin 浏览器输入:http://192.168.1.100/phpmyadmin/

9、使PHP-FPM使用TCP连接(可选)

编辑:
ano /etc/php/7.0/fpm/pool.d/www.conf

内容如下:

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

重新加载PHP-FPM:

service php7.0-fpm reload

再配置一个文件:

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



(责任编辑:IT)