阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Nginx简介与源码编译安装

139次阅读
没有评论

共计 4592 个字符,预计需要花费 12 分钟才能阅读完成。

1.Nginx 简介

Nginx 官方站点:http://nginx.orgNginx 官方文档:http://nginx.org/en/docs

Nginx 是一款免费,开源,轻量级高性能的网站服务器(http server)和反向代理(reverse proxy)服务器程序,能够反向代理 http 协议和 mail 协议。

Nginx 原作者于 2002 年开发 nginx,正式第一版于 2004 年发布,至今 nginx 已经在 web server 占据了很大的市场份额如图(可以在 http://news.netcraft.com/archives/category/web-server-survey/ 查看到):

Nginx 简介与源码编译安装

从这张图可以看出 Nginx 已经在 Web Server 中排名第三了,并且自从 nginx 出来以后市场份额一直有增无减,这是最新发布的统计数据 2014 年 12 月 18 日发布的。

Nginx 与 Apache 的异同:它们都为 http 服务软件,都采用模块化设计结构并且都支持动态语言(php、python、perl)等,同时都支持正反向代理、虚拟主机、URL 重写、压缩资源、SSL 加密等功能,最大差别是 apache 的处理速度慢并且占用很多系统内存资源,

2.Nginx 的安装与配置(编译安装)

首先下载 Nginx 源码包到本地并且解压

wget http://nginx.org/download/nginx-1.6.2.tar.gz    && tar xf nginx-1.6.2.tar.gz

使用./configure –help 查看帮助

./configure –help 或者安装一个第三方软件包(bash-completion)手动编译时可以快速查看支持的参数

Nginx 简介与源码编译安装

可以从图中看出来 nginx1.6.2 可使用的参数有 100 种,介绍下将 nginx 作为 http 服务器的常用参数吧
–conf-path          设置 nginx 的配置文件所在的路径需要指明配置文件的文件名(默认为安装目录下的 conf/nginx.conf)
–error-log-path    设置 nginx 的错误日志文件所在的路径需要指明日志文件的文件名 (默认为安装目录下的 logs/error.log)
–group                设置 nginx 运行时以什么组来运行 worker 进程 (默认为 nobody 组)
–user                  设置 nginx 运行时以什么用户来运行 worker 进程 (默认为 nobody 用户)
–prefix                指明 nginx 的安装所在路径
–sbin-path          指明 nginx 的 sbin 目录所在的目录(默认为安装目录下 sbin/nginx)
–pid-path            指明 nginx 的 pid 文件所在的路径需要指明 pid 文件名 (默认为安装目录下的 logs/nginx.pid)
–lock-path            指明 nginx 的 lock 文件所在的路径
–http-log-path      指明 nginx 的访问文件所在路径 (默认为安装目录下的 log/access.log)
–with-http_gzip_static_module      启用 gzip 压缩模块
–with-http_ssl_mdule                    启用 ssl 模块

 

./configure –prefix=/usr/local/nginx \
–conf-path=/usr/local/nginx/conf/nginx.conf \
–http-log-path=/usr/local/nginx/logs/access.log \
–user=nginx \
–group=nginx \
–error-log-path=/usr/local/nginx/logs/error.log \
–pid-path=/usr/local/nginx/var/nginx.pid \
–lock-path=/usr/local/nginx/var/nginx.lock \
–with-http_gzip_static_module \
–with-http_ssl_module

Nginx 简介与源码编译安装

确定没有任何错误后使用 make && make install 安装

3. 添加服务启动脚本

将如下脚本添加至 /etc/rc.d/init.d/nginx 目录下, 然后使用 chmod u+x /etc/rc.d/init.d/nginx 将 nginx 添加至系统服务 chkconfig –add nginx 如果需要开机启动则可以输入 chkconfig nginx on 即可

 

#!/bin/sh

#

# nginx – this script starts and stops the nginx daemon

#

# chkconfig:  – 85 15

# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \

#              proxy and IMAP/POP3 proxy server

# processname: nginx

# config:      /etc/nginx/nginx.conf

# config:      /etc/sysconfig/nginx

# pidfile:    /var/run/nginx.pid

 

# Source function library.

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

 

# Source networking configuration.

. /etc/sysconfig/network

 

# Check that networking is up.

[“$NETWORKING” = “no”] && exit 0

 

nginx=”/usr/local/nginx/sbin/nginx”

prog=$(basename $nginx)

 

NGINX_CONF_FILE=”/usr/local/nginx/conf/nginx.conf”

 

 

lockfile=/usr/local/nginx/var/nginx.lock

 

make_dirs() {

  # make required directories

  user=`nginx -V 2>&1 | grep “configure arguments:” | sed ‘s/[^*]*–user=\([^]*\).*/\1/g’ -`

  options=`$nginx -V 2>&1 | grep ‘configure arguments:’`

  for opt in $options; do

      if [`echo $opt | grep ‘.*-temp-path’`]; then

          value=`echo $opt | cut -d “=” -f 2`

          if [! -d “$value”]; then

              # echo “creating” $value

              mkdir -p $value && chown -R $user $value

          fi

      fi

  done

}

 

start() {

    [-x $nginx] || exit 5

    [-f $NGINX_CONF_FILE] || exit 6

    make_dirs

    echo -n $”Starting $prog: “

    daemon $nginx -c $NGINX_CONF_FILE

    retval=$?

    echo

    [$retval -eq 0] && touch $lockfile

    return $retval

}

 

stop() {

    echo -n $”Stopping $prog: “

    killproc $prog -QUIT

    retval=$?

    echo

    [$retval -eq 0] && rm -f $lockfile

    return $retval

}

 

restart() {

    configtest || return $?

    stop

    sleep 1

    start

}

 

reload() {

    configtest || return $?

    echo -n $”Reloading $prog: “

    killproc $nginx -HUP

    RETVAL=$?

    echo

}

 

force_reload() {

    restart

}

 

configtest() {

  $nginx -t -c $NGINX_CONF_FILE

}

 

rh_status() {

    status $prog

}

 

rh_status_q() {

    rh_status >/dev/null 2>&1

}

 

case “$1” in

    start)

        rh_status_q && exit 0

        $1

        ;;

    stop)

        rh_status_q || exit 0

        $1

        ;;

    restart|configtest)

        $1

        ;;

    reload)

        rh_status_q || exit 7

        $1

        ;;

    force-reload)

        force_reload

        ;;

    status)

        rh_status

        ;;

    condrestart|try-restart)

        rh_status_q || exit 0

            ;;

    *)

        echo $”Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}”

        exit 2

esac

————————————– 分割线 ————————————–

CentOS 6.2 实战部署 Nginx+MySQL+PHP http://www.linuxidc.com/Linux/2013-09/90020.htm

使用 Nginx 搭建 WEB 服务器 http://www.linuxidc.com/Linux/2013-09/89768.htm

搭建基于 Linux6.3+Nginx1.2+PHP5+MySQL5.5 的 Web 服务器全过程 http://www.linuxidc.com/Linux/2013-09/89692.htm

CentOS 6.3 下 Nginx 性能调优 http://www.linuxidc.com/Linux/2013-09/89656.htm

CentOS 6.3 下配置 Nginx 加载 ngx_pagespeed 模块 http://www.linuxidc.com/Linux/2013-09/89657.htm

CentOS 6.4 安装配置 Nginx+Pcre+php-fpm http://www.linuxidc.com/Linux/2013-08/88984.htm

Nginx 安装配置使用详细笔记 http://www.linuxidc.com/Linux/2014-07/104499.htm

Nginx 日志过滤 使用 ngx_log_if 不记录特定日志 http://www.linuxidc.com/Linux/2014-07/104686.htm

————————————– 分割线 ————————————–

Nginx 的详细介绍 :请点这里
Nginx 的下载地址 :请点这里

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-20发表,共计4592字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中