1. 下载稳定版源代码安装文件:


nginx-1.6.2.tar.gz

http://nginx.org/download/


php-5.5.23.tar.gz

http://php.net/downloads.php


PCRE(使Nginx支持HTTP Rewrite模块):pcre-8.35.tar.gz

ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/


可利用TCMalloc来优化Nginx的性能,需要如下两个包,一般在物理机上配置可提高性能,如果是虚拟机就不要配置这个:

libunwind-1.1.tar.gz(64位系统才需要)

http://ftp.yzu.edu.tw/nongnu/libunwind/

gperftools-2.1.tar.gz

http://download.csdn.net/detail/chunmanchunman/8201565


libmcrypt-2.5.8.tar.gz

http://sourceforge.net/projects/mcrypt/files/Libmcrypt/2.5.8/


mcrypt-2.6.8.tar.gz

http://sourceforge.net/projects/mcrypt/files/MCrypt/2.6.8/


jpegsrc.v9.tar.gz

http://www.ijg.org/files/


libpng-1.6.17.tar.gz

http://sourceforge.net/projects/libpng/files/libpng16/1.6.17/


freetype-2.5.5.tar.gz

http://ftp.twaren.net/Unix/NonGNU/freetype/


mhash-0.9.9.9.tar.gz

http://sourceforge.net/projects/mhash/files/mhash/0.9.9.9/





2. 编译安装Nginx并配置:


1)检查依赖的软件包,没有的要手动安装:

make gcc gcc-c++ autoconf libjpeg-turbo libjpeg-turbo-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 libidn libidn-devel openssl openssl-devel openldap 

openldap-devel openldap-clients openldap-servers portreserve 

gd gd-devel


# yum install e2fsprogs-devel openldap-devel openldap-clients

 openldap-servers portreserve gd gd-devel


2)修改系统内核和资源限制来优化Nginx性能:

# vi /etc/sysctl.conf

net.ipv4.tcp_max_tw_buckets = 10000    # timewait数量

net.ipv4.ip_local_port_range = 1024 65000  # 允许系统打开端口范围

net.ipv4.tcp_tw_recycle = 1   # 启用timewait快速回收,但会引起Android手机连接后台程序频繁超时,所以在APP服务器上设置为0

net.ipv4.tcp_tw_reuse = 1             # 启用timewait重用与新TCP

net.ipv4.tcp_syncookies = 1            # 开启SYN Cookies

net.core.somaxconn = 262144         # 系统能同时发起TCP连接数

net.core.netdev_max_backlog = 262144  #网口比内核处理数据包快时,允许发送到队列的数据包最大数目

net.ipv4.tcp_max_orphans = 262144   # 设置最多多少个TCP套接字不被关联到任何一个用户文件句柄上

net.ipv4.tcp_max_syn_backlog = 262144  

net.ipv4.tcp_synack_retries = 1

net.ipv4.tcp_syn_retries = 1

net.ipv4.tcp_fin_timeout = 1

net.ipv4.tcp_keepalive_time = 30

# /sbin/sysctl -p


# vi /etc/security/limits.conf

*        soft    nofile           10240

*        hard    nofile           65536


3)编译安装pcre-8.35,系统自带的版本过低:

# cd /usr/local/src

# tar xzvf pcre-8.35.tar.gz

# cd pcre-8.35

# ./configure --prefix=/usr/local/pcre

# make && make install


4)编译安装libunwind-1.1和gperftools-2.1,如果是虚拟机不用安装这两个软件包:

# tar xzvf libunwind-1.1.tar.gz

# cd libunwind-1.1

# CFLAGS=-fPIC ./configure

# make CFLAGS=-fPIC

# make CFLAGS=-fPIC install


# tar xzvf gperftools-2.1.tar.gz

# cd gperftools-2.1

# ./configure

# make && make install


# echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf

# /sbin/ldconfig



5)编译安装Nginx:


# cd /usr/local/src

# tar xzvf nginx-1.6.2.tar.gz

# cd nginx-1.6.2

# vi auto/cc/gcc

# debug             # 注释掉这两行,关闭debug模式

# CFLAGS="$CFLAGS -g"

# ./configure --prefix=/usr/local/nginx --with-pcre=/usr/local/src/pcre-8.35 --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gzip_static_module --with-http_sub_module --with-http_stub_status_module


-----------------------------------------------------------------------------------------------

## 下面的配置方法是在物理机上启用TCMalloc来优化Nginx的性能

# ./configure --user=nginxphp --group=nginxphp --prefix=/usr/local/nginx --with-pcre=/usr/local/src/pcre-8.35 --with-google_perftools_module --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gzip_static_module  --with-http_sub_module --with-http_stub_status_module

------------------------------------------------------------------------------------------------

(说明:--with-pcre指向的是pcre源码解压的目录)


# make

# make install


# vi /etc/profile

export PATH=/usr/local/nginx/sbin:$PATH


# source /etc/profile


# nginx -V

nginx version: nginx/1.6.2

built by gcc 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC) 

TLS SNI support enabled

configure arguments: --prefix=/usr/local/nginx --with-pcre=/usr/local/src/pcre-8.35 --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gzip_static_module --with-http_sub_module --with-http_stub_status_module



6)为Google-Perftools TCMalloc添加线程目录,这步是配置启用TCMalloc功能的,如果没有启用TCMalloc就不用配置:


# mkdir /tmp/tcmalloc

# chmod 0777 /tmp/tcmalloc


7)修改Nginx配置文件,这步是配置启用TCMalloc功能的,如果没有启用TCMalloc就不用配置:


# vi /usr/local/nginx/conf/nginx.conf

user  nobody nobody;


#pid        logs/nginx.pid;

google_perftools_profiles /tmp/tcmalloc;


8)启动,停止,热重启Nginx服务:


# /usr/local/nginx/sbin/nginx


# ps -ef|grep nginx

root     30048     1  0 18:18 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx

nginxphp 30049 30048  0 18:18 ?        00:00:00 nginx: worker process

# lsof -n |grep tcmall

nginx     30049  nginxphp    9w      REG                8,3        0    2106703 /tmp/tcmalloc.30049


# kill -QUIT master_pid

# kill -QUIT ‘cat /usr/local/nginx/logs/nginx.pid’


# kill -HUP ‘cat /usr/local/nginx/logs/nginx.pid’


6)添加Nginx为系统服务:

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


#!/bin/bash


# nginx Startup script for the Nginx HTTP Server

# it is v.0.0.2 version.

# chkconfig: - 85 15

# description: Nginx is a high-performance web and proxy server.

# It has a lot of features, but it's not for everyone.

# processname: nginx

# pidfile: /var/run/nginx.pid

# config: /usr/local/nginx/conf/nginx.conf


nginxd=/usr/local/nginx/sbin/nginx

nginx_config=/usr/local/nginx/conf/nginx.conf

nginx_pid=/usr/local/nginx/logs/nginx.pid


RETVAL=0

prog="nginx"


# Source function library.

. /etc/rc.d/init.d/functions


# Source networking configuration.

. /etc/sysconfig/network


# Check that networking is up.

[ ${NETWORKING} = "no" ] && exit 0

[ -x $nginxd ] || exit 0


# Start nginx daemons functions.


start() {

  if [ -e $nginx_pid ];then

    echo "nginx already running...."

    exit 1

  fi

  echo -n $"Starting $prog: "

  daemon $nginxd -c ${nginx_config}

  RETVAL=$?

  echo

  [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx

  return $RETVAL

}


# Stop nginx daemons functions.


stop() {

  echo -n $"Stopping $prog: "

  killproc $nginxd

  RETVAL=$?

  echo

  [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /usr/local/nginx/logs/nginx.pid

}


reload() {

  echo -n $"Reloading $prog: "

  #kill -HUP `cat ${nginx_pid}`

  killproc $nginxd -HUP

  RETVAL=$?

  echo

}


# See how we were called.


case "$1" in

  start)

    start

    ;;

  stop)

    stop

    ;;

  reload)

    reload

    ;;

  restart)

    stop

    start

    ;;

  status)

    status $prog

    RETVAL=$?

    ;;

  *)


  echo $"Usage: $prog {start|stop|restart|reload|status|help}"

  exit 1

esac


exit $RETVAL



# chmod 775 /etc/rc.d/init.d/nginx

# chkconfig nginx on

# chkconfig --level 24 nginx off


3. 编译安装PHP (5.3版以后PHP-FPM已集成到PHP内核中) 并配置:


1)检查依赖的软件包,没有的要手动安装:

make  gcc  gcc-c++  libxml2  libxml2-devel  autoconf libjpeg-turbo

libjpeg-turbo-devel  libpng  libpng-devel  freetype  freetype-devel 

zlib  zlib-devel  glibc  glibc-devel  glib2  glib2-devel


# yum install libjpeg-turbo-devel


2)更新几个PHP依赖的软件源码包:

# cd /usr/local/src

# tar xzvf jpegsrc.v9.tar.gz

# cd jpeg-9

# ./configure --prefix=/usr/local/jpeg9 --enable-shared --enable-static

# make && make install


# tar xzvf libpng-1.6.17.tar.gz

# cd libpng-1.6.17

# ./configure --prefix=/usr/local/png --enable-shared

# make && make install


# tar xzvf freetype-2.5.5.tar.gz

# cd freetype-2.5.5

# ./configure --prefix=/usr/local/freetype --enable-shared

# make && make install


# tar xzvf libmcrypt-2.5.8.tar.gz

# cd libmcrypt-2.5.8

# ./configure --prefix=/usr/local/libmcrypt --enable-shared

# make && make install


# tar xzvf mhash-0.9.9.9.tar.gz

# cd mhash-0.9.9.9

# ./configure --prefix=/usr/local/mhash --enable-shared

# make && make install



-------------------------------------------------------------------------------

## 这个测试环境中没安装成功,以后有机会再测试

# tar xzvf mcrypt-2.6.8.tar.gz

# cd mcrypt-2.6.8

# ./configure --prefix=/usr/local/mcrypt

--with-libmcrypt-prefix=/usr/local/libmcrypt --enable-shared

--------------------------------------------------------------------------------------------


3)编译安装PHP及PHP-FPM:


# cd /usr/local/src

# tar xzvf php-5.5.23.tar.gz

# cd php-5.5.23


(说明:下面的编译方法是针对MySQL服务器在另外的服务器上)

# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc

 --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd 

 --with-jpeg-dir=/usr/local/jpeg9

 --with-png-dir=/usr/local/png --with-freetype-dir=/usr/local/freetype

 --with-mhash=/usr/local/mhash --with-mcrypt=/usr/local/libmcrypt --with-gd

 --with-iconv --with-zlib --with-openssl --with-xmlrpc --with-gettext --with-curl

 --without-pear --enable-fpm --enable-bcmath --enable-shmop --enable-sysvsem

 --enable-mbstring --enable-ftp --enable-gd-native-ttf --enable-pcntl

 --enable-sockets --enable-zip --enable-soap --enable-session --enable-xml

 --enable-inline-optimization --enable-mbregex


http://www.cnxct.com/some-errors-on-compile-php-5-3-8-with-pdo_mysql-and-mysqlnd/


(说明:PHP5.4及以上版本默认连接MySQL的驱动就是mysqlnd,不需要在编译时加with-mysql, with-mysqli, with-pdo-mysql编译参数也行)


(说明:这个编译方法是对MySQL在本机上,只在某些情况下使用,例如集成化的安装环境,数据库和应用服务器在一起安装)

# ./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-mysql-sock=/tmp/mysqld.sock --with-jpeg-dir=/usr/local/jpeg9

 --with-png-dir=/usr/local/png --with-freetype-dir=/usr/local/freetype

 --with-mhash=/usr/local/mhash --with-mcrypt=/usr/local/libmcrypt --with-gd

 --with-iconv --with-zlib --with-openssl --with-xmlrpc --with-gettext --with-curl

 --without-pear --enable-fpm --enable-bcmath --enable-shmop --enable-sysvsem

 --enable-mbstring --enable-ftp --enable-gd-native-ttf --enable-pcntl

 --enable-sockets --enable-zip --enable-soap --enable-session --enable-xml

 --enable-inline-optimization --enable-mbregex


# make

# make install


4)配置PHP和PHP-FPM:

# cp /usr/local/src/php-5.5.23/php.ini-production

 /usr/local/php/etc/php.ini


# vi /usr/local/php/etc/php.ini

disable_functions =

passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,escapeshellcmd,dll,popen,disk_free_space,checkdnsrr,checkdnsrr,getservbyname,getservbyport,disk_total_space,posix_ctermid,posix_get_last_error,posix_getcwd, posix_getegid,posix_geteuid,posix_getgid, posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid, posix_getppid,posix_getpwnam,posix_getpwuid, posix_getrlimit, posix_getsid,posix_getuid,posix_isatty, posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid, posix_setpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,posix_ttyname,posix_uname

(说明: 列出PHP可以禁用的函数,如果某些程序需要用到这个函数,可以删除,取消禁用)


date.timezone = PRC            -- 设置时区

expose_php = Off               -- 禁止显示PHP版本信息

short_open_tag = On            -- 支持PHP短标签 


# cp /usr/local/php/etc/php-fpm.conf.default

 /usr/local/php/etc/php-fpm.conf


# vi /usr/local/php/etc/php-fpm.conf

[global]

pid = run/php-fpm.pid

[www]

user = nobody

group = nobody


5)启动,停止PHP-FPM:


# /usr/local/php/sbin/php-fpm


6)配置PHP-FPM为系统服务:

# cp /usr/local/src/php-5.5.23/sapi/fpm/init.d.php-fpm

 /etc/init.d/php-fpm

# chmod +x /etc/init.d/php-fpm

# chkconfig php-fpm on

# chkconfig --level 24 php-fpm off

# service php-fpm start


4. 配置Nginx支持PHP:


user  nobody nobody;    -- 集成环境此用户名与组要和PHP-FPM的一样

http {

  server {

location / {

        root   html;

        index  index.php index.html index.htm;     -- 增加index.php

}

location ~ \.php$ {

    root          html;

    fastcgi_pass    127.0.0.1:9000;

    fastcgi_index   index.php;

    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;

    include        fastcgi_params;

}

  }

}


(说明:SCRIPT_FILENAME后面的要改为$document_root或绝对路径)



# vi /usr/local/nginx/html/index.php

<?php

  Phpinfo();

?>



5. 在浏览器中访问:http://192.168.1.12,看到下面的信息,表示配置成功:

 


http://zyan.cc/nginx_php_v7/


http://www.linuxidc.com/Linux/2014-06/103266p2.htm



6. 配置PHP Memcached扩展:


1)下载源代码安装文件:


文件版本:memcache-2.2.7.tgz (稳定版)(比较旧的扩展,已不用)

下载地址:http://pecl.php.net/package/memcache


文件版本:libmemcached-1.0.18.tar.gz(比较新的扩展,使用这个)

下载地址:https://launchpad.net/libmemcached/+download


文件版本:memcached-2.2.0.tgz(配合上面的一起使用)

下载地址:http://pecl.php.net/package/memcached




## 可选的基于PHP5&Jquery的Memcached可视化管理监控工具

文件版本:memadmin-1.0.12.tar.gz

下载地址:http://www.junopen.com/memadmin/


------------------------------------------------------------------------------------------

2)安装PHP Memcache扩展(如果有比较旧的环境,可能用到这个)


# cd /usr/local/src

# tar xzvf memcache-2.2.7.tgz

# cd memcache-2.2.7

# /usr/local/php/bin/phpize

# ./configure --with-php-config=/usr/local/php/bin/php-config 

--enable-memcache

# make

# make install

Installing shared extensions:

/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/

--------------------------------------------------------------------------------------------



2)安装PHP Memcached扩展:


# cd /usr/local/src

# tar xzvf libmemcached-1.0.18.tar.gz

# cd libmemcached-1.0.18

# ./configure --prefix=/usr/local/libmemcached

# make

# make install


# tar xzvf memcached-2.2.0.tgz

# cd memcached-2.2.0

# /usr/local/php/bin/phpize

# ./configure --with-php-config=/usr/local/php/bin/php-config 

--with-libmemcached-dir=/usr/local/libmemcached --enable-memcached

# make

# make install

Installing shared extensions:

/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/



3)安装Memcached可视化管理监控工具,可选的:


# cd /usr/local/src

# tar xzvf memadmin-1.0.12.tar.gz

# mv memadmin /usr/local/nginx/html/


4)启用PHP 的Memcached动态扩展:


# vi /usr/local/php/etc/php.ini


extension=memcached.so


#extension=memcache.so


5)重加载PHP-FPM服务进程,使新添的Memcached动态扩展生效:


# service php-fpm reload




7. 配置PHP MongoDB扩展:


1)下载源代码安装文件:


文件版本:mongo-1.6.10.tgz (稳定版) 

下载地址:http://pecl.php.net/package/mongo


2)安装PHP MongoDB扩展:


# cd /usr/local/src

# tar xzvf mongo-1.6.10.tgz

# cd mongo-1.6.10

# /usr/local/php/bin/phpize

# ./configure --with-php-config=/usr/local/php/bin/php-config  --enable-mongo

# make

# make install

Installing shared extensions:

/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/


# ls /usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/

memcached.so  mongo.so  opcache.a  opcache.so


3)启用PHP 的MongoDB动态扩展:


# vi /usr/local/php/etc/php.ini


extension=mongo.so


4)重加载PHP-FPM服务进程,使新添的MongoDB动态扩展生效:


# service php-fpm reload