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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章