centos下安装nginx+php

nginx:

下载:

wget http://nginx.org/download/nginx-1.4.3.tar.gz


安装依赖:

yum install-y gcc pcre-devel zlib-devel make


编译优化:

cdnginx-1.4.3
sed-i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g'auto/cc/gcc


下载libunwind:

wget http://savannah-nongnu-org.ip-connect.vn.ua/libunwind/libunwind-1.1.tar.gz


编译安装libunwind:

tar-zxvf libunwind-1.1.tar.gz
cdlibunwind-1.1
CFLAGS=-fPIC ./configure
make-j4 CFLAGS=-fPIC
makeCFLAGS=-fPIC install


下载google-perftools:

wget https://gperftools.googlecode.com/files/gperftools-2.1.tar.gz


编译安装google-perftools:

tar-zxvf gperftools-2.1.tar.gz
cdgperftools-2.1
yum installgcc-c++
./configure
make-j4 && makeinstall
echo"/usr/local/lib/">> /etc/ld.so.conf


编译参数:

./configure--prefix=/usr/local/nginx--without-http_memcached_module --user=www --group=www --with-http_stub_status_module --with-pcre --with-google_perftools_module


编译:

make-j4 && makeinstall


创建线程目录:

mkdir/dev/shm/tcmalloc
chownwww.www /dev/shm/tcmalloc


修改nginx配置文件:

#pid        logs/nginx.pid; #这一行下面添加
google_perftools_profiles /dev/shm/tcmalloc;


验证google-perftools:

lsof-n | greptcmalloc


nginx配置:

worker_processes  4;    #启动进程数量
worker_cpu_affinity 00000001 00000010 00000100 00001000;  #为每个进程分配cpu
worker_rlimit_nofile 65535;  #描述符数目
worker_connections  65535;   #每个进程的最多连接数
client_header_buffer_size 4k;   #客户端请求头部的缓冲区大小 getconf PAGESIZE查看
open_file_cache max=65535 inactive=60s;  #为打开文件指定缓存的缓存数量 以及超时时间
open_file_cache_valid 80s;     #查看文件缓存的周期
open_file_cache_min_uses 1;     #如果有一个文件在inactive 时间内一次没被使用,它将被移除
fastcgi_connect_timeout 300;   #指定连接到后端FastCGI 的超时时间
fastcgi_cache_path /dev/shm/fastcgi_cachelevels=1:2 keys_zone=TEST:10minactive=5m;  #这个指令为FastCGI 缓存指定一个路径,目录结构等级,关键字区域存储时间和非活动删除时间
fastcgi_send_timeout 300;   #向FastCGI 传送请求的超时时间,这个值是指已经完成两次握手后向FastCGI 传送请求的超时时间
fastcgi_read_timeout 300;   #接收FastCGI 应答的超时时间,这个值是指已经完成两次握手后接收FastCGI 应答的超时时间。
fastcgi_buffer_size 4k;    #指定读取FastCGI 应答第一部分需要用多大的缓冲区,一般第一部分应答不会超过1k,由于页面大小为4k,所以这里设置为4k
fastcgi_buffers 8 4k;   #指定本地需要用多少和多大的缓冲区来缓冲FastCGI 的应答
fastcgi_busy_buffers_size 8k;   #是fastcgi_buffers 的两倍
fastcgi_temp_file_write_size 8k;  #在写入fastcgi_temp_path 时将用多大的数据块,默认值是fastcgi_buffers 的两倍
fastcgi_cache TEST;   #开启FastCGI缓存并且为其制定一个名称,可以有效降低CPU负载,并且防止502错误
#为指定的应答代码指定缓存时间,如上例中将200,302 应答缓存一小时,301 应答缓存1 天,其他为1 分钟
fastcgi_cache_valid 200 302 1h;
fastcgi_cache_valid 301 1d;
fastcgi_cache_valid any 1m;
fastcgi_cache_min_uses 1;  #缓存在fastcgi_cache_path 指令inactive 参数值时间内的最少使用次数,如上例,如果在5 分钟内某文件1 次也没有被使用,那么这个文件将被移除
fastcgi_cache_use_stale error timeout invalid_header http_500;  #定义那些时候用过期缓存




内核优化:

cat<< EOF >> /etc/sysctl.conf  
net.ipv4.tcp_max_tw_buckets = 6000
net.ipv4.ip_local_port_range = 1024 65000 
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_syncookies = 1
net.core.somaxconn = 262144
net.core.netdev_max_backlog = 262144
net.ipv4.tcp_max_orphans = 262144
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
EOF
/sbin/sysctl-p


含义介绍:

#net.ipv4.tcp_max_tw_buckets参数用来设定timewait的数量,默认是180000,这里设为6000。
#net.ipv4.ip_local_port_range选项用来设定允许系统打开的端口范围。
#net.ipv4.tcp_tw_recycle选项用于设置启用timewait快速回收。
#net.ipv4.tcp_tw_reuse选项用于设置开启重用,允许将TIME-WAIT sockets重新用于新的TCP连接。
#net.ipv4.tcp_syncookies选项用于设置开启SYN Cookies,当出现SYN等待队列溢出时,启用cookies进行处理。
#net.core.somaxconn选项默认值是128, 这个参数用于调节系统同时发起的tcp连接数,在高并发的请求中,默认的值可能会导致链接超时或者重传,因此,需要结合并发请求数来调节此值。
#net.core.netdev_max_backlog选项表示当每个网络接口接收数据包的速率比内核处理这些包的速率快时,允许发送到队列的数据包的最大数目。
#net.ipv4.tcp_max_orphans选项用于设定系统中最多有多少个TCP套接字不被关联到任何一个用户文件句柄上。如果超过这个数字,孤立连接将立即被复位并打印出警告信息。这个限制只是为了防止简单的DoS***。不能过分依靠这个限制甚至人为减小这个值,更多的情况是增加这个值。
#net.ipv4.tcp_max_syn_backlog选项用于记录那些尚未收到客户端确认信息的连接请求的最大值。对于有128MB内存的系统而言,此参数的默认值是1024,对小内存的系统则是128。
#net.ipv4.tcp_synack_retries参数的值决定了内核放弃连接之前发送SYN+ACK包的数量。
#net.ipv4.tcp_syn_retries选项表示在内核放弃建立连接之前发送SYN包的数量。
#net.ipv4.tcp_fin_timeout选项决定了套接字保持在FIN-WAIT-2状态的时间。默认值是60秒。正确设置这个值非常重要,有时候即使一个负载很小的Web服务器,也会出现因为大量的死套接字而产生内存溢出的风险。
#net.ipv4.tcp_keepalive_time选项表示当keepalive启用的时候,TCP发送keepalive消息的频度。默认值是2(单位是小时)。



php:

下载:

wget http://cn2.php.net/distributions/php-5.3.27.tar.bz2


安装依赖:

wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/attic/libmcrypt/libmcrypt-2.5.7.tar.gz
make-j4 && makeinstall
yum installlibxml2-devel libcurl-devel libjpeg-devel libpng-devel freetype-devel mysql-devel



编译参数:

./configure-prefix=/usr/local/php--with-iconv-dir--with-freetype-dir--with-jpeg-dir--with-png-dir--with-zlib --with-libxml-dir-enable-xml -disable-rpath -enable-bcmath -enable-shmop -enable-sysvsem -enable-inline-optimization --with-curl -enable-mbregex -enable-fpm -enable-mbstring --with-mcrypt -enable-ftp--with-gd -enable-gd-native-ttf --with-mhash -enable-pcntl -enable-sockets --with-xmlrpc -enable-zip -enable-soap --without-pear --with-gettext --with-mysqli=mysqlnd --with-mysql=mysqlnd --with-pdo-mysql


编译:

make-j4 && makeinstall


复制配置文件:

cpphp.ini-production /usr/local/php/etc/php.ini


安装memcache扩展

wget http://pecl.php.net/get/memcache-2.2.7.tgz
tarvxzf memcache-2.2.7.tgz
cdmemcache-2.2.7
/usr/local/php/bin/phpize
./configure--enable-memcache --with-php-config=/usr/local/php/bin/php-config--with-zlib-dir
make-j4
makeinstall


把php.ini中的extension_dir:

extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-2007xxxx/"


添加一行:

extension=memcache.so


配置php.ini:

magic_quotes_gpc = On      #防止sql注入
open_basedir = /data/www/#限制主目录
memory_limit = 128M        #每个php可用的最大内存
safe_mode = On             #打开安全模式
expose_php = Off           #关闭PHP版本信息
display_errors = Off       #禁止错误提示
error_reporting = E_WARNING & E_ERROR    #错误提示级别
log_errors = On                          #开启错误日志
error_log = /data/logs/php_error.log     #错误日志位置
max_execution_time = 60                  #php执行最大时间
upload_max_filesize = 10M                #上传文件的大小
session.save_path = "/dev/shm"#存储session位置



配置php-fpm.conf

emergency_restart_interval = 5s  #平滑启动时间 解决加速器共享内存问题
error_log = /data/logs/fpm_error.log  #错误日志位置
listen = /tmp/fpm.sock     #监听地址
listen.owner = www         #sock权限
listen.group = www         #sock权限
user = www                 #fpm运行权限
group = www                #fpm运行权限
pm = static                #进出创建模式
pm.max_children = 200      #子进程数目
pm.max_requests = 500      #子进程最大服务次数
request_terminate_timeout = 60s   #子进程执行服务的最大时间
request_slowlog_timeout = 5s      #慢请求日志超时时间
slowlog = /data/logs/fpm-slow.log #慢请求日志位置


安装xcache:

wget http://xcache.lighttpd.net/pub/Releases/3.1.0/xcache-3.1.0.tar.bz2
tar-jxvf xcache-3.1.0.tar.bz2
cdxcache-3.1.0
/usr/local/php/bin/phpize
./configure--enable-xcache --with-php-config=/usr/local/php/bin/php-config
make-j4 && makeinstall


配置php.ini:

[xcache-common]
extension = xcache.so
[xcache.admin]
xcache.admin.enable_auth = On
xcache.admin.user = "xcache"
xcache.admin.pass = ""
[xcache]
xcache.shm_scheme ="mmap"
xcache.size=60M
xcache.count =1
xcache.slots =8K
xcache.ttl=0
xcache.gc_interval =0
xcache.var_size=4M
xcache.var_count =1
xcache.var_slots =8K
xcache.var_ttl=0
xcache.var_maxttl=0
xcache.var_gc_interval =300
xcache.test=Off
xcache.readonly_protection = On
xcache.mmap_path ="/dev/shm/xcache"
xcache.coredump_directory =""
xcache.cacher =On
xcache.stat=On
xcache.optimizer =Off
[xcache.coverager]
xcache.coverager =On
xcache.coveragedump_directory =""


创建xcache文件:

touch/dev/shm/xcache
chownwww.www /dev/shm/xcache


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