1、安装依赖包

yum -y install gcc gcc-c++ openssl openssl-devel zlib zlib-devel pcre pcre-devel

gcc,gcc-c++:主要用来进行编译相关使用
openssl,openssl-devel: 配置https
zlib,zlib-devel:文件的解压缩
pcre,pcre-devel:rewrite模块 

2、安装Nginx

# 切换下载目录
cd /usr/local/src

# 下载 nginx 安装包
wget http://nginx.org/download/nginx-1.13.6.tar.gz

# 下载 echo 模块 (可选)
wget https://github.com/openresty/echo-nginx-module/archive/v0.62.tar.gz

# 解压
tar zxvf nginx-1.13.6.tar.gz
tar zxvf v0.62.tar.gz

# 切换目录
cd nginx-1.13.6

# 编译
./configure --prefix=/usr/local/nginx \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_sub_module \
--with-http_gzip_static_module \
--add-module=/usr/local/src/echo-nginx-module-0.62

# 安装
make && make install

3、启动Nginx

# 关闭防火墙 或者开启80端口
service iptables stop

# 切换目录
cd /usr/local/nginx/sbin

# 启动
./nginx

# 关闭
./nginx -s stop

# 重启
./nginx -s reopen

# 显示版本
./nginx -v

# 重新载入配置文件
./nginx -s reload

# 检查配置文件是否正确
./nginx -t

# 查看编译选项
./nginx -V

常用编译选项说明 

--prefix=path
指定nginx的安装目录。

--conf-path=path
设置nginx.conf配置文件的路径。nginx允许使用不同的配置文件启动,通过命令行中的-c选项。默认为prefix/conf/nginx.conf

--with-pcre
设置PCRE库的源码路径,如果已通过yum方式安装,使用--with-pcre自动找到库文件。使用--with-pcre=PATH时,需要从PCRE网站下载pcre库的源码(版本4.4 – 8.30)并解压,剩下的就交给Nginx的​​​./configure​​​和​​make​​​来完成。perl正则表达式使用在​​location​​​指令和 ​​ngx_http_rewrite_module​​​模块中。

--with-http_ssl_module
使用https协议模块。默认情况下,该模块没有被构建。前提是openssl与openssl-devel已安装

--with-http_stub_status_module
用来监控 Nginx 的当前状态

--with-http_realip_module
通过这个模块允许我们改变客户端请求头中客户端IP地址值(例如X-Real-IP 或 X-Forwarded-For),意义在于能够使得后台服务器记录原始客户端的IP地址

--add-module=path
添加第三方外部模块,这里添加了 echo-nginx-module 模块

4、添加开机启动

这是nginx官方提供的脚本,脚本地址 ​​http://wiki.nginx.org/RedHatNginxInitScript​​ 如是自定义编译安装的nginx,需要修改如下两项配置(代码我已修改好了):

1) 修改执行程序的路径
nginx="/usr/local/nginx/sbin/nginx"

2) 修改配置文件的路径
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

1、创建脚本 

# 切换目录 
vim /etc/init.d/nginx
#!/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"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
if [ -z "`grep $user /etc/passwd`" ]; then
useradd -M -s /bin/nologin $user
fi
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

2、加入启动 

# 设置权限
chmod 755 /etc/init.d/nginx

# 通过脚本对nginx进行管理
/etc/init.d/nginx start # 启动
/etc/init.d/nginx stop # 停止
/etc/init.d/nginx restart # 重启

# 将nginx服务加入chkconfig管理列表
chkconfig --add /etc/init.d/nginx

# 加完以后,可以用service对nginx进行启动相关操作了
service nginx start
service nginx stop
service nginx restart

3、测试 echo

# 条件判断
location /hello {
default_type text/html;
if ($remote_addr = '192.168.1.96') {
echo 'success';
}

if ($remote_addr != '192.168.1.96') {
echo 'failure';
}
}

# 设置自定义变量
location /hello {
default_type text/html;
set $name alice;
echo $name;
}