1.安装nginx依赖包

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

2.下载nginx安装包

wget http://nginx.org/download/nginx-1.18.0.tar.gz

3.解压tar包并进入解压后的文件夹

tar -zxvf nginx-1.18.0.tar.gz


cd nginx-1.18.0

4.配置参数

本文将配置安装文件夹、ssl模块、stream模块

1) 配置安装参数

./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_gzip_static_module --http-client-body-temp-path=/usr/local/nginx/tmp/client/ --http-proxy-temp-path=/usr/local/nginx/tmp/proxy/ --http-fastcgi-temp-path=/usr/local/nginx/tmp/fcgi/ --with-poll_module --with-file-aio --with-http_realip_module --with-http_addition_module --with-http_addition_module --with-http_random_index_module --with-http_stub_status_module --http-uwsgi-temp-path=/usr/local/nginx/uwsgi_temp --http-scgi-temp-path=/usr/local/nginx/scgi_temp --with-pcre=/usr/local/src/pcre-8.41 --with-stream

此处配置了/usr/local/nginx为安装目录,默认启动时的配置参数将使用/usr/local/nginx/conf/nginx.conf

2).编译

make

3).安装

make install

4).添加启动用户

创建 Nginx 运行使用的用户 www:

groupadd www 
useradd -g www www
5).修改配置文件
打开nginx.conf
vi /usr/local/nginx/congf/nginx.conf
修改user选项,改为如下
user www www;
5.配置开机启动

1)在/etc/init.d下新建nginx启动脚本

vim /etc/init.d/nginx

2)复制如下脚本到nginx脚本

#!/bin/sh
# nginx - this script starts and stops the nginx daemin
# chkconfig:   - 85 15
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse 
# processname: nginx
# config:      /usr/local/nginx/conf/nginx.conf
# pidfile:     /usr/local/nginx/logs/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=/var/lock/subsys/nginx

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

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

3)授权

chmod 755 /etc/init.d/nginx

4) 添加到开机启动

chkconfig --add nginx

5)快捷命令

启动: service nginx start
关闭: service nginx stop
重启: service nginx restart
更新配置: service nginx reload
查看状态: service nginx status