欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

CentOS下编译安装nginx及配置缩略图插件的方法教程

程序员文章站 2022-05-24 18:46:47
相信大家都知道利用yum安装nginx 非常方便,但是有些插件并不会默认安装,比如 http_image_filter_module, 因此我们需要编译安装 nginx,已...

相信大家都知道利用yum安装nginx 非常方便,但是有些插件并不会默认安装,比如 http_image_filter_module, 因此我们需要编译安装 nginx,已达到我们的目的。下面来看看详细的方法吧。

安装依赖

yum install -y pcre-devel libmxl2-devel libxslt-devel gd-devel

安装 nginx

wget http://nginx.org/download/nginx-1.9.1.tar.gz
tar -xzvf nginx-1.9.1.tar.gz
cd nginx-1.9.1
./configure\
 --user=nginx\
 --group=nginx\
 --with-http_ssl_module\
 --with-http_spdy_module\
 --with-http_realip_module\
 --with-http_addition_module\
 --with-http_xslt_module\
 --with-http_image_filter_module\
 --with-http_sub_module\
 --with-http_auth_request_module\
 --with-http_stub_status_module\
 --with-http_gzip_static_module 
make 
make install

安装完成后,可以使用如下命令来查看 nginx 安装的模块

[root@linux001 ~]# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.9.1
built by gcc 4.4.7 20120313 (red hat 4.4.7-11) (gcc) 
built with openssl 1.0.1e-fips 11 feb 2013
tls sni support enabled
configure arguments: --user=nginx --group=nginx --with-http_ssl_module --with-http_spdy_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_sub_module --with-http_auth_request_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_image_filter_module

增加启动脚本

新建文件 /etc/init.d/nginx , 内容如下:

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15 
# description: nginx is an http(s) server, http(s) reverse \
#    proxy and imap/pop3 proxy server
# processname: nginx
# config:  /etc/nginx/nginx.conf
# 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"
 
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
 
lockfile=/var/lock/subsys/nginx
 
make_dirs() {
 # make required directories
 user=`$nginx -v 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
 if [ -z "`grep $user /etc/passwd`" ]; then
  useradd -m -s /bin/nologin $user
 fi
 options=`$nginx -v 2>&1 | grep 'configure arguments:'`
 for opt in $options; do
  if [ `echo $opt | grep '.*-temp-path'` ]; then
   value=`echo $opt | cut -d "=" -f 2`
   if [ ! -d "$value" ]; then
    # echo "creating" $value
    mkdir -p $value && chown -r $user $value
   fi
  fi
 done
}
 
start() {
 [ -x $nginx ] || exit 5
 [ -f $nginx_conf_file ] || exit 6
 make_dirs
 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
 sleep 1
 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/null 2>&1
}
 
case "$1" in
 start)
  rh_status_q && exit 0
  $1
  ;;
 stop)
  rh_status_q || exit 0
  $1
  ;;
 restart|configtest)
  $1
  ;;
 reload)
  rh_status_q || exit 7
  $1
  ;;
 force-reload)
  force_reload
  ;;
 status)
  rh_status
  ;;
 condrestart|try-restart)
  rh_status_q || exit 0
   ;;
 *)
  echo $"usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
  exit 2
esac

添加好 /etc/init.d/nginx 后,需要增加执行权限,然后就可以用脚本来 启动、停止 nginx 了。

chmod a+x /etc/init.d/nginx 
/etc/init.d/nginx start
/etc/init.d/nginx stop
/etc/init.d/nginx restart
/etc/init.d/nginx reload

或者以服务来 启动、停止 nginx。

service nginx start
service nginx stop
service nginx restart
service nginx reload

设置自启动

chkconfig --add nginx
chkconfig --level 345 on

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。