现在的位置: 首页Nginx>正文
CentOS 5.5下安装mysql5.1.72+php5.2.17(FastCGI)+nginx1.0.1高性能Web服务器 [原创]
2011年05月08日 Nginx 评论数 13 ⁄ 被围观 40,296 次+

CentOS 5.5下安装mysql5.1.72+php5.2.17(FastCGI)+nginx1.0.1高性能Web服务器 [原创]

由于生产环境都是FreeBSD平台,之前也写了一篇FreeBSD下安装mysql5.1.72+php5.2.17(FastCGI)+nginx1.0.1高性能Web服务器,有童鞋想要帮忙写一篇关于CentOS下的安装教程,其实网上也有很多,但是好多都不是太完整,或多或少的出现些问题,所以就写了一篇 CentOS 5.5下安装mysql5.1.72+php5.2.17(FastCGI)+nginx1.0.1高性能Web服务器

在高并发连接的情况下,Nginx是Apache服务器不错的替代品。Nginx同时也可以作为7层负载均衡服务器来使用。根据我的测试结果,Nginx 1.2.7 + PHP 5.2.17 (FastCGI) 可以承受3万以上的并发连接数,相当于同等环境下Apache的10倍。

第一步:安装系统环境需要的软件

  1. yum install -y 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-libs krb5-devel krb5-server libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers

第二步:编译安装PHP所需要的库

  1. mkdir -p /data/soft
  2. mkdir -p /data/src
  3. cd /data/src
  4. wget http://lcmp.googlecode.com/files/libiconv-1.13.1.tar.gz
  5. tar zxvf libiconv-1.13.1.tar.gz
  6. cd libiconv-1.13.1
  7. ./configure --prefix=/usr/local
  8. make && make install
  9. cd ..
  10. wget http://sourceforge.net/projects/mcrypt/files/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz/download
  11. tar zxvf libmcrypt-2.5.8.tar.gz
  12. cd libmcrypt-2.5.8/
  13. ./configure
  14. make && make install
  15. /sbin/ldconfig
  16. cd libltdl/
  17. ./configure --enable-ltdl-install
  18. make && make install
  19. cd ../../
  20. wget http://sourceforge.net/projects/mhash/files/mhash/0.9.9.9/mhash-0.9.9.9.tar.bz2/download
  21. tar jxvf mhash-0.9.9.9.tar.bz2
  22. cd mhash-0.9.9.9
  23. ./configure
  24. make && make install
  25. echo "/usr/local/lib" >>/etc/ld.so.conf
  26. /sbin/ldconfig
  27. cd ..
  28. wget http://vps.googlecode.com/files/mcrypt-2.6.8.tar.gz
  29. tar zxvf mcrypt-2.6.8.tar.gz
  30. cd mcrypt-2.6.8
  31. /sbin/ldconfig
  32. ./configure
  33. make && make install

第三步:编译安装mysql

  1. cd /data/src
  2. wget http://dev.mysql.com/get/Downloads/MySQL-5.1/mysql-5.1.72.tar.gz/from/http://cdn.mysql.com/
  3. tar zxvf mysql-5.1.72.tar.gz
  4. cd mysql-5.1.72
  5. /usr/sbin/groupadd mysql
  6. /usr/sbin/useradd -g mysql mysql
  7. ./configure --prefix=/data/soft/mysql --enable-assembler --with-extra-charsets=complex --enable-thread-safe-client --with-big-tables --with-readline --with-ssl --with-embedded-server --enable-local-infile
  8. make && make install
  9. chown -R mysql:mysql /data/soft/mysql
  10. chmod u+w /data/soft/mysql
  11. mkdir -p /data/database
  12. chown -R mysql:mysql /data/database

以mysql用户帐号的身份建立数据表:

  1. /data/soft/mysql/bin/mysql_install_db --basedir=/data/soft/mysql --datadir=/data/database --user=mysql

创建mysql配置文件

  1. rm -rf /data/soft/mysql/my.cnf
  2. vi /data/soft/mysql/my.cnf

如下:

  1. [client]
  2. port = 3306
  3. socket = /tmp/mysql.sock
  4. [mysql]
  5. no-auto-rehash
  6. [mysqld]
  7. user = mysql
  8. port = 3306
  9. socket = /tmp/mysql.sock
  10. basedir = /data/soft/mysql
  11. datadir = /data/database
  12. skip-name-resolve
  13. open_files_limit = 600
  14. back_log = 20
  15. max_connections = 1000
  16. max_connect_errors = 200
  17. table_cache = 60
  18. external-locking = FALSE
  19. max_allowed_packet = 16M
  20. sort_buffer_size = 128K
  21. join_buffer_size = 128K
  22. thread_cache_size = 10
  23. thread_concurrency = 8
  24. query_cache_size = 2M
  25. query_cache_limit = 2M
  26. query_cache_min_res_unit = 2k
  27. default_table_type = MyISAM
  28. thread_stack = 192K
  29. transaction_isolation = READ-UNCOMMITTED
  30. tmp_table_size = 512K
  31. max_heap_table_size = 32M
  32. long_query_time = 1
  33. log_long_format
  34. server-id = 1
  35. #log-bin = /data/soft/mysql/binlog
  36. binlog_cache_size = 2M
  37. max_binlog_cache_size = 4M
  38. max_binlog_size = 512M
  39. expire_logs_days = 7
  40. key_buffer_size = 4M
  41. read_buffer_size = 1M
  42. read_rnd_buffer_size = 2M
  43. bulk_insert_buffer_size = 2M
  44. myisam_sort_buffer_size = 4M
  45. myisam_max_sort_file_size = 10G
  46. myisam_max_extra_sort_file_size = 10G
  47. myisam_repair_threads = 1
  48. myisam_recover
  49. [mysqldump]
  50. quick
  51. max_allowed_packet = 16M

修改mysqld脚本

  1. cp /data/soft/mysql/share/mysql/mysql.server /etc/init.d/mysqld
  2. vi /etc/init.d/mysqld

修改

  1. basedir=/data/soft/mysql
  2. datadir=/data/database
  3. ln -s /data/soft/mysql/my.cnf /etc/my.cnf

启动mysql

  1. service mysqld start
  2. lsof -i:3306
  3. netstat -ntupl |grep 3306

测试登录mysql

  1. /data/soft/mysql/bin/mysql -u root -p
  2. mysql> createdatabase hello;
  3. chkconfig --add mysqld
  4. chkconfig mysqld on
  5. chkconfig --list |grep mysqld
  6. service mysqld restart

第四步:编译安装PHP(FastCGI)

1、安装php-5.2.17

  1. cd /data/src
  2. wget http://cn2.php.net/get/php-5.2.17.tar.gz/from/this/mirror
  3. wget http://php-fpm.org/downloads/php-5.2.17-fpm-0.5.14.diff.gz
  4. wget http://www.92csz.com/downloads/php-5.2.17-max-input-vars.patch
  5. tar zxvf php-5.2.17.tar.gz
  6. gzip -cd php-5.2.17-fpm-0.5.14.diff.gz | patch -d php-5.2.17 -p1
  7. cd php-5.2.17
  8. patch -p1 < ../php-5.2.17-max-input-vars.patch
  9. sed -i "s/\!png_check_sig (sig, 8)/png_sig_cmp (sig, 0, 8)/" ext/gd/libgd/gd_png.c
  10. cp ../fopen_wrappers.c main/ 此处参考FreeBSD下php修改源代码解决nginx虚拟主机防webshell跨目录
  11. ./configure --prefix=/data/soft/php --with-config-file-path=/data/soft/php/etc --with-mysql=/data/soft/mysql --with-mysqli=/data/soft/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
  12. make ZEND_EXTRA_LIBS='-liconv'
  13. make install
  14. cp php.ini-dist /data/soft/php/etc/php.ini
  15. cd ..

2、编译安装PHP5扩展模块

  1. wget http://pecl.php.net/get/memcache-2.2.6.tgz
  2. tar zxvf memcache-2.2.6.tgz
  3. cd memcache-2.2.6/
  4. /data/soft/php/bin/phpize
  5. ./configure --with-php-config=/data/soft/php/bin/php-config
  6. make && make install
  7. cd ..
  8. wget http://lcmp.googlecode.com/files/eaccelerator-0.9.6.1.tar.bz2
  9. tar jxvf eaccelerator-0.9.6.1.tar.bz2
  10. cd eaccelerator-0.9.6.1/
  11. /data/soft/php/bin/phpize
  12. ./configure --enable-eaccelerator=shared --with-php-config=/data/soft/php/bin/php-config --without-eaccelerator-use-inode
  13. make && make install
  14. cd ..
  15. wget http://pecl.php.net/get/PDO_MYSQL-1.0.2.tgz
  16. tar zxvf PDO_MYSQL-1.0.2.tgz
  17. cd PDO_MYSQL-1.0.2
  18. /data/soft/php/bin/phpize
  19. ./configure --with-php-config=/data/soft/php/bin/php-config --with-pdo-mysql=/data/soft/mysql
  20. make && make install
  21. cd ..
  22. wget http://launchpadlibrarian.net/73608965/ImageMagick-6.7.0-8.tar.gz
  23. tar zxvf ImageMagick-6.7.0-8.tar.gz
  24. cd ImageMagick-6.7.0-8
  25. ./configure
  26. make && make install
  27. cd ..
  28. wget http://pecl.php.net/get/imagick-3.0.1.tgz
  29. tar zxvf imagick-3.0.1.tgz
  30. cd imagick-3.0.1
  31. /data/soft/php/bin/phpize
  32. ./configure --with-php-config=/data/soft/php/bin/php-config
  33. make && make install
  34. cd ..

3、修改php.ini文件

  1. sed -i "s/output_buffering = Off/output_buffering = On/" /data/soft/php/etc/php.ini
  2. sed -i "s/extension_dir =/; extension_dir =/" /data/soft/php/etc/php.ini
  3. sed -i "s/; cgi.fix_pathinfo=0/cgi.fix_pathinfo=0/" /data/soft/php/etc/php.ini
  4. sed -i 's%;open_basedir =%open_basedir ="/tmp/:/data/www/"%' /data/soft/php/etc/php.ini
  5. sed -i "s/disable_functions =/disable_functions = popen,pentl_exec,passthru,exec,system,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,pfsockopen,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,show_source,escapeshellcmd,escapeshellarg,curl_exec,curl_multi_exec,parse_ini_file,assert/" /data/soft/php/etc/php.ini
  6. sed -i "s/expose_php = On/expose_php = Off/" /data/soft/php/etc/php.ini
  7. sed -i "s/display_errors = On/display_errors = Off/" /data/soft/php/etc/php.ini
  8. sed -i "s/log_errors = Off/log_errors = On/" /data/soft/php/etc/php.ini

在php.ini最后添加如下:

  1. extension_dir = "/data/soft/php/lib/php/extensions/no-debug-non-zts-20060613/"
  2. extension = "memcache.so"
  3. extension = "pdo_mysql.so"
  4. extension = "imagick.so"
  5. [eaccelerator]
  6. zend_extension="/data/soft/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"
  7. eaccelerator.shm_size="1"
  8. eaccelerator.cache_dir="/data/soft/eaccelerator_cache"
  9. eaccelerator.enable="1"
  10. eaccelerator.optimizer="1"
  11. eaccelerator.check_mtime="1"
  12. eaccelerator.debug="0"
  13. eaccelerator.filter=""
  14. eaccelerator.shm_max="0"
  15. eaccelerator.shm_ttl="3600"
  16. eaccelerator.shm_prune_period="3600"
  17. eaccelerator.shm_only="0"
  18. eaccelerator.compress="1"
  19. eaccelerator.compress_level="9"
  20. eaccelerator.keys = "disk_only"
  21. eaccelerator.sessions = "disk_only"
  22. eaccelerator.content = "disk_only"

4、创建eAccelerator缓存目录

  1. mkdir -p /data/soft/eaccelerator_cache

5、创建php-fpm.conf文件

  1. rm /data/soft/php/etc/