##下载nginx源码:

wget http://nginx.org/download/nginx-1.7.8.tar.gz
tar -xv -f nginx-1.7.8.tar.gz -C /usr/local/src/


##安装编译环境和必须的组件:

yum groupinstall 'Development Tools'
yum install pcre pcre-devel zlib zlib-devel openssl openssl-devel gcc gcc-c++ perl perl-devel perl-ExtUtils-Embed.noarch


##创建nginx用户和组:

groupadd -r nginx
useradd -s /sbin/nologin -g nginx -c 'Nginx web server' -r nginx


##创建用于存放临时数据的目录:

mkdir -p /var/tmp/nginx/tmp


##编译安装nginx:

./configure \
--prefix=/usr/local/nginx \#设置nginx的根目录
--conf-path=/etc/nginx/nginx.conf \#设置nginx配置文件的路径
--error-log-path=/var/log/nginx/error.log \#设置错误日志的路径
--http-log-path=/var/log/nginx/access.log \#设置访问日志的路径
--http-client-body-temp-path=/var/tmp/nginx/tmp/client_body \#设置客户端http请求的临时文件存放的位置
--http-proxy-temp-path=/var/tmp/nginx/tmp/proxy \#nginx作为反向代理时,上游服务器产生的http包体临时存放的位置
--http-fastcgi-temp-path=/var/tmp/nginx/tmp/fastcgi \#Fastcgi所使用临时文件的存放位置
--http-uwsgi-temp-path=/var/tmp/nginx/tmp/uwsgi \#uWSGI 所使用临时文件的存放位置
--http-scgi-temp-path=/var/tmp/nginx/tmp/scgi \#SCGI所使用临时文件的存放位置
--with-http_ssl_module \#安装http ssl模块。依赖于OpenSSL开源软件
--with-http_flv_module \#安装http flv模块。以使客户端可以观看、拖动FLV视频
--with-http_mp4_module \#安装http mp4模块。以使客户端可以观看、拖动MP4视频
--with-http_gzip_static_module \#安装http gzip static模块。如果采用gzip模块把一些文档进行gzip格式压缩后再返回给客户端,gzip static模块可以在做gzip压缩前,先查看相同位置是否有已经做过gzip压缩的.gz文件,如果有,就直接返回。这样就可以预先在服务器上做好文档的压缩,给CPU减负
--with-http_perl_module \#安装http perl模块。使nginx支持perl脚本
--with-ld-opt="-Wl,-E" \#编译最终的可执行文件时加入一些第三方库。
--with-mail \#安装邮件服务器反向代理模块,使Nginx可以反向代理IMAP、POP3、SMTP等协议
--with-mail_ssl_module \#安装mail ssl模块。该模块可以使IMAP、POP3、SMTP 等协议基于SSL/TLS协议之上使用。依赖于OpenSSL库
--with-http_stub_status_module \#安装http stub status模块。该模块可以让运行中的Nginx***能统计页面,获取相关的并发连接、请求的信息
--user=nginx \#指定Nginx worker进程运行时所属的用户。注意:不要将启动worker进程的用户设为root,在worker进程出问题时master进程要具备停止/启动worker进程的能力
--group=nginx#指定Nginx worker进程运行时所属的组
make && make install

##安装完后可再检查一下安装的一些信息,如下:

# /usr/local/nginx/sbin/nginx -V

nginx version: nginx/1.7.8

built by gcc 4.8.2 20140120 (Red Hat 4.8.2-16) (GCC)

TLS SNI support enabled

configure arguments: --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/tmp/nginx/tmp/client_body --http-proxy-temp-path=/var/tmp/nginx/tmp/proxy --http-fastcgi-temp-path=/var/tmp/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/tmp/nginx/tmp/uwsgi --http-scgi-temp-path=/var/tmp/nginx/tmp/scgi --with-http_ssl_module --with-http_flv_module --with-http_mp4_module --with-http_gzip_static_module --with-http_perl_module --with-ld-opt=-Wl,-E --with-mail --with-mail_ssl_module --with-http_stub_status_module --user=nginx --group=nginx

##测试一下nginx能否启动(主要是看配置文件是否正确):

# /usr/local/nginx/sbin/nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

##为了在使用nginx命令的时不用每次都输入绝对路径,可以通过修改PATH环境变量,:

# vim /etc/profile

添加:

PATH=${PATH}:/usr/local/nginx/sbin

##如果只想对本用户生效可修改

# vim ~/.bash_profile

修改:

PATH=$PATH:$HOME/bin:/usr/local/nginx/sbin