- 由于本人才疏学浅,对问题认知难免有偏差,本着学习与共享的精神和大家一起探讨,若有不对之处,望大家多多批评指正。
- 1、编译安装PHP 5.2.14所需的支持库:
- tar zxvf libiconv-1.13.1.tar.gz
- cd libiconv-1.13.1/
- ./configure --prefix=/usr/local
- make
- make install
- cd ../
- 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 ../../
- 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
- tar zxvf mcrypt-2.6.8.tar.gz
- cd mcrypt-2.6.8/
- /sbin/ldconfig
- ./configure
- make
- make install
- cd ../
- 关于/etc/ld.so.conf:
- 这个文件记录了编译时使用的动态链接库的路径。
- 默认情况下,编译器只会使用/lib和/usr/lib这两个目录下的库文件
- 将自己可能存放库文件的路径都加入到/etc/ld.so.conf中是明智的选择
- 添加方法也极其简单,将库文件的绝对路径直接写进去就OK了,一行一个。例如:
- /usr/X11R6/lib
- /usr/local/lib
- /opt/lib
- ldconfig是什么?
- 它是一个程序,通常它位于/sbin下,供root用户使用。它的作用就是将/etc/ld.so.conf列出的路径下的库文件 缓存到/etc/ld.so.cache 以供使用,因此当安装完一些库文件,或者修改ld.so.conf增加新的库路径后,需要运行一下/sbin/ldconfig,使所有的库文件都被缓存到ld.so.cache中,如果没做,即使库文件明明就在/usr/lib下的,也是不会被使用的,结果编译过程中报错,缺少xxx库。 切记改动库文件后一定要运行一下ldconfig,在任何目录下运行都可以。
- 2、编译安装MySQL 5.5.3-m3
- /usr/sbin/groupadd mysql
- /usr/sbin/useradd -g mysql mysql
- tar zxvf mysql-5.5.3-m3.tar.gz
- cd mysql-5.5.3-m3/
- ./configure --prefix=/home/pubsrv/mysql/ --enable-assembler --with-extra-charsets=complex --enable-thread-safe-client --with-big-tables --with-readline --with-ssl --with-embedded-server --enable-local-infile --with-plugins=partition,innobase,myisammrg
- make && make install
- chmod +w /home/pubsrv/mysql
- chown -R mysql:mysql /home/pubsrv/mysql/
- cd ../
- ①、创建MySQL数据库存放目录
- mkdir -p /home/pubsrv/mysql/data/
- mkdir -p /home/pubsrv/mysql/logs/binlog/
- mkdir -p /home/pubsrv/mysql/logs/relaylog/
- chown -R mysql:mysql /home/pubsrv/mysql/
- ②、以mysql用户帐号的身份建立数据表:
- /home/pubsrv/mysql/bin/mysql_install_db --basedir=/home/pubsrv/mysql --datadir=/home/pubsrv/mysql/data --user=mysql
- ③、创建my.cnf配置文件:
- vi /home/pubsrv/mysql/my.cnf
- 输入以下内容:
- [client]
- #character-set-server = utf8
- port = 3306
- socket = /tmp/mysql.sock
- [mysqld]
- #character-set-server = utf8
- replicate-ignore-db = mysql
- replicate-ignore-db = test
- replicate-ignore-db = information_schema
- user = mysql
- port = 3306
- socket = /tmp/mysql.sock
- basedir = /home/pubsrv/mysql
- datadir = /home/pubsrv/mysql/data
- log-error = /home/pubsrv/mysql/logs/mysql_error.log
- pid-file = /home/pubsrv/mysql/mysql.pid
- open_files_limit = 10240
- back_log = 600
- max_connections = 5000
- max_connect_errors = 6000
- table_cache = 614
- external-locking = FALSE
- max_allowed_packet = 32M
- sort_buffer_size = 1M
- join_buffer_size = 1M
- thread_cache_size = 300
- #thread_concurrency = 8
- query_cache_size = 512M
- query_cache_limit = 2M
- query_cache_min_res_unit = 2k
- default-storage-engine = MyISAM
- thread_stack = 192K
- transaction_isolation = READ-COMMITTED
- tmp_table_size = 246M
- max_heap_table_size = 246M
- long_query_time = 3
- log-slave-updates
- log-bin = /home/pubsrv/mysql/logs/binlog
- binlog_cache_size = 4M
- binlog_format = MIXED
- max_binlog_cache_size = 8M
- max_binlog_size = 1G
- relay-log-index = /home/pubsrv/mysql/logs/relaylog
- relay-log-info-file = /home/pubsrv/mysql/logs/relaylog
- relay-log = /home/pubsrv/mysql/logs/relaylog
- expire_logs_days = 30
- key_buffer_size = 256M
- read_buffer_size = 1M
- read_rnd_buffer_size = 16M
- bulk_insert_buffer_size = 64M
- myisam_sort_buffer_size = 128M
- myisam_max_sort_file_size = 10G
- myisam_repair_threads = 1
- myisam_recover
- interactive_timeout = 120
- wait_timeout = 120
- skip-name-resolve
- #master-connect-retry = 10
- slave-skip-errors = 1032,1062,126,1114,1146,1048,1396
- #master-host = 192.168.1.2
- #master-user = username
- #master-password = password
- #master-port = 3306
- server-id = 1
- innodb_additional_mem_pool_size = 16M
- innodb_buffer_pool_size = 512M
- innodb_data_file_path = ibdata1:256M:autoextend
- innodb_file_io_threads = 4
- innodb_thread_concurrency = 8
- innodb_flush_log_at_trx_commit = 2
- innodb_log_buffer_size = 16M
- innodb_log_file_size = 128M
- innodb_log_files_in_group = 3
- innodb_max_dirty_pages_pct = 90
- innodb_lock_wait_timeout = 120
- innodb_file_per_table = 0
- #log-slow-queries = /home/pubsrv/mysql/logs/slow.log
- #long_query_time = 10
- [mysqldump]
- quick
- max_allowed_packet = 32M
- ④、创建管理MySQL数据库的shell脚本:
- vi /home/pubsrv/mysql/mysql
- #!/bin/sh
- mysql_port=3306
- mysql_username="admin"
- mysql_password="12345678"
- function_start_mysql()
- {
- printf "Starting MySQL...\n"
- /bin/sh /home/pubsrv/mysql/bin/mysqld_safe --defaults-file=/home/pubsrv/mysql/etc/my.cnf 2>&1 > /dev/null &
- }
- function_stop_mysql()
- {
- printf "Stoping MySQL...\n"
- /home/pubsrv/mysql/bin/mysqladmin -u ${mysql_username} -p${mysql_password} -S /tmp/mysql.sock shutdown
- }
- function_restart_mysql()
- {
- printf "Restarting MySQL...\n"
- function_stop_mysql
- sleep 5
- function_start_mysql
- }
- function_kill_mysql()
- {
- kill -9 $(ps -ef | grep 'bin/mysqld_safe' | grep ${mysql_port} | awk '{printf $2}')
- kill -9 $(ps -ef | grep 'libexec/mysqld' | grep ${mysql_port} | awk '{printf $2}')
- }
- if [ "$1" = "start" ]; then
- function_start_mysql
- elif [ "$1" = "stop" ]; then
- function_stop_mysql
- elif [ "$1" = "restart" ]; then
- function_restart_mysql
- elif [ "$1" = "kill" ]; then
- function_kill_mysql
- else
- printf "Usage: /data0/mysql/${mysql_port}/mysql {start|stop|restart|kill}\n"
- fi
- ⑤、赋予shell脚本可执行权限:
- chmod +x /home/pubsrv/mysql/mysql
- ⑥、启动MySQL:
- /home/pubsrv/mysql/mysql start
- ⑦、通过命令行登录管理MySQL服务器(提示输入密码时直接回车):
- /home/pubsrv/mysql/mysql/bin/mysql -u root -p -S /tmp/mysql.sock
- ⑧、输入以下SQL语句,创建一个具有root权限的用户(admin)和密码(12345678):
- GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost' IDENTIFIED BY '12345678';
- GRANT ALL PRIVILEGES ON *.* TO 'admin'@'127.0.0.1' IDENTIFIED BY '12345678';
- ⑨、(可选)停止MySQL:
- /home/pubsrv/mysql/mysql stop
- 3、编译安装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=/home/pubsrv/php --with-config-file-path=/home/pubsrv/php/etc --with-mysql=/home/pubsrv/mysql --with-mysqli=/home/pubsrv/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
- make ZEND_EXTRA_LIBS='-liconv'
- make install
- cp php.ini-dist /home/pubsrv/php/etc/php.ini
- cd ../
- 4、编译安装PHP5扩展模块
- tar zxvf memcache-2.2.5.tgz
- cd memcache-2.2.5/
- /home/pubsrv/php/bin/phpize
- ./configure --with-php-config=/home/pubsrv/php/bin/php-config
- make
- make install
- cd ../
- tar jxvf eaccelerator-0.9.6.1.tar.bz2
- cd eaccelerator-0.9.6.1/
- /home/pubsrv/php/bin/phpize
- ./configure --enable-eaccelerator=shared --with-php-config=/home/pubsrv/php/bin/php-config
- make
- make install
- cd ../
- tar zxvf PDO_MYSQL-1.0.2.tgz
- cd PDO_MYSQL-1.0.2/
- /home/pubsrv/php/bin/phpize
- ./configure --with-php-config=/home/pubsrv/php/bin/php-config --with-pdo-mysql=/home/pubsrv/mysql
- make
- make install
- cd ../
- tar zxvf ImageMagick.tar.gz
- cd ImageMagick-6.5.1-2/
- ./configure
- make
- make install
- cd ../
- tar zxvf imagick-2.3.0.tgz
- cd imagick-2.3.0/
- /home/pubsrv/php/bin/phpize
- ./configure --with-php-config=/home/pubsrv/php/bin/php-config
- make
- make install
- cd ../
- 5、修改php.ini文件
- 手工修改:查找/home/pubsrv/php/etc/php.ini中的extension_dir = "./"
- 修改为extension_dir = "/home/pubsrv/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
- 修改为cgi.fix_pathinfo=0,防止Nginx文件类型错误解析漏洞。
- 自动修改:若嫌手工修改麻烦,可执行以下shell命令,自动完成对php.ini文件的修改:
- sed -i 's#extension_dir = "./"#extension_dir = "/home/pubsrv/php/lib/php/extensions/no-debug-non-zts-20060613/"\nextension = "memcache.so"\nextension = "pdo_mysql.so"\nextension = "imagick.so"\n#' /home/pubsrv/php/etc/php.ini
- sed -i 's#output_buffering = Off#output_buffering = On#' /home/pubsrv/php/etc/php.ini
- sed -i "s#; always_populate_raw_post_data = On#always_populate_raw_post_data = On#g" /home/pubsrv/php/etc/php.ini
- sed -i "s#; cgi.fix_pathinfo=0#cgi.fix_pathinfo=0#g" /home/pubsrv/php/etc/php.ini
- 6、配置eAccelerator加速PHP:
- mkdir -p /home/pubsrv/eaccelerator_cache
- vi /home/pubsrv/php/etc/php.ini
- 按shift+g键跳到配置文件的最末尾,加上以下配置信息:
- [eaccelerator]
- zend_extension="/home/pubsrv/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"
- eaccelerator.shm_size="64"
- eaccelerator.cache_dir="/home/pubsrv/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"
- 7、创建www用户和组,以及供blog.test.com和www.test.com两个虚拟主机使用的目录:
- /usr/sbin/groupadd www
- /usr/sbin/useradd -g www www
- mkdir -p /data0/htdocs/blog
- chmod +w /data0/htdocs/blog
- chown -R www:www /data0/htdocs/blog
- mkdir -p /data0/htdocs/www
- chmod +w /data0/htdocs/www
- chown -R www:www /data0/htdocs/www
- 8、创建php-fpm配置文件(php-fpm是为PHP打的一个FastCGI管理补丁,可以平滑变更php.ini配置而无需重启php-cgi):
- 在/home/pubsrv/php/etc/目录中创建php-fpm.conf文件:
- rm -f /home/pubsrv/php/etc/php-fpm.conf
- vi /home/pubsrv/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">/home/pubsrv/php/logs/php-fpm.pid</value>
- Error log file
- <value name="error_log">/home/pubsrv/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">0</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">1024</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>
- 9、启动php-cgi进程,监听127.0.0.1的9000端口,进程数为128(如果服务器内存小于3GB,可以只开启64个进程),用户为www:
- ulimit -SHn 65535
- /usr/local/webserver/php/sbin/php-fpm start
- 注:/usr/local/webserver/php/sbin/php-fpm还有其他参数,包括:start|stop|quit|restart|reload|logrotate,修改php.ini后不重启php-cgi,重新加载配置文件使用reload。
- nginx的安装请参阅附件