此文多处从网络博文里借鉴,自己整合如下
系统平台:RHEL 5.4(32位系统)2.6.18-406.el5
yum安装相关函数库,方便快捷,节省时间,避免得不偿失。233333
gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-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
下载安装包
cd /usr/local/src
wget http://blog.s135.com/soft/linux/nginx_php/nginx/nginx-0.8.46.tar.gz \
wget http://blog.s135.com/soft/linux/nginx_php/php/php-5.2.14.tar.gz \
wget http://blog.s135.com/soft/linux/nginx_php/phpfpm/php-5.2.14-fpm-0.5.14.diff.gz \
wget http://blog.s135.com/soft/linux/nginx_php/mysql/mysql-5.5.3-m3.tar.gz \
wget http://blog.s135.com/soft/linux/nginx_php/libiconv/libiconv-1.13.1.tar.gz \
wget http://blog.s135.com/soft/linux/nginx_php/mcrypt/libmcrypt-2.5.8.tar.gz \
wget http://blog.s135.com/soft/linux/nginx_php/mcrypt/mcrypt-2.6.8.tar.gz \
wget http://blog.s135.com/soft/linux/nginx_php/memcache/memcache-2.2.5.tgz \
wget http://blog.s135.com/soft/linux/nginx_php/mhash/mhash-0.9.9.9.tar.gz \
wget http://blog.s135.com/soft/linux/nginx_php/pcre/pcre-8.10.tar.gz \
wget http://blog.s135.com/soft/linux/nginx_php/eaccelerator/eaccelerator-0.9.6.1.tar.bz2 \
wget http://blog.s135.com/soft/linux/nginx_php/pdo/PDO_MYSQL-1.0.2.tgz \
wget http://blog.s135.com/soft/linux/nginx_php/imagick/ImageMagick.tar.gz \
wget http://blog.s135.com/soft/linux/nginx_php/imagick/imagick-2.3.0.tgz
1.安装libiconv
对文本进行编码间的转换,用它来处理中文各种编码之间的转换。
#tar zxvf libiconv-1.13.1.tar.gz
#cd libiconv-1.13.1/
#./configure --prefix=/usr/local
#make
#make install
cd ../
2.安装libmcrypt 实现加密功能的库。
# tar zxvf libmcrypt-2.5.8.tar.gz
# cd libmcrypt-2.5.8/
# ./configure
# make
# make install
# /sbin/ldconfig
# 注:这里不要退出去了。
# cd libltdl/
# ./configure --enable-ltdl-install
# make
# make install
# cd ../
3. 安装mhash(哈稀函数库)
# tar zxvf mhash-0.9.9.9.tar.gz
# cd mhash-0.9.9.9/
# ./configure
# make
# make install
# cd ../
ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la && ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so && ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4 && ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8 && ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a && ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la && ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so && ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2 && ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1 && ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config
4. 安装mcrypt
# tar zxvf mcrypt-2.6.8.tar.gz
# cd mcrypt-2.6.8/
# /sbin/ldconfig
#./configure
# make
# make install
# cd ../
安装mysql
groupadd mysql
useradd -g mysql mysql
mkdir -p /usr/local/mysql
chown mysql:mysql -R /usr/local/mysql
tar zxvf mysql-5.5.3-m3.tar.gz
cd mysql-5.5.3-m3
./configure --prefix=/usr/local/mysql --without-debug --enable-thread-safe-client --with-pthread --enable-assembler --enable-profiling --with-mysqld-ldflags=-all-static --with-client-ldflags=-all-static --with-extra-charsets=all --with-plugins=all --with-mysqld-user=mysql --without-embedded-server --with-server-suffix=-community --with-unix-socket-path=/tmp/mysql.sock
make && make install
#初始化数据库
/usr/local/mysql/bin/mysql_install_db --user=mysql
将MySQL添加到系统服务
cp ./support-files/mysql.server /etc/init.d/mysql
chmod 755 /etc/init.d/mysql
chkconfig --add mysql
chkconfig mysql on
#启动数据库
service mysql start or /usr/local/mysql/bin/mysqld_safe --user=mysql &
#修改 mysql 登录密码
/usr/local/mysql/bin/mysqladmin -uroot password "hello"
#用 mysql 登录
/usr/local/mysql/bin/mysql -uroot -p
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'hello';
FLUSH PRIVILEGES;
#准备 mysql 配置文件
cp ./support-files/my-medium.cnf /etc/my.cnf
#设置连接字符集添加如下两行
vi /etc/my.cnf
[client]
default-character-set=utf8
#重启 mysql 服务让字符集生效
pkill mysql or /usr/local/mysql/bin/mysqladmin shutdown #关闭数据库
/usr/local/mysql/bin/mysqld_safe --user=mysql &
ln -s /usr/local/mysql/lib/mysql/libmysqlclient.so.16 /usr/lib
建立软连接,不然编译php会报错
5. 编译安装 PHP( FastCGI 模式)
tar zxvf php-5.2.14.tar.gz
gzip -cd php-5.2.14-fpm-0.5.14.diff.gz | patch -d php-5.2.14 -p1
cd php-5.2.14/
./configure --prefix=/usr/local/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=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib \
--with-libxml-dir=/usr --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode \
--enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl \
--with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect \
--enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash \
--enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc --enable-zip \
--enable-soap
注:Nginx+PHP整合,在安装时必须启用-–enable-fastcgi和--enable-fpm
make ZEND_EXTRA_LIBS='-liconv'
make install
cp php.ini-dist /usr/local/php/etc/php.ini
cd ../
6.准备编译安装 PHP5 扩展模块
tar zxvf memcache-2.2.5.tgz
cd memcache-2.2.5/
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install
说明:memcache库的位置Installing shared extensions:/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/
cd ../
安装eaccelerator php加速
tar jxvf eaccelerator-0.9.6.1.tar.bz2
cd eaccelerator-0.9.6.1/
/usr/local/php/bin/phpize
./configure --enable-eaccelerator=shared --with-php-config=/usr/local/php/bin/php-config
make && make install
cd ../
安装PDO_MYSQL(数据库连接的支持)
tar zxvf PDO_MYSQL-1.0.2.tgz
cd PDO_MYSQL-1.0.2/
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --with-pdo-mysql=/usr/local/mysql
make && make install
cd ../
安装ImageMagick是Linux下非常强大的图象处理函数与GD类似
tar zxvf ImageMagick.tar.gz
cd ImageMagick-6.5.1-2/
./configure
make
make install
cd ../
安装imagick(连接PHP和ImageMagick的通道)
tar zxvf imagick-2.3.0.tgz
cd imagick-2.3.0/
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install
cd ../
7. 修改 php.ini 文件,让 php 模块生效
vi /usr/local/php/etc/php.ini
查找
extension_dir = "./"
修改为
extension_dir="/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"
并在此行后增加以下几行,然后保存:
extension = "memcache.so"
extension = "pdo_mysql.so"
extension = "imagick.so"
再查找output_buffering = Off
修改为output_buffering = On
去掉注释cgi.fix_pathinfo=0,防止 Nginx 文件类型错误解析漏洞
8.配置 eaccelerator 加速 PHP
mkdir -p /usr/local/eaccelerator_cache
#准备 eaccelerator 缓存目录
vi /usr/local/php/etc/php.ini
到配置文件的最末尾,粘上以下内容:
[eaccelerator]
zend_extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"
eaccelerator.shm_size="64"
eaccelerator.cache_dir="/usr/local/eaccelerator_cache"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="3600"
eaccelerator.shm_prune_period="3600"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"
9.准备 php-cgi 和 nginx 进程执行者用户
groupadd nginx
useradd -g nginx nginx
10. 创建 php-fpm 配置文件- php-fpm.conf
最好备份下/usr/local/php/etc/php-fpm.conf
建立新的配置内容如下
vi /usr/local/php/etc/php-fpm.conf
<?xml version="1.0" ?>
<configuration>
<section name="global_options">
<value name="pid_file">/usr/local/php/logs/php-fpm.pid</value>
<value name="error_log">/usr/local/php/logs/php-fpm.log</value>
<value name="log_level">notice</value>
<value name="emergency_restart_threshold">10</value>
<value name="emergency_restart_interval">1m</value>
<value name="process_control_timeout">5s</value>
<value name="daemonize">yes</value>
</section>
<workers>
<section name="pool">
<value name="name">default</value>
<value name="listen_address">127.0.0.1:9000</value>
<value name="listen_options">
<value name="backlog">-1</value>
<value name="owner"></value>
<value name="group"></value>
<value name="mode">0666</value>
</value>
<value name="php_defines">
<value name="sendmail_path">/usr/sbin/sendmail -t -i</value>
<value name="display_errors">1</value>
</value>
<value name="user">nginx</value>
<value name="group">nginx</value>
<value name="pm">
<value name="style">static</value>
<value name="max_children">128</value>
<value name="apache_like">
<value name="StartServers">20</value>
<value name="MinSpareServers">5</value>
<value name="MaxSpareServers">35</value>
</value>
</value>
<value name="request_terminate_timeout">0s</value>
<value name="request_slowlog_timeout">0s</value>
<value name="slowlog">logs/slow.log</value>
<value name="rlimit_files">51200</value>
<value name="rlimit_core">0</value>
<value name="chroot"></value>
<value name="chdir"></value>
<value name="catch_workers_output">yes</value>
<value name="max_requests">500</value>
<value name="allowed_clients">127.0.0.1</value>
<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>
说明
<value name="display_errors">0</value>
#0 改成 1,页面上会输出错误日志
Unix user of processes
<value name="user">nginx</value>
Unix group of processes
<value name="group">nginx</value>
<value name="max_children">128</value>
#最大子进程数 128,如果内存小于 2G,则 64 个最佳
<value name="rlimit_files">65535</value>
# Set open file desc rlimit,同时打开的文件数,linux 系统允许同时打开的文件数为 1024,修改
linux 系统中允许同时打开的文件,ulimit -SHn 65535,而且这个参数重启后还能生效,加到
/etc/profile 全局配置文件的最后,开机就会生效,ulimit -a 查看 open files 65535
<value name="max_requests">1024</value>
#最大请求数, How much requests each process should execute before respawn.一个子进程能够
回应 1042 个请求
11. 启动 php-cgi(fastcgi)进程,监听 127.0.0.1 的 9000 端口,进程数为 128(如果服务器内
存小于 3GB,可以只开启 64 个进程),用户为 nginx:
/usr/local/php/sbin/php-fpm start