centos7安装nginx

1.安装依赖

#gcc安装,nginx源码编译需要
yum install -y gcc-c++

#PCRE pcre-devel 安装,nginx 的 http 模块使用 pcre 来解析正则表达式
yum install -y pcre pcre-devel

#zlib安装,nginx 使用zlib对http包的内容进行gzip
yum install -y zlib zlib-devel

#OpenSSL 安装,强大的安全套接字层密码库,nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http)
yum install -y openssl openssl-devel

2.创建用户

groupadd www
useradd -g www -s /sbin/nologin www

3.编译安装

# 下载
wget http://nginx.org/download/nginx-1.21.0.tar.gz
# 解压
tar -zxvf nginx-1.21.0.tar.gz
cd nginx-1.21.0
# 默认配置
./configure --with-http_ssl_module --with-stream_ssl_module --with-debug --with-stream --with-http_stub_status_module --with-http_v2_module --with-http_gzip_static_module --with-http_sub_module

# 编译安装
make
make install

4.启动

cd /usr/local/nginx
# 创建日志目录
mkdir logs
# 启动
./sbin/nginx
# 停止,直接查找nginx进程id再使用kill命令强制杀掉进程
./sbin/nginx -s stop
# 退出停止,等待nginx进程处理完任务再进行停止
./sbin/nginx -s quit
# 重新加载配置文件,修改nginx.conf后使用该命令,新配置即可生效
./sbin/nginx -s reload

5.查看nginx进程

ps aux|grep nginx

6.配置方向代理

vim nginx.conf

配置内容:

http {
  upstream fingerprint{
      server 192.168.20.154:9090 weight=1 max_fails=3 fail_timeout=20s;
      server 192.168.20.155:9090 weight=1 max_fails=3 fail_timeout=20s;
  }
  
  location / {
      proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    # 将请求源IP地址设置到请求头中(如果不设置,服务端获取到的IP地址nginx的IP地址)
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    
        proxy_pass http://fingerprint;
  }
}

nginx开机启动

1.在/etc/init.d目录下创建nginx文件

vim /etc/init.d/nginx

文件内容(来源于Red Hat NGINX Init Script | 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/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/etc/nginx/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:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   if [ -n "$user" ]; then
      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
    fi
}

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 $prog -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 a+x /etc/init.d/nginx

3.加入chkconfig管理列表

chkconfig --add /etc/init.d/nginx

4.nginx服务命令

# 启动
service nginx start
# 停止
service nginx stop
# 重启
service nginx restart

5.设置开机自启

chkconfig nginx on

nginx添加新模块

# nginx源码重新编译执行需要添加的模块或者使用--add-module=参数
./configure --with-http_stub_status_module
# 编译,执行make命令后千万不要执行make install
make
# 备份nginx文件
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
# 替换nginx文件
cp objs/nginx /usr/local/nginx/sbin/

结合Keepalived实现nginx高可用

参考文章Keepalived服务高可用)

如果觉得我的文章对你有用,请随意赞赏