=== 源码包下载 ===
wget http://www.monkey.org/~provos/libevent-1.2.tar.gz
wget http://www.danga.com/memcached/dist/memcached-1.2.0.tar.gz
wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-1.8.0-rc2.tgz
wget http://blog.s135.com/soft/linux/nginx_php/rpm/x86_64/libjpeg-devel-6b-33.x86_64.rpm
wget http://blog.s135.com/soft/linux/nginx_php/rpm/x86_64/freetype-devel-2.1.9-1.x86_64.rpm
wget http://blog.s135.com/soft/linux/nginx_php/rpm/x86_64/libpng-devel-1.2.7-1.x86_64.rpm
wget http://sysoev.ru/nginx/nginx-0.7.51.tar.gz
wget http://www.php.net/get/php-5.2.8.tar.gz/from/this/mirror
wget http://php-fpm.anight.org/downloads/head/php-5.2.8-fpm-0.5.10.diff.gz
wget http://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.9.tar.gz/from/http://mysql.stu.edu.tw/
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.13.tar.gz
wget "http://downloads.sourceforge.net/mcrypt/libmcrypt-2.5.8.tar.gz?modtime=1171868460&big_mirror=0"
wget "http://downloads.sourceforge.net/mcrypt/mcrypt-2.6.8.tar.gz?modtime=1194463373&big_mirror=0"
wget http://pecl.php.net/get/memcache-2.2.5.tgz
wget "http://downloads.sourceforge.net/mhash/mhash-0.9.9.9.tar.gz?modtime=1175740843&big_mirror=0"
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.8.tar.gz
wget http://bart.eaccelerator.net/source/0.9.5.3/eaccelerator-0.9.5.3.tar.bz2
wget http://pecl.php.net/get/PDO_MYSQL-1.0.2.tgz
wget http://blog.s135.com/soft/linux/nginx_php/imagick/ImageMagick.tar.gz
wget http://pecl.php.net/get/imagick-2.2.2.tgz
wget http://mirror.provenscaling.com/mysql/enterprise/source/5.0/mysql-5.0.70.tar.gz
wget http://pecl.php.net/get/memcache-2.2.6.tgz
wget http://pecl.php.net/get/mongo-1.0.8.tgz #php的mongodb模块
wget http://nchc.dl.sourceforge.net/project/phpmyadmin/phpMyAdmin/3.3.9.2/phpMyAdmin-3.3.9.2-all-languages.tar.gz
=== 安装相关依赖包 === yum -y install 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 === 安装MYSQL === 1)编译安装 tar -zxvf mysql-5.0.70.tar.gz cd mysql-5.0.70 ./configure --prefix=/usr/local/mysql/ --enable-assembler --with-extra-charsets=complex --enable-thread-safe-client --with-big-tables --with-mysqld-ldflags=-all-static --with-readline --with-ssl --with-embedded-server --enable-local-infile --with-plugins=partition,innobase,myisammrg,sphinx make && make install2)创建mysql数据库存放目录
mkdir -p /data/mysql/ chown -R mysql.mysql /data/mysql3)创建mysqld目录
mkdir -p /var/run/mysqld chown -R mysql:mysql /var/run/mysqld4)添加mysqld启动文件
cd /opt/mysql5 cp share/mysql/mysql.server /etc/rc.d/init.d/mysqld chmod 755 /etc/rc.d/init.d/mysqld5)修改/etc/my.cnf文件
vi /etc/my.cnf a、找到[mysqld],添加以下参数: datadir = /data/mysql //数据存放目录 basedir = /opt/mysql5 //mysql安装目录 pid-file=/var/run/mysqld/mysqld.pid //pid文件存放目录 skip-name-resolve //取消dns解析b、在文件末尾添加以下参数:
[mysqld_safe] log-error=/data/mysql/mysqld.log //设置错误日志存放路径6)以mysql用户帐号的身份建立数据库:
/opt/mysql5/bin/mysql_install_db --user=mysql chown -R mysql.mysql /data/mysql chmod -R 755 /data/mysql7)将mysql命令添加到环境变量中,添加以下参数
vi /etc/profile PATH=$PATH:/opt/mysql5/bin export PATH保存退出执行:source /etc/profile 即可。
7)启动mysql数据库
/opt/mysql5/bin/mysqld_safe --user=mysql & 或者 service mysqld start=== memcached 安装 ===
1、先安装libevent tar zxvf libevent-1.2.tar.gz cd libevent-1.2 ./configure --prefix=/usr make && make install 测试libevent是否安装成功: ls -al /usr/lib | grep libevent lrwxrwxrwx 1 root root 21 11?? 12 17:38 libevent-1.2.so.1 -> libevent-1.2.so.1.0.3 -rwxr-xr-x 1 root root 263546 11?? 12 17:38 libevent-1.2.so.1.0.3 -rw-r–r– 1 root root 454156 11?? 12 17:38 libevent.a -rwxr-xr-x 1 root root 811 11?? 12 17:38 libevent.la lrwxrwxrwx 1 root root 21 11?? 12 17:38 libevent.so -> libevent-1.2.so.1.0.3 以上信息表示安装成功。2、安装memcached,需指定libevent安装的位置
tar zxvf memcached-1.2.0.tar.gz cd memcached-1.2.0 ./configure --prefix=/usr/local/memcached --with-libevent=/usr make && make install3、启动memcached
/usr/local/memcached/bin/memcached -m 64 -p 11212 -u root -l 127.0.0.1 -d4、加入开机启动
echo "/usr/local/memcached/bin/memcached -m 64 -p 11212 -u root -l 127.0.0.1 -d" >> /etc/rc.local5、安装memcache php模块
tar vxzf memcache-2.2.6.tgz cd memcache-2.2.6 /usr/local/php/bin/phpize ./configure --prefix=/usr/local/memcached --with-php-config=/usr/local/php/bin/php-config make make install 安装完后会有类似这样的提示: Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613然后修改php的配置文件,php.ini
extension_dir = “/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/” extension=memcache.so 重启apache然后写个php的脚本测试一下:
<?php
$memcache = new Memcache; //创建一个memcache对象 $memcache->connect('localhost', 11211) or die ("Could not connect"); //连接Memcached服务器 $memcache->set('key', 'test'); //设置一个变量到内存中,名称是key 值是test $get_value = $memcache->get('key'); //从内存中取出key的值 echo $get_value; ?>=== Fast-cgi模式安装PHP ===
1、安装PHP所需的支持库 tar zxvf libiconv-1.13.tar.gz cd libiconv-1.13/ ./configure --prefix=/usr/local make && make installtar 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 installtar zxvf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9/ ./configure make && make installln -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.1tar zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8/ /sbin/ldconfig ./configure make && make install2、FastCGI模式安装PHP
tar zxvf php-5.2.8.tar.gz gzip -cd php-5.2.8-fpm-0.5.10.diff.gz | patch -d php-5.2.8 -p1 cd php-5.2.8/ ./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 make ZEND_EXTRA_LIBS='-liconv' make install cp php.ini-dist /usr/local/php/etc/php.ini3、安装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 installtar 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注:mongoDB服务端安装文档:
http://blog.163.com/leijie131421@126/blog/static/42411145201111135823275/
tar xf mongo-1.0.8.tgz
/usr/local/php/bin/phpize
./configure –with-php-config=/usr/local/php/bin/php-config
make && make install
安装后在php.ini文件中添加extension="mongo.so"
查看是否启用:php -m 如出现mongo即成功
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 installtar zxvf ImageMagick.tar.gz
cd ImageMagick-6.5.1-2/ ./configure make && make installtar 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 install4、修改php.ini文件
将extension_dir = "./" 修改为extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/" 将output_buffering = Off 修改为 On 在文章末尾添加扩展模块 extension = "memcache.so" extension = "pdo_mysql.so" extension = "imagick.so"5、配置eAccelerator加速PHP
mkdir -p /usr/local/eaccelerator_cache 在/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="128" 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="300" eaccelerator.shm_prune_period="120" eaccelerator.shm_only="0" eaccelerator.compress="1" eaccelerator.compress_level="9"修改配置文件 vi /etc/sysctl.conf
kernel.shmmax = 134217728 让配置生效:/sbin/sysctl -p添加www用户
groupadd www useradd -g www -s /sbin/nologin www6、创建php-fpm配置文件(php-fpm是为PHP打的一个FastCGI管理补丁,可以平滑变更php.ini配置而无需重启php-cgi)
rm -f /usr/local/php/etc/php-fpm.conf vi /usr/local/php/etc/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/php/logs/php-fpm.pid</value> Error log file <value name="error_log">/usr/local/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">51200</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">500</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> 7、启动php-cgi进程,监听127.0.0.1的9000端口,进程数为200(如果服务器内存小于3GB,可以只开启64个进程),用户为www ulimit -SHn 51200 /usr/local/php/sbin/php-fpm start === 安装Nginx === 1、安装Nginx所需的pcre库: tar zxvf pcre-7.8.tar.gz cd pcre-7.8/ ./configure make && make install2、安装Nginx
tar zxvf nginx-0.7.51.tar.gz cd nginx-0.7.51/ ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module make && make install3、修改Nginx配置文件vi /usr/local/nginx/conf/nginx.conf
user www www worker_processes 8; error_log logs/error.log; pid logs/nginx.pid; worker_rlimit_nofile 512000; events { use epoll; worker_connections 512000; } location ~ \.php$ { proxy_pass http://127.0.0.1; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fcgi.conf; } 4、在/usr/local/webserver/nginx/conf/目录中创建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;5、配置nginx.conf,启动Nginx
加入如下参数
http {
.....
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 128k;........
server {
listen 80; server_name test150; root /www/test; location / { index index.html index.htm index.php; } location ~ .*\.(php|php5)?$ { #fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fcgi.conf; }}
,....
}
保存退出。
ulimit -SHn 51200 /usr/local/webserver/nginx/sbin/nginx6、将Nginx加入开机启动
ulimit -SHn 51200 /usr/local/webserver/php/sbin/php-fpm start /usr/local/webserver/nginx/sbin/nginx7、优化Linux内核参数 vi /etc/sysctl.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 -p8、修改Nginx配置文件后,在不停止Nginx服务的情况下平滑变更Nginx配置
测试配置文件是否存在错误:/usr/local/webserver/nginx/sbin/nginx -t 杀掉pid文件配置配置立即生效: kill -HUP `cat /usr/local/nginx/logs/nginx.pid`9、编写每天定时切割Nginx日志的脚本
#!/bin/bash logs_path="/usr/local/nginx/logs/" mkdir -p ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/ mv ${logs_path}access.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/access_$(date -d "yesterday" +"%Y%m%d").log kill -USR1 `cat /usr/local/nginx/nginx.pid`10、设置crontab,每天凌晨00:00切割nginx访问日志
crontab -e 输入:00 00 * * * /bin/bash /usr/local/webserver/nginx/sbin/cut_nginx_log.sh=== 安装Zend ===
tar zxvf ZendOptimizer-3.3.9-linux-glibc23-i386.tar.gz cd ZendOptimizer-3.3.9-linux-glibc23-i386 这里要注意,进入data文件夹后,so文件是对应版本的,看好系统中的php版本再安装,我安装的是对应5.2版本PHP的。 把 ZendOptimizer.so 文件拷贝到 /usr/local/Zend/lib zend_optimizer.optimization_level=15 zend_extension="/usr/local/Zend/lib/ZendOptimizer.so" 重启php-fpm /usr/local/webserver/php/sbin/php-fpm restart