nginx.sh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/bin/bash
  2. # Author: yeho <lj2007331 AT gmail.com>
  3. # Blog: http://blog.linuxeye.com
  4. Install_Nginx()
  5. {
  6. cd $oneinstack_dir/src
  7. . ../functions/download.sh
  8. . ../functions/check_os.sh
  9. . ../options.conf
  10. src_url=http://downloads.sourceforge.net/project/pcre/pcre/$pcre_version/pcre-$pcre_version.tar.gz && Download_src
  11. src_url=http://nginx.org/download/nginx-$nginx_version.tar.gz && Download_src
  12. tar xzf pcre-$pcre_version.tar.gz
  13. cd pcre-$pcre_version
  14. ./configure
  15. make && make install
  16. cd ../
  17. tar xzf nginx-$nginx_version.tar.gz
  18. id -u $run_user >/dev/null 2>&1
  19. [ $? -ne 0 ] && useradd -M -s /sbin/nologin $run_user
  20. cd nginx-$nginx_version
  21. # Modify Nginx version
  22. #sed -i 's@#define NGINX_VERSION.*$@#define NGINX_VERSION "1.2"@' src/core/nginx.h
  23. #sed -i 's@#define NGINX_VER.*NGINX_VERSION$@#define NGINX_VER "Linuxeye/" NGINX_VERSION@' src/core/nginx.h
  24. #sed -i 's@Server: nginx@Server: linuxeye@' src/http/ngx_http_header_filter_module.c
  25. # close debug
  26. sed -i 's@CFLAGS="$CFLAGS -g"@#CFLAGS="$CFLAGS -g"@' auto/cc/gcc
  27. if [ "$je_tc_malloc" == '1' ];then
  28. malloc_module="--with-ld-opt='-ljemalloc'"
  29. elif [ "$je_tc_malloc" == '2' ];then
  30. malloc_module='--with-google_perftools_module'
  31. mkdir /tmp/tcmalloc
  32. chown -R ${run_user}.$run_user /tmp/tcmalloc
  33. fi
  34. [ ! -d "$nginx_install_dir" ] && mkdir -p $nginx_install_dir
  35. ./configure --prefix=$nginx_install_dir --user=$run_user --group=$run_user --with-http_stub_status_module --with-http_spdy_module --with-http_ssl_module --with-ipv6 --with-http_gzip_static_module --with-http_realip_module --with-http_flv_module $malloc_module
  36. make && make install
  37. if [ -d "$nginx_install_dir/conf" ];then
  38. echo -e "\033[32mNginx install successfully! \033[0m"
  39. else
  40. rm -rf $nginx_install_dir
  41. echo -e "\033[31mNginx install failed, Please Contact the author! \033[0m"
  42. kill -9 $$
  43. fi
  44. [ -z "`grep ^'export PATH=' /etc/profile`" ] && echo "export PATH=$nginx_install_dir/sbin:\$PATH" >> /etc/profile
  45. [ -n "`grep ^'export PATH=' /etc/profile`" -a -z "`grep $nginx_install_dir /etc/profile`" ] && sed -i "s@^export PATH=\(.*\)@export PATH=$nginx_install_dir/bin:\1@" /etc/profile
  46. . /etc/profile
  47. cd ../../
  48. OS_CentOS='/bin/cp init/Nginx-init-CentOS /etc/init.d/nginx \n
  49. chkconfig --add nginx \n
  50. chkconfig nginx on'
  51. OS_Debian_Ubuntu='/bin/cp init/Nginx-init-Ubuntu /etc/init.d/nginx \n
  52. update-rc.d nginx defaults'
  53. OS_command
  54. sed -i "s@/usr/local/nginx@$nginx_install_dir@g" /etc/init.d/nginx
  55. mv $nginx_install_dir/conf/nginx.conf{,_bk}
  56. if [ "$Apache_version" == '1' -o "$Apache_version" == '2' ];then
  57. /bin/cp conf/nginx_apache.conf $nginx_install_dir/conf/nginx.conf
  58. else
  59. /bin/cp conf/nginx.conf $nginx_install_dir/conf/nginx.conf
  60. fi
  61. sed -i "s@/home/wwwroot/default@$home_dir/default@" $nginx_install_dir/conf/nginx.conf
  62. sed -i "s@/home/wwwlogs@$wwwlogs_dir@g" $nginx_install_dir/conf/nginx.conf
  63. sed -i "s@^user www www@user $run_user $run_user@" $nginx_install_dir/conf/nginx.conf
  64. [ "$je_tc_malloc" == '2' ] && sed -i 's@^pid\(.*\)@pid\1\ngoogle_perftools_profiles /tmp/tcmalloc;@' $nginx_install_dir/conf/nginx.conf
  65. # worker_cpu_affinity
  66. CPU_num=`cat /proc/cpuinfo | grep processor | wc -l`
  67. if [ $CPU_num == 2 ];then
  68. sed -i 's@^worker_processes.*@worker_processes 2;\nworker_cpu_affinity 10 01;@' $nginx_install_dir/conf/nginx.conf
  69. elif [ $CPU_num == 3 ];then
  70. sed -i 's@^worker_processes.*@worker_processes 3;\nworker_cpu_affinity 100 010 001;@' $nginx_install_dir/conf/nginx.conf
  71. elif [ $CPU_num == 4 ];then
  72. sed -i 's@^worker_processes.*@worker_processes 4;\nworker_cpu_affinity 1000 0100 0010 0001;@' $nginx_install_dir/conf/nginx.conf
  73. elif [ $CPU_num == 6 ];then
  74. sed -i 's@^worker_processes.*@worker_processes 6;\nworker_cpu_affinity 100000 010000 001000 000100 000010 000001;@' $nginx_install_dir/conf/nginx.conf
  75. elif [ $CPU_num == 8 ];then
  76. sed -i 's@^worker_processes.*@worker_processes 8;\nworker_cpu_affinity 10000000 01000000 00100000 00010000 00001000 00000100 00000010 00000001;@' $nginx_install_dir/conf/nginx.conf
  77. else
  78. echo Google worker_cpu_affinity
  79. fi
  80. # logrotate nginx log
  81. cat > /etc/logrotate.d/nginx << EOF
  82. $wwwlogs_dir/*nginx.log {
  83. daily
  84. rotate 5
  85. missingok
  86. dateext
  87. compress
  88. notifempty
  89. sharedscripts
  90. postrotate
  91. [ -e /var/run/nginx.pid ] && kill -USR1 \`cat /var/run/nginx.pid\`
  92. endscript
  93. }
  94. EOF
  95. sed -i "s@^web_install_dir.*@web_install_dir=$nginx_install_dir@" options.conf
  96. ldconfig
  97. service nginx start
  98. }