生产环境
Nginx
部署手册
- Nginx安装
- 确保进行了安装了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
)确保进行了安装了
linux
常用必备支持库。检查是否安装了
g++
、
gcc
。
rpm -qa | grep gcc
之后需要出现
3
个包如下图所示。如果没有出现。需要安装
g++
、
gcc
。
# yum install gcc-c++
- 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
- 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
- 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;
}
}
}
- 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
- 配置监控状态模块
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: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
1075
1064
6253
Reading:
0
Writing:
5
Waiting:
95
2.3.3
参数说明
active connections –
活跃的连接数量
server accepts handled requests —
总共处理了
107520387
个连接
,
成功创建
107497834
次握手
,
总共处理了
639121056
个请求
每个连接有三种状态
waiting
、
reading
、
writing
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]}'