欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Nginx安装和使用

程序员文章站 2022-06-11 16:21:57
...

安装前准备(centos7)

  1. 安装必须的环境
    yum install gcc gcc-c++ openssl openssl-devel pcre pcre-devel

  2. 去官网下载tengine官方网站 http://tengine.taobao.org/

  3. 安装
    下载
    wget http://tengine.taobao.org/download/tengine-2.0.3.tar.gz
    解压
    tar -zxvf tengine-2.0.3.tar.gz
    进入解压目录
    cd tengine-2.0.3
    编译,如果成功会有MakeFile
    ./configure
    //编译并且安装
    make && make install
    编写允许脚本
    vi /etc/init.d/nginx
    (注意:TENGINE_HOME="/opt/luoyu/nginx"
    nginx="/opt/luoyu/nginx/sbin/nginx"
    prog=$(basename $nginx)
    NGINX_CONF_FILE="/opt/luoyu/nginx/conf/nginx.conf"
    是你的nginx安装目录)

#!/bin/bash
#
# 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
  
TENGINE_HOME="/opt/luoyu/nginx"
nginx="/opt/luoyu/nginx/sbin/nginx"
prog=$(basename $nginx)
  
NGINX_CONF_FILE="/opt/luoyu/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
  1. 加入脚本
    chmod +x /etc/init.d/nginx
    chkconfig nginx on
    service nginx start

  2. 下面是没有脚本的nginx命令

LINUX启动Nginx的命令:
一、查询是否启动
[[email protected]]# ps -ef | grep nginx
root     25225     1  0 19:26 ?        00:00:00 nginx: master process /app/nginx/sbin/nginx
www      25229 25225  0 19:26 ?        00:00:00 nginx: worker process
root     25247 19431  0 19:30 pts/0    00:00:00 grep nginx

二、启动
[[email protected]]# /app/nginx/sbin/nginx
[[email protected]]# ps -ef | grep nginx  
root     25192     1  0 19:22 ?        00:00:00 nginx: master process /app/nginx/sbin/nginx
www      25193 25192  0 19:22 ?        00:00:00 nginx: worker process
root     25195 19431  0 19:22 pts/0    00:00:00 grep nginx

三、停止
从容停止Nginx:
kill -QUIT 主进程号
[[email protected]]# kill -QUIT 19513
[[email protected]]# ps -ef | grep nginx
root     25190 19431  0 19:22 pts/0    00:00:00 grep nginx

快速停止Nginx:
kill -TERM 主进程号
[[email protected]]# kill -TERM 25192
[[email protected]]# ps -ef | grep nginx
root     25203 19431  0 19:23 pts/0    00:00:00 grep nginx
[[email protected]]# 

强制停止Nginx:
kill -9 主进程号  
[[email protected]]# kill -9 25205
[[email protected]]# ps -ef | grep nginx
www      25206     1  0 19:24 ?        00:00:00 nginx: worker process
root     25210 19431  0 19:24 pts/0    00:00:00 grep nginx

四、重启
[[email protected]]# /app/nginx/sbin/nginx -s reload
[[email protected]]# 
  1. 反向代理
server
        {
        listen      80;
        server_name  www.domain.net;
     
        location / {
                proxy_pass        http://www.domain.com;
                proxy_set_header  Host            $host;
                proxy_set_header  X-Real-IP        $remote_addr;
                proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
        }
 
      access_log  off;
  }