Nginx安装与部署

来源:互联网 发布:免费下载淘宝旺旺 编辑:程序博客网 时间:2024/06/11 04:33

生产环境Nginx部署手册
  1. Nginx安装
  1. 确保进行了安装了linux常用必备支持库
CentOS安装软件的时候,可能缺少一部分支持库,而报错。这里首先安装系统常用的支持库。那么在安装的时候就会减少很多的错误的出现。
# yum install -y gcc gdb strace gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs patch e2fsprogs-devel krb5-devel libidn libidn-devel openldap-devel nss_ldap openldap-clients openldap-servers libevent-devel libevent uuid-devel uuid mysql-devel
  1. 正则表达式库安装
    1)确保进行了安装了linux常用必备支持库。检查是否安装了g++gccrpm -qa | grep gcc 之后需要出现3个包如下图所示。如果没有出现。需要安装g++gcc
    # yum install gcc-c++
  1.  Nginx安装
创建安装目录与日志目录
   安装目录
    # mkdir /usr/local/nginx
    1)判断系统是否安装了zlib-devel。如果没有安装。使用
    # yum install -y zlib-devel
2)解压
    # cd /usr/local/nginx
    # tar zxvf nginx-1.8.1.tar.gz
    3)进入目录
    # cd nginx-1.8.1
    4)配置。通常将软件安装在/usr/local/目录下。
[root@node-a nginx]# ./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --without-http_rewrite_module --without-http_gzip_module --with-http_stub_status_module
5)编译
[root@node-a nginx]#make
6) 安装
[root@node-a nginx]#make install
7) 检查是否安装成功
    # cd  /usr/local/nginx/
# ./nginx -t
结果显示:
nginx: the configuration file /usr/local/nginx/nginx.conf syntax is ok
    nginx: configuration file /usr/local/nginx/nginx.conf test is successful
  1. Nginx维护
停止服务器:[root@node-a nginx]# ./nginx -s stop
检查配置:[root@node-a nginx]# ./nginx -t
启动服务器:[root@node-a nginx]# ./nginx
nginx -s命令:
stop — fast shutdown
quit — graceful shutdown
reload — reloading the configuration file
reopen — reopening the log files
或者(需要配置自动重启服务(详见2.2),配置自动重启服务后以下两条命令才能执行)
service nginx stop  
service nginx start
  1. Nginx配置文件
配置文件仅供参考,根据实际情况调整:
[root@node-a nginx]# vi nginx.conf
#user  nobody;
worker_processes  1;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    #access_log  logs/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    #gzip  on;
   #http访问
   server {
      listen       8080;
      server_name  localhost;
      #charset koi8-r;
      #access_log  logs/8080.access.log  main;
      #error_log   logs/8080.error.log;
     location / {
            root   /voice;
            index  index.html index.htm;
      }
   }
   #http访问
   server {
      listen       8000;
      server_name  localhost;
      #charset koi8-r;
      #access_log  logs/8000.access.log  main;
      #error_log   logs/8000.error.log;
     location / {
            stub_status on;
            root   html;
     }
   }
}
  1. Nginx配置自动重启服务
[root@node-a nginx]#mkdir  /etc/nginx/
[root@node-a nginx]#cp /usr/local/nginx/mime.types /etc/nginx/
[root@node-a nginx]#cp /usr/local/nginx/nginx.conf /etc/nginx/
[root@node-a nginx]#ln -s /usr/local/nginx/nginx /usr/sbin/nginx
[root@node-a nginx]#vim /etc/sysconfig/nginx
NGINX=/usr/sbin/nginx
CONFFILE=/etc/nginx/nginx.conf
[root@node-a nginx]# vim /etc/rc.d/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: /us/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/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/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' -`
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
[root@node-a nginx]#chmod 775 /etc/rc.d/init.d/nginx
[root@node-a nginx]#chkconfig --add nginx
[root@node-a nginx]#chkconfig nginx on
[root@node-a nginx]#chkconfig nginx --list
  1. 配置监控状态模块
2.3.1状态模块配置
先使用命令查看是否已经安装这个模块:--with-http_stub_status_module
[root@XXXXXX nginx]# ./nginx -V (V大写会显示版本号和模块等信息、v小写仅显示版本信息。
  如果已经安装,会在显示的信息中包含 --with-http_stub_status_module信息。如果没有此模块,需要重新安装,编译命令如下:
./configure –with-http_stub_status_module
确定安装此模块之后,修改nginx配置即可,注意以下配置需要放在http{server{配置内容}},配置内容如下:
location /hxbcdnstatus {
            stub_status            on;
            access_log             off;
          allow 127.0.0.1;
            deny all;
            #auth_basic              "NginxStatus";
            #auth_basic_user_file  conf/nginxstaus;
}
访问方式:http://127.0.0.1/hxbcdnstatus
http://127.0.0.1:8000
配置完成后需要重启Nginx服务:
service nginx stop  
service nginx start
此处默认只有本地访问,如果远程可以查看需要加相关的IP或者干脆去掉Deny all即可。去掉Deny all即可远程查看。
2.3.2状态查看
  配置完成后在浏览器中输入http://127.0.0.1/hxbcdnstatus查看,显示信息如下:
Active connections:100
server accepts handled requests
 10751064 6253
Reading:0 Writing: 5 Waiting: 95
2.3.3参数说明
  active connections –活跃的连接数量
  server accepts handled requests —总共处理了107520387个连接, 成功创建107497834次握手,总共处理了639121056个请求
  每个连接有三种状态waitingreadingwriting
  reading —读取客户端的Header信息数.这个操作只是读取头部信息,读取完后马上进入writing状态,因此时间很短。
  writing —响应数据到客户端的Header信息数.这个操作不仅读取头部,还要等待服务响应,因此时间比较长。
  waiting —开启keep-alive后等候下一次请求指令的驻留连接.
  正常情况下waiting数量是比较多的,并不能说明性能差。反而如果reading+writing数量比较多说明服务并发有问题。
   补充:
  查看Nginx并发进程数:ps -ef | grep nginx | wc -l
  查看Web服务器TCP连接状态:netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
原创粉丝点击