nginx+php-fpm+apc+mysql编译安装
目前让PHP最快的环境组合是:Nginx + PHP-FPM + APC + MYSQL。本文将讲解 nginx 服务器、php、apc、mysql的安装配置及应用。
安装前提示:linux 软件在编译安装后,他的安装位置和配置文件都可以在源码目录中的文件中找到,如果无法确定软件的相关文件安装在哪里,可以仔细的查看一下源码中的文本文件。
Nginx?
Nginx (“engine x”) 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器 。详细介绍可以见http://wiki.nginx.org/Chs。
PHP-FPM?
PHP-FPM是PHP FastCGI Process Manager 的缩写,即 PHP FastCGI 进程管理器,他是目前最看好的 php 解析器。PHP-FPM 从 php5.3.3 版本开始被整合进 php 安装包中,安装时只要启用即可。
注意:即使你坚持使用Apache,也有很多原因跳过mod_php,直接通过FastCGI解析PHP。使用mod_php时,Apache处理载入PHP的每个请求会载入所有的库,这是一个巨大的无畏开销。如果使用FastCGI,PHP的行为更象应用程序服务器,PHP-FPM以及spawn-fcgi按需要载入和杀掉PHP实例,这样做有很多好处,其中很重要的一点就是减少内存开销。
APC?
APC,全称是Alternative PHP Cache,官方翻译为”可选PHP缓存”。详细介绍可见http://pecl.php.net/package/apc或者http://php.net/manual/en/book.apc.php。
开始安装
我们使用的是全新安装的 debian6,第一件事情是安装所有的依赖包。
$apt-getinstallmakebisonflex gccpatchautoconfsubversion locate
$apt-getinstalllibpcre3-devlibssl-devlibmhash-devlibmhash2libpq-devlibpq5libsyck0-dev
$apt-getinstalllibtidy-devcurllibcurl4-openssl-devlibcurl3
libcurl3-gnutlszlib1gzlib1g-devlibxslt1-devlibzip-devlibzip1
libxml2libsnmp-baselibsnmp15libsnmp-devlibxml2-devlibjpeg62
libjpeg62-devlibpng12-0libpng12-devzlib1gzlib1g-devlibfreetype6
libfreetype6-devlibbz2-devlibxpm-devlibmcrypt-devlibmcrypt4
sqlite3 bzip2 build-essentiallibreadline5-devlibedit-devautoconf
准备好依赖包之后,我们就可以开始。
编译和安装 PHP 及 php-fpm
我们下载的是 PHP 5.3.9 源代码包,已经集成了 PHP-FPM(PHP5.3.3以前的需要下载对应的 PHP-FPM 补丁),然后再编译。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
$wgethttp://cn.php.net/get/php-5.3.9.tar.gz/from/this/mirror
$tar zvxfphp-5.3.9.tar.gz
$wgethttp://php-fpm.anight.org/downloads/head/php-5.2.8-fpm-0.5.10.diff.gz#下载FPM补丁
//$gzip-cdphp-5.2.8-fpm-0.5.10.diff.gz|sudopatch-dphp-5.2.8-p1#PHP5.3.3 以前的版本先打补丁
$cdphp-5.3.9
$./configure
–with-mysql=/usr/local/mysql
–with-mysql-sock=/tmp/mysqld.sock
–with-mysqli=/usr/local/mysql/bin/mysql_config
–with-fpm-user=www-data
–with-fpm-group=www-data
–with-mcrypt
–with-zlib
–with-pgsql
–with-curl
–with-freetype-dir
–with-jpeg-dir
–with-png-dir
–with-libxml-dir
–with-gd
–with-bz2
–with-XMLrpc
–with-mhash
–with-pcre-regex
–disable-debug
–disable-pdo
–disable-rpath
–enable-inline-optimization
–enable-fpm
–enable-mbstring
–enable-gd-native-ttf
–enable-gd-jis-conv
–enable-sockets
–enable-sysvsem
–enable-sysvshm
–enable-pcntl
–enable-mbregex
–enable-zip
–enable-memcache
–enable-xml
./configure
–prefix=/usr/local/php
–with-config-file-path=/usr/local/php/etc
–enable-fpm
–with-fpm-user=www-data
–with-fpm-group=www-data
–with-libxml-dir=/usr/local/libxml
–with-openssl=/usr/local
–with-zlib=/usr
–with-gd=/usr/local/gd
–with-jpeg-dir=/usr/local/jpeg
–with-png-dir=/usr/local
–with-freetype-dir=/usr/local/freetype
–enable-gd-native-ttf
–enable-gd-jis-conv
–with-mhash=/usr/local
–enable-mbstring
–with-mcrypt=/usr/local
–with-mysql=/usr/local/mysql
–with-mysql-sock=/tmp/mysqld.sock
–with-mysqli=/usr/local/mysql/bin/mysql_config
–enable-sockets
–enable-sysvmsg
–enable-sysvsem
–enable-sysvshm
–without-pear
make
make install
//$sudostrip/usr/local/bin/php-cgi#去除 php-cgi
如果在运行上面命令时遇到错误:
/usr/bin/ld:cannotfind-lxxx
意思是编译过程找不到对应库文件。其中,-lxxx 表示库文件 libxxx.so 的符号链接。
解决办法是 apt-get install libxxx-dev 或者是查看符号链接是否正确。
另外,确保你正确启用和禁用了特定的 PHP 配置选项。紧接着我们通过PECL安装一些将会用到的模块:
$peclinstallmemcache
//$pecl install apc
//$pecl install syck-beta
在安装apc模块时,请务必关闭Apache选项,如果你没有关闭,它也会提醒你。
接下来复制我们常用的php.ini:
$cpphp-5.3.9/php.ini-recommended/usr/local/lib/php/php.ini
最后,我们建立符号链接:
$mkdir/etc/php/
$ln-s/usr/local/lib/php/php.ini/etc/php/php.ini
PHP编译到此结束,剩下要做的事情是修改php-fpm.conf设置,用文本编辑器打开/etc/php/php-fpm.conf,将进程属主用户修改为www-data。
配置 php-fpm
因为从 php5.3.3 以后,php-fpm 被整合进 php 的安装包之中,在编译时只要设定启用参数了,那么 php-fpm 也会被默认安装。默认安装的 php-fpm 位置是:
/usr/local/etc/php-fpm.conf //配置文件安装位置
/usr/local/sbin/php-fpm //启动文件安装位置
默认安装的 php-fpm 给我们准备了一个程序来控制 fastcgi 进程,这个文件是 $PREFIX/sbin/php-fpm($PREFIX 是安装目录),但是此文件只能启动 php-fpm,要想关闭 php-fpm,需要查看 master进程号,然后 kill 掉:
$/usr/local/sbin/php-fpm#启动
$ps-ef|grepphp-fpm#查看进程
$killpid
我想使用平滑启动/关闭管理模式来管理 php-fpm 该怎么做呢?
php-fpm 同样也给我们提供了一个平滑启动/关闭的控制程序,这个文件是php-5.3.9/sapi/fpm/init.d.php-fpm(编译后的源码包中),把它复制为 /etc/init.d/php-fpm:
$cp/data/software/php-5.3.9/sapi/fpm/init.d.php-fpm/etc/init.d/php-fpm
该程序有如下参数:
start 启动php的fastcgi进程
stop 强制终止php的fastcgi进程
quit 平滑终止php的fastcgi进程
restart 重启php的fastcgi进程
reload 重新加载php的php.ini
logrotate 重新启用log文件
通过查看 /data/software/php-5.3.9/sapi/fpm/init.d.php-fpm,我们能够知道 php-fpm 的日志及进程号默认是保存在 ${prefix} 的var下,但是配置文件 php-fpm.conf 默认是没有开启的,找到对应的地方去掉注释即可。
这样,在修改了php.ini之后,我们可以使用
$/etc/init.d/php-fpmreload
这样,就保持了在php的fastcgi进程持续运行的状态下,又重新加载了php.ini。
编译和安装 MySQL
下载和编译 MySQL,但是先不要安装:
# wget http://mysql.mirror.rafal.ca/Downloads/MySQL-5.1/mysql-5.1.50.tar.gz
# tar zxvf mysql-5.1.50.tar.gz
# cd mysql-5.1.50
# ./configure
–prefix=”/usr/local/mysql-5.1.50″
–enable-thread-safe-client
–with-extra-charsets=all
# make
需要改几个权限问题才能安装 MySQL,否则会出现 Access denied for user ‘root’@’localhost’ (using password: NO) 经典问题:
# groupadd mysql
# useradd -g mysql mysql
# cp support-files/my-small.cnf /etc/my.cnf
# vi /etc/my.conf
…
[mysqld]
user =mysql
…
# chown -R mysql:mysql /usr/local/mysql-5.1.50/
# chmod 777 /tmp
安装和启动 MySQl,修改 root 密码,登录 MySQL:
# cd mysql-5.1.50
# make install
# /usr/local/mysql-5.1.50/bin/mysql_install_db –user=mysql
# /usr/local/mysql-5.1.50/bin/mysqld_safe &
# /usr/local/mysql-5.1.50/bin/mysqladmin -u root password ‘new-password’
# /usr/local/mysql-5.1.50/bin/mysql -u root -p
编译Nginx
和编译PHP一样非常简单:
$wgethttp://nginx.org/download/nginx-1.1.12.tar.gz
$tar zxvfnginx-1.1.12.tar.gz
$cdnginx-1.1.12
$./configure–add-module=../nginx_mod_h264_streaming-2.2.7–prefix=/usr/local/nginx–with-http_realip_module–with-http_sub_module–with-http_flv_module–with-http_dav_module–with-http_gzip_static_module–with-http_stub_status_module–with-http_addition_module–with-pcre=../pcre-8.21–with-http_ssl_module–with-zlib=../zlib-1.2.3–with-http_secure_link_module
$make
$make install
注意:要修改nginx目录及文件权限,以免运行时因权限问题发生错误。
在etc下创建一个符号链接,方便管理:
$ln-s/usr/local/nginx/etc/nginx
配置/etc/nginx/conf/nginx.conf,最终修改结果如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
userwww-data;
worker_processes6;
pid/var/run/nginx.pid;#kill ‘cat /var/run/nginx.pid>$2&’
events{
worker_connections1024;
useepoll;
}
http {
includemime.types;
default_type application/octet-stream;
sendfileon;
keepalive_timeout1010;
gzip on;
gzip_comp_level1;
gzip_proxied any;
gzip_typestext/plaintext/cssapplication/x-javascripttext/xmlapplication/xmlapplication/xml+rsstext/javascript;
log_formatmain‘$remote_addr–$remote_user[$time_local]‘
‘”$request”$status$body_bytes_sent“$http_referer”‘
‘”$http_user_agent”“$http_x_forwarded_for”‘;
access_log/var/log/nginx_access.logmain;
error_log/var/log/nginx_error.logdebug;
include/usr/local/nginx/sites-enabled/*;#加载可用的web配置。风格自定
}
优化设置FastCGI参数,可以避免Nginx 503错误,打开/etc/nginx/fastcgi_params,添加以下参数:
fastcgi_connect_timeout60;
fastcgi_send_timeout180;
fastcgi_read_timeout180;
fastcgi_buffer_size128k;
fastcgi_buffers4256k;
fastcgi_busy_buffers_size256k;
fastcgi_temp_file_write_size256k;
fastcgi_intercept_errors on;
最后,我们创建一个SystemV风格的启动脚本,保存为/etc/init.d/nginx。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/bin/sh
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/sbin/nginx
NAME=nginx
DESC=nginx
test-x$DAEMON||exit0
# Include nginx defaults if available
if [-f/etc/default/nginx];then
./etc/default/nginx
fi
set -e
case “$1″in
start)
echo -n“Starting$DESC:”
start-stop-daemon–start–quiet–pidfile/usr/local/nginx/logs/$NAME.pid
–exec $DAEMON—$DAEMON_OPTS
echo“$NAME.”
;;
stop)
echo -n“Stopping$DESC:”
start-stop-daemon–stop–quiet–pidfile/usr/local/nginx/logs/$NAME.pid
–exec $DAEMON
echo“$NAME.”
;;
restart|force-reload)
echo -n“Restarting$DESC:”
start-stop-daemon–stop–quiet–pidfile
/usr/local/nginx/logs/$NAME.pid–exec$DAEMON
sleep1
start-stop-daemon–start–quiet–pidfile
/usr/local/nginx/logs/$NAME.pid–exec$DAEMON—$DAEMON_OPTS
echo “$NAME.”
;;
reload)
echo-n“Reloading$DESCconfiguration:”
start-stop-daemon–stop–signalHUP–quiet–pidfile/usr/local/nginx/logs/$NAME.pid
–exec$DAEMON
echo “$NAME.”
;;
*)
N=/etc/init.d/$NAME
echo “Usage:$N{start|stop|restart|force-reload}”>&2
exit1
;;
esac
exit0
不要忘了设置可执行权限。
设置你的网站
这一阶段的工作主要按你自己的习惯完成,这里仅做一个粗略的介绍。首先我们创建一个目录来存放我们的网站配置文件,在创建一个目录用来存放激活的文件(此目录存放指向可用配置的符号链接):
$mkdir/usr/local/nginx/sites-available
$mkdir/usr/local/nginx/sites-enabled
$ln-s/usr/local/nginx/sites-enabled/etc/sites
接下来为我们的网站增加一个conf文件/etc/nginx/sites-available/default.conf,内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
server{
listen *:80;
location /{
root/var/www/default/pub;#网站根目录
index index.php;#默认主页
# if file exists return it right away
if(-f$request_filename){
break;
}
# otherwise rewrite the fucker
if (!-e$request_filename){
rewrite^(.+)$/index.php$1last;
break;
}
}
# if the request starts with our frontcontroller, pass it on to fastcgi
location~^/index.php
{
fastcgi_pass127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME/var/www/default/pub$fastcgi_script_name;
fastcgi_paramPATH_INFO$fastcgi_script_name;
include /usr/local/nginx/conf/fastcgi_params;
}
}
为新增加的网站设置添加符号链接,即激活新的网站设置:
$ln-s/etc/nginx/sites-available/default.conf/etc/nginx/sites-enabled/00default
启动
$php-fmstart
$/etc/init.d/nginxstart
end