Centos6.3 編譯源碼搭建 PHP(fast-cgi)+Nginx+Mysql+Memcached+Redis高性能服務器

操作系統:Centos6.3
Mysql版本:5.6.16
PHP版本:5.5.9
Nginx版本:1.2.6
Redis版本:2.8.9
Memcached版本:1.4.5

安裝依賴庫

yum -y groupinstall "Development tools"

yum install -y gcc gcc-c++  make cmake zlib zlib-devel openssh openssl openssl-devel net-snmp pcre pcre-devel  libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel xinetd lrzsz dos2unix telnet python-devel mysql-devel libxslt-devel unixODBC-devel sqlite-devel

一.編譯安裝Mysql

    傳送門:http://blog.csdn.net/bronze_boy/article/details/29323811


二.編譯安裝Nginx

1. 檢查安裝依賴庫

2. 添加用戶和用戶組    

  groupadd -r nginx
  useradd -r -g nginx nginx

3. 解壓,編譯

  tar -zxf nginx-1.2.6.tar.gz
  cd nginx-1.2.6

./configure --prefix=/home/nginx --sbin-path=/home/nginx/sbin/nginx --conf-path=/home/nginx/nginx.conf --error-log-path=/home/nginx/log/error.log --http-log-path=/home/nginx/log/access.log --pid-path=/home/nginx/nginx.pid --lock-path=/home/nginx/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/home/nginx/client --http-proxy-temp-path=/home/nginx/proxy --http-fastcgi-temp-path=/home/nginx/fcgi --http-uwsgi-temp-path=/home/nginx/uwsgi --http-scgi-temp-path=/home/nginx/scgi  --with-pcre=/home/packages/pcre-8.35

    注意pcre=的是pcre的源碼包路徑,不是安裝路徑  

  make && make install

4. 爲nginx提供sysV init 腳本    

  vi /etc/rc.d/init.d/nginx

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig:   - 85 15
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /home/nginx/nginx.conf
# config:      /home/nginx/sysconfig
# pidfile:     /home/nginx/run/nginx.pid
 
# Source function library.
. /etc/rc.d/init.d/functions
 
# Source networking configuration.
. /etc/sysconfig/network
 
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
 
nginx="/home/nginx/sbin/nginx"
prog=$(basename $nginx)
 
NGINX_CONF_FILE="/home/nginx/nginx.conf"
 
[ -f /home/nginx/sysconfig ] && . /home/nginx/sysconfig
 
lockfile=/home/nginx
 
make_dirs() {
  # make required directories
  user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
  options=`$nginx -V 2>&1 | grep 'configure arguments:'`
  for opt in $options; do
      if [ `echo $opt | grep '.*-temp-path'` ]; then
          value=`echo $opt | cut -d "=" -f 2`
          if [ ! -d "$value" ]; then
              # echo "creating" $value
              mkdir -p $value && chown -R $user $value
          fi
      fi
  done
}
 
start() {
   [ -x $nginx ] || exit 5
   [ -f $NGINX_CONF_FILE ] || exit 6
   make_dirs
   echo -n $"Starting $prog: "
   daemon $nginx -c $NGINX_CONF_FILE
   retval=$?
   echo
   [ $retval -eq 0 ] && touch $lockfile
   return $retval
}
 
stop() {
   echo -n $"Stopping $prog: "
   killproc $prog -QUIT
   retval=$?
   echo
   [ $retval -eq 0 ] && rm -f $lockfile
   return $retval
}
 
restart() {
   configtest || return $?
   stop
   sleep 1
   start
}
 
reload() {
   configtest || return $?
   echo -n $"Reloading $prog: "
   killproc $nginx -HUP
   RETVAL=$?
   echo
}
 
force_reload() {
   restart
}
 
configtest() {
 $nginx -t -c $NGINX_CONF_FILE
}
 
rh_status() {
   status $prog
}
 
rh_status_q() {
   rh_status >/dev/null 2>&1
}
 
case "$1" in
   start)
       rh_status_q && exit 0
       $1
       ;;
   stop)
       rh_status_q || exit 0
       $1
       ;;
   restart|configtest)
       $1
       ;;
   reload)
       rh_status_q || exit 7
       $1
       ;;
   force-reload)
       force_reload
       ;;
   status)
       rh_status
       ;;
   condrestart|try-restart)
       rh_status_q || exit 0
           ;;
   *)
       echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
       exit 2
esac

5. 修改權限,啓動服務

  chmod u+x /etc/rc.d/init.d/nginx
  chkconfig --add nginx
  chkconfig nginx on
  service nginx start   啓動nginx

6. 錯誤信息

chown: missing operand after `/home/nginx/client'
Try `chown --help' for more information.
chown: missing operand after `/home/nginx/proxy'
Try `chown --help' for more information.
chown: missing operand after `/home/nginx/fcgi'
Try `chown --help' for more information.
chown: missing operand after `/home/nginx/uwsgi'
Try `chown --help' for more information.
chown: missing operand after `/home/nginx/scgi'
Try `chown --help' for more information.
Starting nginx: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
                                                           [FAILED]

 解決辦法:

   更改目錄權限:chown -R nginx:nginx /home/nginx

   之前安裝過apache佔用了80端口,vi nginx.conf 修改端口爲8999

   ok. 再次啓動  

    

  service nginx start
  Starting nginx:                                            [  OK  ]

三. 編譯安裝 memcached

    安裝依賴關係   

  yum install libevent libevent-devel -y
  tar -zxf memcached-1.4.5.tar.gz
  cd memcached-1.4.5
  ./configure --prefix=/home/memcached
  make && make install

    配置環境變量  

  vi etc/profile
  PATH=/home/memcached/bin:$PATH
  export PATH

    激活環境變量 

  source /etc/profile

    開啓memcached        

  memcached -d -m 10 -u root -p 11211 -c 1024 -P /home/memcached/memcached.pid

    上面的參數意思是 啓動一個守護進程  分配了10M存儲,用root開啓,端口11211 最大同時併發鏈接1024個

    設置開機啓動       

  echo "memcached -d -m 10 -u root -p 11211 -c 1024 -P /home/memcached/memcached.pid" >> /etc/rc.loacl

    關閉memcached : 

  kill cat /home/memcached/memcached.pid

四. 編譯安裝PHP

1. 安裝libmcrypt

  tar -zxf libmcrypt-2.5.8.tar.gz 
  cd libmcrypt-2.5.8
  ./configure --prefix=/home/rely/libmcrypt
  make && make install

2. 編譯php

./configure --prefix=/home/php --with-config-file-path=/home/php/etc --with-mysql=/home/mysql --with-mysqli=/home/mysql/bin/mysql_config --with-mysql-sock=/home/mysql/mysql.sock --with-gd --with-iconv  --with-zlib  --enable-xml --enable-magic-quotes --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curlwrappers --enable-mbregex  --enable-fpm --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --enable-session --with-mcrypt --with-curl --with-jpeg-dir --with-freetype-dir --with-mcrypt=/home/rely/libmcrypt/

    編譯通過,但是報警告:

  configure: WARNING: unrecognized options: --enable-magic-quotes, --enable-safe-mode, --with-curlwrappers

    這是新版本的php已經不再支持這幾個選項,默認就是開啓的

    刪掉這三個配置選項重新編譯 。

 ./configure --prefix=/home/php --with-config-file-path=/home/php/etc --with-mysql=/home/mysql --with-mysqli=/home/mysql/bin/mysql_config --with-mysql-sock=/home/mysql/mysql.sock --with-gd --with-iconv  --with-zlib  --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --enable-mbregex  --enable-fpm --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --enable-session --with-mcrypt --with-curl --with-jpeg-dir --with-freetype-dir --with-mcrypt=/home/rely/libmcrypt/ 

  ok.Thank for using php

注意:

    如果寫成 --with-libmcrypt=/....... 就會報錯:configure: error: mcrypt.h not found. Please reinstall libmcrypt

    make報錯:make: *** [ext/fileinfo/libmagic/apprentice.lo] Error 1

    這是因爲虛擬內存不夠 ....configure的時候加上   –disable-fileinfo

    編譯又報錯:configure: error: /bin/sh ./config.sub disable-fileinfo failed

    free -m 查看內存使用情況,swap直接是0.....找個地方建塊虛擬內存吧

    dd if=/dev/zero of=/tmp/big_swap bs=1024 count=100000    #這個意思是輸出到 /tmp/big_swap  每一塊1024B=1K,總共100000塊

    du -sh /tmp/big_swap  #查看是否符合要求

    ls -al /tmp/big_swap

    mkswap /tmp/big_swap      設爲swap分區文件

    swapon big_swap  立即啓用交換分區文件

   free -m 查看一下現在的內存使用情況,一切正常             

              total       used       free     shared    buffers     cached
Mem:           994        924         70          0          2        372
-/+ buffers/cache:        548        446
Swap:         1953          0       1953

設置開機啓動.....  vi/etc/fstab

添加 swap                    /tmp/big_swap           swap    defaults        0 0

   注意:增加虛擬內存別太大了,太多會導致系統錯誤

 到php資源目錄下,make && make install

 終於ok了,到/home/php/etc 目錄下    cp php-fpm.conf.default php-fpm.conf

開啓 /home/php/sbin/php-fpm -c /home/php/etc/php-fpm.conf

注意修改 php.ini後要記得重啓 php-fpm   停止php-fpm  kill -INT PID

在php的源碼目錄  cp php.ini-production /home/php/etc

添加一個軟連接:ln -s /home/php/etc/php.ini  /etc/php.ini

關於 Nginx跟php怎麼整合就不解釋了.....


五. 編譯安裝redis

    下載最新穩定版本 2.8.9 http://www.redis.cn/download.html

  tar -zxf  redis-2.8.9.tar.gz
  cd  redis-2.8.9
  make
  make PREFIX=/home/redis install
  vi /home/redis/redis.conf     簡單配置,完成

    動態加載 phpredis 模塊,整合php跟redis

  tar -zxvf owlient-phpredis-2.1.1-1-g90ecd17.tar.gz  phpredis
  cp phpredis /usr/include/php/ext/ (將文件拷貝至php源碼目錄的ext目錄中) 
  /home/php/bin/phpize (運行phpize,如果不知道在哪裏,可以用whereis phpize命令) 
  cd /home/php/include/php/ext/phpredis/ 
  ./configure –with-php-config=/usr/bin/php-config (注意自己的php-config路徑) 
  make && make install 

    如果報錯找不到 config.m4    find / -name config0.m4

    找到 config0.m4     重命名

  cp config0.m4 config.m4

    再次編譯安裝

    完成後修改 php.ini    添加 extension=redis.so       還有 extension_dir 必要的時候也需要修改

  ok.  kill -INT ****   ./php-fpm -y /home/php/etc/php-fpm.conf   service nginx restart   重啓服務器

    用php頁面測試   

  $redis = new Redis();    $redis->set("aa","zzzzz");     echo $redis->get("aa");    

    用瀏覽器訪問,頁面輸出zzzzz

   

    


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章