Nginx安装配置

安装

下载地址http://nginx.org/en/download.html
检查是否安装:
find -name nginx
查看安装位置:
whereis nginx

安装需要的依赖
yum install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel

解压安装

tar -zxvf nginx-1.13.4.tar.gz
cd /developer/nginx-1.13.4/
./configure
make
make install

配置

一、 环境变量

vim /etc/profile

export NGINX_HOME=/usr/local/nginx

export PATH=$NGINX_HOME/sbin:$PATH

二、 80端口监听设置
参考文档:Nginx和Apache共用80端口的配置方案

修改apache的80监听端口,指定具体IP,解决端口占用问题

vim /etc/httpd/conf/httpd.conf
Listen 80; -> Listen 127.0.0.1:80

ifconfig //查看ip

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.18.15.158  netmask 255.255.240.0  broadcast 172.18.15.255
        ether 00:16:3e:0a:4d:d8  txqueuelen 1000  (Ethernet)
        RX packets 445387237  bytes 462358036230 (430.6 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 352428  bytes 146581212 (139.7 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1  (Local Loopback)
        RX packets 45603  bytes 11175814 (10.6 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 45603  bytes 11175814 (10.6 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vim /usr/local/nginx/conf/nginx.conf

  1. listen 80;改为listen 172.18.15.158:80;,127.0.0.1的被apache使用了
  2. 在http {}的末尾加上include vhost/*.conf;,这样我们可以在vhost目录下建以.conf结尾的文件作为用户自定义代理配置。

三、 开机自动启动

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/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 $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

内容参考官网的:https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/

注意
1. nginx=”/usr/local/nginx/sbin/nginx” 修改成nginx执行程序所在的路径。
2. NGINX_CONF_FILE=”/usr/local/nginx/conf/nginx.conf” 修改成配置文件的路径

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

使用chkconfig进行管理
chkconfig –add /etc/init.d/nginx
chkconfig nginx on //设置终端模式开机启动

systemctl nginx start
systemctl nginx stop


常见问题

  • vim /usr/local/nginx/logs/error.log
    可以查看具体的错误日志信息

  • nginx: [error] open() “/usr/local/nginx/logs/nginx.pid” failed (2: No such file or directory)
    使用nginx -c的参数指定nginx.conf文件的位置

    nginx -c /usr/local/nginx/conf/nginx.conf
    nginx -s reload

  • 403 Forbidden
    参考:http://blog.csdn.net/lanjianhun/article/details/26836343

  • nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
    端口已经被占用了,查看被谁占用了

    netstat -ltudpn

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:29418           0.0.0.0:*               LISTEN      2668/GerritCodeRevi 
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      1972/mysqld         
tcp        0      0 0.0.0.0:81              0.0.0.0:*               LISTEN      2347/nginx: master  
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      2464/sshd           
tcp        0      0 0.0.0.0:8088            0.0.0.0:*               LISTEN      2668/GerritCodeRevi 
tcp        0      0 0.0.0.0:8089            0.0.0.0:*               LISTEN      2347/nginx: master  
tcp6       0      0 :::80                   :::*                    LISTEN      801/httpd           
udp        0      0 172.18.15.158:123       0.0.0.0:*                           845/ntpd            
udp        0      0 127.0.0.1:123           0.0.0.0:*                           845/ntpd            
udp        0      0 0.0.0.0:123             0.0.0.0:*                           845/ntpd            
udp6       0      0 :::123                  :::*                                845/ntpd 

可以修改占用程序的端口,也可以将代理端口换一个没有被使用的端口

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值