5月 26th, 2008 | |
Posted in Nginx < by Michael Field >
所有源代码安装打包下载地址:nginx_php_source.zip
二进制包下载地址:nginx-php-fullbin-centos4(直接解压在linux根目录下, 所有完成编译后的文件在/nginx目录中)
一、安装PHP 5.2.5(FastCGI模式)
1、编译安装PHP 5.2.5所需的支持库:(libiconv/freetype2/libpng/jpeg-6b/libxml/libmcrypt/gb/Zend/)
tar zxvf libiconv-1.11.tar.gz
cd libiconv-1.11/
./configure –prefix=/nginx/libiconv
make
make install
cd ../
tar zxvf freetype-2.3.5.tar.gz
cd freetype-2.3.5/
./configure –prefix=/nginx/freetype2
make
make install
cd ../
tar zxvf libpng-1.2.20.tar.gz
cd libpng-1.2.20/
./configure –prefix=/nginx/libpng
make
make install
cd ../
tar zxvf jpegsrc.v6b.tar.gz
cd jpeg-6b/
./configure –enable-static –enable-shared –prefix=/nginx/jpeg-6b
make
make install
make install-lib
cd ../
tar zxvf libxml2-2.6.30.tar.gz
cd libxml2-2.6.30/
./configure –prefix=/nginx/libxml
make
make install
cd ../
tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8/
./configure –prefix=/nginx/libmcrypt
make
make install
/sbin/ldconfig
cd libltdl/
./configure –enable-ltdl-install –prefix=/nginx/libmcrypt/libltdl
make
make install
cd ../../
tar zxvf mhash-0.9.9.tar.gz
cd mhash-0.9.9/
./configure –prefix=/nginx/mhash
make
make install
cd ../
cp /nginx/libmcrypt.* /usr/lib
ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
tar zxvf mcrypt-2.6.6.tar.gz
cd mcrypt-2.6.6/
./configure –prefix=/nginx/mcrypt
make
make install
cd ../
tar zxvf gd-2.0.35.tar.gz
cd gd-2.0.35
./configure –prefix=/nginx/gd2 –with-jpeg=/nginx/jpeg-6b –with-png=/nginx/libpng –with-zlib=/nginx/zlib –with-freetype=/nginx/freetype2
make
make install
cd ../
2、mysql5.1安装
tar zxvf mysql-5.1.23-rc-linux-i686-glibc23.tar.gz
mv mysql-5.1.23-rc-linux-i686-glibc23 /nginx/mysql
cd /nginx/mysql
groupadd mysql
useradd -g mysql mysql -s /sbin/nologin
chown mysql:root . -R
chown mysql:mysql data -R
scripts/mysql_install_db –user=mysql
cp support-files/my-medium.cnf /etc/my.cnf
bin/mysqld_safe –user=mysql &
cp /nginx/mysql/lib/libmysqlclient* /usr/lib/
以上支持库安装完毕后
3、编译安装PHP5.2.5
tar zxvf php-5.2.5.tar.gz
cd php-5.2.5
./configure –prefix=/nginx/php –with-config-file-path=/nginx/php/etc –with-mysql=/nginx/mysql/ –with-iconv-dir=/nginx/libiconv –with-freetype-dir=/nginx/freetype2 –with-jpeg-dir=/nginx/jpeg-6b –with-png-dir=/nginx/libpng –with-zlib-dir=/nginx/zlib –with-libxml-dir=/nginx/libxml –with-gd=/nginx/gd2 –enable-xml –disable-debug –disable-rpath –enable-discard-path –enable-bcmath –enable-shmop –enable-sysvsem –enable-inline-optimization –with-curl –with-curlwrappers –enable-mbregex –enable-fastcgi –enable-force-cgi-redirect –enable-mbstring –with-mcrypt=/nginx/libmcrypt –enable-zend-multibyte –enable-sockets –enable-maintainer-zts –enable-inline-optimization
make
make install
cp php.ini-dist /nginx/php/etc/php.ini
二、安装PHP扩展模块
[memcache]
tar zxvf memcache-2.2.1.tgz
cd memcache-2.2.1/
/nginx/php/bin/phpize
./configure –with-php-config=/nginx/php/bin/php-config –prefix=/nginx/memcache/ –with-zlib-dir=/nginx/zlib/
make
make install
cd ../
[ZendOptimizer]
tar zxvf ZendOptimizer-3.3.0a-linux-glibc21-i386.tar.gz
cd ZendOptimizer-3.3.0a-linux-glibc21-i386
./install.sh
文字图形界面-同意安装协议-选择Zend安装目录-选择php.ini所在目录-不使用apache web server-installing-finished
修改php.ini
vi /nginx/php/etc/php.ini
修改如下:
extension_dir = “/nginx/php/lib/php/extensions/no-debug-zts-20060613″
并增加一条记录:
extension=”memcache.so”
保存退出
三、增加用户nginx和组/发布主目录/创建nginx日志目录
groupadd nginx -g 88
useradd -u 88 -g nginx nginx -s /sbin/nologin
mkdir /nginx/htdocs
chmod +w /nginx/htdocs/
chown nginx:nginx /nginx/htdocs/ -R
mkdir /var/log/nginx
chown nginx:nginx /var/log/nginx -R
chmod +w /var/log/nginx
四、设定fast-cgi
将lighttpd编译后的spawn-fcgi,复制到php/bin目录中,用以启动php-cgi。
tar zxvf lighttpd-1.4.13.tar.gz
cd lighttpd-1.4.13
./configure –prefix=/opt/lighttpd/
make
make install
cp /opt/lighttpd/bin/spawn-fcgi /nginx/php/bin
chmod +x /nginx/php/bin/spawn-fcgi
启动php-cgi进程,监听127.0.0.1的10080端口,进程数为64(如果服务器内存小于4GB,最好只开启25个进程),用户为nginx:
/nginx/php/bin/spawn-fcgi -a 127.0.0.1 -p 10080 -C 64 -u nginx -f /nginx/php/bin/php-cgi
注:本次安装启动后发生如下问题:(这里总结一个问题的发生原因;)
spawn-fcgi.c.215: child signaled: 11
(启动时发生故障,结果为mamcache的php模块路径设定出错,导致php工作异常,无法启动php-cgi)
修正php.ini中的extensions_dirs参数后故障修复;
注:安装成功后php -m模块组显示如下:
[PHP Modules]
bcmath
ctype
curl
date
dom
filter
gd
hash
iconv
json
libxml
mbstring
mcrypt
memcache
mysql
pcre
PDO
pdo_sqlite
posix
Reflection
session
shmop
SimpleXML
sockets
SPL
SQLite
standard
sysvsem
tokenizer
xml
xmlreader
xmlwriter
Zend Optimizer
zlib
[Zend Modules]
Zend Extension Manager
Zend Optimizer
五、安装和设定Nginx 0.6.31
tar zxvf nginx-0.6.31.tar.gz
cd nginx-0.6.31
./configure –user=nginx –group=nginx –prefix=/nginx –with-http_stub_status_module
make
make install
创建nginx配置文件(实例bbs站配置,供参考)
cp /nginx/conf/nginx.conf /nginx/conf/nginx.conf.bak
vi /nginx/conf/nginx.conf
user? nginx nginx;
worker_processes 10;
error_log? /var/log/nginx/nginx_error.log? crit;
pid??????? /var/log/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;
events
{
????? use epoll;
????? worker_connections 51200;
}
http
{
????? include?????? mime.types;
????? default_type? application/octet-stream;
????? charset? gb2312;?????
????? server_names_hash_bucket_size 128;
?????
????? #sendfile on;
????? #tcp_nopush???? on;
????? keepalive_timeout 60;
????? tcp_nodelay on;
????? gzip on;
????? gzip_min_length? 1k;
????? gzip_buffers???? 4 8k;
????? gzip_http_version 1.1;
????? gzip_types?????? text/plain application/x-javascript text/css text/html application/xml;
????? server
????? {
????????????? listen?????? 80;
????????????? server_name? bbs.xxxxx.com;
????????????? index index.html index.htm index.php;
????????????? root? /nginx/htdocs/new/;
????????????? error_page 404 http://www.xxxxx.com/error.html;
????????????? error_page 500 http://www.xxxxx.com/error.html;
????????????? if (-d $request_filename)
????????????? {
?????????????????? rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
????????????? }
????????????????????????????
????????????? location ~ .*\.php?$
????????????? {
?????????????????? include fast-cgi.conf;?????
?????????????????? fastcgi_pass? 127.0.0.1:10080;
?????????????????? fastcgi_index index.php;
????????????? }
#????????????? location /log/ #alias设定
#????????????? {
#????????????????? alias /var/log/nginx/;
#????????????????? index err.html;
#????????????? }
????????????? log_format? access? '$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/nginx_access.log access;
????? }
}
设定fast-cgi.conf可参考nginx的conf目录下的fastcgi_params文档
vi /nginx/conf/fast-cgi.conf
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? SCRIPT_FILENAME??? $document_root$fastcgi_script_name;
fastcgi_param? SCRIPT_NAME??????? $fastcgi_script_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;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
# fastcgi_param? REDIRECT_STATUS??? 200;
六、设定自动启动脚本及系统优化
将以下语句写入开机自动启动脚本rc.local中:(内存4G以下的请设定php-cgi进程数25 就是以下的-C参数)
ulimit -SHn 51200
/nginx/php/bin/spawn-fcgi -a 127.0.0.1 -p 10080 -C 64 -u nobody -f /nginx/php/bin/php-cgi #启动fast-cgi
/nginx/sbin/nginx -c /nginx/conf/nginx.conf #启动nginx
相关内核参数优化写入/etc/sysctrl.conf
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.ip_local_port_range = 5000??? 65000
执行 /sbin/sysctl -p生效
阅读内文