ngnix 安装
1安装PCRE库
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ 下载最新的 PCRE 源码包,使用下面命令下载编译和安装 PCRE 包:
cd /data/apps/ wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.42.tar.gz tar -zxvf pcre-8.42.tar.gz cd pcre-8.42 ./configure make make install
查看是否安装pcre
rpm -qa pcre
安装pcre
http://mirror.centos.org/centos/6/os/x86_64/Packages/pcre-7.8-7.el6.x86_64.rpm https://centos.pkgs.org/6/centos-x86_64/pcre-7.8-7.el6.x86_64.rpm.html rpm -ivh pcre-7.8-6.el6.x86_64.rpm
卸载pcre
rpm -e --nodeps pcre
2 有网络的情况下
2-2、安装编译开发工具类库
用yum安装、更新开发工具"Development Tools"和"Server Platform Deveopment",而nginx会依赖openssl-devel和pcre-devel类库,安装如下:
[root@node2 nginx]# yum groupinstall "Development Tools" "Server Platform Deveopment"
[root@node2 ~]# yum install openssl-devel pcre-devel
2-3、创建用户和用户组
分别创建名为"nginx"的用户和组,用来运行nginx的worker进程,操作如下:
[root@node2 nginx]# groupadd -r nginx
[root@node2 nginx]# useradd -r -g nginx nginx
2-4、编译并安装
安装nginx
Nginx 一般有两个版本,分别是稳定版和开发版,您可以根据您的目的来选择这两个版本的其中一个
cd /data/apps wget http://nginx.org/download/nginx-1.14.1.tar.gz tar -zxvf nginx-1.14.1.tar.gz
cd nginx-1.14.1
先configure指定编译选项,如安装目录、上面创建的运行用户、需要的扩展模块(SSL、FastCGI)等,选项及参数说明:http://nginx.org/en/docs/configure.html,操作如下:
[root@node2 nginx]# ./configure \ --prefix=/usr \ --sbin-path=/usr/sbin/nginx \ --conf-path=/etc/nginx/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --pid-path=/var/run/nginx/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --user=nginx \ --group=nginx \ --with-http_ssl_module \ --with-http_flv_module \ --with-http_stub_status_module \ --with-http_gzip_static_module \ --http-client-body-temp-path=/var/tmp/nginx/client/ \ --http-proxy-temp-path=/var/tmp/nginx/proxy/ \ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \ --http-scgi-temp-path=/var/tmp/nginx/scgi \ --with-pcre
Configure成功如下:
之后进行安装
make && make install
2-5、为nginx提供SysV init服务脚本
先创建/etc/init.d/nginx服务脚本,这基于ngInx自身提供的命令实现的,脚本内容如下:
vim /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/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@node2 nginx]# vim /etc/init.d/nginx [root@node2 nginx]# chmod +x /etc/init.d/nginx [root@node2 nginx]# chkconfig --add nginx [root@node2 nginx]# chkconfig nginx on [root@node2 nginx]# chkconfig --list nginx
2-6、启动并访问测试
启动nginx,查看网络状态,可以看到nginx正在监听80端口;用测试主机访问nginx主机的IP,可以看到nginx的欢迎页面,过程如下:
[root@node2 nginx]# service nginx start [root@node2 nginx]# netstat -ntulp | grep nginx
如果启动之后发现报错,错误信息为
[root@XXXXXXXX nginx-1.14.1]# service nginx start
Starting nginx: /usr/sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
查询 ll /usr/local/lib 看一看这个目录 如果有就关联上
如果是32位系统
[root@lee ~]# ln -s /usr/local/lib/libpcre.so.1 /lib
如果是64位系统
[root@lee ~]# ln -s /usr/local/lib/libpcre.so.1 /lib64
然后在启动nginx就OK了
service nginx start
配置本地yum源或者第三方yum源
清理元数据缓存
yum clean all
重新建立元数据缓存
yum makecache
yum install -y unzip zip;
安装监控软件
nginx-module-vts
https://github.com/vozlt/nginx-module-vts
下载路径 https://codeload.github.com/vozlt/nginx-module-vts/zip/master
unzip nginx-module-vts-master.zip
上传以后 解压以后 目录为
/data/apps/nginx-module-vts-master
移动到软件目录
mv /data/apps/nginx-module-vts-master /opt/soft/nginx-module-vts-master
进入ngnix目录
执行命令
./configure \ --prefix=/usr \ --sbin-path=/usr/sbin/nginx \ --conf-path=/etc/nginx/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --pid-path=/var/run/nginx/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --user=nginx \ --group=nginx \ --with-http_ssl_module \ --with-http_flv_module \ --with-http_stub_status_module \ --with-http_gzip_static_module \ --http-client-body-temp-path=/var/tmp/nginx/client/ \ --http-proxy-temp-path=/var/tmp/nginx/proxy/ \ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \ --http-scgi-temp-path=/var/tmp/nginx/scgi \ --with-pcre \ --add-module=/opt/soft/nginx-module-vts-master
Configure成功如下:
之后进行安装
make && make install
然后下载 nginx-vts-exporter
三、nginx-vts-exporter的使用
exporter会收集nginx性能指标的JSON格式数据,并汇总后暴露监控接口给Prometheus。
https://github.com/hnlq715/nginx-vts-exporter/releases/
下载链接为
wget https://github.com/hnlq715/nginx-vts-exporter/releases/download/v0.10.3/nginx-vts-exporter-0.10.3.linux-amd64.tar.gz
启动命令为
/data/apps/nginx-vts-exporter-0.10.3.linux-amd64/nginx-vts-exporter -nginx.scrape_timeout 10 -nginx.scrape_uri http://127.0.0.1/status/format/json