nginx 编译安装详解

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx 编译安装详解相关的知识,希望对你有一定的参考价值。

参考技术A

编译自:
http://nginx.org/en/docs/install.html
https://www.nginx.com/resources/wiki/start/topics/tutorials/installoptions/

如果需要特别的功能,可进行编译安装:

源码包中提供 configure 脚本用于编译前定义 nginx 各方面的配置。
执行 configure 脚本最后生成 Makefile,make 命令根据 Makefile 进行编译安装。

configure 命令支持如下参数:

--prefix=path

--sbin-path=path

--conf-path=path

--pid-path=path

--error-log-path=path

--http-log-path=path

--user=name

--group=name

--with-select_module
--without-select_module

--with-poll_module
--without-poll_module

--without-http_gzip_module

--without-http_rewrite_module

--without-http_proxy_module

--with-http_ssl_module

--with-pcre=path

--with-pcre-jit

--with-zlib=path

--with-cc-opt=parameters

--with-ld-opt=parameters

Example of parameters usage (all of this needs to be typed in one line):

配置完成后,使用 make 命令编译安装 nginx。

版权信息 :
本文编译自 nginx.org 的部分,遵循其原来的 licence 声明: 2-clause BSD-like license

本文亦有部分编译自 nginx.com

nginx 编译安装与配置详解

一、编译安装

1、使用yum安装所需的包,虽然需要编译几个依赖包,pcre、zlib、openssl

yum -y groupinstall "Development Tools""Server Platform Development"

yum install pcre-devel zlib-devel openssl-devel gcc* (一般系统装好了,以下的这几个包openssl、zlib、pcre都已经安装上了,注意: pcre-devel zlib-devel openssl-devel这几个devel包要装上,不然编译的时候会报错)


2、添加用户和组

groupadd -r nginx

useradd -r -g nginx nginx

 

3、解压安装文件

tar -zxvf nginx-1.10.1.tar.gz

cd nginx-1.10.1


4、配置选项

./configure --prefix=/usr/local/nginx #nginx家目录

--sbin-path=/usr/sbin #nginx主程序路径

--conf-path=/etc/nginx/nginx.conf #nginx主配置文件路径

--error-log-path=/var/log/nginx/error.log #错误日志路径

--http-log-path=/var/log/nginx/access.log #访问日志路径

--pid-path=/var/run/nginx/nginx.pid #进程ID路径

--lock-path=/var/lock/nginx.lock #nginx锁文件路径

--user=nginx #worker进程所属的用户名称

--group=nginx #worker进程所属的组名称

--with-http_ssl_module #启用http_ssl模块

--with-http_flv_module #启用服务端伪流媒体模块

--with-http_stub_status_module #启用健康检查模块

--with-http_gzip_static_module #启用ngnix 的静态缓存模块

--http-client-body-temp-path=/var/tmp/nginx/client #启用为http连接的请求实体临时文件设置路径

--http-proxy-temp-path=/var/tmp/nginx/proxy #为http代理临时文件设置路径

--http-fastcgi-temp-path=/var/tmp/nginx/fcgi #为http fastcgi临时文件设置路径

--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi #为http uwcgi临时文件设置路径

--http-scgi-temp-path=/var/tmp/nginx/scgi  #为http scgi临时文件设置路径

--with-pcre #使用pcre库文件

--with-stream #启用TCP/UDP代理模块



运行配置检查:

Configuration summary

  + using system PCRE library

  + using system OpenSSL library

  + md5: using OpenSSL library

  + sha1: using OpenSSL library

  + using system zlib library

 

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

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

  nginx modules path: "/usr/local/modules"

  nginx configuration prefix: "/etc/nginx"

  nginx configuration file: "/etc/nginx/nginx.conf"

  nginx pid file: "/var/run/nginx/nginx.pid"

  nginx error log file: "/var/log/nginx/error.log"

  nginx http access log file: "/var/log/nginx/access.log"

  nginx http client request body temporary files: "/var/tmp/nginx/client/"

  nginx http proxy temporary files: "/var/tmp/nginx/proxy/"

  nginx http fastcgi temporary files: "/var/tmp/nginx/fcgi/"

  nginx http uwsgi temporary files: "/var/tmp/nginx/uwsgi"

  nginx http scgi temporary files: "/var/tmp/nginx/scgi

 

5、编译和安装

make && make install


6、在/var/tmp目录下建立nginx目录

mkdir -p /var/tmp/nginx


7、检查配置文件是否有语法错误

[[email protected] ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful


8、编辑服务启动脚本

vim /etc/init.d/nginx

#! /bin/bash

#

# 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

# pidfile:       /var/run/nginx/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/sbin/nginx"

prog=$(basename $nginx)

NGINX_CONF_FILE="/etc/nginx/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/nginx.lock

start() {

  [ -x $nginx ] || exit 5

  [ -f $NGINX_CONF_FILE ] || exit 6

  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


9、启动nginx

[[email protected] init.d]# service nginx start
正在启动 nginx:                                           [确定]

10、停止nginx

[[email protected] init.d]# service nginx stop
停止 nginx:                                               [确定]



11、查看进程情况

[[email protected] init.d]# ps -ef|grep nginx
root      10342      1  0 02:04 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx     10344  10342  0 02:04 ?        00:00:00 nginx: worker process                   
root      10350   1613  0 02:07 pts/0    00:00:00 grep nginx

12、查看监听的端口

[[email protected] init.d]# netstat -tnlp|grep nginx
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      10342/nginx


13、查看测试页

技术分享




















本文出自 “服务器运维” 博客,请务必保留此出处http://shamereedwine.blog.51cto.com/5476890/1874601

以上是关于nginx 编译安装详解的主要内容,如果未能解决你的问题,请参考以下文章

Linux下Nginx编译安装过程详解

Linux下nginx编译安装教程和编译参数详解

Nginx服务简介及编译安装详解

nginx 编译安装与配置详解

CentOS7 - Nginx安装目录与编译参数详解

编译安装nginx及参数详解