上次根据张晏发表的文章和根据我管理的服务器实际情况写了一个一键搭建PHP+NGINX+MYSQL高性能服务器脚步,现在对该脚本加于说明,方便读者理解。
【转载请注明出处 开源爱好者:伍波露 20091123 http://wubolu.iteye.com/admin/blogs/523603】
实验环境:CentOS 5.4_64_bit
说明:安装所需的程序库。
编译安装PHP 5.2.10所需的支持库(FastCGI模式)
说明:每一个Web请求PHP都必须重新解析php.ini、重新载入全部dll扩展并重初始化全部数据结构。使用FastCGI,所有这些都只在进程启动时发生一次。一个额外的好处是,持续数据库连接(Persistent database connection)可以工作。
FastCGI 是一个可伸缩、高速的在web server和脚本语言间通迅的接口
tar zxvf libiconv-1.13.tar.gz
cd libiconv-1.13/
./configure --prefix=/usr/local
make && make install
cd ../
#GNU的libiconv主要是解决多语言编码处理转换等应用问题。
cd /root/nginx
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 ../
#libmcrypt是加密算法扩展库。支持DES, 3DES, RIJNDAEL, Twofish, IDEA, GOST, CAST-256, ARCFOUR, SERPENT, SAFER+等算法。
cd /root/nginx
tar zxvf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9/
./configure
make && make install
cd ../
#本函数库支援多种杂凑演算法,如最出名的MD5、SHA1或GOST,还有其它多种的杂凑演算法;还要编译mhash程式库,之后才能编译PHP程式,在编译PHP程式时,记得要加--with-mhash选项打开系统的mhash功能。
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
#/usr/lib作用:常用的动态链接库和软件包的配置文件。
cd /root/nginx
tar zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8/
/sbin/ldconfig
./configure
make && make install
cd ../
#mcrypt也是一个功能强大的加密算法扩展库。
安装MySQL
/usr/sbin/useradd -g mysql mysql
tar zxvf mysql-5.4.0-beta.tar.gz
cd mysql-5.4.0-beta
CFLAGS="-O6 -mpentiumpro -fomit-frame-pointer" CXX=gcc CXXFLAGS="-O6 -mpentiumpro -fomit-frame-pointer -felide-constructors -fno-exceptions -fno-rtti"
./configure --prefix=/usr/local/mysql --localstatedir=/usr/local/mysql/var/ --with-server-suffix=-enterprise-gpl --without-debug --with-big-tables --with-extra-charsets=latin1,gb2312,big5,utf8,GBK --with-extra-charsets=all --with-pthread --enable-static --enable-thread-safe-client --with-client-ldflags=-all-static --with-mysqld-ldflags=-all-static --enable-assembler --without-innodb --without-ndb-debug --without-isam --with-federated-storage-engine
make && make install
cp support-files/my-medium.cnf /usr/local/mysql/my.cnf
/usr/local/mysql/bin/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
PHP安装
tar zxvf php-5.3.0.tar.gz
gzip -cd php-5.3.0-fpm-0.5.12-rc.diff.gz | patch -d php-5.3.0 -p1
cd php-5.3.0/
./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 --without-pear --with-zlib --enable-pdo --with-pdo-mysql --with-mysql
#mysqli扩展技术不仅可以调用MySQL的存储过程、处理MySQL事务,而且还可以使访问数据库工作变得更加稳定。
make ZEND_EXTRA_LIBS='-liconv'
make install
cd ../
rm -f /usr/local/php/etc/php.ini
cp php.ini /usr/local/php/etc/php.ini
安装PHP扩展模块
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
cd ../
说明:memcache是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个统一的巨大的hash表,它能够用来存储各种格式的数据,包括图像、视频、文件以及数据库检索的结果等。Memcache是danga.com的一个项目,最早是为 LiveJournal 服务的,最初为了加速 LiveJournal 访问速度而开发的,后来被很多大型的网站采用。目前全世界不少人使用这个缓存项目来构建自己大负载的网站,来分担数据库的压力。起初作者编写它可能是为了提高动态网页应用,为了减轻数据库检索的压力,来做的这个缓存系统。它的缓存是一种分布式的,也就是可以允许不同主机上的多个用户同时访问这个缓存系统, 这种方法不仅解决了共享内存只能是单机的弊端,同时也解决了数据库检索的压力,最大的优点是提高了访问获取数据的速度!基于memcache作者对分布式cache的理解和解决方案。 memcache完全可以用到其他地方 比如分布式数据库, 分布式计算等领域。
cd /root/nginx
tar xzvf libevent-1.4.11-stable.tar.gz
cd libevent-1.4.11-stable
./configure && make && make install
ln -s /usr/local/lib/libevent-1.4.so.2 /usr/lib
cd ../
说明:libevent是一个异步事件处理软件函式库,以BSD许可证释出。
libevent提供了一组应用程序编程接口(API),让程式设计师可以设定某些事件发生时所执行的函式,也就是说,libevent可以用来取代网络服务器所使用的循环检查架构
由于可以省去对网络的处理,且拥有不错的效能,有些软件使用libevent作为网络底层的函式库,如:memcached、Tor 。
cd /root/nginx
tar xzvf memcached-1.2.8.tar.gz
cd memcached-1.2.8
./configure --prefix=/usr/local/memcached --with-libevent=/usr/
make && make install
ln -s /usr/local/memcached/bin/memcached /etc/init.d/memcached
cd ../
说明:memcached是由Danga Interactive开发的,高性能的,分布式的内存对象缓存系统,用于在动态应用中减少数据库负载,提升访问速度。
cd /root/nginx
tar jxvf eaccelerator-0.9.5.3.tar.bz2
cd eaccelerator-0.9.5.3/
/usr/local/php/bin/phpize
./configure --enable-eaccelerator=shared --with-php-config=/usr/local/php/bin/php-config
make && make install
cd ../
说明:eaccelerator是一个自由开放源码php加速器,优化和动态内容缓存,提高了性能php脚本的缓存性能,使得PHP脚本在编译的状态下,对服务器的开销几乎完全消除。 它还有对脚本起优化作用,以加快其执行效率。使您的PHP程序代码执效率能提高1-10倍。
cd /root/nginx
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 ../
cd /root/nginx
tar zxvf ImageMagick.tar.gz
cd ImageMagick-6.5.1-2/
./configure
make && make install
cd ../
cd /root/nginx
tar zxvf imagick-2.2.2.tgz
cd imagick-2.2.2/
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install
cd ../
cp php.ini /usr/local/php/etc/php.ini
说明:就是把修改好的php.ini替换上去,修改内容:
/usr/local/webserver/php/etc/php.ini中的extension_dir = "./"
修改为extension_dir = "/usr/local/webserver/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
并在文件最后添加以下配置信息:
[eaccelerator] zend_extension="/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so" eaccelerator.shm_size="64" eaccelerator.cache_dir="/usr/local/webserver/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" |
/usr/sbin/groupadd www
/usr/sbin/useradd -g www www
mkdir -p /usr/local/www/www
chmod +w /usr/local/www/www
chown -R www:www /usr/local/www/www
说明:创建虚拟主机目录。
rm -f /usr/local/php/etc/php-fpm.conf
rm -f /etc/sysctl.conf
cp php-fpm.conf /usr/local/php/etc/php-fpm.conf
说明:php-fpm是为PHP打的一个FastCGI管理补丁,可以平滑变更php.ini配置而无需重启php-cgi。
php-fpm.conf内容如下:(如果安装 Nginx + PHP 用于程序调试,将以下的<value name="display_errors">0</value>改为<value name="display_errors">1</value>,以便显示PHP错误信息,否则,Nginx 会报状态为500的空白错误页)
<?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/webserver/php/logs/php-fpm.pid</value> Error log file <value name="error_log">/usr/local/webserver/php/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">1</value> </value> Unix user of processes <value name="user">www</value> Unix group of processes <value name="group">www</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">128</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">20</value> Sets the desired minimum number of idle server processes. Used only when 'apache-like' pm_style is selected <value name="MinSpareServers">5</value> Sets the desired maximum number of idle server processes. Used only when 'apache-like' pm_style is selected <value name="MaxSpareServers">35</value> </value> </value> The timeout (in seconds) for serving a single request after which the worker process will be terminated Should be used when 'max_execution_time' ini option does not stop script execution for some reason '0s' means 'off' <value name="request_terminate_timeout">0s</value> The timeout (in seconds) for serving of single request after which a php backtrace will be dumped to slow.log file '0s' means 'off' <value name="request_slowlog_timeout">0s</value> The log file for slow requests <value name="slowlog">logs/slow.log</value> Set open file desc rlimit <value name="rlimit_files">65535</value> Set max core size rlimit <value name="rlimit_core">0</value> Chroot to this directory at the start, absolute path <value name="chroot"></value> Chdir to this directory at the start, absolute path <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">102400</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> |
cp sysctl.conf /etc/
说明:sysctl.conf内容如下
net.ipv4.tcp_max_syn_backlog = 65536 net.core.netdev_max_backlog = 32768 net.core.somaxconn = 32768 net.core.wmem_default = 8388608 net.core.rmem_default = 8388608 net.core.rmem_max = 16777216 net.core.wmem_max = 16777216 net.ipv4.tcp_timestamps = 0 net.ipv4.tcp_synack_retries = 2 net.ipv4.tcp_syn_retries = 2 net.ipv4.tcp_tw_recycle = 1 #net.ipv4.tcp_tw_len = 1 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_mem = 94500000 915000000 927000000 net.ipv4.tcp_max_orphans = 3276800 #net.ipv4.tcp_fin_timeout = 30 #net.ipv4.tcp_keepalive_time = 120 net.ipv4.ip_local_port_range = 1024 65535 |
/sbin/sysctl -p
说明:使配置马上生效。
/etc/init.d/memcached -d -m 512 -u root -l 127.0.0.1 -p 11211
说明:-d选项是启动一个守护进程,-m是分配给memcache使用的内存数量,单位是MB,-u是运行Memcache的用户,-l是监听的服务器IP地址,-p是设置Memcache监听的端口,最好是1024以上的端口,-c选项是最大运行的并发连接数,默认是1024,按照服务器的负载量来设定。
ulimit -SHn 51200
说明:软硬限制,同一时间最多可开启的文件数。
/usr/local/php/sbin/php-fpm start
说明:启动PHP。
tar zxvf pcre-7.8.tar.gz
cd pcre-7.8/
./configure
make && make install
cd ../
说明:安装nginx所需的pcre库。
cd /root/nginx
tar zxvf nginx-0.7.61.tar.gz
cd nginx-0.7.61/
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
cd ../
说明:安装nginx,参数说明待续。
rm -f /usr/local/nginx/conf/nginx.conf
rm -f /usr/local/nginx/conf/fcgi.conf
cp nginx.conf /usr/local/nginx/conf/nginx.conf
cp fcgi.conf /usr/local/nginx/conf/fcgi.conf
说明:更改配置文件,关于nginx的配置文件博客里有详细介绍。创建fcgi.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; |
ulimit -SHn 51200
/usr/local/nginx/sbin/nginx
mkdir -p /home/sh
cp cut_nginx_log.sh /home/sh/cut_nginx_log.sh
cd /root/nginx
tar zxvf phpMyAdmin-3.2.0-all-languages.tar.gz
mv phpMyAdmin-3.2.0-all-languages /usr/local/www/www/mysql123