lnmp一鍵安裝&&調優,lnmp,memcache,全文檢索

全文檢索 中文問題還沒搞定,歡迎提出各類優化參數優化意見
#!/bin/bash
echo $1 
#exit;
ret=$(ps aux | grep -E "php|nginx|mysql" | grep -v grep)
if [ $1 = "killall" ] && [ -n $ret  ] ;then
#kill 掉 nmp進程
ps aux | grep -E "php|nginx|mysql|memcached" | grep -v grep   | gawk '{print $2}' | tr "\n" " " | xargs kill
echo "killall ok"
fi  

current_path=$(pwd)   #腳本當前目錄
#檢查softs目錄	   
if [ ! -d $current_path/softs ]; then
	mkdir -p $current_path/softs
fi
cd $current_path/softs
echo  "當前腳本執行目錄: $(pwd)"
echo  "當前系統PATH: $PATH"
/usr/sbin/setenforce 0  #馬上關閉 SELINUX,1爲開啓
ulimit -SHu 65535
ulimit -SHn 65535  #注ulimit -SHn 65535 等效 ulimit -n 65535 ,-S 指soft #,-H 指hard)
sleep 3
echo "添加yum源"
# 幫助網頁在 http://mirrors.163.com/.help/centos.html
if [ ! -f /etc/yum.repos.d/CentOS-Base.repo.bak ];then
 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
fi
v_num=$(cat /etc/redhat-release | grep -o  "[0-9]" | head -1)
yum_url="http://mirrors.163.com/.help/CentOS$v_num-Base-163.repo -O /etc/yum.repos.d/CentOS-Base.repo"
wget $yum_url -O /etc/yum.repos.d/CentOS-Base.repo
yum makecache  # 重建cache

#編譯環境安裝
yum -y install 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 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers libtool libtool-ltdl libtool-ltdl-devel libevent libevent-devel  libmcrypt libmcrypt-devel libaio-devel cmake ImageMagick-devel  vim-enhanced glibc-common automake expat-devel
#各下載軟件url

#全文檢索 
coreseek_url="http://www.wapm.cn/uploads/csft/4.0/coreseek-4.1-beta.tar.gz"
#官方版本
#http://sphinxsearch.com/files/sphinx-2.0.6-release.tar.gz



#php 
php_url="http://php.net/get/php-5.4.11.tar.gz/from/cn2.php.net/mirror"

    ############php擴展系列##################
	
	php_mcrypt_url="http://downloads.sourceforge.net/mcrypt/mcrypt-2.6.8.tar.gz?modtime=1194463373&big_mirror=0"    
	#wget http://nchc.dl.sourceforge.net/project/mcrypt/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz
	
	#一個能夠供PHP調用ImageMagick功能的PHP擴展。\
	#使用這個擴展可使PHP具有和ImageMagick相同的功能。
	php_imagick_url="http://pecl.php.net/get/imagick-3.0.1.tgz"
	
	#Alternative PHP Cache (APC)是一種對PHP有效的開放源高速緩衝儲存器工具,\
	#它可以緩存opcode的php中間碼。\
    #PHP APC提供兩種緩存功能,即緩存Opcode(目標文件),\
    #咱們稱之爲apc_compiler_cache。\
	#同時它還提供一些接口用於PHP開發人員將用戶數據駐留在內存中,\
	#咱們稱之爲apc_user_cache。
	php_apc_url="http://pecl.php.net/get/APC-3.1.14.tgz"
	
	#php pdo長鏈接
	php_pdo_mysql_url="http://pecl.php.net/get/PDO_MYSQL-1.0.2.tgz"
	
	#一個能夠供PHP調用ImageMagick功能的PHP擴展。使用這個擴展可使PHP具有和ImageMagick相同的功能。
    php_imagick_url="http://pecl.php.net/get/imagick-3.1.0RC2.tgz"
	#Memcache是一個高性能的分佈式的內存對象緩存系統 如下兩個都是 php的擴展 
	memcache_url="http://pecl.php.net/get/memcache-3.0.7.tgz"
	memcached_url="http://pecl.php.net/get/memcached-2.1.0.tgz"
	############php擴展系列end#################################################
	
#nginx
nginx_url="http://nginx.org/download/nginx-1.2.6.tar.gz"

#tengine
tengine_url="http://tengine.taobao.org/download/tengine-1.4.2.tar.gz"

#memcached   守護進程源碼 這個是服務端 php擴展也有個memcached 不要搞混
memcached_url="http://memcached.googlecode.com/files/memcached-1.4.15.tar.gz"

#libiconv庫爲須要作轉換的應用提供了一個iconv()的函數,\
#以實現一個字符編碼到另外一個字符編碼的轉換 07-Aug-2011 13:59 last update
libiconv_url="http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz"

#libmcrypt是加密算法擴展庫。支持DES, 3DES, RIJNDAEL, Twofish,\
#IDEA, GOST, CAST-256, ARCFOUR, SERPENT, SAFER+等算法。系統庫
libmcrypt_url="http://downloads.sourceforge.net/mcrypt/libmcrypt-2.5.8.tar.gz?modtime=1171868460&big_mirror=0" 

#Mhash擴展庫支持12種混編算法
mhash_url="http://nchc.dl.sourceforge.net/project/mhash/mhash/0.9.9.9/mhash-0.9.9.9.tar.bz2"
#wget "http://downloads.sourceforge.net/mhash/mhash-0.9.9.9.tar.gz?modtime=1175740843&big_mirror=0"

#PCRE(Perl Compatible Regular Expressions)是一個Perl庫,\
#包括 perl 兼容的正規表達式庫。
perl_url="ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.32.tar.gz"

#imageMagick是一套功能強大、穩定並且免費的工具集和開發包,\
#能夠用來讀、寫和處理超過89種基本格式的圖片文件,\
#包括流行的TIFF、JPEG、GIF、 PNG、PDF以及PhotoCD等格式
imagemagick_url="http://blog.s135.com/soft/linux/nginx_php/imagick/ImageMagick.tar.gz"

#下載CMAKE
cmake_url="http://www.cmake.org/files/v2.8/cmake-2.8.10.tar.gz"

#安裝Percona DB  mysql 一個分支 下載Percona DB
mysql_url="http://www.percona.com/redir/downloads/Percona-Server-5.5/Percona-Server-5.5.28-29.3/source/Percona-Server-5.5.28-rel29.3.tar.gz"

#Bison is a general-purpose parser generator that converts an annotated context-free grammar into a deterministic LR or generalized LR (GLR) parser employing LALR(1) parser tables. As an experimental feature, Bison can also generate IELR(1) or canonical LR(1) parser tables. Once you are proficient with Bison, you can use it to develop a wide range of language parsers, from those used in simple desk calculators to complex programming languages.
bison_url="http://ftp.gnu.org/gnu/bison/bison-2.7.tar.gz"

#gperftools 依賴庫
#http://download.savannah.gnu.org/releases/libunwind/libunwind-1.1.tar.gz
#libunwind_url="ftp://mirror.switch.ch/pool/2/mirror/fedora/linux/development/rawhide/x86_64/os/Packages/l/libunwind-1.0.1-4.fc18.x86_64.rpm"
libunwind_url="http://download.savannah.gnu.org/releases/libunwind/libunwind-1.1.tar.gz"
#gperftools
gperftools_url="https://gperftools.googlecode.com/files/gperftools-2.0.tar.gz"


#######開始循環下載##########
urls=($php_url $php_mcrypt_url $php_imagick_url $php_apc_url $php_pdo_mysql_url \
      $php_imagick_url $nginx_url $libiconv_url $libmcrypt_url $memcache_url \
	   $mhash_url $perl_url $imagemagick_url $cmake_url $mysql_url $bison_url \
	   $tengine_url $libunwind_url $gperftools_url $memcached_url $coreseek_url)

color="\033[41;32;5m"
color_end="\033[41;32;0m"
for url in ${urls[@]}
do
	wget -nc -c "$url" 
done	
files=$(pwd)/* #$(ls -la | awk '/^-/{print $NF}')
for file_name in  $files; do
	 #dir_name=${file_name%-*}
	 ext=${file_name##*.}
	if [[ $ext = "tgz" || $ext = "bz2" || $ext = "gz" ]]; then
		tar xvf  $file_name
		sleep 1
	fi
done

default=("libiconv" "libmcrypt" "mhash" "bison" "pcre" "ImageMagick" "libunwind" "memcached-1.4.15","coreseek-4.1")

echo $(pwd)

for file_name in ${default[@]} ; do 
   cd ./$file_name*/
   
   if [ "$file_name" = "libmcrypt" ]; then
		/sbin/ldconfig
		cd libltdl/
		./configure --enable-ltdl-install && make && make install
		cd ../
    elif [ "$file_name" = "libunwind" ];then
		CFLAGS=-fPIC ./configure
		make CFLAGS=-fPIC  && \
		make CFLAGS=-fPIC install
	elif [ $file_name = "memcached-1.4.15" ];then
	     ./configure --prefix=/usr/local/webserver/memcached  --enable-64bit 
		 make && make install 
    else 
		./configure && make && make install
   fi
   cd ../
done
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 

cd mcrypt-2.6.8/
/sbin/ldconfig
LD_LIBRARY_PATH=/usr/local/lib  && export  LD_LIBRARY_PATH   && ./configure \
 && make && make install
cd ../


#mysql安裝
/usr/sbin/groupadd mysql
/usr/sbin/useradd -g mysql mysql


####copy sphinxse 到mysql 存儲引擎目錄 準備安裝
cd coreseek-4.1*/
if [ ! -d  ../Percona-Server-5.5.28-rel29.3/storage/sphinx ];then
		mkdir -p ../Percona-Server-5.5.28-rel29.3/storage/sphinx
fi
cd csft-*/
cp -rf ./mysqlse ../../Percona-Server-5.5.28-rel29.3/storage/sphinx

cd ../../

chown -R root:root ./Percona-Server-5.5.28-rel29.3/
cd  ./Percona-Server-5.5.28-rel29.3/

sh BUILD/autorun.sh
#配置 安裝
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/webserver/mysql -DWITH_INNOBASE_STORAGE_ENGINE=1 -DENABLED_LOCAL_INFILE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8  -DDEFAULT_COLLATION=utf8_general_ci -DWITH_DEBUG=0 -DBUILD_CONFIG=mysql_release -DFEATURE_SET=community  -DWITH_SPHINX_STORAGE_ENGINE=1 -DWITH_EMBEDDED_SERVER=OFF -DIGNORE_AIO_CHECK=1 
make && make install 

  
chmod +w /usr/local/webserver/mysql 
chown -R mysql:mysql /usr/local/webserver/mysql 
mkdir -p /data/mysql/3306/data/  
mkdir -p /data/mysql/3306/binlog/   
mkdir -p /data/mysql/3306/relaylog/  
chown -R mysql:mysql /data/mysql/  

#初始化數據庫
/usr/local/webserver/mysql/scripts/mysql_install_db --basedir=/usr/local/webserver/mysql --datadir=/data/mysql/3306/data --user=mysql 
cp support-files/my-innodb-heavy-4G.cnf /data/mysql/3306/my.cnf 

cat >/data/mysql/3306/my.cnf  <<EOF 
[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 = /usr/local/webserver/mysql
datadir = /data/mysql/3306/data
log-error = /data/mysql/3306/mysql_error.log
pid-file = /data/mysql/3306/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 = /data/mysql/3306/binlog/binlog
binlog_cache_size = 4M
binlog_format = MIXED
max_binlog_cache_size = 8M
max_binlog_size = 1G
relay-log-index = /data/mysql/3306/relaylog/relaylog
relay-log-info-file = /data/mysql/3306/relaylog/relaylog
relay-log = /data/mysql/3306/relaylog/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 = /data/mysql/3306/slow.log
#long_query_time = 10

[mysqldump]
quick
max_allowed_packet = 32M
EOF

#建立管理腳本
if [ ! -d /data/sh ]; then
	mkdir -p /data/sh
fi
cat >/data/sh/mysql.sh <<EOF
#!/bin/bash

mysql_port="3306"
mysql_username="admin"
mysql_password="12345678"

function_start_mysql()
{
    printf "Starting MySQL...\n"
     /usr/local/webserver/mysql/bin/mysqld_safe --defaults-file=/data/mysql/\$mysql_port/my.cnf 2>&1 > /dev/null &
	echo "啓動完成"
}

function_stop_mysql()
{
    printf "Stoping MySQL...\n"
    /usr/local/webserver/mysql/bin/mysqladmin -u \${mysql_username} -p\${mysql_password} -S /tmp/mysql.sock shutdown 2>&1 > /dev/null &
	sleep 3
}

function_restart_mysql()
{
    printf "Restarting MySQL...\n"
    function_stop_mysql
    sleep 5
    function_start_mysql
}

function_kill_mysql()
{
    kill  \$(ps aux | grep mysql | grep -v grep | gawk '{print \$2}' | tr -s "\n" " ")
	#kill \$(ps ef | grep bin/mysqld_safe | grep -v /bin/grep | gawk '{print \$1}')
	echo 'kill done'
	
}

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: /data/mysql/\$mysql_port/mysql {start|stop|restart|kill}\n"
fi
EOF

chmod +x /data/sh/mysql.sh
 
#啓動數據庫
#/data/sh/mysql.sh start

/usr/local/webserver/mysql/bin/mysql -u root  -S /tmp/mysql.sock -e "GRANT ALL PRIVILEGES ON *.* TO admin@'localhost' IDENTIFIED BY '12345678';
GRANT ALL PRIVILEGES ON *.* TO admin@'127.0.0.1' IDENTIFIED BY '12345678';
flush privileges;"

#/data/sh/mysql.sh stop && 
 cp -frp /usr/lib64/libldap* /usr/lib/ && \
 #ln -s /usr/local/webserver/mysql/lib/libmysqlclient.so.18  /usr/lib64/   
 ln -s /usr/local/webserver/mysql/lib /usr/local/webserver/mysql/lib64
 
cd ../

###############安裝 全文檢索引擎##########
cd coreseek-4.1*/
  cd mmseg-3*/
   ./bootstrap
    ./configure --prefix=/usr/local/mmseg3
	 make && make install
	 cd  ./data/
	 /usr/local/mmseg3/bin/mmseg -u unigram.txt
	 cp -ua ./unigram.txt.uni /usr/local/coreseek/dict/uni.lib
	 cd ../../csft-*/
	 sh buildconf.sh
	 ./configure --prefix=/usr/local/coreseek  --without-unixodbc --with-mmseg --with-mmseg-includes=/usr/local/mmseg3/include/mmseg/ --with-mmseg-libs=/usr/local/mmseg3/lib/ --with-mysql
	 make && make install 
	 #cp -ua ../testpack/etc/csft_mysql.conf /usr/local/coreseek/etc/csft_mysql.conf
	 ####
	 
	 
	 ##############分詞源配置#######################
	 prefix="/usr/local/coreseek"
     cat > /usr/local/coreseek/etc/csft_mysql.conf <<EOF
#MySQL數據源配置,詳情請查看:http://www.coreseek.cn/products-install/mysql/
#請先將var/test/documents.sql導入數據庫,並配置好如下的MySQL用戶密碼數據庫

#源定義
source mysql
{
    type                    = mysql

    sql_host                = 127.0.0.1
    sql_user                = admin
    sql_pass                = 12345678
    sql_db                    = test
    sql_port                = 3306
    sql_query_pre            = SET NAMES utf8

    sql_query                = SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS date_added, title, content FROM documents
                                                              #sql_query第一列id需爲整數
                                                              #title、content做爲字符串/文本字段,被全文索引
    sql_attr_uint            = group_id           #從SQL讀取到的值必須爲整數
    sql_attr_timestamp        = date_added #從SQL讀取到的值必須爲整數,做爲時間屬性

    sql_query_info_pre      = SET NAMES utf8                                        #命令行查詢時,設置正確的字符集
    sql_query_info            = SELECT * FROM documents WHERE id=\$id #命令行查詢時,從數據庫讀取原始數據信息
}

#index定義
index mysql
{
    source            = mysql             #對應的source名稱
    path            = $prefix/var/data/mysql #請修改成實際使用的絕對路徑,例如:/usr/local/coreseek/var/...
    docinfo            = extern
    mlock            = 0
    morphology        = none
    min_word_len        = 1
    html_strip                = 0

    #中文分詞配置,詳情請查看:http://www.coreseek.cn/products-install/coreseek_mmseg/
	#stopwords		= /path/to/stowords.txt #禁止搜索關鍵字的位置
    charset_dictpath = /usr/local/mmseg3/etc/ #BSD、Linux環境下設置,/符號結尾
    #charset_dictpath = etc/                             #Windows環境下設置,/符號結尾,最好給出絕對路徑,例如:C:/usr/local/coreseek/etc/...
    charset_type        = utf-8
	charset_table      = 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F 
	ngram_len        = 0
}

#全局index定義
indexer
{
    mem_limit            = 128M
}

#searchd服務定義
searchd
{
    listen                  =   9312
    read_timeout        = 5
    max_children        = 30
    max_matches            = 1000
    seamless_rotate        = 0
    preopen_indexes        = 0
    unlink_old            = 1
    pid_file = $prefix/var/log/searchd_mysql.pid  #請修改成實際使用的絕對路徑,例如:/usr/local/coreseek/var/...
    log = $prefix/var/log/searchd_mysql.log        #請修改成實際使用的絕對路徑,例如:/usr/local/coreseek/var/...
    query_log = $prefix/var/log/query_mysql.log #請修改成實際使用的絕對路徑,例如:/usr/local/coreseek/var/...
    binlog_path =                                #關閉binlog日誌
}
EOF
    cd ../../
    /usr/local/coreseek/bin/indexer -c /usr/local/coreseek/etc/csft_mysql.conf --all 
	/usr/local/coreseek/bin/searchd -c /usr/local/coreseek/etc/csft_mysql.conf
	/usr/local/coreseek/bin/indexer -c  /usr/local/coreseek/etc/csft_mysql.conf --all --rotate
#啓動
#/usr/local/coreseek/bin/searchd -c /usr/local/coreseek/etc/csft_mysql.conf
#中止
#/usr/local/coreseek/bin/searchd -c /usr/local/coreseek/etc/csft_mysql.conf --stop  
#更新索引
#/usr/local/coreseek/bin/indexer -c  /usr/local/coreseek/etc/csft_mysql.conf --all --rotate
		 
		 
##########################安裝php#############################
if [ ! -d  /usr/local/webserver/php/etc ] ;then 
	mkdir -p /usr/local/webserver/php/etc
fi

/usr/sbin/groupadd www
/usr/sbin/useradd -g www www

mkdir -p /data/www
chmod +w /data/www
chown -R www:www /data/www

cd php* 
./configure --prefix=/usr/local/webserver/php --with-config-file-path=/usr/local/webserver/php/etc --with-mysql=/usr/local/webserver/mysql --with-mysqli=/usr/local/webserver/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-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex  --enable-fpm  --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 --without-pear  --with-libdir=lib64 
 make ZEND_EXTRA_LIBS='-liconv'  &&  \
 make install

cp -u php.ini-production /usr/local/webserver/php/etc/php.ini
cd ../ 

cd memcache-3.0.7/

/usr/local/webserver/php/bin/phpize 
./configure --with-php-config=/usr/local/webserver/php/bin/php-config
make && make install
cd ../

cd APC*/
/usr/local/webserver/php/bin/phpize && \
./configure --with-php-config=/usr/local/webserver/php/bin/php-config  --enable-apc-mmap --enable-apc --enable-apc-filehits&& \
make && make install
cd ../

ln -s /usr/local/webserver/mysql/include/* /usr/local/include/

cd PDO_MYSQL*/
/usr/local/webserver/php/bin/phpize && \
./configure --with-php-config=/usr/local/webserver/php/bin/php-config --with-pdo-mysql=/usr/local/webserver/mysql && \
make && make install
cd ../

cd imagick*/
/usr/local/webserver/php/bin/phpize && \
./configure --with-php-config=/usr/local/webserver/php/bin/php-config   --with-libdir=lib64 && \
make && make install
cd ../

######php 各類優化 #######
php_ini="/usr/local/webserver/php/etc/php.ini"
sed -i 's#extension_dir = "./"#\nextension_dir = "/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20100525/"\nextension = "memcache.so"\nextension = "pdo_mysql.so"\nextension = "imagick.so"\nextension = "apc.so"\n#' $php_ini
sed -i 's#output_buffering = Off#output_buffering = On#' $php_ini
sed -i "s#;always_populate_raw_post_data = On#always_populate_raw_post_data = On#g"  $php_ini
sed -i "s#;cgi.fix_pathinfo=0#cgi.fix_pathinfo=0#g" $php_ini
sed -i 's#expose_php = On#expose_php = Off#' 
sed -i 's#disable_functions =#disable_functions =exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source;#' $php_ini
sed -i "s#session.save_handler =#session.save_handler = memcache;#" $php_ini
sed -i "s#session.save_path =#session.save_path ='tcp://127.0.0.1:11211';#" $php_ini



#php-fpm 優化
php_fpm_path="/usr/local/webserver/php/etc/php-fpm.conf"
cp -u /usr/local/webserver/php/etc/php-fpm.conf.default $php_fpm_path
sed -i "s#;rlimit_files = 1024#rlimit_files = 65535#" $php_fpm_path
children_num=50  #每一個子進程消耗 20M左右 50*20=1G
sed -i "s#pm.max_children#pm.max_children = $children_num;#" $php_fpm_path
ret_num=8000
sed -i "s#;pm.max_requests#;pm.max_requests = $ret_num;#" $php_fpm_path
sed -i "s#user = nobody#user = www#" $php_fpm_path
sed -i "s#group = nobody#group = www#" $php_fpm_path

#/usr/local/webserver/php/sbin/php-fpm -t && \
#/usr/local/webserver/php/sbin/php-fpm 
#linsten="/tmp/php-cgi.sock" 配置 sock通信
#############nginx安裝################
#rpm -ivh ./libunwind-1.0.1-4.fc18.x86_64.rpm

#安裝替換標準c庫的內存分配函數malloc 50ns完成一次內存分配 而標準c須要300ns
cd gperftools* 
./configure && \
make && make install
#註釋:64位系統必須添加:--enable-frame-pointers
echo "/usr/local/lib" >> /etc/ld.so.conf.d/usr_local_lib.conf 
echo "/usr/local/include" >> /etc/ld.so.conf 
/sbin/ldconfig
/sbin/ldconfig -N
#爲 gperftools 添加線程目錄:
mkdir -p /tmp/tcmalloc
chmod 0777 /tmp/tcmalloc
cd ../

#export LD_PRELOAD=/usr/local/lib/libtcmalloc_minimal.so
sed -i 's#executing mysqld_safe#executing mysqld_safe\nexport LD_PRELOAD=/usr/local/lib/libtcmalloc_minimal.so\n#' /usr/local/webserver/mysql/bin/mysqld_safe
#文件位置:nginx-1.3.4/auto/lib/google-perftools/conf。將文件中的/usr/local/lib替換成你安##裝的位置,個人位置是/usr/local/googleopreftools,而後再次編譯就不會出現上面的錯誤。個人編
ln -s   /usr/local/lib/libtcmalloc_minimal.so.4.1.0  /usr/local/lib/libtcmalloc.so
ln -s   /usr/local/lib/libtcmalloc_minimal.so.4.1.0  /usr/local/lib/libtcmalloc.so.4
ln -s /usr/local/lib/libprofiler.so /lib64/libprofiler.so.0

cd nginx*
#sed -i "s#/usr/local#/usr/local/gperftools#g" auto/lib/google-perftools/conf && \
#sed -i "s#/opt/local#/usr/local/gperftools#g" auto/lib/google-perftools/conf

./configure --user=www --group=www  --prefix=/usr/local/webserver/nginx \
--with-http_stub_status_module --with-http_ssl_module --with-http_flv_module \
--with-http_gzip_static_module --with-google_perftools_module  --http-fastcgi-temp-path=/tmp/fast_cache&& \
make && make install
cd ../

if [ ! -d /usr/local/webserver/nginx/conf/sites ]; then
	mkdir -p  /usr/local/webserver/nginx/conf/sites
fi

woker_num=$(cat /proc/cpuinfo  | grep processor | wc -l)
#sysctl -a | grep fs.file 查看linux系統文件描述符的方法:
#getconf PAGESIZE 內存分頁大小
cat >/usr/local/webserver/nginx/conf/nginx.conf <<EOF
user  www www;

worker_processes $woker_num;
#爲每一個進程分配cpu,例中將8個進程分配到8 個cpu,固然能夠寫多個,或者將一
#個進程分配到多個cpu。
#worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
error_log  /usr/local/webserver/nginx/logs/nginx_error.log  crit;

pid        /usr/local/webserver/nginx/nginx.pid;
google_perftools_profiles /tmp/tcmalloc;
#Specifies the value for maximum file descriptors that can be opened by #this process.
worker_rlimit_nofile 65535;

events
{
  use epoll;
  worker_connections 65535;
  #每一個進程容許的最多鏈接數,理論上每臺nginx #服務器的最大鏈接數爲worker_processes*worker_connections。
}

http
{
  include       mime.types;
  default_type  application/octet-stream;

  charset  utf-8;

  server_names_hash_bucket_size 128;
  large_client_header_buffers 4 32k;
  client_max_body_size 8m;

  sendfile on;
  tcp_nopush     on;

  keepalive_timeout 60;
 
   fastcgi_cache_path   /tmp/fastcgi_cache  levels=1:2
                       keys_zone=TEST:10m
                       inactive=5m;
  client_header_buffer_size 4k;
  #客戶端請求頭部的緩衝區大小,這個能夠根據你的系統分頁大小來設置,通常一個請求頭的大小不#會超過1k,不過因爲通常系統分頁都要大於1k,因此這裏設置爲分頁大小。
  
  open_file_cache max=65535 inactive=20s;
  #這個將爲打開文件指定緩存,默認是沒有啓用的,max #指定緩存數量,建議和打開文件數一致,inactive 是指通過多長時間文件沒被請求後刪除緩存。
  open_file_cache_valid 80s; #這個是指多長時間檢查一次緩存的有效信息。
  open_file_cache_min_uses 1;
  #open_file_cache 指令中的inactive #參數時間內文件的最少使用次數,若是超過這個數字,文件描述符一直是在緩存中打開的,如上例#,若是有一個文件在inactive 時間內一次沒被使用,它將被移除。
  tcp_nodelay on;

  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 64k;
  fastcgi_buffers 4 64k;
  fastcgi_busy_buffers_size 128k;
  fastcgi_temp_file_write_size 128k;
  fastcgi_cache TEST;
  fastcgi_cache_valid 200 302 1h;
  fastcgi_cache_valid 301 1d;
  fastcgi_cache_valid any 1m;

#爲指定的應答代碼指定緩存時間,如上例中將200,302 應答緩存一小時,301 應答緩存1 
#天,其餘爲1 分鐘。
  fastcgi_cache_min_uses 1;
  fastcgi_cache_use_stale error timeout invalid_header http_500;

   

#緩存在fastcgi_cache_path 指令inactive 參數值時間內的最少使用次數,如上例,若是在5 #分鐘內某文件1 次也沒有被使用,那麼這個文件將被移除。
  gzip off;
  gzip_min_length  1k;
  gzip_buffers     4 16k;
  gzip_http_version 1.0;
  gzip_comp_level 2;
  gzip_types       text/plain application/x-javascript text/css application/xml;
  gzip_vary on;

  log_format  main  '\$remote_addr - \$remote_user [\$time_local] \$request '
                    '"\$status" \$body_bytes_sent' "\$http_referer"
                    '"\$http_user_agent" "\$http_x_forwarded_for"';

  server
  {
    listen          80;
    server_name     denyipaccess;
    index index.php index.html;
    root /dev/null;
	#location /ustats {

	 #ustats memsize=3m;

	 #ustats_refresh_interval 6000;

	 #ustats_html_table_width 95;

	 #ustats_html_table_height 95;

	#}  
  }
  
  include sites/*.conf;

}
EOF

#/usr/sbin/lsof -n | grep tcmalloc
mkdir -p /data/logs
chmod +w /data/logs
chown -R www:www /data/logs


#/usr/local/webserver/nginx/sbin/nginx -t && 
#/usr/local/webserver/nginx/sbin/nginx 

#/usr/local/webserver/nginx/sbin/nginx -s quit nginx 退出 
#ps aux | grep php | grep -v grep | gawk '{print $2}' | tr -s "\n" " " | #xargs kill
#開機自動啓動
line_num=$(cat /etc/rc.local | grep -E "memcached|php-fpm|nginx|mysql|SHu|SHn" | wc -l)
if [ $line_num -le 0 ];then
cat >/etc/rc.local<<EOF
ulimit  -SHu 65535
ulimit -SHn 65535
/data/sh/mysql.sh start
/usr/local/webserver/php/sbin/php-fpm 
/usr/local/webserver/nginx/sbin/nginx
/usr/local/webserver/memcached/bin/memcached -l 127.0.0.1 -p 11211 -d -u www -m 1024 -P /usr/local/webserver/memcached/pid/memcached.pid
EOF
fi
line_num=$(cat /etc/security/limits.conf | grep 65535 | wc -l)
if [ $line_num -le 0 ]; then
cat >>/etc/security/limits.conf<<EOF
* soft nofile 65535
* hard nofile 65535
* soft nproc 65535
* hard nproc 65535
EOF
fi

if [ ! -f /etc/sysctl.conf.bak ];then
	cp /etc/sysctl.conf /etc/sysctl.conf.bak
fi
line_num=$(cat /etc/sysctl.conf  | grep 65536 | wc -l)
if [ $line_num -le 0 ];then
cat >>/etc/sysctl.conf<<EOF
net.ipv4.ip_forward = 0
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
net.ipv4.tcp_max_tw_buckets = 6000
net.ipv4.tcp_sack = 1
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_rmem = 4096 87380 4194304
net.ipv4.tcp_wmem = 4096 16384 4194304
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.netdev_max_backlog = 262144
net.core.somaxconn = 262144
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_fin_timeout = 1
net.ipv4.tcp_keepalive_time = 30
net.ipv4.ip_local_port_range = 1024 65000
EOF
fi

/sbin/sysctl -p
echo "內核優化結束"
echo "正在啓動mysql....."
/data/sh/mysql.sh start
echo "正在檢查php-fpm.conf配置文件....."
/usr/local/webserver/php/sbin/php-fpm -t 
echo "正在啓動php-fpm...."
/usr/local/webserver/php/sbin/php-fpm 
echo "正在檢查nginx.conf文件...."
/usr/local/webserver/nginx/sbin/nginx -t 
echo "正在啓動nginx...."
/usr/local/webserver/nginx/sbin/nginx 
echo "正在啓動memechahed....."
memsize=$(free -m | awk '{print $2}' | head -2 | grep -E "[0-9]")
if [ $memsize -le 1024 ];then
memsize=128
elif [ $memsize -ge 7863 ] ; then
memsize=512
elif [ $memsize -ge 32099 ] ; then
memsize=1024
fi
/usr/local/webserver/memcached/bin/memcached -l 127.0.0.1 -p 11211 \
 -d -u www -m $memsize -P /usr/local/webserver/memcached/pid/memcached.pid
echo "lnmp&memcached啓動完成."

#CREATE TABLE documents_sphinxse
#(
#    id          INTEGER UNSIGNED NOT NULL,
#    weight      INTEGER NOT NULL,
#    query       VARCHAR(3072) NOT NULL,
#    group_id    INTEGER,
#    INDEX(query)
#) ENGINE=SPHINX CONNECTION="sphinx://localhost:9312/mysql";



#linux 默認值 open files 和 max user processes 爲 1024
#ulimit -n #open files
#1024
#ulimit -u  #max user processes
#1024
#問題描述: 說明 server 只容許同時打開 1024 個文件,處理 1024 個用戶進程
#使用ulimit -a 能夠查看當前系統的全部限制值
#ps aux | grep -E "php|mysql|nginx"  查看 lnmp  是否運行



####ngixn 腳本切割日誌
#cat >>/data/sh/cut_nginx_log.sh<<EOF\
#!/bin/bash
# This script run at 00:00
# The Nginx logs path
#logs_path_kerry="/data/logs/www/"
#logs_path_kerry="/data/logs/www/"
#mv ${logs_path_kerry}kerry_nginx.log ${logs_path_kerry}$kerry_nginx_$(date -d #"yesterday" +"%Y%m%d").log
#mv ${logs_path_king}king_nginx.log ${logs_path_king}king_nginx_$(date -d "yesterday" #+"%Y%m%d").log
#kill -USR1 `cat /usr/local/nginx/nginx.pid`
#EOF

#pmap -x  pid  # 查看線程棧的大小
#ll /proc/pid/fd/  #查看那進程打開的文件描述符

####################################################################  javascript

慎用 還沒所有寫完,有時間再來寫

相關文章
相關標籤/搜索