nginx1.4.6+php5.5.11+mysql5.6.17+mecache+opcache

要用到的软件:
libiconv-1.13.tar.gz
libmcrypt-2.5.8.tar.gz
mcrypt-2.6.8.tar.gz
mhash-0.9.9.9.tar.gz
memcache-2.2.5.tgz
PDO_MYSQL-1.0.2.tgz
imagick-3.1.2.tgz
pcre-8.21.tar.gz
mysql-5.6.17.tar.gz
ImageMagick.tar.gz
php-5.4.21.tar.gz
nginx-1.4.6.tar.gz

系统安装需求:
centos 6.4 *86_64 mini安装

1 安装解决依赖关系:
yum install -y wget gcc gcc-c++ autoconf libjpeg libjpeg-devel perl perl* perl-CPAN libpng libpng-devel freetype freetype-devel lib2 lib2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers png jpeg autoconf gcc cmake make gcc-c++ gcc ladp ldap* ncurses ncurses-devel zlib zlib-devel zlib-static pcre pcre-devel pcre-static openssl openssl-devel perl libtoolt openldap-devel lib2-devel ntpdate cmake gd* gd2 ImageMagick-devel jpeg jpeg* pcre-dev* fontconfig libpng lib2 zip unzip gzip

yum install libxml2 libxml2-devel


2 下载所有使用到的软件:

wget http://ftp.gnu.org/gnu/libiconv/libiconv-1.13.tar.gz
wget http://lcmp.googlecode.com/files/libmcrypt-2.5.8.tar.gz
wget http://jaist.dl.sourceforge.net/project/mcrypt/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz
wget http://jaist.dl.sourceforge.net/project/mhash/mhash/0.9.9.9/mhash-0.9.9.9.tar.gz
wget http://vps.googlecode.com/files/PDO_MYSQL-1.0.2.tgz
wget http://pecl.php.net/get/imagick-3.1.2.tgz
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.21.tar.gz
wget http://mynginx.googlecode.com/files/ImageMagick.tar.gz
wget https://lnamp-web-server.googlecode.com/files/eaccelerator-eaccelerator-42067ac.tar.gz

NGINX1.4.6: http://nginx.org/download/nginx-1.4.6.tar.gz

MYSQL5.6.17 http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.17.tar.gz

PHP5.5.11 http://us1.php.net/get/php-5.5.11.tar.gz/from/this/mirror

先前分别在两台机器上编译过两次不同MYSQL版本:MYSQL5.6.12和MYSQL5.6.17 编译命令都是如下:

cmake /
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql /
-DMYSQL_DATADIR=/usr/local/mysql/data /
-DSYSCONFDIR=/etc /
-DWITH_MYISAM_STORAGE_ENGINE=1 /
-DWITH_INNO_STORAGE_ENGINE=1 /
-DWITH_MEMORY_STORAGE_ENGINE=1 /
-DWITH_READLINE=1 /
-DMYSQL_UNIX_ADDR=/tmp/mysqld.sock /
-DMYSQL_TCP_PORT=3306 /
-DENABLED_LOCAL_INFILE=1 /
-DWITH_PARTITION_STORAGE_ENGINE=1 /
-DEXTRA_CHARSETS=all /
-DDEFAULT_CHARSET=utf8 /
-DDEFAULT_COLLATION=utf8_general_ci

完成后,继续下面的操作

cd /usr/local/mysql
chown -R mysql:mysql . (为了安全安装完成后请修改权限给root用户)
s/mysql_install_db --user=mysql (先进行这一步再做如下权限的修改)
chown -R root:mysql . (将权限设置给root用户,并设置给mysql组, 取消其他用户的读写执行权限,仅留给mysql "rx"读执行权限,其他用户无任何权限)
chown -R mysql:mysql ./data (给数据库存放目录设置成mysql用户mysql组,并赋予chmod -R ug+rwx 读写执行权限,其他用户权限一律删除仅给mysql用户权限)
下面的命令是可选的,将mysql的配置文件拷贝到/etc

cp support-files/my-medium.cnf /etc/my.cnf (原始老版本是此操作,5.6.12版本的是如下文件地址)
cp support-files/my-default.cnf /etc/my.cnf (并给/etc/my.cnf +x权限 同时删除 其他用户的写权限,仅仅留给root 和工作组 rx权限,其他一律删除连rx权限都删除)
#修改my.cnf配置
vim /etc/my.cnf

#[mysqld] 添加:
datadir=/data/mysql
default-storage-engine=MyISAM
启动mysql:

[c-sharp] view plaincopy
bin/mysqld_safe --user=mysql &
#启动mysql,看是否成功
netstat -tnl|grep 3306
[c-sharp] view plaincopy
bin/mysqld_safe --user=mysql &
#启动mysql,看是否成功
netstat -tnl|grep 3306
上面是一种启动mysql的方法,还有一种简单的方便,如下:

[c-sharp] view plaincopy
#将mysql的启动服务添加到系统服务中
cp support-files/mysql.server /etc/init.d/mysql
#现在可以使用下面的命令启动mysql
service mysql start
#停止mysql服务
service mysql stop
#重启mysql服务
service mysql restart
将mysql服务添加到开机启动项,让mysql服务开机启动

[c-sharp] view plaincopy
chkconfig --add mysql
修改默认root账户密码,默认密码为空

修改密码 cd 切换到mysql所在目录

cd /usr/local/mysql

#./bin/mysqladmin -u root password

回车在接下来的提示中设置新密码即可。。

#启动MySQL
/usr/local/mysql/bin/mysqld_safe –defaults-file=/etc/my.cnf &
#或者
/etc/init.d/mysql start (service mysql start)

#测试MySQL是否启动
# 1)查看是否有进程mysql
ps -ef | grep mysql
# 2)查看端口是否运行
netstat -tnl | grep 3306
# 3)读取mysql版本信息
mysqladmin version



PHP5.4.8常用编译命令
./configure --prefix=/usr/local/php /
--with-apxs2=/usr/local/apache2/bin/apxs / #此行仅在apache+php下使用,nginx+php时请去掉
--with-config-file-path=/usr/local/php/etc /
--with-mysql=/usr/local/mysql /
--with-mysqli=/usr/local/mysql/bin/mysql_config /
--with-iconv-dir /
--with-jpeg-dir /
--with-png-dir /
--with-zlib /
--with-lib-dir /
--enable- /
--disable-rpath /
--enable-bcmath /
--enable-shmop /
--enable-sysvsem /
--enable-inline-optimization /
--with-curl /
--with-curlwrappers /
--enable-mbregex /
--enable-fpm /
--enable-mbstring /
--with-mcrypt /
--with-gd /
--with-freetype-dir /
--enable-gd-native-ttf /
--with-openssl /
--with-mhash /
--enable-pcntl /
--enable-sockets /
--with-ldap /
--with-ldap-sasl /
--with-rpc /
--enable-zip /
--enable-soap /
--enable-session /
--with-gettext /
--enable-pdo /
--with-pdo-mysql /
--enable-shared /
--enable-ftp /
--enable-exif /
--with-pear
php5.5之后新增下面可选命令
--enable-opcache /
线上服务器可以加入以下命令关闭调试模式:
--disable-debug /
php5.5.11后无法识别的废旧编译选项(如果提示WRING警告无法识别--with-curlwrappers无视即可):
--with-curlwrappers
使用NGINX处理请去掉下面一行,下面一行仅在APACHE下使用
--with-apxs2=/usr/local/apache2/bin/apxs /
使用NGINX可以加入下面两行指定FPM运行账户权限:
--with-fpm-user=www /
--with-fpm-group=www /
PHP编译选项说明:
""" 安装路径 """
--prefix=/usr/local/php /
""" php.ini 配置文件路径 """
--with-config-file-path=/etc /
""" 优化选项 """
--enable-inline-optimization /
--disable-debug /
--disable-rpath /
--enable-shared /
""" 启用 opcache,默认为 ZendOptimizer+(ZendOpcache) """
--enable-opcache /
""" FPM """
--enable-fpm /
--with-fpm-user=www /
--with-fpm-group=www /
""" MySQL """
--with-mysql=mysqlnd /
--with-mysqli=mysqlnd /
--with-pdo-mysql=mysqlnd /
""" 国际化与字符编码支持 """
--with-gettext /
--enable-mbstring /
--with-iconv /
""" 加密扩展 """
--with-mcrypt /
--with-mhash /
--with-openssl /
""" 数学扩展 """
--enable-bcmath /
""" Web 服务,依赖 lib """
--enable-soap /
--with-lib-dir /
""" 进程控制 """
--enable-pcntl /
--enable-shmop /
--enable-sysvsem /
""" socket & curl """
--enable-sockets /
--with-curl /
""" 压缩与归档 """
--with-zlib /
--enable-zip /
--with-bz2 /
""" GNU Readline 命令行快捷键绑定 """
--with-readline /
""" 禁用 SQLite 支持扩展 """
--without-sqlite3 /
--without-pdo-sqlite /
""" 更多 PHP 扩展与应用库 """
--with-pear



在Linux下安装PHP,源代码方式安装,总需要配置很多参数。这里列出常用配置参数,并详细用中文解释说明了。给大家一些参考

编译PHP的时候慎用 –with-curlwrappers参数

./configure

--prefix=/usr/local/PHP php 安装目录
--with-apxs2=/usr/local/apache/bin/apxs 表示使用apache2.0以上版本,apxs表示使用的apache1.0版本
--with-config-file-path=/usr/local/PHP/etc 指定php.ini位置
--with-MySQL=/usr/local/mysql mysql安装目录,对mysql的支持
--with-MySQLi=/usr/local/mysql/bin/mysql_config mysqli文件目录,优化支持
--enable-safe-mode 打开安全模式
--enable-ftp 打开ftp的支持
--enable-zip 打开对zip的支持
--with-bz2 打开对bz2文件的支持
--with-jpeg-dir 打开对jpeg图片的支持
--with-png-dir 打开对png图片的支持
--with-freetype-dir 打开对freetype字体库的支持
--without-iconv 关闭iconv函数,种字符集间的转换
--with-lib-dir 打开lib2库的支持
--with-rpc 打开-rpc的c语言
--with-zlib-dir 打开zlib库的支持
--with-gd 打开gd库的支持
--enable-gd-native-ttf 支持TrueType字符串函数库
--with-curl 打开curl浏览工具的支持
--with-curlwrappers 运用curl工具打开url流
--with-ttf 打开freetype1.*的支持,可以不加了
--with-xsl 打开XSLT 文件支持,扩展了lib2库 ,需要libxslt软件
--with-gettext 打开gnu 的gettext 支持,编码库用到
--with-pear 打开pear命令的支持,PHP扩展用的
--enable-calendar 打开日历扩展功能
--enable-mbstring 多字节,字符串的支持
--enable-bcmath 打开图片大小调整,用到zabbix监控的时候用到了这个模块
--enable-sockets 打开 sockets 支持
--enable-exif 图片的元数据支持
--enable-magic-quotes 魔术引用的支持
--disable-rpath 关闭额外的运行库文件
--disable-debug 关闭调试模式
--with-mime-magic=/usr/share/file/magic.mime 魔术头文件位置
--enable-opcache //PHP5.5X+新增代码加速功能

CGI方式安装才用的参数
--enable-fpm 打上PHP-fpm 补丁后才有这个参数,CGI方式安装的启动程序
--enable-fastCGI 支持fastcgi方式启动PHP
--enable-force-CGI-redirect 同上 ,帮助里没有解释
--with-ncurses 支持ncurses 屏幕绘制以及基于文本终端的图形互动功能的动态库
--enable-pcntl freeTDS需要用到的,可能是链接mssql 才用到

mhash和mcrypt算法的扩展
--with-mcrypt 算法
--with-mhash 算法

--with-gmp
--enable-inline-optimization
--with-openssl openssl的支持,加密传输时用到的
--enable-d
--with-pcre-dir=/usr/local/bin/pcre-config perl的正则库案安装位置
--disable-dmalloc
--with-gdbm dba的gdbm支持
--enable-sigchild
--enable-sysvsem
--enable-sysvshm
--enable-zend-multibyte 支持zend的多字节
--enable-mbregex
--enable-wddx
--enable-shmop
--enable-soap



准备PHP的配置文件

1
2
# cd /home/down/php-5.5.11
# cp php.ini-production /usr/local/php/etc/php.ini
准备php_fpm的配置文件

1
2
3
4
# cd /usr/local/php/etc/
# cp php-fpm.conf.defaultphp-fpm.conf
# vim php-fpm.conf
修改php-fpm.conf配置文件内容去掉如下前面的分号,并根据实际硬件配置进行更改

这里仅做简单配置,下面会有PHP-FPM的详细的优化配置讲解


pid = run/php-fpm.pid
error_log = log/php-fpm.log
listen = 127.0.0.1:9000
pm.max_children = 40
pm.start_servers = 12
pm.min_spare_servers = 4
pm.max_spare_servers = 20
pm.max_requests = 1000

使用php-fpm启动php(需要注意的是启动配置文件需要给用户组+rx权限尤其要添加到服务器项的+x权限,否则会因没有可执行权限而产生的异常错误)

Php-fpm代码

php5.5X+版本提供了php-fpm的管理工具,可以很方便的start,stop,restart

Php-fpm代码

// 个人习惯把php相关命令丢在这

#cd /usr/local/php/sbin/



// 在安装文件中复制该文件

#cp /home/down/php-5.5.11/sapi/fpm/init.d.php-fpm ./

#chmod 755 init.d.php-fpm


// start | stop | restart

#./init.d.php-fpm start

另一种PHP-FPM启动方式:

测试php/php-fpm是否安装成功:

#测试php-fpm配置(实际路径根据自己PHP安装路径自行替换)
/usr/local/php/sbin/php-fpm-t
/usr/local/php/sbin/php-fpm-c/usr/local/php/etc/php.ini -y/usr/local/php/etc/php-fpm.conf -t

出现类似如下提示证明测试通过,PHP/php-fpm安装成功
[18-Apr-2014 23:20:15] NOTICE: configuration file /usr/local/php/etc/php-fpm.conf test is successful
(一点小插曲:第一次测试时启动时出现如下错误,vim /usr/local/php/etc/php-fpm.conf 打开找到第52行发现52行的修改注释忘记加注释符号分号“;”了,OK搞定

Starting php-fpm [18-Apr-2014 23:09:15] ERROR: [/usr/local/php/etc/php-fpm.conf:52] value is NULL for a ZEND_INI_PARSER_ENTRY
[18-Apr-2014 23:09:15] ERROR: failed to load configuration file '/usr/local/php/etc/php-fpm.conf'
[18-Apr-2014 23:09:15] ERROR: FPM initialization failed)

#启动php-fpm
/usr/local/php/sbin/php-fpm
/usr/local/php/sbin/php-fpm-c/usr/local/php/etc/php.ini -y/usr/local/php/etc/php-fpm.conf

#关闭php-fpm
kill -INT `cat /usr/local/php/var/run/php-fpm.pid`

#重启php-fpm
kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`

为php-fpm提供Sysv init脚本,并将其添加至服务列表 致使开机自动启动

1
2
3
4
5
6
# cd/home/down/php-5.5.11
# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
# cp sapi/fpm/init.d.php-fpm/etc/init.d/php-fpm
# chmod +x /etc/init.d/php-fpm
# chkconfig --add php-fpm
# chkconfig php-fpm on
完成如上操作即可在命令行使用service php-fpm start | stop | restart 进行 启动|停止|重启操作php-fpm了

锚点php-fpm.conf重要参数详解
pid = run/php-fpm.pid
#pid设置,默认在安装目录中的var/run/php-fpm.pid,建议开启

error_log = log/php-fpm.log
#错误日志,默认在安装目录中的var/log/php-fpm.log

log_level = notice
#错误级别. 可用级别为: alert(必须立即处理), error(错误情况), warning(警告情况), notice(一般重要信息), debug(调试信息). 默认: notice.

emergency_restart_threshold = 60
emergency_restart_interval = 60s
#表示在emergency_restart_interval所设值内出现SIGSEGV或者SIGBUS错误的php-cgi进程数如果超过 emergency_restart_threshold个,php-fpm就会优雅重启。这两个选项一般保持默认值。

process_control_timeout = 0
#设置子进程接受主进程复用信号的超时时间. 可用单位: s(秒), m(分), h(小时), 或者 d(天) 默认单位: s(秒). 默认值: 0.

daemonize = yes
#后台执行fpm,默认值为yes,如果为了调试可以改为no。在FPM中,可以使用不同的设置来运行多个进程池。 这些设置可以针对每个进程池单独设置。

listen = 127.0.0.1:9000
#fpm监听端口,即nginx中php处理的地址,一般默认值即可。可用格式为: 'ip:port', 'port', '/path/to/unix/socket'. 每个进程池都需要设置.

listen.backlog = -1
#backlog数,-1表示无限制,由操作系统决定,此行注释掉就行。backlog含义参考:http://www.3gyou.cc/?p=41

listen.allowed_clients = 127.0.0.1
#允许访问FastCGI进程的IP,设置any为不限制IP,如果要设置其他主机的nginx也能访问这台FPM进程,listen处要设置成本地可被访问的IP。默认值是any。每个地址是用逗号分隔. 如果没有设置或者为空,则允许任何服务器请求连接

listen.owner = www
listen.group = www
listen.mode = 0666
#unix socket设置选项,如果使用tcp方式访问,这里注释即可。

user = www
group = www
#启动进程的帐户和组

pm = dynamic #对于专用服务器,pm可以设置为static。
#如何控制子进程,选项有static和dynamic。如果选择static,则由pm.max_children指定固定的子进程数。如果选择dynamic,则由下开参数决定:
pm.max_children #,子进程最大数
pm.start_servers#,启动时的进程数
pm.min_spare_servers#,保证空闲进程数最小值,如果空闲进程小于此值,则创建新的子进程
pm.max_spare_servers#,保证空闲进程数最大值,如果空闲进程大于此值,此进行清理

pm.max_requests = 1000
#设置每个子进程重生之前服务的请求数. 对于可能存在内存泄漏的第三方模块来说是非常有用的. 如果设置为 '0' 则一直接受请求. 等同于 PHP_FCGI_MAX_REQUESTS 环境变量. 默认值: 0.

pm.status_path =/status
#FPM状态页面的网址. 如果没有设置, 则无法访问状态页面. 默认值: none. munin监控会使用到

ping.path =/ping
#FPM监控页面的ping网址. 如果没有设置, 则无法访问ping页面. 该页面用于外部检测FPM是否存活并且可以响应请求. 请注意必须以斜线开头 (/)。

ping.response = pong
#用于定义ping请求的返回相应. 返回为 HTTP 200 的 text/plain 格式文本. 默认值: pong.

request_terminate_timeout = 0
#设置单个请求的超时中止时间. 该选项可能会对php.ini设置中的'max_execution_time'因为某些特殊原因没有中止运行的脚本有用. 设置为 '0' 表示 'Off'.当经常出现502错误时可以尝试更改此选项。

request_slowlog_timeout = 10s
#当一个请求该设置的超时时间后,就会将对应的PHP调用堆栈信息完整写入到慢日志中. 设置为 '0' 表示 'Off'

slowlog = log/$pool.log.slow
#慢请求的记录日志,配合request_slowlog_timeout使用

rlimit_files = 1024
#设置文件打开描述符的rlimit限制. 默认值: 系统定义值默认可打开句柄是1024,可使用 ulimit -n查看,ulimit -n 2048修改。

rlimit_core = 0
#设置核心rlimit最大限制值. 可用值: 'unlimited' 、0或者正整数. 默认值: 系统定义值.

chroot =
#启动时的Chroot目录. 所定义的目录需要是绝对路径. 如果没有设置, 则chroot不被使用.

chdir =
#设置启动目录,启动时会自动Chdir到该目录. 所定义的目录需要是绝对路径. 默认值: 当前目录,或者/目录(chroot时)

catch_workers_output =yes
#重定向运行过程中的stdout和stderr到主要的错误日志文件中. 如果没有设置, stdout 和 stderr 将会根据FastCGI的规则被重定向到 /dev/null . 默认值: 空.
锚点PHP-FPM常见错误及解决办法整理
如果file_get_contents请求的远程资源如果反应过慢,file_get_contents就会一直卡在那里不会超时。我们知道php.ini 里面max_execution_time 可以设置 PHP 脚本的最大执行时间,但是,在 php-cgi(php-fpm) 中,该参数不会起效。真正能够控制 PHP 脚本最大执行时间的是 php-fpm.conf 配置文件中的request_terminate_timeout参数。

request_terminate_timeout默认值为 0 秒,也就是说,PHP 脚本会一直执行下去。这样,当所有的 php-cgi 进程都卡在 file_get_contents() 函数时,这台 Nginx+PHP 的 WebServer 已经无法再处理新的 PHP 请求了,Nginx 将给用户返回“502 Bad Gateway”。修改该参数,设置一个 PHP 脚本最大执行时间是必要的,但是,治标不治本。例如改成 30s,如果发生 file_get_contents() 获取网页内容较慢的情况,这就意味着 150 个 php-cgi 进程,每秒钟只能处理 5 个请求,WebServer 同样很难避免”502 Bad Gateway”。解决办法是request_terminate_timeout设置为10s或者一个合理的值,或者给file_get_contents加一个超时参数。

1
2
3
4
5
6
7
$ctx = stream_context_create(array(
'http'=>array(
'timeout'=> 10 //设置一个超时时间,单位为秒
)
));

file_get_contents($str, 0,$ctx);
锚点2,max_requests参数配置不当,可能会引起间歇性502错误:
1
pm.max_requests = 1000
设置每个子进程重生之前服务的请求数. 对于可能存在内存泄漏的第三方模块来说是非常有用的. 如果设置为 ’0′ 则一直接受请求. 等同于 PHP_FCGI_MAX_REQUESTS 环境变量. 默认值: 0.
这段配置的意思是,当一个 PHP-CGI 进程处理的请求数累积到 500 个后,自动重启该进程。

但是为什么要重启进程呢?

一般在项目中,我们多多少少都会用到一些 PHP 的第三方库,这些第三方库经常存在内存泄漏问题,如果不定期重启 PHP-CGI 进程,势必造成内存使用量不断增长。因此 PHP-FPM 作为 PHP-CGI 的管理器,提供了这么一项监控功能,对请求达到指定次数的 PHP-CGI 进程进行重启,保证内存使用量不增长。

正是因为这个机制,在高并发的站点中,经常导致 502 错误,我猜测原因是 PHP-FPM 对从 NGINX 过来的请求队列没处理好。不过我目前用的还是 PHP 5.3.2,不知道在 PHP 5.3.3 中是否还存在这个问题。

目前我们的解决方法是,把这个值尽量设置大些,尽可能减少 PHP-CGI 重新 SPAWN 的次数,同时也能提高总体性能。在我们自己实际的生产环境中发现,内存泄漏并不明显,因此我们将这个值设置得非常大(204800)。大家要根据自己的实际情况设置这个值,不能盲目地加大。

话说回来,这套机制目的只为保证 PHP-CGI 不过分地占用内存,为何不通过检测内存的方式来处理呢?我非常认同高春辉所说的,通过设置进程的峰值内在占用量来重启 PHP-CGI 进程,会是更好的一个解决方案。

锚点3,php-fpm的慢日志,debug及异常排查神器:
request_slowlog_timeout设置一个超时的参数,slowlog设置慢日志的存放位置

1
tail -f /var/log/www.slow.log
上面的命令即可看到执行过慢的php过程。
大家可以看到经常出现的网络读取超过、Mysql查询过慢的问题,根据提示信息再排查问题就有很明确的方向了。

锚点php-fpm优化
锚点1、php-fpm优化参数介绍
他们分别是:pm、pm.max_children、pm.start_servers、pm.min_spare_servers、pm.max_spare_servers。

pm:表示使用那种方式,有两个值可以选择,就是static(静态)或者dynamic(动态)。
在更老一些的版本中,dynamic被称作apache-like。这个要注意看配置文件的说明。

下面4个参数的意思分别为:

pm.max_children:静态方式下开启的php-fpm进程数量
pm.start_servers:动态方式下的起始php-fpm进程数量
pm.min_spare_servers:动态方式下的最小php-fpm进程数
pm.max_spare_servers:动态方式下的最大php-fpm进程数量
区别:

如果dm设置为 static,那么其实只有pm.max_children这个参数生效。系统会开启设置数量的php-fpm进程。
如果dm设置为 dynamic,那么pm.max_children参数失效,后面3个参数生效。
系统会在php-fpm运行开始 的时候启动pm.start_servers个php-fpm进程,
然后根据系统的需求动态在pm.min_spare_servers和pm.max_spare_servers之间调整php-fpm进程数

锚点2、服务器具体配置
对于我们的服务器,选择哪种执行方式比较好呢?事实上,跟Apache一样,运行的PHP程序在执行完成后,或多或少会有内存泄露的问题。
这也是为什么开始的时候一个php-fpm进程只占用3M左右内存,运行一段时间后就会上升到20-30M的原因了。

对于内存大的服务器(比如8G以上)来说,指定静态的max_children实际上更为妥当,因为这样不需要进行额外的进程数目控制,会提高效率。
因为频繁开关php-fpm进程也会有时滞,所以内存够大的情况下开静态效果会更好。数量也可以根据 内存/30M 得到,比如8GB内存可以设置为100,
那么php-fpm耗费的内存就能控制在 2G-3G的样子。如果内存稍微小点,比如1G,那么指定静态的进程数量更加有利于服务器的稳定。
这样可以保证php-fpm只获取够用的内存,将不多的内存分配给其他应用去使用,会使系统的运行更加畅通。

对于小内存的服务器来说,比如256M内存的VPS,即使按照一个20M的内存量来算,10个php-cgi进程就将耗掉200M内存,那系统的崩溃就应该很正常了。
因此应该尽量地控制php-fpm进程的数量,大体明确其他应用占用的内存后,给它指定一个静态的小数量,会让系统更加平稳一些。或者使用动态方式,
因为动态方式会结束掉多余的进程,可以回收释放一些内存,所以推荐在内存较少的服务器或VPS上使用。具体最大数量根据 内存/20M 得到。
比如说512M的VPS,建议pm.max_spare_servers设置为20。至于pm.min_spare_servers,则建议根据服务器的负载情况来设置,比如服务器上只是部署php环境的话,比较合适的值在5~10之间。

锚点本服务器配置 锚点1、服务器基本信息:
硬盘:数据盘30G、系统盘20G
内存:1.5G
CPU:双核
系统:CentOS 6.3 64位
带宽:独享2M

锚点2、部署的应用
Git、SVN、Apache、Tomcat、PHP、Nginx、Mysql、JDK

锚点3、优化后的参数
1
2
3
4
pm = dynamic
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8


NGINX
./configure --user=www --group=www --prefix=/usr/local/nginx /
--with-http_stub_status_module /
--with-http_ssl_module /
--with-http_realip_module /
--with-http_image_filter_module /
--with-http_gzip_static_module /
--with-http_flv_module /
--with-pcre

检查是否安装成功
cd /usr/local/nginx/sbin
./nginx -t
结果显示:
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
启动nginx
cd /usr/local/nginx/sbin 目录下面 输入 ./nginx 启动 nginx
检查是否启动成功
ie 浏览器中输入 http://192.168.15.132 (切换成自己的IP地址即可)
停止nginx

pkill -9 nginx

将NGINX添加为系统服务:

1、编写脚本,名为nginx

vim /etc/init.d/nginx

chmod 755 /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on

更换NGINX启动方式 测试是否成功
service nginx start 启动
service nginx stop 停止
service nginx restart 重启
/etc/init.d/nginx 的内容如下:
#!/bin/sh


#
# nginx - this starts and stops the nginxdaemon
#
# chkconfig: - 8515
# deion: Nginx is an HTTP(S) server, HTTP(S) reverse/
# proxy and IMAP/POP3 proxy server
# processname: nginx
#config: /etc/nginx/nginx.conf
#config: /etc/sysconfig/nginx
#pidfile: /var/run/nginx.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit0

nginx="/usr/local/nginx/sbin/nginx"
prog=$(name $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

[ -f /etc/sysconfig/nginx ] && ./etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx

start() {
[ -x $nginx] || exit 5
[ -f$NGINX_CONF_FILE ] || exit 6
echo -n$"Starting $prog: "
daemon$nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval-eq 0 ] && touch$lockfile
return$retval
}

stop() {
echo -n$"Stopping $prog: "
killproc$prog -QUIT
retval=$?
echo
[ $retval-eq 0 ] && rm -f$lockfile
return$retval
killall -9 nginx
}

restart() {
configtest|| return $?
stop
sleep1
start
}

reload() {
configtest|| return $?
echo -n$"Reloading $prog: "
killproc$nginx -HUP
RETVAL=$?
echo
}

force_reload() {
restart
}

configtest() {
$nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
status$prog
}

rh_status_q() {
rh_status>/dev/null2>&1
}

case "$1" in
start)
rh_status_q && exit0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0{start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac

##########################################################################
编辑nginx配置文件,整合nginx与php

说明:nginx默认页面路径已经更改为了/home/var/www;



location ~ /.php$ {
root /home/var/www;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param _FILENAME /s$fastcgi__name;
include fastcgi_params;
}
添加默认默认页面

index index.php index.htmlindex.htm;
编辑fastcgi_params文件,如果为空或不存在该文件则创建并添加如下内容,如果已有内容则忽略如下步骤

vim /usr/local/nginx/conf/fastcgi_params --如果为空把原有内容更改如下,不为空则忽略:
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param _FILENAME $document_root$fastcgi__name; //这一句如果默认配置文件里没有请手动添加进去,否则更改站点目录后PHP访问会出现not found
fastcgi_param _NAME $fastcgi__name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
更改NGINX默认站点目录

vim /usr/local/nginx/conf/nginx.conf (实际配置文件安装路径)注意替换下面的红色部位,具体NGINX优化安全设置,以后将单独写文章做设置,敬请期待

将其中的
location / {
root html;
index index.php index.html index.htm;
}
改为
location / {
root /home/var/www;
index index.php index.html index.htm;
}
然后再将
location ~ /.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param _FILENAME /$fastcgi__name;
include fastcgi_params;
}
改为
location ~ /.php$ {
root /home/var/www;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param _FILENAME $document_root$fastcgi__name;
include fastcgi_params;
}
然后重启nginx 即可


更改index.php页面内容

vim /home/var/www/index.php

<?php phpinfo(); ?>

http://127.0.0.1/ 浏览器打开 验证是否 整合NGINX+PHP-FPM 整合成功则出现 PHPINFO 页面信息。。。

安装MEMCACHE,和OPCACHE扩展



(因为php5.5X+版本已经内置了memcache和opcache扩展,所以不必另行下载,即可采用以下方式进行编译)

1.memcache

如果需要为php增加memcache扩展,按如下方法进行:

cd /usr/local/php/bin

./pecl install memcache



安装时会提示是否开启session支持,根据需要输入no 或者 yes,安装后路径如下:

/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/memcache.so



需要手动配置到php.ini中,增加如下内容,即表示启用memcache扩展:

extension=memcache.so

保存后,重启Apache,查看http://192.168.36.24/test.php进行验证。



2.Zend OPCache

为了提高php运行速度,可以启用Zend OPCache,启用方法很简单,安照上面的方法安装php,则已经在

/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/下生成了opcache.so,只需将其配置到php.ini中即可。

vim /usr/local/php/etc/php.ini

找到[opcache]

在其下面增加:

zend_extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/opcache.so

然后打开并配置相应的配置项即可,比如:

opcache.enable=1
opcache.enable_cli=1
opcache.memory_consumption=256
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=5000
opcache.revalidate_freq=60
opcache.load_comments=1

保存后,重启Apache,查看http://192.168.36.24/test.php进行验证。会看到如下信息:

with Zend OPcache v7.0.3-dev, Copyright (c) 1999-2013, by Zend Technologies




PHP安装中出现的常见问题及错误解决方法见下方:

出现如下错误php安装出错:configure: error: mcrypt.h not found. Please reinstall libmcrypt.,意思是,没有查找到mcrytp.h,需要安装libcrytp,在下面的地址下载libmarypt:
wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/attic/libmcrypt/libmcrypt-2.5.8.tar.gz

安装:
66 tar -zxvf libmcrypt-2.5.8.tar.gz
67 cd libmcrypt-2.5.7
67 ./configure
70 make
71 make install
然后再安装PHP

centos 6.2 64位版本上安装LNMP,configure php的时候出现下面错误而退出
configure: error: Cannot find ldap libraries in /usr/lib

解决办法:

cp -frp /usr/lib64/libldap* /usr/lib/
然后再./configure.. ...重新编译即可

error 1

checking for 2-config path...
configure: error: 2-config not found. Please check your lib2 installation.

(看提示就明白 是一个lib库没装 先用 yum search 名字 看是否能搜到名字 ,找到名字后 把软件包 开发包装上)

解决办法

yum install lib2-devel.x86_64

error 2

checking for pkg-config... /usr/bin/pkg-config
configure: error: Cannot find OpenSSL's <evp.h>

这是ssl没装

解决办法

yum install openssl.x86_64 openssl-devel.x86_64 -y

error 3

checking for BZip2 in default path... not found
configure: error: Please reinstall the BZip2 distribution

这是bzip2软件包没有安装

解决办法

yum install bzip2-devel.x86_64 -y

error 4

configure: error: Please reinstall the libcurl distribution -
easy.h should be in <curl-dir>/include/curl/

curl和curl库文件没有安装

解决办法

yum install libcurl.x86_64 libcurl-devel.x86_64 -y

error 5

checking whether to enable JIS-mapped Japanese font support in GD... no
checking for fabsf... yes
checking for floorf... yes
configure: error: jpeglib.h not found

GD库没有安装

解决办法

yum install libjpeg.x86_64 libpng.x86_64 freetype.x86_64 libjpeg-devel.x86_64 libpng-devel.x86_64 freetype-devel.x86_64 -y

error 6

checking for stdarg.h... (cached) yes
checking for mcrypt support... yes
configure: error: mcrypt.h not found. Please reinstall libmcrypt.

libmcrypt库没有安装 ,要是不能用yun安装的话 就要去下载个gz包 自己编译安装

(编译安装 ./configure make && make install)

error7、configure: error: Cannot find ldap.h
办法:
yum install php-ldap.x86_64 -y
yum install openldap-devel.x86_64 -y

error8、configure: error: Don’t know how to define struct flock on this system, set –enable-opcache=no
解决方案:(注:修改后记得执行 chmod gu+x 给 /etc/ld.so.conf.d/local.conf文件添加可执行权限,否则无效)
vim /etc/ld.so.conf.d/local.conf (local.conf可以随便起名称,以.conf为后缀就可)
添加
/usr/local/lib (如果是64位系统 路径请写为 /usr/local/lib)
:wq 保存
ldconfig -v 生效(或者ldconfig)

error9、error: Cannot find ldap libraries in /usr/lib 这个可能是由于libldap32和libldap64冲突:
解决方法:cp -frp /usr/lib64/libldap* /usr/lib/

锚点有关nginx的if(!-e)问题与解决方法 在配置URL重写时,启动NGINX出现如下错误提示:
[root@localhost nginx]# service nginx restart
nginx: [emerg] unknown directive "if(!-e" in /usr/local/nginx/conf/nginx.conf:48
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
一,nginx的url重写问题
vim /usr/local/nginx/conf/nginx.conf
if (!-e $request_filename){
//此处旋转url重写的规则
}
重起时,老师报如上红色部分错误。。
二,解决方法
原因是这样的:if和(之间必须有个空格。(小插曲,还好我有谷歌,百度就是坑,全市灌水SEO垃圾站,还是国外的网站靠谱,如果你遇到问题,尽量用谷歌翻译成英文到国外网站上去找答案,一找一个准~)

由于时间关系 PHP和NGINX安装后的详细配置部分暂时省略,后期添加,敬请期待

 

实例php-apache:

./configure --prefix=/usr/local/php \
--with-apxs2=/usr/local/apache/bin/apxs \
--with-config-file-path=/usr/local/php/etc \
--enable-pdo \
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-iconv-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-lib-dir \
--enable- \
--enable-opcache \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--with-curlwrappers \
--enable-mbregex \
--enable-mbstring \
--with-mcrypt \
--with-gd \
--with-freetype-dir \
--enable-gd-native-ttf \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-ldap \
--with-ldap-sasl \
--with-rpc \
--enable-zip \
--enable-soap \
--enable-session \
--with-gettext \
--enable-shared \
--enable-ftp \
--enable-exif \
--with-pear \
--with-bz2 \
--with-readline \

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值