~$ sudo apt-get install nginx
The following extra packages will be installed:
libgd2-noxpm libjpeg-turbo8 libjpeg8 nginx-common nginx-full
Suggested packages:
libgd-tools
The following NEW packages will be installed:
libgd2-noxpm libjpeg-turbo8 libjpeg8 nginx nginx-common nginx-full
~$ dpkg -l | grep nginx
ii nginx 1.1.19-1ubuntu0.7 small, but very powerful and efficient web server and mail proxy
ii nginx-common 1.1.19-1ubuntu0.7 small, but very powerful and efficient web server (common files)
ii nginx-full 1.1.19-1ubuntu0.7 nginx web server with full set of core modules
~$ dpkg -L nginx
~$ dpkg -L nginx-common
~$ dpkg -L nginx-full
重点留意如下文件:
/lib/systemd/system/nginx.service
/usr/share/nginx
/usr/share/nginx/www
/usr/share/nginx/www/index.html
/usr/share/nginx/www/50x.html
/etc/init.d/nginx
/etc/ufw/applications.d/nginx
/etc/default/nginx
/etc/nginx
/etc/nginx/conf.d
/etc/nginx/nginx.conf
/etc/logrotate.d/nginx
/var/lib/nginx
/var/log/nginx
/usr/sbin/nginx
配置:/etc/nginx/nginx.conf
日志:/var/log/nginx
默认网页:/usr/share/nginx/www/index.html
Nginx 的使用非常简洁,通过 nginx -h 或 man nginx 即可查看。常用命令:
nginx -t: test configuration and exit
nginx -s stop: fast shutdown
nginx -s quit: graceful shutdown(一般关闭 nginx 用此选项)
nginx -s reload: reloading the configuration file
nginx -s reopen: reopening the log files
nginx -p prefix : set prefix path(prefix 并非是 working directory,仅仅是默认配置路径(prefix/conf/nginx.conf)、默认日志路径(prefix/logs/error.log, prefix/logs/access.log)、默认d 文件目录(prefix/logs/nginx.pid)等的前缀,因此,配置中的相对路径(logs/nginx.pid, ogs/access.log, etc.)不是相对 working directory, 而是相对 prefix,它自身的默认值在 configure 时指定)(https://www.nginx.com/resources/wiki/start/topics/tutorials/installoptions/)
Note: -s 是发送信号,那么怎么知道往哪个进程发送信号呢?关键在于 pid 文件,pid 文件存有进程的 pid, 执行 -s 操作就是向这个 pid 发送信号,但是 pid 文件的配置跟 prefix 和配置文件(pid 指令)都相关,因此执行 -s 操作的命令必须跟启动命令匹配,即:
如果启动命令是:
/opt/nginx/sbin/nginx -p /opt/nginx/ -c /opt/nginx/conf/nginx.conf
那么,-s 命令就是:
/opt/nginx/sbin/nginx -p /opt/nginx/ -c /opt/nginx/conf/nginx.conf -s quit
如果不匹配,则很可能找不到 pid 文件或找到错误的 pid 文件,可能杀死其他进程,产生意想不到的后果。笔者就是在一台机器上部署了两套 nginx, 一套生产用,一套测试用,生产采用默认配置,结果不小心结束测试用的 nginx 时没有指定这些参数,意外把生产环境的 nginx 结束了。
~$ sudo nginx
~$ ps aux | grep nginx
root 2851 0.0 0.0 62864 1212 ? Ss 22:45 0:00 nginx: master process nginx
www-data 2852 0.0 0.0 63220 1900 ? S 22:45 0:00 nginx: worker process
www-data 2853 0.0 0.0 63220 1900 ? S 22:45 0:00 nginx: worker process
www-data 2854 0.0 0.0 63220 1640 ? S 22:45 0:00 nginx: worker process
www-data 2855 0.0 0.0 63220 1896 ? S 22:45 0:00 nginx: worker process
hwx 2857 0.0 0.0 9392 940 pts/0 S+ 22:45 0:00 grep --color=auto nginx
~$ sudo nginx -s quit
~$ ps aux | grep nginx
hwx 2862 0.0 0.0 9392 940 pts/0 S+ 22:45 0:00 grep --color=auto nginx
启动 nginx 之后通过浏览器访问,将显示 /usr/share/nginx/www/index.html 中的内容。
http://nginx.org/en/docs/beginners_guide.html
http://wiki.nginx.org/CommandLine
1. 每一个配置项都是一个指令(directive),http://nginx.org/en/docs/dirindex.html
2. nginx 由多个模块组成,http://nginx.org/en/docs/
3. 每个指令属于一个上下文环境(context),如 http 指令属于最顶层的 context(main)(http://nginx.org/en/docs/http/ngx_http_core_module.html#http), 而 sever 属于 http 这个 context(http://nginx.org/en/docs/http/ngx_http_core_module.html#server); 每个指令属于特性的模块,如 http 指令属于 http core 模块(http://nginx.org/en/docs/http/ngx_http_core_module.html)
配置虚拟主机可以在 /etc/nginx/nginx.conf 的 http block 中直接添加 server block, 但是这样做不够优雅, /etc/nginx/nginx.conf http block 有这么一段:
http {
......
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
可以用文件包含的方式,一个文件配置一个主机,事实上,我们在安装完 nginx 之后访问 http://localhost/ 出现的页面正是 /etc/nginx/sites-enabled/default 呈现的。下面我们增加两个虚拟主机,一个是 example.com(www.example.com), 一个是 example-php.com(www.example-php.com). 配置文件分别为 /etc/nginx/conf.d/example.com.conf 和 /etc/nginx/conf.d/example-php.com.conf.
配置变更之后注意先测试配置文件是否正确:
~$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: [emerg] open() "/var/log/nginx/example.com/access.log" failed (2: No such file or directory)
nginx: configuration file /etc/nginx/nginx.conf test failed
~$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
配置的日志目录必须手动创建,所以要先创建 /var/log/nginx/example.com 目录,测试通过之后 reload 一下, example.com.conf 内容如下:
server {
listen 80;
root /var/www/example.com;
index index.html index.htm index.php;
# Make site accessible from http://example.com/, http://www.example.com/
server_name example.com www.example.com;
#location / {
# # First attempt to serve request as file, then
# # as directory, then fall back to index.html
# try_files $uri $uri/ /index.html;
# # Uncomment to enable naxsi on this location
# # include /etc/nginx/naxsi.rules
#}
access_log /var/log/nginx/example.com/access.log;
error_log /var/log/nginx/example.com/error.log;
# /var/www/example.com/404.html
error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/www;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
fastcgi_pass 127.0.0.1:9000;
#fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
我们配置了 3 个 server block(/etc/nginx/conf.d/example.com.conf, /etc/nginx/conf.d/example-php.com.conf, /etc/nginx/sites-enabled/default ), 但是通过 ip 访问总是访问的是 example-php.com, 原因是因为读取配置时 example-php.com 是第一个 server block, 如果不显式设置 default server 就以第一个作为 default server. default server 最多只能指定一个,同时将 example.com 和 example-php.com 都指定为 default server:
~$ sudo nginx -t
nginx: [emerg] a duplicate default server for 0.0.0.0:80 in /etc/nginx/conf.d/example.com.conf:2
nginx: configuration file /etc/nginx/nginx.conf test failed
指定 default server 的方法:
listen 80 default;
http://nginx.org/en/docs/http/ngx_http_core_module.html
http://wiki.nginx.org/PHPFcgiExample
http://wiki.nginx.org/FullExample
http://wiki.nginx.org/FullExample2
php 和 nginx 配合只能使用 FastCGI 模式,因此除了安装 php 以外,还需安装 PHP FPM (FastCGI Process Manager)。
原理见:http://ixdba.blog.51cto.com/2895551/806622(Nginx+FastCGI运行原理)
~$ sudo apt-get install php5
The following extra packages will be installed:
apache2-mpm-prefork apache2-utils apache2.2-bin apache2.2-common libapache2-mod-php5 libapr1
libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap php5-cli php5-common
Suggested packages:
apache2-doc apache2-suexec apache2-suexec-custom php-pear php5-suhosin
The following NEW packages will be installed:
apache2-mpm-prefork apache2-utils apache2.2-bin apache2.2-common libapache2-mod-php5 libapr1
libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap php5 php5-cli php5-common
0 upgraded, 12 newly installed, 0 to remove and 130 not upgraded.
~$ dpkg -l | grep php
ii libapache2-mod-php5 5.3.10-1ubuntu3.17 server-side, HTML-embedded scripting language (Apache 2 module)
ii php5 5.3.10-1ubuntu3.17 server-side, HTML-embedded scripting language (metapackage)
ii php5-cli 5.3.10-1ubuntu3.17 command-line interpreter for the php5 scripting language
ii php5-common 5.3.10-1ubuntu3.17 Common files for packages built from the php5 source
php5-cli 为 php 的命令行解释器。
~$ dpkg -L php5
~$ dpkg -L php5-common
~$ dpkg -L php5-cli
一些重要的文件:
/usr/lib/php5
/var/lib/php5
/etc/cron.d/php5
/etc/php5
/etc/php5/conf.d
/etc/php5/conf.d/pdo.ini
/etc/php5/cli
/etc/php5/cli/conf.d
~$ sudo apt-get install php5-fpm
Suggested packages:
php-pear
The following NEW packages will be installed:
php5-fpm
0 upgraded, 1 newly installed, 0 to remove and 130 not upgraded.
...
Creating config file /etc/php5/fpm/php.ini with new version
~$ dpkg -L php5-fpm
/usr/sbin/php5-fpm
/etc/init.d/php5-fpm
/etc/php5/fpm
/etc/php5/fpm/pool.d
/etc/php5/fpm/pool.d/www.conf
/etc/php5/fpm/php-fpm.conf
/etc/php5/fpm/conf.d
可执行文件:/usr/sbin/php5-fpm
~$ sudo service php5-fpm
Usage: /etc/init.d/php5-fpm {start|stop|status|restart|reload|force-reload}
~$ sudo service php5-fpm status
* php5-fpm is running
~$ ps aux | grep php-fpm
root 1784 0.0 0.1 59164 3756 ? Ss 17:42 0:00 php-fpm: master process (/etc/php5/fpm/php-fpm.conf)
www-data 1785 0.0 0.1 59164 3264 ? S 17:42 0:00 php-fpm: pool www
www-data 1786 0.0 0.1 59164 3264 ? S 17:42 0:00 php-fpm: pool www
www-data 1787 0.0 0.1 59164 3264 ? S 17:42 0:00 php-fpm: pool www
www-data 1788 0.0 0.1 59164 3264 ? S 17:42 0:00 php-fpm: pool www
hwx 2281 0.0 0.0 9392 944 pts/0 S+ 18:17 0:00 grep --color=auto php-fpm
php-fpm 工作进程的用户和用户组要和 nginx 的工作进程的用户和用户组配成一致(没有验证不一致是是否有问题,可能跟 Web Server 交互时 TCP 方式无所谓,但是 UNIX socket 方式会涉及到权限问题,见下文 /etc/php5/fpm/pool.d/www.conf 中的提示):
~$ ps -e -o cmd,pid,user,uid,group,gid | grep php-fpm
CMD PID USER UID GROUP GID
php-fpm: master process (/e 1784 root 0 root 0
php-fpm: pool www 1785 www-data 33 www-data 33
php-fpm: pool www 1786 www-data 33 www-data 33
php-fpm: pool www 1787 www-data 33 www-data 33
php-fpm: pool www 1788 www-data 33 www-data 33
grep --color=auto php-fpm 2314 hwx 1000 hwx 1000
~$ ps -e -o cmd,pid,user,uid,group,gid | grep nginx
CMD PID USER UID GROUP GID
nginx: master process /usr/ 1523 root 0 root 0
nginx: worker process 1524 www-data 33 www-data 33
nginx: worker process 1525 www-data 33 www-data 33
nginx: worker process 1526 www-data 33 www-data 33
nginx: worker process 1527 www-data 33 www-data 33
grep --color=auto nginx 2316 hwx 1000 hwx 1000
默认都是 www-data 用户和 www-data 用户组,无需修改,如果需要修改,请按如下方式修改:
/etc/nginx/nginx.conf
user www-data;
worker_processes 4;
pid /var/run/nginx.pid;
; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
; will be used.
user = www-data
group = www-data
; Set permissions for unix socket, if one is used. In Linux, read/write
; permissions must be set in order to allow connections from a web server. Many
; BSD-derived systems allow connections regardless of permissions.
; Default Values: user and group are set as the running user
; mode is set to 0660
;listen.owner = www-data
;listen.group = www-data
;listen.mode = 0660
nginx 和 php-fpm 的工作方式是:nginx 作为 proxy, 将请求转发给 php-fpm. 因此,nginx 监听客户端来的请求,nginx 绑定 tcp:80 端口;php-fpm 监听 nginx 来的请求,也需要绑定一个端口,这样 nginx 才能向 php-fpm 发起请求,如果双方都在同一台机器,那么 php-fpm 既可以绑定 tcp 端口,又可以直接使用 UNIX socket, 我们看一下默认情况:
~$ sudo netstat -ap
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 localhost:9000 *:* LISTEN 1784/php-fpm.conf)
tcp 0 0 *:http *:* LISTEN 1523/nginx
...
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags Type State I-Node PID/Program name Path
unix 3 [ ] STREAM CONNECTED 10187 1784/php-fpm.conf)
unix 3 [ ] STREAM CONNECTED 11489 1523/nginx
unix 3 [ ] STREAM CONNECTED 11491 1523/nginx
unix 3 [ ] STREAM CONNECTED 11492 1523/nginx
unix 3 [ ] STREAM CONNECTED 11490 1523/nginx
unix 3 [ ] STREAM CONNECTED 11488 1523/nginx
unix 3 [ ] STREAM CONNECTED 10188 1784/php-fpm.conf)
unix 3 [ ] STREAM CONNECTED 11494 1523/nginx
unix 3 [ ] STREAM CONNECTED 11487 1523/nginx
unix 3 [ ] STREAM CONNECTED 11493 1523/nginx
~$ sudo netstat -anp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 1784/php-fpm.conf)
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1523/nginx
...
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags Type State I-Node PID/Program name Path
unix 3 [ ] STREAM CONNECTED 10187 1784/php-fpm.conf)
unix 3 [ ] STREAM CONNECTED 11489 1523/nginx
unix 3 [ ] STREAM CONNECTED 11491 1523/nginx
unix 3 [ ] STREAM CONNECTED 11492 1523/nginx
unix 3 [ ] STREAM CONNECTED 11490 1523/nginx
unix 3 [ ] STREAM CONNECTED 11488 1523/nginx
unix 3 [ ] STREAM CONNECTED 10188 1784/php-fpm.conf)
unix 3 [ ] STREAM CONNECTED 11494 1523/nginx
unix 3 [ ] STREAM CONNECTED 11487 1523/nginx
unix 3 [ ] STREAM CONNECTED 11493 1523/nginx
可以看出,
nginx 绑定 tcp:0.0.0.0:80, php-fpm 绑定 127.0.0.1:9000.
根据上文 nginx 的配置,我们在 /var/www/example-php.com 目录创建 phpinfo.php 文件,内容如下:
访问 http://example-php.com/phpinfo.php 看是否出现 phpinfo() 打印的信息。
~$ sudo vi /etc/php5/fpm/pool.d/www.conf
; The address on which to accept FastCGI requests.
; Valid syntaxes are:
; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific address on
; a specific port;
; 'port' - to listen on a TCP socket to all addresses on a
; specific port;
; '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
;listen = 127.0.0.1:9000
listen = /var/run/php5-fpm.sock
/etc/php5/fpm/pool.d$ sudo php5-fpm -t
[11-Apr-2015 23:12:36] NOTICE: configuration file /etc/php5/fpm/php-fpm.conf test is successful
测试配置文件没问题之后重启 php5-fpm.
修改 nginx 配置文件:
/etc/nginx$ sudo vi conf.d/example-php.com.conf
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php5-fpm.sock;
测试没问题之后重启 nginx.
访问 http://example-php.com/phpinfo.php 时出现错误:
==> error.log <==
2015/04/11 23:16:57 [crit] 5274#0: *58 connect() to unix:/var/run/php5-fpm.sock failed (13: Permission denied) while connecting to upstream, client: 192.168.142.1, server: example-php.com, request: "GET /phpinfo.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "example-php.com"
是 /var/run/php5-fpm.sock 的权限问题:
~$ ll /var/run/php5-fpm.sock
srw-rw---- 1 root root 0 Apr 11 23:13 /var/run/php5-fpm.sock=
/etc/php5/fpm/pool.d/www.conf 开启如下三项:
; Set permissions for unix socket, if one is used. In Linux, read/write
; permissions must be set in order to allow connections from a web server. Many
; BSD-derived systems allow connections regardless of permissions.
; Default Values: user and group are set as the running user
; mode is set to 0660
listen.owner = www-data
listen.group = www-data
listen.mode = 0660
再查看:
~$ ll /var/run/php5-fpm.sock
srw-rw---- 1 www-data www-data 0 Apr 11 23:22 /var/run/php5-fpm.sock=
问题解决。
~$ sudo apt-get install mysql-client mysql-server
The following extra packages will be installed:
libdbd-mysql-perl libdbi-perl libhtml-template-perl libmysqlclient18 libnet-daemon-perl
libplrpc-perl libterm-readkey-perl mysql-client-5.5 mysql-common mysql-server-5.5
mysql-server-core-5.5
Suggested packages:
libipc-sharedcache-perl tinyca mailx
The following NEW packages will be installed:
libdbd-mysql-perl libdbi-perl libhtml-template-perl libmysqlclient18 libnet-daemon-perl
libplrpc-perl libterm-readkey-perl mysql-client mysql-client-5.5 mysql-common mysql-server
mysql-server-5.5 mysql-server-core-5.5
0 upgraded, 13 newly installed, 0 to remove and 130 not upgraded.
~$ sudo apt-get install phpmyadmin
The following extra packages will be installed:
dbconfig-common fontconfig-config libfontconfig1 libgd2-xpm libmcrypt4 libt1-5 libxpm4 php5-gd
php5-mcrypt php5-mysql ttf-dejavu-core
Suggested packages:
libgd-tools libmcrypt-dev mcrypt
The following packages will be REMOVED:
libgd2-noxpm
The following NEW packages will be installed:
dbconfig-common fontconfig-config libfontconfig1 libgd2-xpm libmcrypt4 libt1-5 libxpm4 php5-gd
php5-mcrypt php5-mysql phpmyadmin ttf-dejavu-core
0 upgraded, 12 newly installed, 1 to remove and 130 not upgraded.
┌────────────────────────────────┤ Configuring phpmyadmin ├────────────────────────────────┐
│ Please choose the web server that should be automatically configured to run phpMyAdmin. │
│ │
│ Web server to reconfigure automatically: │
│ │
│ [*] apache2 │
│ [ ] lighttpd │
│ │
└──────────────────────────────────────────────────────────────────────────────────────────┘
没有 nginx 的选项,直接先选 apache2.
┌─────────────────────────────────┤ Configuring phpmyadmin ├──────────────────────────────────┐
│ │
│ The phpmyadmin package must have a database installed and configured before it can be │
│ used. This can be optionally handled with dbconfig-common. │
│ │
│ If you are an advanced database administrator and know that you want to perform this │
│ configuration manually, or if your database has already been installed and configured, you │
│ should refuse this option. Details on what needs to be done should most likely be │
│ provided in /usr/share/doc/phpmyadmin. │
│ │
│ Otherwise, you should probably choose this option. │
│ │
│ Configure database for phpmyadmin with dbconfig-common? │
│ │
│ │
│ │
└─────────────────────────────────────────────────────────────────────────────────────────────┘
由于之前没有配置过 MySQL, 所以这部也 "Yes".
dbconfig-common: writing config to /etc/dbconfig-common/phpmyadmin.conf
Creating config file /etc/dbconfig-common/phpmyadmin.conf with new version
Creating config file /etc/phpmyadmin/config-db.php with new version
granting access to database phpmyadmin for phpmyadmin@localhost: success.
verifying access for phpmyadmin@localhost: success.
creating database phpmyadmin: success.
verifying database phpmyadmin exists: success.
populating database via sql... done.
dbconfig-common: flushing administrative password
Processing triggers for libc-bin ...
ldconfig deferred processing now taking place
过程中会修改 /etc/dbconfig-common/phpmyadmin.conf 和 /etc/phpmyadmin/config-db.php, 并创建一个 phpmyadmin@localhost 的 MySQL 账户并将信息写入 /etc/phpmyadmin/config-db.php.
我们看一下 phpMyAdmin 都安装了哪些文件:
~$ dpkg -L phpmyadmin
/etc/phpmyadmin
/etc/phpmyadmin/apache.conf
/etc/phpmyadmin/config.footer.inc.php
/etc/phpmyadmin/config.header.inc.php
/etc/phpmyadmin/config.inc.php
/etc/phpmyadmin/lighttpd.conf
/etc/phpmyadmin/phpmyadmin.desktop
/etc/phpmyadmin/phpmyadmin.service
/usr/share/phpmyadmin
/usr/share/dbconfig-common/data/phpmyadmin
配置:/etc/phpmyadmin
项目目录:/usr/share/phpmyadmin
为 phpMyAdmin 创建 nginx 的虚拟主机:
server {
listen 80;
root /usr/share/phpmyadmin;
index index.html index.htm index.php;
# Make site accessible from http://phpmyadmin.xxx/, http://www.phpmyadmin.xxx/
server_name phpmyadmin.xxx www.phpmyadmin.xxx;
access_log /var/log/nginx/phpmyadmin/access.log;
error_log /var/log/nginx/phpmyadmin/error.log;
# /var/www/example-php.com/404.html
error_page 404 /404.html;
location = /404.html {
root /var/www/example-php.com;
}
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/www;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
/etc/nginx/conf.d$ ls
example.com.conf example-php.com.conf phpmyadmin.conf
/etc/nginx/conf.d$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: [emerg] open() "/var/log/nginx/phpmyadmin/access.log" failed (2: No such file or directory)
nginx: configuration file /etc/nginx/nginx.conf test failed
/etc/nginx/conf.d$ sudo mkdir /var/log/nginx/phpmyadmin
/etc/nginx/conf.d$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
缺少 mysqli 扩展。请检查 PHP 配置。
访问 http://example-php.com/phpinfo.php 根本找不到 mysql 和 mysqli 的项,所以是 php-fpm 配置 mysql 和 mysqli 扩展的部分除了问题。
那么,mysql 和 mysqli 是啥东东呢?其实他是 php 的 mysql 扩展,看名字的确以为是 mysql 自带了,我们从安装包中可以找到踪迹:
$ dpkg -l | grep mysql
ii libdbd-mysql-perl 4.020-1build2 Perl5 database interface to the MySQL database
ii libmysqlclient18 5.5.41-0ubuntu0.12.04.1 MySQL database client library
ii mysql-client 5.5.41-0ubuntu0.12.04.1 MySQL database client (metapackage depending on the latest version)
ii mysql-client-5.5 5.5.41-0ubuntu0.12.04.1 MySQL database client binaries
ii mysql-client-core-5.5 5.5.37-0ubuntu0.12.04.1 MySQL database core client binaries
ii mysql-common 5.5.41-0ubuntu0.12.04.1 MySQL database common files, e.g. /etc/mysql/my.cnf
ii mysql-server 5.5.41-0ubuntu0.12.04.1 MySQL database server (metapackage depending on the latest version)
ii mysql-server-5.5 5.5.41-0ubuntu0.12.04.1 MySQL database server binaries and system database setup
ii mysql-server-core-5.5 5.5.41-0ubuntu0.12.04.1 MySQL database server binaries
ii php5-mysql 5.3.10-1ubuntu3.17 MySQL module for php5
$ dpkg -L mysql-server-core-5.5
/usr/bin/my_print_defaults
/usr/bin/mysql_install_db
/usr/bin/mysql_upgrade
/usr/sbin/mysqld
$ dpkg -L mysql-server-5.5
/usr/bin/myisamlog
/usr/bin/mysqlbinlog
/usr/bin/mysqld_multi
/usr/bin/myisampack
/usr/bin/mysql_convert_table_format
/usr/bin/mysql_zap
/usr/bin/mysql_setpermission
/usr/bin/mysql_secure_installation
/usr/bin/resolveip
/usr/bin/perror
/usr/bin/mysqlhotcopy
/usr/bin/mysqltest
/usr/bin/msql2mysql
/usr/bin/mysqld_safe
/usr/bin/myisamchk
/usr/bin/replace
/usr/bin/mysql_tzinfo_to_sql
/usr/bin/resolve_stack_dump
/usr/lib/mysql
/usr/lib/mysql/plugin
/usr/lib/mysql/plugin/auth_test_plugin.so
/usr/lib/mysql/plugin/ha_example.so
/usr/lib/mysql/plugin/qa_auth_server.so
/usr/lib/mysql/plugin/adt_null.so
/usr/lib/mysql/plugin/auth_socket.so
/usr/lib/mysql/plugin/qa_auth_interface.so
/usr/lib/mysql/plugin/auth.so
/usr/lib/mysql/plugin/mypluglib.so
/usr/lib/mysql/plugin/semisync_master.so
/usr/lib/mysql/plugin/semisync_slave.so
/usr/lib/mysql/plugin/qa_auth_client.so
/usr/lib/mysql/plugin/libdaemon_example.so
/etc/mysql
/etc/mysql/conf.d
/etc/mysql/conf.d/mysqld_safe_syslog.cnf
/etc/mysql/debian-start
/etc/init/mysql.conf
/etc/init.d/mysql
$ dpkg -L mysql-common
/etc/mysql
/etc/mysql/my.cnf
/etc/mysql/conf.d
$ dpkg -L mysql-client-core-5.5
/usr/bin/mysql
/usr/bin/mysqlcheck
$ dpkg -L mysql-client-5.5
/usr/bin/mysql_plugin
/usr/bin/mysqlimport
/usr/bin/mysqldumpslow
/usr/bin/innochecksum
/usr/bin/mysqlshow
/usr/bin/mysqladmin
/usr/bin/mysqlbug
/usr/bin/mysqldump
/usr/bin/mysql_find_rows
/usr/bin/mysqlaccess
/usr/bin/mysql_client_test
/usr/bin/myisam_ftdump
/usr/bin/mysqlreport
/usr/bin/mysql_waitpid
/usr/bin/mysqlslap
/usr/bin/innotop
/usr/bin/mysql_fix_extensions
/usr/bin/mysqlanalyze
/usr/bin/mysqloptimize
/usr/bin/mysqlrepair
$ dpkg -L libmysqlclient18
/usr/lib/x86_64-linux-gnu/libmysqlclient.so.18.0.0
/usr/lib/x86_64-linux-gnu/libmysqlclient.so.18
/usr/lib/x86_64-linux-gnu/libmysqlclient_r.so.18
/usr/lib/x86_64-linux-gnu/libmysqlclient_r.so.18.0.0
$ dpkg -L php5-mysql
/usr/lib/php5
/usr/lib/php5/20090626
/usr/lib/php5/20090626/pdo_mysql.so
/usr/lib/php5/20090626/mysql.so
/usr/lib/php5/20090626/mysqli.so
/etc/php5
/etc/php5/conf.d
/etc/php5/conf.d/pdo_mysql.ini
/etc/php5/conf.d/mysqli.ini
/etc/php5/conf.d/mysql.ini
从最后一个包 php5-mysql 可以看出,mysql.so, mysqli.so 都是 php-mysql 包安装的。那么接下来我们该如何修改配置呢?
配置非常多:
~$ ls /etc/php5/
apache2 cli conf.d fpm
由于我们这里是通过 php-fpm 在运行 php, 所以应该修改 php-fpm 的配置,查看一下现有配置:
/etc/php5/fpm/conf.d$ cat mysql.ini mysqli.ini pdo_mysql.ini
; configuration for php MySQL module
extension=mysql.so
; configuration for php MySQL module
extension=mysqli.so
; configuration for php MySQL module
extension=pdo_mysql.so
根据上面 php5-mysql 包中 so 的路径情况看,这里的路径是不正确的,因此将每个 so 的路径改为 "/usr/lib/php5/20090626/xxx.so", 测试没问题:
$ sudo php5-fpm -t
[12-Apr-2015 17:48:44] NOTICE: configuration file /etc/php5/fpm/php-fpm.conf test is successful
成功访问 http://phpmyadmin.xxx/, 访问 http://example-php.com/phpinfo.php 时也出现了 mysql, mysqli,pdo_mysql 的项。