nginx.sh 4.5 KB

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