zoukankan      html  css  js  c++  java
  • Nginx 安装配置教程

    1.安装 Nginx 的先决条件

    依赖库:GCC, PCRE, zlib, OpenSSL

    * GCC (Nginx 由 C 语言编写,因此需要在系统上安装一个编译工具) 基本上 Linux 自带,可以通过命令 gcc 查看是否安装,显示 no input files, 即为已安装。

    如果为 comand not found ,即未安装。通过

    apt-get install gcc

    yum install gcc

    来安装。

    * PCRE库(Nginx 的 Rewrite 模块和 HTTP 模块会使用到 PCRE 正则表达式语法)。通过

    apt-get install libpcre3 pibpcre3-dev

    yum install pcre pcre-devel

    来安装。

    * zilb(在 Nginx 的各种模块中需要使用 gzip 压缩)。通过

    apt-get install zlib1g zlig1g-dev

    yum install lib zlib-devel

    来安装。

    * OpenSSL (在 Nginx 中,如果服务器提供安全网页则会用到 OpenSSL 库)。通过

    apt-get install openssl libssl-dev

    yum install openssl openssl-dev 

    来安装。

    2.通过源码安装 Nginx

    在 /home 下新建一个 src 目录并移动到当前目录:

    mkdir src && cd src

    下载Nginx压缩包文件

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

    官网地址如下,可以选择自己需要的版本(稳定版,测试版等)

    http://nginx.org/en/download.html

    解压

    tar zxf nginx-1.14.0.tar.gz

    移动到解压目录,结构如下

    执行安装

    ./configure

    ./configure 进行程序验证过程,以便确定系统包含所有必要的组成成分。

    make

    make 对应用程序进行编译,一个成功的 build 编译应该会出现最后的信息:

    make[1]: leaving directory followed by the project source path.

    make install

    make install 复制编译后的文件(也包括资源文件)到安装目录。

    3.启动 Nginx

    Nginx 的默认安装目录是 /usr/local/nginx,

    通过

    /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

    启动 Nginx。

    查看进程是否启动:

    ps -ef | grep nginx

    4.控制 Nginx 服务

    Nginx 属于运行于后台的类型,即作为守护程序。所以启动 Nginx 后屏幕并不会输出任何信息,这代表 Nginx 已经正确运行。我们来把 Nginx添加到系统服务中。

    a. 为 Nginx 建立 init 脚本

    init 脚本是作为启动服务的脚本,可以通过一些命令来控制一个应用程序的 start、stop 或者其他操作。例如通过 /etc/init.d/httd start 开启 http 服务

    /etc/init.d/httpd start

    该命令和 service httpd start 是等效的。

    我们来为 Nginx 建立 init 脚本,在 /etc/init.d/(在有些系统下,/etc/init.d/ 实际是 /etc/rc.d/init.d/ 的符号链接)下新建一个名为 nginx 的脚本:

    vim nginx

    写入下列内容:

    #! /bin/sh
     
    ### BEGIN INIT INFO
    # Provides:          nginx
    # Required-Start:    $all
    # Required-Stop:     $all
    # Default-Start:     2 3 4 5
    # Default-Stop:      0 1 6
    # Short-Description: starts the nginx web server
    # Description:       starts nginx using start-stop-daemon
    ### END INIT INFO
     
    PATH=/opt/bin:/opt/sbin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
    DAEMON=/opt/sbin/nginx
    NAME=nginx
    DESC=nginx
     
    test -x $DAEMON || exit 0
     
    # Include nginx defaults if available
    if [ -f /etc/default/nginx ] ; then
            . /etc/default/nginx
    fi
     
    set -e
     
    case "$1" in
      start)
            echo -n "Starting $DESC: "
            start-stop-daemon --start --quiet --pidfile /var/run/nginx.pid 
                    --exec $DAEMON -- $DAEMON_OPTS
            echo "$NAME."
            ;;
      stop)
            echo -n "Stopping $DESC: "
            start-stop-daemon --stop --quiet --pidfile /var/run/nginx.pid 
                    --exec $DAEMON
            echo "$NAME."
            ;;
      restart|force-reload)
            echo -n "Restarting $DESC: "
            start-stop-daemon --stop --quiet --pidfile 
                    /var/run/nginx.pid --exec $DAEMON
            sleep 1
            start-stop-daemon --start --quiet --pidfile 
                    /var/run/nginx.pid --exec $DAEMON -- $DAEMON_OPTS
            echo "$NAME."
            ;;
      reload)
          echo -n "Reloading $DESC configuration: "
          start-stop-daemon --stop --signal HUP --quiet --pidfile /var/run/nginx.pid 
              --exec $DAEMON
          echo "$NAME."
          ;;
      *)
            N=/etc/init.d/$NAME
            echo "Usage: $N {start|stop|restart|force-reload}" >&2
            exit 1
            ;;
    esac
     
    exit 0

    不同系统下内容不一样,可以在 Nginx 官网找到具体内容的写法。

     https://www.nginx.com/resources/wiki/start/topics/examples/initscripts/

    b. 安装 Nginx 的 init 脚本

    通过 chmod 命令来授予该脚本的可执行权限:

    chmod +x /etc/init.d/nginx

    到此,我们便可以通过 /etc/init.d/nginx start 或 service nginx start 命令来启动服务了。

    c. 让 Nginx 的 init 脚本在适当的运行级自动启动

    * 基于 Debian 系的发布:

    update-rc.d -f nginx defaults

    然后重启系统,运行

    ps -ef | grep nginx

    查看重启后 Nginx 是否自动启动。

    * 基于 Red Hat 系的发布:

    chkconfig --add nginx

    执行上述命令后,便可以检验该服务的运行级别:

    chkconfig --list nginx

    至此,我们便完成了从安装到配置 Nginx 自启动的过程。

  • 相关阅读:
    【洛谷3778】[APIO2017] 商旅(分数规划+Floyd)
    【AT4114】[ARC095D] Permutation Tree(简单题)
    【AT4352】[ARC101C] Ribbons on Tree(容斥+DP)
    【AT4169】[ARC100D] Colorful Sequences(DP)
    【洛谷4581】[BJOI2014] 想法(随机算法)
    【洛谷5659】[CSP-S2019] 树上的数(思维)
    【AT4439】[AGC028E] High Elements(线段树)
    【CF590E】Birthday(AC自动机+二分图匹配)
    【洛谷4298】[CTSC2008] 祭祀(Dilworth定理+二分图匹配)
    【洛谷3774】[CTSC2017] 最长上升子序列(杨表)
  • 原文地址:https://www.cnblogs.com/weixuqin/p/9327838.html
Copyright © 2011-2022 走看看