zoukankan      html  css  js  c++  java
  • Nginx学习---企业级nginx环境搭建

    1.1. nginx安装环境

    1.系统要求

    nginx是C语言开发,建议在linux上运行,本教程使用Centos6.5作为安装环境。

         1-1 安装 GCC

    源码安装nginx需要依赖gcc环境,需要安装gcc:

    yum install gcc-c++ 

         1-2 安装 PCRE

    PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。

    yum install -y pcre pcre-devel
    注:pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库。
    

         1-3 安装 zlib

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

    yum install -y zlib zlib-devel

         1-4 openssl

    OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。

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

    yum install -y openssl openssl-devel

    2. 编译安装

    创建临时文件夹

    mkdir -p /var/temp/nginx/client

    下载nginx文件: https://files.cnblogs.com/files/ftl1012/nginx-1.8.0.tar.gz

    将nginx-1.8.0.tar.gz拷贝至linux服务器。

    解压并进入目录:

    tar -zxvf nginx-1.8.0.tar.gz; cd nginx-1.8.0

    3. configure安装

    ./configure 
    --prefix=/usr/local/nginx 
    --pid-path=/var/run/nginx/nginx.pid 
    --lock-path=/var/lock/nginx.lock 
    --error-log-path=/var/log/nginx/error.log 
    --http-log-path=/var/log/nginx/access.log 
    --with-http_gzip_static_module 
    --http-client-body-temp-path=/var/temp/nginx/client 
    --http-proxy-temp-path=/var/temp/nginx/proxy 
    --http-fastcgi-temp-path=/var/temp/nginx/fastcgi 
    --http-uwsgi-temp-path=/var/temp/nginx/uwsgi 
    --http-scgi-temp-path=/var/temp/nginx/scgi
    echo "-------------------------------------------------------"
    make;make install
    

    4. 安装成功查看安装目录

      cd /usr/local/nginx

    image

    5. 启动nginx

    echo "cd /usr/local/nginx/sbin/;./nginx" >> /etc/rc.local
    注意:执行./nginx启动nginx,这里可以-c指定加载的nginx配置文件,如下:
          ./nginx -c /usr/local/nginx/conf/nginx.conf    如果不指定-c,nginx在启动时默认加载conf/nginx.conf文件,此文件的地址也可以在编译安装nginx时指定./configure的参数(--conf-path= 指向配置文件(nginx.conf))
    

    6. 查看服务状态

    ps -axu |grep --color=auto nginx     # 4879是主进程,4880是工作的进程

    image

    7. 关闭nginx

    方式1,快速停止:此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。

         cd /usr/local/nginx/sbin; ./nginx -s stop      

    方式2,完整停止(建议使用):此方式停止步骤是待nginx进程处理任务完毕进行停止。

    cd /usr/local/nginx/sbin;./nginx -s quit
    

    8. 测试是否成功

       nginx安装成功,启动nginx,即可访问虚拟机上的nginx:

    浏览器输入VM的IP即可

    image

    9. 开机自启动nginx(编写shell脚本)

    9-1 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.conf
    nginxd=/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

    9-2 设置文件的访问权限

    chmod a+x /etc/init.d/nginx   (a+x ==> all user can execute  所有用户可执行)

    这样在控制台就很容易的操作nginx了:查看Nginx当前状态、启动Nginx、停止Nginx、重启Nginx…

    image

    如果修改了nginx的配置文件nginx.conf,也可以使用上面的命令重新加载新的配置文件并运行,可以将此命令加入到rc.local文件中,这样开机的时候nginx就默认启动了

    9-3 加入到rc.local文件中

      echo "/etc/init.d/nginx start " >>/etc/rc.local
    

     【更多参考】

    1.2. Ngnix基于域名的配置_server

    1.3. Ngnix基于域名的配置_include

    1.4. Nginx基于虚拟主机别名的设置

    1.5. Nginx增加日志选项

    1.6. NGINX的重定向rewrite

    1.7. Nginx性能问题

    1.8. Nginx的留底下载文件

  • 相关阅读:
    php类自动加载
    tp5自定义分页参数
    cURL error 60: SSL certificate problem...
    ajax动态刷新的元素,导致绑定事件失效
    thinkphp5省市区三级联动例子
    restful状态码常用
    mysql的like子句
    mysql官方的测试数据库employees超30万的数据,安装方法介绍
    Aes加解密,php
    php5.6,Ajax报错,Warning: Cannot modify header information
  • 原文地址:https://www.cnblogs.com/ftl1012/p/9296005.html
Copyright © 2011-2022 走看看