centos6.0 x64下自动编译安装NPM的脚本

发布时间:2020-11-25编辑:脚本学堂
在centos x64下自动编译安装nginx、php、和mysql,在centos 6.0 x64下测试通过,虽然编译安装后工作正常,但是脚本比较乱,并且中间有很多编译错误。

centos x64下自动编译安装nginx、php、和mysql,在centos 6.0 x64下测试通过,虽然编译安装后工作正常,但是脚本比较乱,并且中间有很多编译错误。
大家在使用中请根据自己的实际情况进行调整与修改,以适合您的需要。

本来还计划安装google-perftools的,但是,google-perftools虽然编译安装了,在nginx启动时报错,所以注释掉了google-perftools的编译安装。
nginx的模块ngx_http_upstream_keepalive在我这里一直编译出错,因此也注释掉了。
脚本可以接受的参数是:init|mysql|php|nginx|cfgall|cfgsys|cfgphp|cfgnginx|all
init:用于初始化系统
mysql:用于编译安装mysql
php:用于编译安装php
nginx:用于编译安装nginx
cfgall:用于配置系统、php和nginx
cfgsys:用于软件编译安装完毕后配置系统
cfgphp:用于配置php
cfgnginx:用于配置nginx
all:包括所有,初始化系统、安装软件、配置软件、配置系统
脚本中均用中文注释,不影响脚本运行。
需要注意的是,如果拷贝脚本内容,在windows下用editplus之类的软件进行编辑的话,需要将文件格式保存成unix类型。
脚本比较乱,另外本人菜鸟,脚本基本上是命令的堆积,没有考虑到更多的方面,有什么错误或者建议请大家不吝指点。

 

复制代码 代码如下:

#!/bin/bash
########################################################
### autonpm ##
### 自动安装nginx、php和mysql ##
### centos 6.0 x64下测试通过 ##
### 作者:土豆大葱 ##
### blog:tddc.blog.chinaunix.net ##
########################################################
#---------------------安装一些必须的软件及初步准备-------------------
#设置编译优化参数,对准不同cpu的安全参数,请参考gentoo safe cflags,网址:http://en.gentoo-wiki.com/wiki/Safe_Cflags
export CHOST="x86_64-pc-linux-gnu"
export CFLAGS="-march=native -mtune=generic -O3 -pipe"
export CXXFLAGS="${CFLAGS}"
#设置Safe LDFLAGS,参见:http://en.gentoo-wiki.com/wiki/Safe_LDFLAGS
#export LDFLAGS="-Wl,-O1 -Wl,--as-needed"
yum install wget -y
#-----------------------------设置一些变量-----------------------
#文件名以及文件下载地址需要手工设置,文件解压后的目录名自动获取,无需手工设置
#------设置文件名变量------
mysql_tar_gz_name="mysql-5.5.22.tar.gz"
php_tar_gz_name="php-5.2.17.tar.gz"
nginx_tar_gz_name="nginx-1.1.9.tar.gz"
libunwind_tar_gz_name="libunwind-1.0.1.tar.gz"
gperftools_tar_gz_name="gperftools-2.0.tar.gz"
#--------php module---------
phpfpm_tar_gz_name="php-5.2.17-fpm-0.5.14.diff.gz"
freetype_tar_gz_name="freetype-2.4.9.tar.gz"
memcache_tar_gz_name="memcache-2.2.6.tgz"
eaccelerator_tar_gz_name="eaccelerator-0.9.6.1.tar.bz2"
mhash_tar_gz_name="mhash-0.9.9.9.tar.bz2"
libmcrypt_tar_gz_name="libmcrypt-2.5.8.tar.bz2"
mcrypt_tar_gz_name="mcrypt-2.6.8.tar.gz"
nmodmemc_tar_gz_name="agentzh-memc-nginx-module.tar.gz"
#nmodkeep_tar_gz_name="ngx_http_upstream_keepalive.tar.gz"
nmodcache_tar_gz_name="ngx_cache_purge-1.5.tar.gz"
#------设置文件解压后的目录名,无需改动,可自动获取------
mysql_source_dir_name=""
php_source_dir_name=""
nginx_source_dir_name=""
libunwind_source_dir_name=""
gperftools_source_dir_name=""
#--------php module---------
phpfpm_source_dir_name=""
freetype_source_dir_name=""
memcache_source_dir_name=""
eaccelerator_source_dir_name=""
mhash_source_dir_name=""
libmcrypt_source_dir_name=""
mcrypt_source_dir_name=""
nmodmemc_source_dir_name=""
#nmodkeep_source_dir_name=""
nmodcache_source_dir_name=""
#------设置文件下载地址------
mysql_download_url="http://mysql.cdpa.nsysu.edu.tw/Downloads/MySQL-5.5/mysql-5.5.22.tar.gz"
php_download_url="http://cn.php.net/get/php-5.2.17.tar.gz/from/this/mirror"
nginx_download_url="http://nginx.org/download/nginx-1.1.9.tar.gz"
libunwind_download_url="http://mirror.yongbok.net/nongnu/libunwind/libunwind-1.0.1.tar.gz"
gperftools_download_url="http://gperftools.googlecode.com/files/gperftools-2.0.tar.gz"
phpfpm_download_url="http://php-fpm.org/downloads/php-5.2.17-fpm-0.5.14.diff.gz"
freetype_download_url="http://download.savannah.gnu.org/releases/freetype/freetype-2.4.9.tar.gz"
memcache_download_url="http://pecl.php.net/get/memcache-2.2.6.tgz"
eaccelerator_download_url="http://nchc.dl.sourceforge.net/project/eaccelerator/eaccelerator/eAccelerator%200.9.6.1/eaccelerator-0.9.6.1.tar.bz2"
mhash_download_url="http://nchc.dl.sourceforge.net/project/mhash/mhash/0.9.9.9/mhash-0.9.9.9.tar.bz2"
libmcrypt_download_url="http://nchc.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.bz2"
mcrypt_download_url="http://nchc.dl.sourceforge.net/project/mcrypt/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz"
nmodmemc_download_url="https://github.com/agentzh/memc-nginx-module/tarball/master"
#nmodkeep_download_url="http://mdounin.ru/hg/ngx_http_upstream_keepalive/archive/tip.tar.gz"
nmodcache_download_url="http://labs.frickle.com/files/ngx_cache_purge-1.5.tar.gz"
#---------设置安装路径--------
#设置基本安装路径,nginx、php、mysql均安装于此路径下
install_path="/usr/local/webserver"
htdocs_path="/data0/htdocs"
nginx_log_path="/data1/logs"
mysql_data_all_path="/data0/mysql"
mysql_data_path="$mysql_data_all_path/data"
mysql_binlog_path="$mysql_data_all_path/binlog"
mysql_relaylog_path="$mysql_data_all_path/relaylog"
mysql_cfg_path="/etc/mysql"
lib_path="/usr"
#设置安装日志文件
install_log_name="myinstall.log"
#获取安装文件目录
src=$(pwd)/
install_log=$src$install_log_name
#测试安装日志文件是否存在,如果存在,删除日志文件,然后新建一个日志文件。如果不存在则新建一个日志文件
if [ -e $install_log ]
then
rm -rf $install_log
touch $install_log
else touch $install_log
fi
#---------路径设置完毕--------
#函数,用于写入安装日志
write_log ()
{
echo "$(date +%X):$1" >> $install_log
sync
}
#函数,初始化
init_nmp ()
{
yum install autoconf make gcc gcc-c++ cmake ncurses ncurses-devel bison openssl openssl-devel
libxml2 libxml2-devel gd-devel curl libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel patch pcre pcre-devel
glibc glibc-devel glib2 glib2-devel e2fsprogs e2fsprogs-devel flex libtool -y
new_mkdir "$install_path"
new_mkdir "$mysql_data_path"
new_mkdir "$mysql_binlog_path"
new_mkdir "$mysql_relaylog_path"
new_mkdir "$mysql_cfg_path"
groupadd mysql
useradd -r -s /sbin/nologin -g mysql mysql
groupadd www
useradd www -s /sbin/nologin -g www
chown -R mysql:mysql ${mysql_data_all_path}
new_mkdir "/etc/php.d"
#----测试一些库文件----
write_log "----------Test some lib----------"
test_lib "libfreetype"
test_lib "libjpeg"
test_lib "libpng"
test_lib "libgd"
test_lib "libcrypto"
test_lib "libssl"
test_lib "libz"
test_lib "libxml2"
test_lib "libevent"
test_lib "libcurl"
test_lib "libcurses"
test_lib "libglib"
ln -s /usr/lib64/openssl /usr/lib/openssl
write_log "----------Test lib end----------"
}
#函数,用来测试源码包是否存在,不存在的话从网上下载,并设置源码包解压后的目录名
test_pak ()
{
cd $src
write_log "test pak $1"
if [ -s $1 ]
then
a=$2
eval $2=$(tar -tf $1 | sed -n '$p' | cut -d / -f 1) && write_log "Now $1 path is : ${!a}"
else
if $(wget -O $1 $3)
then
a=$2
eval $2=$(tar -tf $1 | sed -n '$p' | cut -d / -f 1) && write_log "Now $1 path is : ${!a} "
else
write_log "Error: can not download $1"
exit 1
fi
fi
}
#函数,用于解压缩源码包
untar_source ()
{
cd $src
write_log "---------- Begin install $1 ----------"
if [ -d $2 ]
then rm -rf $2
fi
if $(tar xf $1)
then
write_log "untar $1 successfuly"
cd $2
else
write_log "Error: untar $1 fail,exit this progame"
exit 1
fi
}
#函数,用于编译源码包
build_source ()
{
write_log "---Now path is $PWD---"
write_log "---Now CFLAGS is $CFLAGS---"
if [ -z "$1" ]
then
write_log "------config error info------"
./configure 2>>${install_log}
else
write_log "Config is: $1"
write_log "------config error info------"
./configure $1 2>>${install_log}
fi
write_log "------Make error info------"
make 2>>${install_log} && write_log "------Make install error info------" && make install 2>>${install_log}
write_log "---------- end install ----------"
}
#函数,创建一个文件夹
new_mkdir ()
{
if [ -d $1 ]
then
PS3="$1 already exists,do you want to :"
select d_answer in "continue to use the directory." "Del it and recreate directory." "Input a new path."; do
break
done
case "$d_answer" in
"Continue to use the directory." ) echo "You select continue to use the directory.";;
"Del it and recreate directory." )
rm -rf $1
mkdir -p $1
;;
esac
else
mkdir -p $1
write_log "$1 create successfuly"
fi
}
#函数,测试库文件是否在/usr/lib中,如果不在,就从/usr/lib64中链接
test_lib ()
{
if [ -e /usr/lib/$1* ]
then write_log "$1 is in lib"
else
echo "$1 is not in lib,now link it from lib64"
for element in /usr/lib64/$1*; do
if [ -h "${element}" ]
then
ln -s /usr/lib64/$(readlink $element) /usr/lib/$(basename $element)
else
ln -s ${element} /usr/lib/$(basename $element)
fi
done
fi
}

#---------测试源码包---------
#test_pak $libunwind_tar_gz_name "libunwind_source_dir_name" $libunwind_download_url &&
#test_pak $gperftools_tar_gz_name "gperftools_source_dir_name" $gperftools_download_url &&
test_pak $nmodmemc_tar_gz_name "nmodmemc_source_dir_name" $nmodmemc_download_url &&
test_pak $nmodcache_tar_gz_name "nmodcache_source_dir_name" $nmodcache_download_url &&
test_pak $nginx_tar_gz_name "nginx_source_dir_name" $nginx_download_url &&
test_pak $mysql_tar_gz_name "mysql_source_dir_name" $mysql_download_url &&
test_pak $libmcrypt_tar_gz_name "libmcrypt_source_dir_name" $libmcrypt_download_url &&
test_pak $mhash_tar_gz_name "mhash_source_dir_name" $mhash_download_url &&
test_pak $mcrypt_tar_gz_name "mcrypt_source_dir_name" $mcrypt_download_url &&
test_pak $phpfpm_tar_gz_name "phpfpm_source_dir_name" $phpfpm_download_url &&
test_pak $php_tar_gz_name "php_source_dir_name" $php_download_url &&
test_pak $memcache_tar_gz_name "memcache_source_dir_name" $memcache_download_url &&
test_pak $eaccelerator_tar_gz_name "eaccelerator_source_dir_name" $eaccelerator_download_url &&
write_log "test pak over"
#---------源码包测试完毕---------
#---------设置编译参数--------
libunwind_cfg="--prefix=${lib_path}"
gperftools_cfg="--prefix=${lib_path} --enable-frame-pointers"
mysql_cfg="-DCMAKE_INSTALL_PREFIX=$install_path/mysql
-DMYSQL_DATADIR=$mysql_data_path
-DDEFAULT_CHARSET=utf8
-DWITH_EXTRA_CHARSETS=utf8,gbk,gb2312
-DDEFAULT_COLLATION=utf8_general_ci
-DWITH_LIBWRAP=1
-DWITH_SSL=yes
-DWITH_INNOBASE_STORAGE_ENGINE=1
-DWITH_ARCHIVE_STORAGE_ENGINE=1
-DWITH_BLACKHOLE_STORAGE_ENGINE=1
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1
-DSYSCONFDIR=$mysql_cfg_path
-DMYSQL_USER=mysql"
php_cfg="--prefix=${install_path}/php --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d
--with-mysql=${install_path}/mysql --with-mysqli=${install_path}/mysql/bin/mysql_config
--enable-fastcgi --enable-ftp --enable-fpm --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-curl --with-openssl
--enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-mhash --enable-sockets --enable-zip
--without-sqlite --disable-pdo"
nginx_cfg="--prefix=${install_path}/nginx --user=www --group=www
--with-http_stub_status_module --with-http_ssl_module --with-openssl=
--with-http_addition_module --with-zlib=
--with-file-aio --with-http_gzip_static_module
--add-module=../${nmodmemc_source_dir_name}
--add-module=../${nmodcache_source_dir_name}"
memcache_cfg="--enable-memcache --with-php-config=${install_path}/php/bin/php-config"
eaccelerator_cfg="--enable-eaccelerator --with-php-config=${install_path}/php/bin/php-config"
write_log "config end"
#---------编译参数设置完毕----
#---------------------config end---------------------------
#---------------------安装结束-----------------------------

#如果是64位系统,需要安装libunwind,然后再安装google-perftools
is_x64 ()
{
if [ $(uname -p) = "x86_64" ]
then
write_log "Notice: your os is x86_64,need to install libunwind"
write_log "libunwind installing ..."
untar_source $libunwind_tar_gz_name $libunwind_source_dir_name
CFLAGS=-fPIC ./configure ${libunwind_cfg}
make CFLAGS=-fPIC 2>>${install_log}
make CFLAGS=-fPIC install 2>>${install_log}
fi
}
#================安装mysql、php和nginx==============
install_mysql ()
{
#new_mkdir $install_path
#====安装mysql====
write_log "----------------Mysql install begin-------------------"
untar_source ${mysql_tar_gz_name} ${mysql_source_dir_name}
write_log "mysql cfg is : ${mysql_cfg}"
write_log "------Mysql config error info------"
cmake . ${mysql_cfg} 2>>${install_log}
write_log "------Mysql Make error info------"
make 2>>${install_log} && write_log "------Make install error info------" && make install 2>>${install_log}
write_log "----------------Mysql make install end----------------"
cd $install_path/mysql
chown -R mysql .
chgrp -R mysql .
rm -rf sql-bench mysql-test
chown -R mysql:mysql ${mysql_data_all_path}
cp support-files/my-huge.cnf ${mysql_cfg_path}/my.cnf
scripts/mysql_install_db --user=mysql --basedir=${install_path}/mysql --datadir=${mysql_data_path}
chown -R root .
cp support-files/mysql.server /etc/init.d/mysqld
chmod 744 /etc/init.d/mysqld
chkconfig --level 345 mysqld on
write_log "Mysql is add to services"
#service mysqld start
#write_log "Mysql is start"
}
#====mysql安装完毕====
#====安装php====
install_php ()
{
write_log "================Begian install php================"
#----安装freetype2----
#--freetype2已经被安装,yum gd时是gd的依赖包
#untar_source ${freetype_tar_gz_name} ${freetype_source_dir_name}
#make setup
#write_log "------Make error info------"
#make 2>>${install_log} && write_log "------Make install error info------" && make install 2>>${install_log} && write_log "------Freetype2 install end------"
#cd ../
#----freetype2安装完毕----
#----安装libmcrypt----
write_log "-----Begian install libmcrypt-----"
untar_source ${libmcrypt_tar_gz_name} ${libmcrypt_source_dir_name}
build_source
/sbin/ldconfig
cd libltdl/
build_source --enable-ltdl-install
write_log "-----End install libmcrypt-----"
#cd ../../
#----libmcrypt安装完毕----
#----安装mhash----
write_log "-----Begian install mhash-----"
untar_source $mhash_tar_gz_name $mhash_source_dir_name
build_source
#cd ../
#----mhash安装完毕----
#----设置库连接----
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
write_log "-----End install mhash-----"
#----库连接设置完毕----
#----安装mcrypt----
write_log "-----Begian install mcrypt-----"
untar_source $mcrypt_tar_gz_name $mcrypt_source_dir_name
/sbin/ldconfig
build_source
write_log "-----End install mcrypt-----"
#cd ../
#----mcrypt安装完毕----
#----开始安装php----
write_log "------Begian install php------"
cd ${src}
if [ -d ${php_source_dir_name} ]
then rm -rf ${php_source_dir_name}
fi
tar xf $php_tar_gz_name
gzip -cd ${phpfpm_tar_gz_name} | patch -d ${php_source_dir_name} -p1
cd ${php_source_dir_name}
write_log "Now path is ${PWD}"
build_source "${php_cfg}"
cp php.ini-dist /etc/php.ini
write_log "-----End install php-----"
#----安装Memcached----
write_log "-----Begian install memcached-----"
untar_source ${memcache_tar_gz_name} ${memcache_source_dir_name}
${install_path}/php/bin/phpize
build_source "${memcache_cfg}"
write_log "-----End install memcached-----"
#----安装eaccelerator----
write_log "-----Begian install eaccelerator-----"
untar_source ${eaccelerator_tar_gz_name} ${eaccelerator_source_dir_name}
${install_path}/php/bin/phpize
build_source "${eaccelerator_cfg}"
write_log "-----End install eaccelerator-----"
write_log "================End install php================"
#====php安装完毕====
#====安装nginx====
}
install_nginx ()
{
#is_x64
#安装google-perftools
#untar_source $gperftools_tar_gz_name $gperftools_source_dir_name
#build_source "${gperftools_cfg}"
untar_source ${nmodmemc_tar_gz_name} ${nmodmemc_source_dir_name}
#untar_source $nmodkeep_tar_gz_name $nmodkeep_source_dir_name
untar_source ${nmodcache_tar_gz_name} ${nmodcache_source_dir_name}
untar_source ${nginx_tar_gz_name} ${nginx_source_dir_name}
sed -i 's:^CFLAGS="$CFLAGS -g"$:#CFLAGS="$CFLAGS -g":' auto/cc/gcc
build_source "${nginx_cfg}"
}
#====nginx安装完毕====
#====配置nginx====
cfg_nginx ()
{
cat > /etc/init.d/nginx << "EOF"
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse
# proxy and IMAP/POP3 proxy server # processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/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="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
lockfile=/var/lock/subsys/nginx
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
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
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/null2>&1
}
case "$1" in
start)
rh_status_q &&exit0
$1
;;
stop)
rh_status_q ||exit0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q ||exit7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q ||exit0
;;
*)
echo$"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit2
esac
EOF
sed -i "s:/usr/local/nginx/sbin/nginx:${install_path}/nginx/sbin/nginx:" /etc/init.d/nginx
sed -i "s:/usr/local/nginx/conf/nginx.conf:${install_path}/nginx/conf/nginx.conf:" /etc/init.d/nginx
chmod +x /etc/init.d/nginx
chkconfig –-add nginx
chkconfig nginx on
new_mkdir ${htdocs_path}
new_mkdir ${nginx_log_path}
chmod +w ${htdocs_path}
chown -R www:www ${htdocs_path}
chmod +w ${nginx_log_path}
chown -R www:www ${nginx_log_path}
mkdir -p /tmp/tcmalloc
chmod 0777 /tmp/tcmalloc
rm -f ${install_path}/nginx/conf/nginx.conf
cat > ${install_path}/nginx/conf/nginx.conf << "EOF"
user www www;

worker_processes 1;

error_log /data1/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;
}

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

  #charset gb2312;
 
  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  client_max_body_size 8m;
 
  sendfile on;
  tcp_nopush on;

  keepalive_timeout 60;

  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;

  gzip on;
  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;

  #limit_zone crawler $binary_remote_addr 10m;

  server
  {
listen 80;
server_name localhost;
index index.html index.htm index.php;
root /data0/htdocs;

#limit_conn crawler 20;
 
location ~ .*.(php|php5)?$
{
  #fastcgi_pass unix:/tmp/php-cgi.sock;
  fastcgi_pass 127.0.0.1:9000;
  fastcgi_index index.php;
  include fastcgi.conf;
}

location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
{
  expires 30d;
}

location ~ .*.(js|css)?$
{
  expires 1h;
}

log_format access '$remote_addr - $remote_user [$time_local] "$request" '
  '$status $body_bytes_sent "$http_referer" '
  '"$http_user_agent" $http_x_forwarded_for';
access_log /data1/logs/access.log access;
  }
}
EOF
sed -i "s:/data1/logs/nginx_error.log:${nginx_log_path}/nginx_error.log:" ${install_path}/nginx/conf/nginx.conf
sed -i "s:/usr/local/webserver/nginx/nginx.pid:${install_path}/nginx/nginx.pid:" ${install_path}/nginx/conf/nginx.conf
sed -i "s:/data0/htdocs:${htdocs_path}:" ${install_path}/nginx/conf/nginx.conf
sed -i "s:/data1/logs/access.log:${nginx_log_path}/access.log:" ${install_path}/nginx/conf/nginx.conf
#service nginx start
}
#====nginx配置完毕====
#====配置php====
cfg_php ()
{
write_log "begian config php"
mkdir -p /tmp/eaccelerator_cache
chmod 0777 /tmp/eaccelerator_cache
sed -i "s#extension_dir = "./"#extension_dir = "${install_path}/php/lib/php/extensions/no-debug-non-zts-20060613/"nextension = "memcache.so"n#" /etc/php.ini
sed -i 's#output_buffering = Off#output_buffering = On#' /etc/php.ini
sed -i "s#; always_populate_raw_post_data = On#always_populate_raw_post_data = On#g" /etc/php.ini
sed -i "s#; cgi.fix_pathinfo=0#cgi.fix_pathinfo=0#g" /etc/php.ini
cat >> /etc/php.ini << "EOF"
[eaccelerator]
zend_extension="/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"
eaccelerator.shm_size="64"
eaccelerator.cache_dir="/tmp/eaccelerator_cache"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="3600"
eaccelerator.shm_prune_period="3600"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"
EOF
sed -i "s#/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/#${install_path}/php/lib/php/extensions/no-debug-non-zts-20060613/#g" /etc/php.ini
sed -i 's#Unix user of processes#Unix user of processesn<value name="user">www</value>n#' ${install_path}/php/etc/php-fpm.conf
sed -i 's#Unix group of processes#Unix group of processesn<value name="group">www</value>n#' ${install_path}/php/etc/php-fpm.conf
sed -i 's#<value name="max_children">5</value>#<value name="max_children">128</value>#' ${install_path}/php/etc/php-fpm.conf
sed -i 's#<value name="rlimit_files">1024</value>#<value name="rlimit_files">65535</value>#' ${install_path}/php/etc/php-fpm.conf
sed -i 's#<value name="max_requests">500</value>#<value name="max_requests">1024</value>#' ${install_path}/php/etc/php-fpm.conf
write_log "php config end"
}
#====php配置完毕====
#====配置操作系统====
cfg_sys ()
{
#----关闭ipv6----
cat >> /etc/modprobe.d/dist.conf << "EOF"
alias net-pf-10 off
alias ipv6 off
EOF
#----禁用selinux----
sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config
echo "selinux is disabled,you must reboot!"
service iptables stop
service ip6tables stop
chkconfig --del iptables
chkconfig --del ip6tables
#----配置系统参数----
cat >> /etc/sysctl.conf << "EOF"
# Add
net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog = 32768
net.core.somaxconn = 32768

net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216

net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2

net.ipv4.tcp_tw_recycle = 1
#net.ipv4.tcp_tw_len = 1
net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_max_orphans = 3276800

#net.ipv4.tcp_fin_timeout = 30
#net.ipv4.tcp_keepalive_time = 120
net.ipv4.ip_local_port_range = 1024 65535
EOF
#设置启动项
echo -ne "
* soft nofile 65536
* hard nofile 65536
" >> /etc/security/limits.conf
echo "${install_path}/php/sbin/php-fpm start" >> /etc/rc.local
}
#====操作系统配置完毕====
case $1 in
init)
init_nmp
;;
mysql)
install_mysql
;;
php)
install_php
;;
nginx)
install_nginx
;;
cfgall)
cfg_sys
cfg_php
cfg_nginx
;;
cfgsys)
cfg_sys
;;
cfgphp)
cfg_php
;;
cfgnginx)
cfg_nginx
;;
all)
init_nmp
install_nginx
install_mysql
install_php
cfg_sys
cfg_php
cfg_nginx
;;
*)
echo "Usage:`basename $0` {init|mysql|php|nginx|cfgall|cfgsys|cfgphp|cfgnginx|all}"
;;
esac

附:脚本文件下载