Nginx基础应用--------基于CentOS6源码安装

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

 【引自asd1123509133的博客】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. 环境

  1. [root@nginx ~]# cat /etc/redhat-release 
  2.  
  3. CentOS release 6.8 (Final) 
  4.  
  5. [root@nginx ~]# uname -r 
  6.  
  7. 2.6.32-504.el6.x86_64  

3. 安装

* 临时关闭selinux(可选)

  1. [root@nginx ~]# setenforce 0 

* 关闭iptables(可选)

  1. [root@nginx ~]# service iptables stop 

* 创建www用户

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

* 安装pcre库依赖

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

* 安装ssl库依赖

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

* 进入下载目录

  1. cd /usr/local/src 

* 下载nginx源码包

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

* 解压nginx源码包

  1. tar zxvf nginx-1.11.10.tar.gz 

* 进入nginx包目录

  1. 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 

* 编译并安装

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

* 做nginx软链接

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

4. 创建启动脚本

* /etc/init.d/nginx

  1. #!/bin/sh 
  2. # nginx - this script starts and stops the nginx daemon 
  3. # chkconfig:   - 85 15 
  4. # description:  NGINX is an HTTP(S) server, HTTP(S) reverse \ 
  5. #               proxy and IMAP/POP3 proxy server 
  6. # processname: nginx 
  7. # config:      /usr/local/nginx/conf/nginx.conf 
  8. # config:      /etc/sysconfig/nginx 
  9. # pidfile:     /var/run/nginx.pid 
  10. # Source function library. 
  11. . /etc/rc.d/init.d/functions 
  12.  
  13. # Source networking configuration. 
  14. . /etc/sysconfig/network 
  15.  
  16. Check that networking is up. 
  17. "$NETWORKING" = "no" ] && exit 0 
  18.  
  19. nginx="/usr/local/nginx/sbin/nginx" 
  20. prog=$(basename $nginx) 
  21.  
  22. NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" 
  23.  
  24. [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx 
  25.  
  26. lockfile=/var/lock/subsys/nginx 
  27.  
  28. make_dirs() { 
  29.    # make required directories 
  30.    user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -` 
  31.    if [ -z "`grep $user /etc/passwd`" ]; then 
  32.        useradd -M -s /bin/nologin $user 
  33.    fi 
  34.    options=`$nginx -V 2>&1 | grep 'configure arguments:'
  35.    for opt in $options; do 
  36.        if [ `echo $opt | grep '.*-temp-path'` ]; then 
  37.            value=`echo $opt | cut -d "=" -f 2` 
  38.            if [ ! -d "$value" ]; then 
  39.                # echo "creating" $value 
  40.                mkdir -p $value && chown -R $user $value 
  41.            fi 
  42.        fi 
  43.    done 
  44.  
  45. start() { 
  46.     [ -x $nginx ] || exit 5    [ -f $NGINX_CONF_FILE ] || exit 6 
  47.     make_dirs    echo -n $"Starting $prog: " 
  48.     daemon $nginx -c $NGINX_CONF_FILE 
  49.     retval=$? 
  50.     echo 
  51.     [ $retval -eq 0 ] && touch $lockfile 
  52.     return $retval 
  53.  
  54. stop() { 
  55.     echo -n $"Stopping $prog: " 
  56.     killproc $prog -QUIT    retval=$? 
  57.     echo 
  58.     [ $retval -eq 0 ] && rm -f $lockfile 
  59.     return $retval 
  60.  
  61. restart() { 
  62.     configtest || return $? 
  63.     stop 
  64.     sleep 1 
  65.     start 
  66.  
  67. reload() { 
  68.     configtest || return $? 
  69.     echo -n $"Reloading $prog: " 
  70.     killproc $nginx -HUP    RETVAL=$? 
  71.     echo 
  72.  
  73. force_reload() { 
  74.     restart 
  75.  
  76. configtest() { 
  77.     $nginx -t -c $NGINX_CONF_FILE 
  78.  
  79. rh_status() { 
  80.     status $prog 
  81.  
  82. rh_status_q() { 
  83.     rh_status >/dev/null 2>&1 
  84.  
  85. case "$1" in 
  86.     start) 
  87.         rh_status_q && exit 0 
  88.         $1 
  89.         ;; 
  90.     stop) 
  91.         rh_status_q || exit 0 
  92.         $1 
  93.         ;; 
  94.     restart|configtest) 
  95.         $1 
  96.         ;; 
  97.     reload) 
  98.         rh_status_q || exit 7 
  99.         $1 
  100.         ;; 
  101.     force-reload) 
  102.         force_reload 
  103.         ;; 
  104.     status) 
  105.         rh_status      
  106.         ;; 
  107.     condrestart|try-restart) 
  108.         rh_status_q || exit 0             
  109.         ;; 
  110.     *) 
  111.         echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" 
  112.         exit 2 
  113. esac  

* 改变nginx脚本文件权限

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

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

  1. [root@nginx ~]# chkconfig --add nginx 
  2.  
  3. [root@nginx ~]# chkconfig nginx on  

5. 服务启动测试

  1. [root@nginx ~]# service nginx start  

 

 

 

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

6. 访问测试

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

 

 

 

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

7. 总结

以需求驱动技术,技术本身没有优略之分,只有业务之分。

责任编辑:庞桂玉 来源: 51CTO博客
相关推荐

2017-03-20 15:08:04

RedisNoSQLcentos6

2014-07-30 10:35:34

Linux系统启动

2014-08-05 16:30:12

CentOS6LAMP

2021-04-13 15:09:16

CentOS6EOL项目

2021-06-24 14:45:33

AWS 应用程序NGINX

2010-01-15 20:59:54

2010-01-14 17:37:34

CentOS prev

2022-07-12 17:24:48

NginxDocker

2011-05-10 13:54:36

CentOS 6

2010-02-23 13:41:00

2013-04-10 15:12:03

MySQL 5.6

2011-08-15 14:05:42

CentOS 6

2021-01-04 08:15:16

CentOS 7Python3.9Python

2014-11-28 11:20:54

LEMPNginxMariaDB

2015-05-25 19:34:06

KickstartCentOS

2011-07-27 10:06:53

RHEL 6Oracle 10g

2012-08-08 10:10:30

CentOS 6操作系统

2010-04-06 10:00:52

CentOS系统

2013-12-04 14:45:22

2022-12-06 08:44:20

点赞
收藏

51CTO技术栈公众号