Centos6.5安装Nginx

来源:互联网 发布:erp开源软件 编辑:程序博客网 时间:2024/06/06 03:17

Nginx是一款高性能反向代理软件,现在应用极其广泛,软件负载均衡一般有Nginx和LVS两种方式,其中Nginx更为流行,因为它安装配置简单易于维护,还能缓存一些静态资源。
首先安装依赖库
1、安装g++

yum install gcc gcc-c++

提示
Another app is currently holding the yum lock; waiting for it to exit…
使用

rm -f /var/run/yum.pid

强制关闭yum进程
2、安装pcre库

cd /usr/fuyuwei/softwarewget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar.gztar -zxvf pcre-8.37.tar.gz -C /usr/localcd /usr/local/pcre-8.37/./configuremakemake install

3、安装zlib库

cd /usr/fuyuwei/softwarewget http://zlib.net/zlib-1.2.11.tar.gztar -zxvf zlib-1.2.11.tar.gz -C /usr/localcd /usr/local/zlib-1.2.11./confiuremakemake install

4、安装openssl

cd /usr/fuyuwei/softwarewget https://www.openssl.org/source/openssl-1.0.1t.tar.gztar -zxvf openssl-1.0.1t.tar.gz -C /usr/localcd /usr/local/openssl-1.0.1t./configuremakemake install

5、安装nginx

cd /usr/fuyuwei/softwarewget http://nginx.org/download/nginx-1.1.10.tar.gztar -zxvf nginx-1.1.10.tar.gz -C /usr/localcd /usr/local#去掉版本号,要不编译之后sbin会在另一个文件夹下mv nginx-1.1.10 nginxcd /usr/local/nginx./configuremakemake install

6、启动nginx

#80端口可能被Apache占用,修改端口号为8090vim /usr/local/nginx/conf/nginx.confserver {        listen       8090;        server_name  localhost;        #charset koi8-r;        #access_log  logs/host.access.log  main;        location / {            root   html;            index  index.html index.htm;        }/usr/local/nginx/sbin/nginx -c /usr/local/nginx/config/#报错提示/usr/local/nginx/sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory#执行ln -s /lib64/libpcre.so.0.0.1 /lib64/libpcre.so.0.1#创建日志文件touch /usr/local/nginx/logs/error.logtouch /usr/local/nginx/logs/access.log#再次启动netstat -tulnp|grep 8090tcp        0      0 0.0.0.0:8090                0.0.0.0:*                   LISTEN      27172/nginx   #关闭防火墙services iptables stop

7、测试访问
这里写图片描述
8、Nginx的重启、关闭、启动
停止:

#查看进程号ps -ef|grep nginx#从容停止Nginx  kill -QUIT master进程号  #快速停止Nginx  kill -TERM master进程号  #强制停止Nginx  kill -9 master进程号 

重启:

cd /usr/local/nginx/sbin./nginx -s reload#或者查找当前nginx进程号,然后输入命令:kill -HUP 进程号 实现重启nginx服务

9、验证nginx.conf配置文件是否正确

cd /usr/local/nginx/sbin/./nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

10、Nginx启动脚本

#!/bin/sh  # chkconfig: 2345 85 15  # description:Nginx Server  NGINX_HOME=/usr/local/nginx  NGINX_SBIN=$NGINX_HOME/sbin/nginx  NGINX_CONF=$NGINX_HOME/conf/nginx.conf  NGINX_PID=$NGINX_HOME/logs/nginx.pid  NGINX_NAME="Nginx"  . /etc/rc.d/init.d/functions  if [ ! -f $NGINX_SBIN ]  then      echo "$NGINX_NAME startup: $NGINX_SBIN not exists! "      exit  fi  start() {      $NGINX_SBIN -c $NGINX_CONF      ret=$?      if [ $ret -eq 0 ]; then          action $"Starting $NGINX_NAME: " /bin/true      else          action $"Starting $NGINX_NAME: " /bin/false      fi  }  stop() {      kill `cat $NGINX_PID`      ret=$?      if [ $ret -eq 0 ]; then          action $"Stopping $NGINX_NAME: " /bin/true      else          action $"Stopping $NGINX_NAME: " /bin/false      fi  }  restart() {      stop      start  }  check() {      $NGINX_SBIN -c $NGINX_CONF -t  }  reload() {      kill -HUP `cat $NGINX_PID` && echo "reload success!"  }  relog() {      kill -USR1 `cat $NGINX_PID` && echo "relog success!"  }  case "$1" in      start)          start          ;;      stop)          stop          ;;      restart)          restart          ;;      check|chk)          check          ;;      status)          status -p $NGINX_PID          ;;      reload)          reload          ;;      relog)          relog          ;;      *)          echo $"Usage: $0 {start|stop|restart|reload|status|check|relog}"          exit 1  esac

上面是nginx的启动脚本,只要把它拷贝至

/etc/init.d

目录下,就可以通过

service nginx start

等目录操作nginx。

11、Nginx平滑升级
使用新的可执行程序替换旧的可执行程序,对于编译安装的Nginx可以将新版本编译安装到旧版本的Nginx安装路径中。发送以下指令

kill -USR2 旧版本的Nginx的 master pid#旧版本的Nginx主进程重命名他的.pid魏.oldbin,然后执行新版本的Nginx的可执行程序,依次启动新的主进程和新的工作进程

此时新旧版本的Nginx实例会同时运行,要逐步停止旧版本的Nginx实例必须发送WINCH新号给旧的主进程,然后他的工作进程就将开始从容关闭

-WINCH 旧版本的Nginx的 master pidkill -WINCH 旧版本的Nginx的 master pid

一段时间后,旧的工作进程处理了所有已连接请求退出,之后由新的工作进程来处理新来的请求。
这时候我们可以选择使用旧版本还是新版本

kill -TERM newpid#Nginx将在不重载配置文件的情况下启动他的工作进程kill -HUP oldpid#从容关闭其工作进程kill -QUIT newpid#强制退出kill -TERM newpid
原创粉丝点击