以 FastCGI 模式安装 PHP5

2009-08-10  23:52  |  分类:应用技术  |  标签:  |  1,009 次浏览

软件下载:
PHP5(http://www.php.net/downloads.php):
wget http://cn.php.net/get/php-5.2.10.tar.bz2/from/this/mirror

php-fpm(http://php-fpm.org/Download):
wget http://php-fpm.org/downloads/php-5.2.10-fpm-0.5.13.diff.gz

安装依赖的包:
CentOS:
yum install mysql-devel gd-devel libxml2-devel

Debian:
apt-get install libmysqlclient15-dev libgd2-xpm-dev libxml2-dev

应用 php-fpm 补丁:
tar jxvf php-5.2.10.tar.bz2
gzip -cd php-5.2.10-fpm-0.5.13.diff.gz | patch -d php-5.2.10 -p1

安装 PHP5:

cd php-5.2.10
./configure \
--prefix=/usr/local/php-fcgi \
--enable-discard-path \
--enable-fastcgi \
--enable-fpm \
--enable-force-cgi-redirect \
--enable-mbstring \
--enable-sockets \
--with-config-file-path=/usr/local/php-fcgi/etc \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-zlib \
--with-mysql \
--with-mysqli

make
make install

cp -f php.ini-recommended /usr/local/php-fcgi/etc/php.ini


一些必备模块的安装:
安装 ZendOptimizer:
Zend 的优化器,免费使用,如果加密了 PHP 程序文件,这是必须的。
如果没有使用 Zend 优化器,PHP 进程的内存会难以释放。
下载: http://www.zend.com/en/products/guard/downloads
tar zxvf ZendOptimizer-3.3.3-linux-glibc23-i386.tar.gz
cd ZendOptimizer-3.3.3-linux-glibc23-i386
./install

安装 eAccelerator:
PHP scripts 的缓存,对于提高 PHP 程序执行速度很有帮助。
下载: http://eaccelerator.net/
tar jxvf eaccelerator-0.9.5.3.tar.bz2
cd eaccelerator-0.9.5.3
/usr/local/php-fcgi/bin/phpize
./configure --enable-eaccelerator=shared --with-php-config=/usr/local/php-fcgi/bin/php-config
make
make install

安装 memcache:
用于操作 Memcached ,应用非常广泛。
下载: http://pecl.php.net/package/memcache
tar zxvf memcache-2.2.5.tgz
cd memcache-2.2.5
/usr/local/php-fcgi/bin/phpize
./configure --with-php-config=/usr/local/php-fcgi/bin/php-config
make
make install

PHP5 的配置:
vi /usr/local/php-fcgi/etc/php.ini

# 开启短标记支持
short_open_tag = On
# 最大执行时间
max_execution_time = 5
# 最大使用内存
memory_limit = 8M
# 扩展模块的目录
extension_dir = "/usr/local/php-fcgi/lib/php/extensions/no-debug-non-zts-20060613"
# 关闭 mysql 持久连接。高访问量下会把数据库可用连接用完。
mysql.allow_persistent = Off
# 缩短连接超时时间
mysql.connect_timeout = 10
# session 文件存储目录
session.save_path = "/tmpfs"
# session 过期时间设为一小时
session.gc_maxlifetime = 3600
# 关闭 session.bug_compat_warn
session.bug_compat_warn = 0

# 开启 eAccelerator
[eAccelerator]
extension="eaccelerator.so"
# 可使用内存设为了 16M ,太大会启动不了 PHP ,以 CGI 模式运行的 PHP ,各进程间也不能共享内存, eAccelerator 的共享模式也就无效了。
eaccelerator.shm_size="16"
eaccelerator.cache_dir="/tmpfs"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="0"
eaccelerator.shm_prune_period="0"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"

# 在 eAccelerator 之后启用 ZendOptimizer ,避免 eaccelerator.so 不能加载。
[Zend]
zend_optimizer.optimization_level=1023
zend_extension_manager.optimizer=/usr/local/Zend/lib/Optimizer-3.3.3
zend_optimizer.version=3.3.3
zend_extension=/usr/local/Zend/lib/ZendExtensionManager.so

# 开启 memcache
[memcache]
extension=memcache.so

用 tmpfs 提升性能:
mkdir /tmpfs
在 /etc/fstab 中加入
tmpfs /tmpfs tmpfs size=256m,mode=1777 0 0

mount -a

定时清除:
vi /etc/crontab
0 * * * * root find /tmpfs -type f -amin +60 -exec rm {} \;

配置 php-fpm ,如:
vi /usr/local/php-fcgi/etc/php-fpm.conf

<?xml version="1.0" ?>
<configuration>
 
        All relative paths in this config are relative to php's install prefix
 
        
<section name="global_options">
 
                Pid file
                
<value name="pid_file">/usr/local/php-fcgi/logs/php-fpm.pid</value>
 
                Error log file
                
<value name="error_log">/usr/local/php-fcgi/logs/php-fpm.log</value>
 
                Log level
                
<value name="log_level">notice</value>
 
                When this amount of php processes exited with SIGSEGV or SIGBUS ...
                
<value name="emergency_restart_threshold">10</value>
 
                ... in a less than this interval of time, a graceful restart will be initiated.
                Useful to work around accidental curruptions in accelerator's shared memory.
                
<value name="emergency_restart_interval">1m</value>
 
                Time limit on waiting child's reaction on signals from master
                
<value name="process_control_timeout">5s</value>
 
                Set to 'no' to debug fpm
                
<value name="daemonize">yes</value>
 
        
</section>
 
        
<workers>
 
                
<section name="pool">
 
                        Name of pool. Used in logs and stats.
                        
<value name="name">default</value>
 
                        Address to accept fastcgi requests on.
                        Valid syntax is 'ip.ad.re.ss:port' or just 'port' or '/path/to/unix/socket'
                        
<value name="listen_address">127.0.0.1:9000</value>
 
                        
<value name="listen_options">
 
                                Set listen(2) backlog
                                
<value name="backlog">-1</value>
 
                                Set permissions for unix socket, if one used.
                                In Linux read/write permissions must be set in order to allow connections from web server.
                                Many BSD-derrived systems allow connections regardless of permissions.
                                
<value name="owner"></value>
                                
<value name="group"></value>
                                
<value name="mode">0666</value>
                        
</value>
 
                        Additional php.ini defines, specific to this pool of workers.
                        
<value name="php_defines">
                                
<value name="sendmail_path">/usr/sbin/sendmail -t -i</value>
                                
<value name="display_errors">0</value>
                        
</value>
 
                        Unix user of processes
                        
<value name="user">www-data</value>
 
                        Unix group of processes
                        
<value name="group">www-data</value>
 
                        Process manager settings
                        
<value name="pm">
 
                                Sets style of controling worker process count.
                                Valid values are 'static' and 'apache-like'
                                
<value name="style">static</value>
 
                                Sets the limit on the number of simultaneous requests that will be served.
                                Equivalent to Apache MaxClients directive.
                                Equivalent to PHP_FCGI_CHILDREN environment in original php.fcgi
                                Used with any pm_style.
                                
<value name="max_children">16</value>
 
                                Settings group for 'apache-like' pm style
                                
<value name="apache_like">
 
                                        Sets the number of server processes created on startup.
                                        Used only when 'apache-like' pm_style is selected
                                        
<value name="StartServers">2</value>
 
                                        Sets the desired minimum number of idle server processes.
                                        Used only when 'apache-like' pm_style is selected
                                        
<value name="MinSpareServers">4</value>
 
                                        Sets the desired maximum number of idle server processes.
                                        Used only when 'apache-like' pm_style is selected
                                        
<value name="MaxSpareServers">16</value>
 
                                
</value>
 
                        
</value>
 
                        Time limit on waiting execution of single request
                        Should be used when 'max_execution_time' ini option does not terminate execution for some reason
                        
<value name="request_execution_timeout">31s</value>
 
                        Set open file desc rlimit
                        
<value name="rlimit_files">65536</value>
 
                        Set max core size rlimit
                        
<value name="rlimit_core">0</value>
 
                        Chroot to this directory at the start
                        
<value name="chroot"></value>
 
                        Chdir to this directory at the start
                        
<value name="chdir"></value>
 
                        Redirect workers' stdout and stderr into main error log.
                        If not set, they will be redirected to /dev/null, according to FastCGI specs
                        
<value name="catch_workers_output">yes</value>
 
                        How much requests each process should execute before respawn.
                        Useful to work around memory leaks in 3rd party libraries.
                        For endless request processing please specify 0
                        Equivalent to PHP_FCGI_MAX_REQUESTS
                        
<value name="max_requests">65536</value>
 
                        Comma separated list of ipv4 addresses of FastCGI clients that allowed to connect.
                        Equivalent to FCGI_WEB_SERVER_ADDRS environment in original php.fcgi (5.2.2+)
                        Makes sense only with AF_INET listening socket.
                        
<value name="allowed_clients">127.0.0.1</value>
 
                        Pass environment variables like LD_LIBRARY_PATH
                        All $VARIABLEs are taken from current environment
                        
<value name="environment">
                                
<value name="HOSTNAME">$HOSTNAME</value>
                                
<value name="PATH">/usr/local/bin:/usr/bin:/bin</value>
                                
<value name="TMP">/tmp</value>
                                
<value name="TMPDIR">/tmp</value>
                                
<value name="TEMP">/tmp</value>
                                
<value name="OSTYPE">$OSTYPE</value>
                                
<value name="MACHTYPE">$MACHTYPE</value>
                                
<value name="MALLOC_CHECK_">2</value>
                        
</value>
 
                
</section>
 
        
</workers>
 
</configuration>

系统的相关设置:
echo -ne "
fs.file-max = 65536
" >> /etc/sysctl.conf
sysctl -p

echo -ne "
www-data soft nofile 65536
www-data hard nofile 65536
" >> /etc/security/limits.conf

启动 PHP-FCGI:
/usr/local/php-fcgi/sbin/php-fpm start

可写入 /etc/rc.local 文件开机自启动。

喜欢本文,那就收藏到: Del.icio.us Google书签 Digg Live Bookmark Technorati Furl Yahoo书签 Facebook 百度搜藏 新浪ViVi 365Key网摘 天极网摘 和讯网摘 博拉网 POCO网摘 添加到饭否 QQ书签 Digbuzz我挖网

1条评论 关于 “以 FastCGI 模式安装 PHP5”

  1. linuxpig 发表于: 五月 5th, 2010 9:53

    没怎么看明白。。

    回复


发表您的评论