Lyon

个人博客

授人以鱼,亦授人以渔.


Linux应用WEB服务器专题一:Nginx

一.部署

1. 系统环境

安装环境:

yum install pcre pcre-devel zlib zlib-devel automake make gcc libcurl libcurl-devel libxml2 libxml2-devel openssl openssl-devel libevent libevent-devel 

2.编译

下载包:

wget https://nginx.org/download/nginx-1.13.9.tar.gz && tar zxvf nginx-1.13.9.tar.gz && cd nginx-1.13.9

配置:

./configure --prefix=/usr/local/nginx   --with-http_ssl_module  --with-http_v2_module  --with-http_stub_status_module --with-pcre --with-http_gzip_static_module

编译:

make -j16 && make install

3.配置应用

编辑启动脚本:

vi /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:      /etc/sysconfig/nginx
# pidfile:     /var/run/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 /usr/local/nginx ] && . /usr/local/nginx

lockfile=/var/lock/subsys/nginx

make_dirs() {
   # make required directories
   user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   if [ -n "$user" ]; then
      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
    fi
}

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

脚本来源:http://wiki.nginx.org/RedHatNginxInitScript

配置开机启动:

chmod a+x /etc/init.d/nginx
/etc/init.d/nginx start
/etc/init.d/nginx stop
chkconfig --add /etc/init.d/nginx
chkconfig nginx on

二.配置详解

三.性能调优

四.其他

附录开启密码验证脚本:

rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum -y install nginx
cat > /etc/nginx/conf.d/default.conf <<EOF
server {
    listen       80 default_server;
    listen       [::]:80 default_server;
    server_name  _;
    default_type 'text/html';
    charset utf-8;
    root         /var/www/html;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

   location /
        {
                auth_basic "Nginx Passwd Server;
                auth_basic_user_file /etc/nginx/conf.d/webpasswd; 
                autoindex on;
        }
  location ~ \.php$ {  
           root           /var/www/html;  
           fastcgi_pass   127.0.0.1:9000;  
           fastcgi_index  index.php;  
           fastcgi_param  SCRIPT_FILENAME  \$document_root\$fastcgi_script_name;  
           include        fastcgi_params;  
                auth_basic "Nginx Passwd Server";
                auth_basic_user_file /etc/nginx/conf.d/webpasswd; 
                autoindex on;
       }  

}

EOF
touch /etc/nginx/conf.d/webpasswd && chmod 400 /etc/nginx/conf.d/webpasswd
chown nginx. /etc/nginx/conf.d/webpasswd
/usr/local/nginx/sbin/nginx
htpasswd -c /etc/nginx/conf.d/webpasswd lyon

本文只是简述,详细讨论可以联系我,资料仅供学习参考!