Centos7安装nginx说明


本文所有的操作基于root用户进行

1 安装nginx

1.1 软件准备

  1. openssl-1.1.1d.tar.gz 百度网盘下载
  2. pcre-8.43.tar.gz 百度网盘下载
  3. zlib-1.2.11.tar.gz 百度网盘下载
  4. nginx-1.17.4.tar.gz 百度网盘下载

将上述软件拷贝到centos的/tmp目录下

1.2 解压软件

使用以下指令进行解压

cd /tmp
tar -xvf openssl-1.1.1d.tar.gz
tar -xvf pcre-8.43.tar.gz
tar -xvf zlib-1.2.11.tar.gz
tar -xvf nginx-1.17.4.tar.gz

1.3 安装命令

cd /tmp/nginx-1.17.4
./configure --prefix=/opt/nginx --with-stream --with-http_ssl_module --with-stream_ssl_module --with-stream_realip_module --with-stream_ssl_preread_module --with-pcre=/tmp/pcre-8.43 --with-zlib=/tmp/zlib-1.2.11 --with-openssl=/tmp/openssl-1.1.1d 
make
make install

1.4 常见问题

  1. 在使用make指令的时候,可能会出现以下提示,原因是缺少gcc-c++
checking windows.h usability... no
checking windows.h presence... no
checking for windows.h... no
configure: error: Invalid C++ compiler or C++ compiler flags
make[1]: *** [/tmp/pcre-8.43/Makefile] Error 1
make[1]: Leaving directory `/tmp/nginx-1.17.4'
make: *** [build] Error 2

解决方式:
要求Centos能够连接外网,可以使用命令curl http://www.baidu.com来验证服务器是否可以访问外网
使用以下命令来安装gcc-c++:

yum -y install gcc-c++

2 nginx常用命令

2.1 启动

cd /opt/nginx/sbin
./nginx

如果出现以下提示,则说明nginx的监听端口被占用,此时需要去修改nginx的监听端口

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()

2.2 停止

cd /opt/nginx/sbin
./nginx -s stop

2.3 重启

cd /opt/nginx/sbin
./nginx -s reload

2.4 配置验证

cd /opt/nginx/sbin
./nginx -t

使用上述命令后,如果出现以下提示,则配置正确

nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx/conf/nginx.conf test is successful

3 配置说明

nginx的配置文件所在路径:
/opt/nginx/conf/nginx.conf
nginx.conf文件进行修改时,可以使用vi或者vim编辑命令来操作,编辑完成后需要先按Esc键,然后再按:wq进行保存并退出

3.1 nginx端口

nginx.conf的部分内容如下,如果


##中间内容省略,这里没有显示##

http {

    ##中间内容省略,这里没有显示##
    
    server {
        # 如果端口被占用,则将listen监听的端口进行修改
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
    }

    ##中间内容省略,这里没有显示##
    
}

3.2 http代理转发

例如,通过浏览器访问http://localhost/getWeixinIp,nginx将代理访问https://api.weixin.qq.com/cgi-bin/getcallbackip并将内容获取返回,配置如下:

##中间内容省略,这里没有显示##
http{
    ##中间内容省略,这里没有显示##
    server {
        #如果端口被占用,则将listen监听的端口进行修改
        listen       80;
        server_name  localhost;
		
		location / {
            root   html;
            index  index.html index.htm;
        }
		
		#关键配置
		location ^~ /getWeixinIp/ {
            proxy_set_header Host $host:$server_port;
            proxy_pass https://api.weixin.qq.com/cgi-bin/getcallbackip;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }
		
	}
    ##中间内容省略,这里没有显示##
}

3.3 tcp代理转发

#stream是与http是同一级的
stream {
	upstream myproxy{
		#127.0.0.1为目标IP,3306为目标端口(这里是本机的mysql端口)
        server 127.0.0.1:3306 max_fails=3 fail_timeout=10s;
	}
	server {
		listen 8071;
        proxy_pass myproxy;
	}
}

以上配置解释说明:
当nginx启动的时候,这个8071端口也会被开启监听。此时当客户端访问nginx的8071端口时,就会被转到myproxy中的127.0.0.1:3306这个地址。
我们使用telnet(用于tcp连接使用,windows可能需要手动去启用下这个程序)来访问这个端口和查看这个端口是否开启。
即当我们使用telnet 127.0.0.1:8071的时候,实际上会被代理转发到127.0.0.1:3306如下:

[root@oem-3y8e7n2o9 conf]# telnet 127.0.0.1 8071
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
N
5.6.12-logCRR(jb▒[:NZZ31;u]Q^mysql_native_password

4 将nginx配置成服务自启

当nginx配置完成后,我们可以将nginx配置成服务,让它跟随系统开启时自动启动,这样当系统重启的时候,无需再进行进入系统进行手动使用命令启动。

4.1 新增/etc/init.d/nginx

可详见https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/说明
使用以下命令
vim /etc/init.d/nginx
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:     /opt/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="/opt/nginx/sbin/nginx"
prog=$(basename $nginx)

# 根据具体路径进行修改
NGINX_CONF_FILE="/opt/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:.*--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

以上文件内容中需要修改的有:
# pidfile: /opt/nginx/logs/nginx.pid需要修改成nginx所在目录下logs/nginx.pid
nginx="/opt/nginx/sbin/nginx"需要修改成nginx实际的可执行文件路径
NGINX_CONF_FILE="/opt/nginx/conf/nginx.conf"需要修改成nginx配置文件的实际路径

4.2 设置执行权限

chmod a+x /etc/init.d/nginx

4.3 注册成服务

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

4.4 添加开机启动

cd /etc/init.d
chkconfig nginx on

4.5 服务启动的命令

  1. 启动systemctl start nginx
  2. 停止systemctl stop nginx
  3. 重启systemctl restart nginx
  4. 查看状态systemctl status nginx
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章