---
title: "CentOS 7 安装Nginx-1.8.0"
author: "yjph83"
date: "2016-07-12T14:20:12Z"
tags: ["nginx"]
categories: ["开源软件"]
slug: "centos-7-nginx-1-8-0"
draft: false
lastmod: "2016-07-12T18:24:23Z"
id: 2310754
url: "https://yjph83.iteye.com/blog/2310754"
---

原文：[http://my.oschina.net/liucao/blog/470241](http://my.oschina.net/liucao/blog/470241 "CentOS 7 安装Nginx-1.9.2") （CentOS 7 安装Nginx-1.9.2）

 

一、准备工作

1、安装必备工具：

 

$ yum -y install gcc gcc-c++ autoconf automake

$ yum -y install zlib zlib-devel openssl openssl-devel pcre-devel

说明：

pcre: 用来作地址重写的功能。

zlib：nginx 的gzip模块,传输数据打包，省流量（但消耗资源）。

openssl：提供ssl加密协议。

 

2、新建一个系统级用户组和匿名用户，以及下面编译时使用

$ groupadd -r nginx

$ useradd  -g nginx  nginx

二、Nginx编译安装：

 

1、下载Nginx：http://nginx.org/en/download.html

2、解压编译：

 

$ tar -zxvf nginx-1.8.0.tar.gz

$ cd nginx-1.8.0/

 

$ ./configure \\

--prefix=/usr/local/nginx \\

--sbin-path=/usr/local/nginx/sbin/nginx \\

--conf-path=/usr/local/nginx/conf/nginx.conf \\

--error-log-path=/var/log/nginx/error.log \\

--http-log-path=/var/log/nginx/access.log \\

--pid-path=/var/run/nginx.pid \\

--lock-path=/var/run/nginx.lock \\

--http-client-body-temp-path=/var/cache/nginx/client\_temp \\

--http-proxy-temp-path=/var/cache/nginx/proxy\_temp \\

--http-fastcgi-temp-path=/var/cache/nginx/fastcgi\_temp \\

--http-uwsgi-temp-path=/var/cache/nginx/uwsgi\_temp \\

--http-scgi-temp-path=/var/cache/nginx/scgi\_temp \\

--user=nginx \\

--group=nginx \\

--with-http\_ssl\_module \\

--with-http\_realip\_module \\

--with-http\_addition\_module \\

--with-http\_sub\_module \\

--with-http\_dav\_module \\

--with-http\_flv\_module \\

--with-http\_mp4\_module \\

--with-http\_gunzip\_module \\

--with-http\_gzip\_static\_module \\

--with-http\_random\_index\_module \\

--with-http\_secure\_link\_module \\

--with-http\_stub\_status\_module \\

--with-http\_auth\_request\_module \\

--with-mail \\

--with-mail\_ssl\_module \\

--with-file-aio \\

--with-ipv6 \\

--with-http\_spdy\_module \\

--with-cc-opt='-O2 -g -pipe -Wp,-D\_FORTIFY\_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'

上面的参数的作用可以通过--help来查看下文附加1中有提供

 

$ ./configure --help

编译日志：

checking for OS

 + Linux 3.10.0-229.4.2.el7.x86\_64 x86\_64

checking for C compiler ... found

 + using GNU C compiler

 + gcc version: 4.8.3 20140911 (Red Hat 4.8.3-9) (GCC) 

checking for gcc -pipe switch ... found

checking for gcc builtin atomic operations ... found

checking for C99 variadic macros ... found

checking for gcc variadic macros ... found

checking for unistd.h ... found

checking for inttypes.h ... found

checking for limits.h ... found

checking for sys/filio.h ... not found

checking for sys/param.h ... found

checking for sys/mount.h ... found

checking for sys/statvfs.h ... found

checking for crypt.h ... found

checking for Linux specific features

checking for epoll ... found

checking for EPOLLRDHUP ... found

checking for O\_PATH ... found

checking for sendfile() ... found

checking for sendfile64() ... found

checking for sys/prctl.h ... found

checking for prctl(PR\_SET\_DUMPABLE) ... found

checking for sched\_setaffinity() ... found

checking for crypt\_r() ... found

checking for sys/vfs.h ... found

checking for poll() ... found

checking for /dev/poll ... not found

checking for kqueue ... not found

checking for crypt() ... not found

checking for crypt() in libcrypt ... found

checking for F\_READAHEAD ... not found

checking for posix\_fadvise() ... found

checking for O\_DIRECT ... found

checking for F\_NOCACHE ... not found

checking for directio() ... not found

checking for statfs() ... found

checking for statvfs() ... found

checking for dlopen() ... not found

checking for dlopen() in libdl ... found

checking for sched\_yield() ... found

checking for SO\_SETFIB ... not found

checking for SO\_REUSEPORT ... found

checking for SO\_ACCEPTFILTER ... not found

checking for TCP\_DEFER\_ACCEPT ... found

checking for TCP\_KEEPIDLE ... found

checking for TCP\_FASTOPEN ... not found

checking for TCP\_INFO ... found

checking for accept4() ... found

checking for eventfd() ... found

checking for int size ... 4 bytes

checking for long size ... 8 bytes

checking for long long size ... 8 bytes

checking for void * size ... 8 bytes

checking for uint64\_t ... found

checking for sig\_atomic\_t ... found

checking for sig\_atomic\_t size ... 4 bytes

checking for socklen\_t ... found

checking for in\_addr\_t ... found

checking for in\_port\_t ... found

checking for rlim\_t ... found

checking for uintptr\_t ... uintptr\_t found

checking for system byte ordering ... little endian

checking for size\_t size ... 8 bytes

checking for off\_t size ... 8 bytes

checking for time\_t size ... 8 bytes

checking for setproctitle() ... not found

checking for pread() ... found

checking for pwrite() ... found

checking for sys\_nerr ... found

checking for localtime\_r() ... found

checking for posix\_memalign() ... found

checking for memalign() ... found

checking for mmap(MAP\_ANON|MAP\_SHARED) ... found

checking for mmap("/dev/zero", MAP\_SHARED) ... found

checking for System V shared memory ... found

checking for POSIX semaphores ... not found

checking for POSIX semaphores in libpthread ... found

checking for struct msghdr.msg\_control ... found

checking for ioctl(FIONBIO) ... found

checking for struct tm.tm\_gmtoff ... found

checking for struct dirent.d\_namlen ... not found

checking for struct dirent.d\_type ... found

checking for sysconf(\_SC\_NPROCESSORS\_ONLN) ... found

checking for openat(), fstatat() ... found

checking for getaddrinfo() ... found

checking for PCRE library ... found

checking for PCRE JIT support ... found

checking for OpenSSL library ... found

checking for zlib library ... found

creating objs/Makefile

 

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"

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

  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/http.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: "uwsgi\_temp"

  nginx http scgi temporary files: "scgi\_temp"

好像很成功。

 

3、安装:

$ make && make install

 

4、启动：

$ nginx -c /etc/nginx/nginx.conf

 

如果提示缺什么包，直接用yum安装

$ yum install xxxxx

 

如果提示：

nginx: \[emerg] mkdir() "/var/tmp/nginx/client\_temp" failed (2: No such file or directory)

则用管理员去mkdir 创建文件夹后再启动。

 

对于nginx的操作，指令比较长，也不便于操作，因此可以使用脚本，教程中转：

 

CentOS 7 Nginx 控制脚本

 

附加1：

 

 --help                             print this message

 

  --prefix=PATH                      set installation prefix

  --sbin-path=PATH                   set nginx binary pathname

  --conf-path=PATH                   set nginx.conf pathname

  --error-log-path=PATH              set error log pathname

  --pid-path=PATH                    set nginx.pid pathname

  --lock-path=PATH                   set nginx.lock pathname

 

  --user=USER                        set non-privileged user for

                                     worker processes

  --group=GROUP                      set non-privileged group for

                                     worker processes

 

  --build=NAME                       set build name

  --builddir=DIR                     set build directory

 

  --with-select\_module               enable select module

  --without-select\_module            disable select module

  --with-poll\_module                 enable poll module

  --without-poll\_module              disable poll module

 

  --with-threads                     enable thread pool support

 

  --with-file-aio                    enable file AIO support

  --with-ipv6                        enable IPv6 support

 

  --with-http\_ssl\_module             enable ngx\_http\_ssl\_module

  --with-http\_spdy\_module            enable ngx\_http\_spdy\_module

  --with-http\_realip\_module          enable ngx\_http\_realip\_module

  --with-http\_addition\_module        enable ngx\_http\_addition\_module

  --with-http\_xslt\_module            enable ngx\_http\_xslt\_module

  --with-http\_image\_filter\_module    enable ngx\_http\_image\_filter\_module

  --with-http\_geoip\_module           enable ngx\_http\_geoip\_module

  --with-http\_sub\_module             enable ngx\_http\_sub\_module

  --with-http\_dav\_module             enable ngx\_http\_dav\_module

  --with-http\_flv\_module             enable ngx\_http\_flv\_module

  --with-http\_mp4\_module             enable ngx\_http\_mp4\_module

  --with-http\_gunzip\_module          enable ngx\_http\_gunzip\_module

  --with-http\_gzip\_static\_module     enable ngx\_http\_gzip\_static\_module

  --with-http\_auth\_request\_module    enable ngx\_http\_auth\_request\_module

  --with-http\_random\_index\_module    enable ngx\_http\_random\_index\_module

  --with-http\_secure\_link\_module     enable ngx\_http\_secure\_link\_module

  --with-http\_degradation\_module     enable ngx\_http\_degradation\_module

  --with-http\_stub\_status\_module     enable ngx\_http\_stub\_status\_module

 

  --without-http\_charset\_module      disable ngx\_http\_charset\_module

  --without-http\_gzip\_module         disable ngx\_http\_gzip\_module

  --without-http\_ssi\_module          disable ngx\_http\_ssi\_module

  --without-http\_userid\_module       disable ngx\_http\_userid\_module

  --without-http\_access\_module       disable ngx\_http\_access\_module

  --without-http\_auth\_basic\_module   disable ngx\_http\_auth\_basic\_module

  --without-http\_autoindex\_module    disable ngx\_http\_autoindex\_module

  --without-http\_geo\_module          disable ngx\_http\_geo\_module

  --without-http\_map\_module          disable ngx\_http\_map\_module

  --without-http\_split\_clients\_module disable ngx\_http\_split\_clients\_module

  --without-http\_referer\_module      disable ngx\_http\_referer\_module

  --without-http\_rewrite\_module      disable ngx\_http\_rewrite\_module

  --without-http\_proxy\_module        disable ngx\_http\_proxy\_module

  --without-http\_fastcgi\_module      disable ngx\_http\_fastcgi\_module

  --without-http\_uwsgi\_module        disable ngx\_http\_uwsgi\_module

  --without-http\_scgi\_module         disable ngx\_http\_scgi\_module

  --without-http\_memcached\_module    disable ngx\_http\_memcached\_module

  --without-http\_limit\_conn\_module   disable ngx\_http\_limit\_conn\_module

  --without-http\_limit\_req\_module    disable ngx\_http\_limit\_req\_module

  --without-http\_empty\_gif\_module    disable ngx\_http\_empty\_gif\_module

  --without-http\_browser\_module      disable ngx\_http\_browser\_module

  --without-http\_upstream\_hash\_module

                                     disable ngx\_http\_upstream\_hash\_module

  --without-http\_upstream\_ip\_hash\_module

                                     disable ngx\_http\_upstream\_ip\_hash\_module

  --without-http\_upstream\_least\_conn\_module

                                     disable ngx\_http\_upstream\_least\_conn\_module

  --without-http\_upstream\_keepalive\_module

                                     disable ngx\_http\_upstream\_keepalive\_module

  --without-http\_upstream\_zone\_module

                                     disable ngx\_http\_upstream\_zone\_module

 

  --with-http\_perl\_module            enable ngx\_http\_perl\_module

  --with-perl\_modules\_path=PATH      set Perl modules path

  --with-perl=PATH                   set perl binary pathname

 

  --http-log-path=PATH               set http access log pathname

  --http-client-body-temp-path=PATH  set path to store

                                     http client request body temporary files

  --http-proxy-temp-path=PATH        set path to store

                                     http proxy temporary files

  --http-fastcgi-temp-path=PATH      set path to store

                                     http fastcgi temporary files

  --http-uwsgi-temp-path=PATH        set path to store

                                     http uwsgi temporary files

  --http-scgi-temp-path=PATH         set path to store

                                     http scgi temporary files

 

  --without-http                     disable HTTP server

  --without-http-cache               disable HTTP cache

 

  --with-mail                        enable POP3/IMAP4/SMTP proxy module

  --with-mail\_ssl\_module             enable ngx\_mail\_ssl\_module

  --without-mail\_pop3\_module         disable ngx\_mail\_pop3\_module

  --without-mail\_imap\_module         disable ngx\_mail\_imap\_module

  --without-mail\_smtp\_module         disable ngx\_mail\_smtp\_module

 

  --with-stream                      enable TCP proxy module

  --with-stream\_ssl\_module           enable ngx\_stream\_ssl\_module

  --without-stream\_access\_module     disable ngx\_stream\_access\_module

  --without-stream\_upstream\_hash\_module

                                     disable ngx\_stream\_upstream\_hash\_module

  --without-stream\_upstream\_least\_conn\_module

                                     disable ngx\_stream\_upstream\_least\_conn\_module

  --without-stream\_upstream\_zone\_module

                                     disable ngx\_stream\_upstream\_zone\_module

 

  --with-google\_perftools\_module     enable ngx\_google\_perftools\_module

  --with-cpp\_test\_module             enable ngx\_cpp\_test\_module

 

  --add-module=PATH                  enable an external module

 

  --with-cc=PATH                     set C compiler pathname

  --with-cpp=PATH                    set C preprocessor pathname

  --with-cc-opt=OPTIONS              set additional C compiler options

  --with-ld-opt=OPTIONS              set additional linker options

  --with-cpu-opt=CPU                 build for the specified CPU, valid values:

                                     pentium, pentiumpro, pentium3, pentium4,

                                     athlon, opteron, sparc32, sparc64, ppc64

 

  --without-pcre                     disable PCRE library usage

  --with-pcre                        force PCRE library usage

  --with-pcre=DIR                    set path to PCRE library sources

  --with-pcre-opt=OPTIONS            set additional build options for PCRE

  --with-pcre-jit                    build PCRE with JIT compilation support

 

  --with-md5=DIR                     set path to md5 library sources

  --with-md5-opt=OPTIONS             set additional build options for md5

  --with-md5-asm                     use md5 assembler sources

 

  --with-sha1=DIR                    set path to sha1 library sources

  --with-sha1-opt=OPTIONS            set additional build options for sha1

  --with-sha1-asm                    use sha1 assembler sources

 

  --with-zlib=DIR                    set path to zlib library sources

  --with-zlib-opt=OPTIONS            set additional build options for zlib

  --with-zlib-asm=CPU                use zlib assembler sources optimized

                                     for the specified CPU, valid values:

                                     pentium, pentiumpro

 

  --with-libatomic                   force libatomic\_ops library usage

  --with-libatomic=DIR               set path to libatomic\_ops library sources

 

  --with-openssl=DIR                 set path to OpenSSL library sources

  --with-openssl-opt=OPTIONS         set additional build options for OpenSSL

 

  --with-debug                       enable debug logging