centos 一键安装配置nginx脚本

àì夳堔傛蜴生んèń 2022-07-14 14:51 352阅读 0赞

centos 一键安装配置nginx脚本

installNginx.ssh

用vi或则vim编辑 installNginx.ssh

  1. #!/bin/bash
  2. # author:kwin
  3. # Email:kwinwong@hotmail.com
  4. src="/usr/local/src/"
  5. cd $src
  6. #找到指定进程,并杀死
  7. #findPortKill 80
  8. findPortKill (){
  9. processe=`lsof -i:${1} -n|awk '{print $2}'|grep '^[1-9]'`
  10. for i in $processe
  11. do
  12. # echo "Kill the $1 process [ $i ]"
  13. kill -9 $i
  14. done
  15. }
  16. #将命令所在目录添加到系统参数PATH中,方便调用
  17. addToPATH(){
  18. bin=${1}
  19. echo $PATH|grep ${bin} >/dev/null
  20. if [ $? -ne 0 ]; then
  21. echo "export PATH=\$PATH:${bin}">>/etc/profile
  22. fi
  23. }
  24. nginxConf(){
  25. cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
  26. sed -i '35,79c include ../site/*.conf;' /usr/local/nginx/conf/nginx.conf
  27. mkdir /usr/local/nginx/site
  28. cat>/usr/local/nginx/site/default.conf<<EOF
  29. server {
  30. listen 80;
  31. server_name localhost;
  32. gzip on; #开启gizip
  33. gzip_buffers 32 4K;#压缩在内存中缓冲32块? 每块4K
  34. gzip_comp_level 6 ;#压缩级别 推荐6
  35. gzip_min_length 4000;#开始压缩的最小长度4bit
  36. gzip_types text/css text/xml apploation/x-javascript;#只对CSSXMLHTMLJS文件进行压缩
  37. #charset koi8-r;
  38. #access_log logs/host.access.log main;
  39. root /var/www;
  40. location / {
  41. # try_files \$uri \$uri/ /index.php?\$query_string;
  42. index index.html index.htm index.php;
  43. #如果是jpg、jpeg、gif、png、js、css则缓存一天
  44. if (\$fastcgi_script_name ~* \.[jpg|jpeg|gif|png|js|css] ) {
  45. expires 1d;
  46. }
  47. }
  48. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  49. #
  50. location ~ \.php\$ {
  51. fastcgi_pass 127.0.0.1:9000;
  52. fastcgi_index index.php;
  53. fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
  54. include fastcgi_params;
  55. }
  56. #error_page 404 /404.html;
  57. # redirect server error pages to the static page /50x.html
  58. #
  59. error_page 500 502 503 504 /50x.html;
  60. location = /50x.html {
  61. root html;
  62. }
  63. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  64. #
  65. #location ~ \.php\$ {
  66. # proxy_pass http://127.0.0.1;
  67. #}
  68. # deny access to .htaccess files, if Apache's document root
  69. # concurs with nginx's one
  70. #
  71. #location ~ /\.ht {
  72. # deny all;
  73. #}
  74. }
  75. EOF
  76. }
  77. addNginxService(){
  78. cat>/etc/init.d/nginx<<EOF
  79. #!/bin/sh
  80. #
  81. # nginx - this script starts and stops the nginx daemin
  82. #
  83. # chkconfig: - 85 15
  84. # description: Nginx is an HTTP(S) server, HTTP(S) reverse \
  85. # proxy and IMAP/POP3 proxy server
  86. # processname: nginx
  87. # config: /usr/local/nginx/conf/nginx.conf
  88. # pidfile: /usr/local/nginx/logs/nginx.pid
  89. # Source function library.
  90. . /etc/rc.d/init.d/functions
  91. # Source networking configuration.
  92. . /etc/sysconfig/network
  93. # Check that networking is up.
  94. [ "$NETWORKING" = "no" ] && exit 0
  95. nginx="/usr/local/nginx/sbin/nginx"
  96. prog="Nginx"
  97. NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
  98. lockfile=/var/lock/subsys/nginx
  99. start() {
  100. [ -x $nginx ] || exit 5
  101. [ -f $NGINX_CONF_FILE ] || exit 6
  102. echo -n $"Starting $prog: "
  103. daemon $nginx -c $NGINX_CONF_FILE
  104. retval=$?
  105. echo
  106. [ $retval -eq 0 ] && touch $lockfile
  107. return $retval
  108. }
  109. stop() {
  110. echo -n $"Stopping $prog: "
  111. killproc $prog -QUIT
  112. retval=$?
  113. echo
  114. [ $retval -eq 0 ] && rm -f $lockfile
  115. return $retval
  116. }
  117. restart() {
  118. configtest || return $?
  119. stop
  120. start
  121. }
  122. reload() {
  123. configtest || return $?
  124. echo -n $"Reloading $prog: "
  125. killproc $nginx -HUP
  126. RETVAL=$?
  127. echo
  128. }
  129. force_reload() {
  130. restart
  131. }
  132. configtest() {
  133. $nginx -t -c $NGINX_CONF_FILE
  134. }
  135. rh_status() {
  136. status $prog
  137. }
  138. rh_status_q() {
  139. rh_status >/dev/null 2>&1
  140. }
  141. case "$1" in
  142. start)
  143. rh_status_q && exit 0
  144. $1
  145. ;;
  146. stop)
  147. rh_status_q || exit 0
  148. $1
  149. ;;
  150. restart|configtest)
  151. $1
  152. ;;
  153. reload)
  154. rh_status_q || exit 7
  155. $1
  156. ;;
  157. force-reload)
  158. force_reload
  159. ;;
  160. status)
  161. rh_status
  162. ;;
  163. condrestart|try-restart)
  164. rh_status_q || exit 0
  165. ;;
  166. *)
  167. echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
  168. exit 2
  169. esac
  170. EOF
  171. chmod 755 /etc/init.d/nginx
  172. chkconfig --add nginx
  173. chkconfig --level 2345 nginx on
  174. }
  175. #安装nginx
  176. installNginx(){
  177. yum -y install pcre-devel openssl openssl-devel gcc gcc-c++ ncurses-devel perlddd
  178. fileName="nginx-1.9.9"
  179. package="${fileName}.tar.gz"
  180. installDir="/usr/local/nginx"
  181. if test ! -f ${package}
  182. then
  183. wget http://nginx.org/download/${package}
  184. fi
  185. tar zxvf $package
  186. cd $fileName
  187. ./configure --prefix=${installDir}
  188. make && make install
  189. echo "安装完成"
  190. nginxConf
  191. #如果出现错误 找到80占用的进程并杀死,再重启
  192. #如果还有问题 请自行调试配置文件
  193. /usr/local/nginx/sbin/nginx 1> /dev/null 2>&1
  194. if [ $? -ne 0 ]; then
  195. findPortKill 80
  196. /usr/local/nginx/sbin/nginx
  197. fi
  198. #sleep : 默认以秒为单位。
  199. #usleep : 默认以微秒为单位。
  200. #1s = 1000ms = 1000000us
  201. usleep 100000
  202. pid=`cat /usr/local/nginx/logs/nginx.pid`
  203. echo "nginx 已经启动,进程号为${pid}"
  204. bin="${installDir}/sbin"
  205. #将命令所在目录添加到系统参数PATH中,方便调用
  206. addToPATH ${bin}
  207. #设置开机启动
  208. addNginxService
  209. }
  210. installNginx

赋予installNginx.ssh文件可执行限权

  1. # chmod +x installNginx.ssh

执行安装

  1. ./installNginx.ssh

基本命令

  1. service nginx [start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest]

虚拟主机管理

在安装目录(//usr/local/nginx/)里的site目录中有一个default.conf默认虚拟主机管理模版,后续拓展虚拟主机可以直接复制default.conf文件,文件名格式为 *.conf,只要后缀为.conf就能解析

发表评论

表情:
评论列表 (有 0 条评论,352人围观)

还没有评论,来说两句吧...

相关阅读