阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

CentOS 6.8下源码安装 Nginx 1.11.10

165次阅读
没有评论

共计 11080 个字符,预计需要花费 28 分钟才能阅读完成。

1. 背景 

      介绍:

    Nginx 是一款高性能的 HTTP 和反向代理服务器,能够选择高效的 epoll(linux2.6 内核)、kqueue(freebsd)、eventport(solaris10)作为网络 I / O 模型,能够支持高达 50000 个并发连接数的响应,而内存、CPU 等系统资源消耗却非常低、运行非常稳定。

  选择的理由:

  * 支持高并发连接:nginx 使用高效的多路复用模型(epoll/linux, kqueue/freebsd, eventport/solaris)

  * 内存消耗少:在服务器 3W 并发连接下,开启 10 个 Nginx 进程消耗 150MB 内存(15MB*10)

  * 成本低廉:购买 F5 BIG-IP、NetScaler 等负载均衡交换机需要几十万 RMB,而开源 Nginx 替代这些商业设备。

  * 其他理由:网络配置简单;支持 rewrite 重写规则,能够根据域名、URL 的不同、将 HTTP 请求分到不同的后端服务器群组;内置的健康检查功能;节省带宽,支持 GZIP 压缩,可以添加浏览器本地缓存的 Header 头;支持热部署,能够在不间断服务的情况下、对软件版本进行升级

  应用范围:

  * Web 服务:    设置多虚拟主机的服务并配合 fast-cgi 或 tomcat 支持动态网页

      Nginx 是近年来比较火的一个 www 服务的软件, 与 Apache 和 lighttpd 以及 tomcat 等功能类似,但是 nginx 要比前者有着卓越的性能,比如:采用了 epoll 模型,内存消耗小等优点;

  *  反向代理, 多虚拟主机的代理:

      指以代理服务器来接受 Internet 上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给 Internet 上请求连接的客户端;

  * 七层的负载均衡: 单多虚拟主机不同服务器之间的访问;

      负载均衡是由多台服务器以对称的方式组成一个服务器集合,每台都是等价地位,通过某种负载分担技术,将外部发送来的请求均匀分配到对称结构中某一台服务器上,来接收到请求的服务器独立地回应客户的请求;

    * 正向代理:  代理上网

      代理内部网络对 Internet 的链接请求,客户机必须指定代理服务器,并将本来要直接发送到 web 服务器上的 http 请求发送到代理服务器中,由代理服务器请求并返回响应内容;

    * 缓存服务

      为 proxy 和 fastcgi 做缓存服务,提高访问速度,相当于 squid 功能;

2. 环境

[root@nginx ~]# cat /etc/RedHat-release
CentOS release 6.8 (Final)
[root@nginx ~]# uname -r
2.6.32-504.el6.x86_64

3. 安装

  * 临时关闭 selinux(可选)

[root@nginx ~]# setenforce 0

  * 关闭 iptables(可选)

[root@nginx ~]# service iptables stop

  * 创建 www 用户

[root@nginx ~]# useradd -r -s /sbin/nologin -M www

  * 安装 pcre 库依赖

[root@nginx ~]# yum install pcre pcre-devel -y

  * 安装 ssl 库依赖

[root@nginx ~]# yum install openssl openssl-devel -y

  * 进入下载目录

cd /usr/local/src

  * 下载 nginx 源码包

wget http://nginx.org/download/nginx-1.11.10.tar.gz

  * 解压 nginx 源码包

 tar zxvf nginx-1.11.10.tar.gz

  * 进入 nginx 包目录

cd nginx-1.11.10

  * 指定安装目录、用户、模块

1 [root@nginx ~]# ./configure –prefix=/usr/local/nginx-1.11.10 –user=www –group=www –with-http_ssl_module –with-http_stub_status_module

  * 编译并安装

[root@nginx ~]# make && make install

  * 做 nginx 软链接

[root@nginx ~]# ln -s /usr/local/nginx-1.11.10 /usr/local/nginx

4. 创建启动脚本

  * /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:      /usr/local/nginx/conf/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 /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

  * 改变 nginx 脚本文件权限

[root@nginx ~]# chmod 755 /etc/init.d/nginx

  * 添加进 service 管理服务并设置开机启动

[root@nginx ~]# chkconfig –add nginx
[root@nginx ~]# chkconfig nginx on

5. 服务启动测试

[root@nginx ~]#  service nginx start

CentOS 6.8 下源码安装 Nginx 1.11.10

可以看到 80 默认的 80 端口 nginx 已经开始监听

6. 访问测试

* 通过浏览器测试, 此 nginx 宿主机 ip 为 192.168.222.128

CentOS 6.8 下源码安装 Nginx 1.11.10

访问成功,nginx 已经成功返回页面

更多详情见请继续阅读下一页的精彩内容:http://www.linuxidc.com/Linux/2017-03/141908p2.htm

下面关于 Nginx 的文章您也可能喜欢,不妨参考下:

CentOS 7.2 下编译安装 PHP7.0.10+MySQL5.7.14+Nginx1.10.1  http://www.linuxidc.com/Linux/2016-09/134804.htm

Linux 下编译安装 Nginx 1.8.1 及配置 http://www.linuxidc.com/Linux/2017-02/140495.htm

CentOS 6.4 安装配置 Nginx+Pcre+php-fpm http://www.linuxidc.com/Linux/2013-08/88984.htm

CentOS 7.2 源码安装 Nginx http://www.linuxidc.com/Linux/2017-03/141246.htm

Nginx 的 500,502,504 错误解决方法 http://www.linuxidc.com/Linux/2015-03/115507.htm

Nginx 版本号修改隐藏及记录用户请求需要的时间  http://www.linuxidc.com/Linux/2017-02/141044.htm

CentOS 7 编译安装 Nginx1.10.2 脚本启动失败解决思路 http://www.linuxidc.com/Linux/2017-01/139794.htm

优化

* nginx 配置文件优化

– nginx 进程数,建议按照 cpu 数目来指定,一般为它的倍数:

– worker_processes 4;

– 为每个进程绑定 cpu:

– worker_cpu_affinity 00000001 00000010 00000100 00001000;

– nginx 进程打开的最多文件描述符数目:

– worker_rlimit_nofile 102400;

– 使用 epoll 的 I / O 复用模型:

– use epoll;

– 每个进程允许的最多连接数:

– worker_connections 102400;

– keepalive 超时时间:

– keepalive_timeout 60;

– 客户端请求头部的缓冲区大小: (分页大小可以用命令 getconf PAGESIZE 取得):

– client_header_buffer_size 4k;

– 打开文件指定缓存,默认是没有启用的,max 指定缓存数量,建议和打开文件数一致,inactive 是指经过多长时间文件没被请求后删除缓存:

– open_file_cache max=102400 inactive=20s;

– 指定多长时间检查一次缓存的有效信息:

– open_file_cache_valid 30s;

– 设置最少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的:

– open_file_cache_min_uses 1;

* 系统优化

– timewait 的数量,默认是 180000。(Deven: 因此如果想把 timewait 降下了就要把 tcp_max_tw_buckets 值减小):

– net.ipv4.tcp_max_tw_buckets = 6000

– 允许系统打开的端口范围:

– net.ipv4.ip_local_port_range = 1024    65000

– 启用 timewait 快速回收:

– net.ipv4.tcp_tw_recycle = 1

– 开启重用。允许将 TIME-WAIT sockets 重新用于新的 TCP 连接:

– net.ipv4.tcp_tw_reuse = 1

– 开启 SYN Cookies,当出现 SYN 等待队列溢出时,启用 cookies 来处理:

– net.ipv4.tcp_syncookies = 1

– web 应用中 listen 函数的 backlog 默认会给我们内核参数的 net.core.somaxconn 限制到 128,而 nginx 定义的 NGX_LISTEN_BACKLOG 默认为 511:

– net.core.somaxconn = 262144

– 允许送到队列的数据包的最大数目:

– net.core.netdev_max_backlog = 262144

– 系统中最多有多少个 TCP 套接字不被关联到任何一个用户文件句柄上:

– net.ipv4.tcp_max_orphans = 262144

– 记录的那些尚未收到客户端确认信息的连接请求的最大值:

– net.ipv4.tcp_max_syn_backlog = 262144

– 时间戳可以避免序列号的卷绕:

– net.ipv4.tcp_timestamps = 0

– 内核放弃连接之前发送 SYN+ACK 包:

– net.ipv4.tcp_synack_retries = 1

– 内核放弃建立连接之前发送 SYN 包的数量:

– net.ipv4.tcp_syn_retries = 1

– 保持在 FIN-WAIT- 2 状态的时间:

– net.ipv4.tcp_fin_timeout = 1

– 当 keepalive 起用的时候,TCP 发送 keepalive 消息的频度。缺省是 2 小时:

– net.ipv4.tcp_keepalive_time = 30

Nginx 的详细介绍:请点这里
Nginx 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-03/141908.htm

1. 背景 

      介绍:

    Nginx 是一款高性能的 HTTP 和反向代理服务器,能够选择高效的 epoll(linux2.6 内核)、kqueue(freebsd)、eventport(solaris10)作为网络 I / O 模型,能够支持高达 50000 个并发连接数的响应,而内存、CPU 等系统资源消耗却非常低、运行非常稳定。

  选择的理由:

  * 支持高并发连接:nginx 使用高效的多路复用模型(epoll/linux, kqueue/freebsd, eventport/solaris)

  * 内存消耗少:在服务器 3W 并发连接下,开启 10 个 Nginx 进程消耗 150MB 内存(15MB*10)

  * 成本低廉:购买 F5 BIG-IP、NetScaler 等负载均衡交换机需要几十万 RMB,而开源 Nginx 替代这些商业设备。

  * 其他理由:网络配置简单;支持 rewrite 重写规则,能够根据域名、URL 的不同、将 HTTP 请求分到不同的后端服务器群组;内置的健康检查功能;节省带宽,支持 GZIP 压缩,可以添加浏览器本地缓存的 Header 头;支持热部署,能够在不间断服务的情况下、对软件版本进行升级

  应用范围:

  * Web 服务:    设置多虚拟主机的服务并配合 fast-cgi 或 tomcat 支持动态网页

      Nginx 是近年来比较火的一个 www 服务的软件, 与 Apache 和 lighttpd 以及 tomcat 等功能类似,但是 nginx 要比前者有着卓越的性能,比如:采用了 epoll 模型,内存消耗小等优点;

  *  反向代理, 多虚拟主机的代理:

      指以代理服务器来接受 Internet 上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给 Internet 上请求连接的客户端;

  * 七层的负载均衡: 单多虚拟主机不同服务器之间的访问;

      负载均衡是由多台服务器以对称的方式组成一个服务器集合,每台都是等价地位,通过某种负载分担技术,将外部发送来的请求均匀分配到对称结构中某一台服务器上,来接收到请求的服务器独立地回应客户的请求;

    * 正向代理:  代理上网

      代理内部网络对 Internet 的链接请求,客户机必须指定代理服务器,并将本来要直接发送到 web 服务器上的 http 请求发送到代理服务器中,由代理服务器请求并返回响应内容;

    * 缓存服务

      为 proxy 和 fastcgi 做缓存服务,提高访问速度,相当于 squid 功能;

2. 环境

[root@nginx ~]# cat /etc/RedHat-release
CentOS release 6.8 (Final)
[root@nginx ~]# uname -r
2.6.32-504.el6.x86_64

3. 安装

  * 临时关闭 selinux(可选)

[root@nginx ~]# setenforce 0

  * 关闭 iptables(可选)

[root@nginx ~]# service iptables stop

  * 创建 www 用户

[root@nginx ~]# useradd -r -s /sbin/nologin -M www

  * 安装 pcre 库依赖

[root@nginx ~]# yum install pcre pcre-devel -y

  * 安装 ssl 库依赖

[root@nginx ~]# yum install openssl openssl-devel -y

  * 进入下载目录

cd /usr/local/src

  * 下载 nginx 源码包

wget http://nginx.org/download/nginx-1.11.10.tar.gz

  * 解压 nginx 源码包

 tar zxvf nginx-1.11.10.tar.gz

  * 进入 nginx 包目录

cd nginx-1.11.10

  * 指定安装目录、用户、模块

1 [root@nginx ~]# ./configure –prefix=/usr/local/nginx-1.11.10 –user=www –group=www –with-http_ssl_module –with-http_stub_status_module

  * 编译并安装

[root@nginx ~]# make && make install

  * 做 nginx 软链接

[root@nginx ~]# ln -s /usr/local/nginx-1.11.10 /usr/local/nginx

4. 创建启动脚本

  * /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:      /usr/local/nginx/conf/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 /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

  * 改变 nginx 脚本文件权限

[root@nginx ~]# chmod 755 /etc/init.d/nginx

  * 添加进 service 管理服务并设置开机启动

[root@nginx ~]# chkconfig –add nginx
[root@nginx ~]# chkconfig nginx on

5. 服务启动测试

[root@nginx ~]#  service nginx start

CentOS 6.8 下源码安装 Nginx 1.11.10

可以看到 80 默认的 80 端口 nginx 已经开始监听

6. 访问测试

* 通过浏览器测试, 此 nginx 宿主机 ip 为 192.168.222.128

CentOS 6.8 下源码安装 Nginx 1.11.10

访问成功,nginx 已经成功返回页面

更多详情见请继续阅读下一页的精彩内容:http://www.linuxidc.com/Linux/2017-03/141908p2.htm

下面关于 Nginx 的文章您也可能喜欢,不妨参考下:

CentOS 7.2 下编译安装 PHP7.0.10+MySQL5.7.14+Nginx1.10.1  http://www.linuxidc.com/Linux/2016-09/134804.htm

Linux 下编译安装 Nginx 1.8.1 及配置 http://www.linuxidc.com/Linux/2017-02/140495.htm

CentOS 6.4 安装配置 Nginx+Pcre+php-fpm http://www.linuxidc.com/Linux/2013-08/88984.htm

CentOS 7.2 源码安装 Nginx http://www.linuxidc.com/Linux/2017-03/141246.htm

Nginx 的 500,502,504 错误解决方法 http://www.linuxidc.com/Linux/2015-03/115507.htm

Nginx 版本号修改隐藏及记录用户请求需要的时间  http://www.linuxidc.com/Linux/2017-02/141044.htm

CentOS 7 编译安装 Nginx1.10.2 脚本启动失败解决思路 http://www.linuxidc.com/Linux/2017-01/139794.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计11080字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中