美文网首页
CentOS6.5安装Nginx

CentOS6.5安装Nginx

作者: 十二月的柒 | 来源:发表于2017-10-25 10:57 被阅读0次

    1.编译依赖 gcc 环境,如果没有 gcc 环境,需要安装 gcc

    命令:yum install gcc-c++

    2.nginx 的 http 模块使用 pcre 来解析正则表达式

    命令:yum install -y pcre pcre-devel

    3.zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip,所以需要在 linux 上安装 zlib 库

    命令:yum install -y zlib zlib-devel

    4.nginx 不仅支持 http 协议,还支持 https(即在 ssl 协议上传输 http),所以需要在 linux安装 openssl 库

    命令:yum install -y openssl openssl-devel

    5.下载源码[目录:/usr/local/src]

    命令:wget  http://nginx.org/download/nginx-1.8.0.tar.gz

    6.解压[目录:/usr/local/src/nginx-1.8.0]

    命令:tar –zxvf nginx-1.8.0.tar.gz

    7.编译[目录:/usr/local/src/nginx-1.8.0]

    命令:./configure

    编译成功信息:

    nginx path prefix: "/usr/local/nginx"

    nginx binary file: "/usr/local/nginx/sbin/nginx"

    nginx configuration prefix: "/usr/local/nginx/conf"

    nginx configuration file: "/usr/local/nginx/conf/nginx.conf"

    nginx pid file: "/usr/local/nginx/logs/nginx.pid"

    nginx error log file: "/usr/local/nginx/logs/error.log"

    nginx http access log file: "/usr/local/nginx/logs/access.log"

    nginx http client request body temporary files: "client_body_temp"

    nginx http proxy temporary files: "proxy_temp"

    nginx http fastcgi temporary files: "fastcgi_temp"

    nginx http uwsgi temporary files: "uwsgi_temp"

    nginx http scgi temporary files: "scgi_temp"

    8.安装[目录:/usr/local/src/nginx-1.8.0]

    命令:make && make install

    9.启动[目录:/usr/local/nginx/sbin]

    命令:./nginx

    备注:Nginx 的配置文件在 /usr/local/nginx/conf 目录;

    启动命令:./nginx

    无间断重启命令:./nginx -s reload

    强制停止命令:./nginx -s stop

    延迟停止命令:./nginx -s quit

    10.查看进程,检测是否启动成功

    命令:ps aux | grep nginx

    结果:

    root      1952  0.0  0.0  4356  716 pts/0    S+  19:46  0:00 grep nginx

    root    32377  0.0  0.1  5784  1260 ?        Ss  19:04  0:00 nginx: master process /usr/local/nginx/sbin/nginx

    nobody  32440  0.0  0.1  6320  1656 ?        S    19:20  0:00 nginx: worker process

    11.防火墙开启80端口(nginx默认端口)

    命令:/sbin/iptables -I INPUT -p tcp --dport 8080 -j ACCEPT

    命令:/etc/rc.d/init.d/iptables save

    12.加载流程示意图

    13.Nginx添加到Service

    步骤一(编辑文件)

    命令:vi /etc/init.d/nginx

    #!/bin/bash

    # nginx Startup script for the Nginx HTTP Server

    # it is v.0.0.2 version.

    # chkconfig: - 85 15

    # description: Nginx is a high-performance web and proxy server.

    #              It has a lot of features, but it's not for everyone.

    # processname: nginx

    # pidfile: /var/run/nginx.pid

    # config: /usr/local/nginx/conf/nginx.confnginxd=/usr/local/nginx/sbin/nginx

    nginx_config=/usr/local/nginx/conf/nginx.conf

    nginx_pid=/var/run/nginx.pid

    RETVAL=0

    prog="nginx"

    # Source function library.

    . /etc/rc.d/init.d/functions

    # Source networking configuration.

    . /etc/sysconfig/network

    # Check that networking is up.

    [ ${NETWORKING} = "no" ] && exit 0

    [ -x $nginxd ] || exit 0

    # Start nginx daemons functions.

    start() {

    if [ -e $nginx_pid ];then

    echo "nginx already running...."

    exit 1

    fi

    echo -n $"Starting $prog: "

    daemon $nginxd -c ${nginx_config}

    RETVAL=$?

    echo

    [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx

    return $RETVAL

    }

    # Stop nginx daemons functions.

    stop() {

    echo -n $"Stopping $prog: "

    killproc $nginxd

    RETVAL=$?

    echo

    [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid

    }

    # reload nginx service functions.

    reload() {

    echo -n $"Reloading $prog: "

    #kill -HUP `cat ${nginx_pid}`

    killproc $nginxd -HUP

    RETVAL=$?

    echo

    }

    # See how we were called.

    case "$1" in

    start)

    start

    ;;

    stop)

    stop

    ;;

    reload)

    reload

    ;;

    restart)

    stop

    start

    ;;

    status)

    status $prog

    RETVAL=$?

    ;;

    *)

    echo $"Usage: $prog {start|stop|restart|reload|status|help}"

    exit 1

    esac

    exit $RETVAL

    步骤二(添加执行权限)

    命令:chkconfig --add /etc/init.d/nginx

    命令:chmod755/etc/init.d/nginx

    命令:chkconfig --add nginx

    步骤三(开机启动)

    /sbin/chkconfig --level2345nginx on

    步骤四(服务相关命令)

    service nginx start

    service nginx stop

    service nginx reload

    service nginx restart

    相关文章

      网友评论

          本文标题:CentOS6.5安装Nginx

          本文链接:https://www.haomeiwen.com/subject/iewtpxtx.html