nginx环境搭建

1、下载nginx

2、解压并安装

tar zxvf nginx-1.7.8.tar.gz  --解压
mv nginx-1.7.8  nginx  --更改文件名
useradd -s /sbin/nologin -M nginx  --创建nginx用户 
yum install -y gcc openssl-devel pcre-devel zlib-devel libxml2-devel bzip2-devel wget   --安装依赖库
cd nginx
./configure  --prefix=/usr/local/nginx  --error-log-path=/var/log/nginx/error.log   --http-log-path=/var/log/nginx/access.log   --user=nginx   --group=nginx   --with-http_ssl_module   --with-http_stub_status_module   --with-http_gzip_static_module   --with-pcre   --with-stream  
make && make install

3、验证Nginx配置文件是否正确

  

4、启动和停止

  cd /usr/local/nginx/sbin

  启动:./nginx

  重启:nginx -s reload

  停止:nginx -s stop

  强制停止:pkill -9 nginx

5、可能出现的问题

  nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
  [root@xiaoxitest sbin]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
  [root@xiaoxitest sbin]# ./nginx -s reload  #重启OK

6、访问验证

  

 7、增加service启动

vim /etc/init.d/nginx

#!/bin/bash

# nginx - this script starts and stops the nginx daemon 

# 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" 

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx 

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 
killall -9 nginx 
} 

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

  

service nginx start  启动

service nginx stop  关闭

service nginx restart  重启

原文地址:https://www.cnblogs.com/xiaoxitest/p/6402082.html