nginx

CentOS7离线安装Nginx及配置

下载离线包:

用浏览器打开地址,选择您要下载的版本:http://nginx.org/packages/centos/7/x86_64/RPMS/

 

 

如图,我下载的1.16.1版。

执行安装:

将下载的rpm包上传到服务器,然后赋予可执行权限,执行安装。

sudo yum install -y nginx-1.16.1-1.el7.ngx.x86_64.rpm

启动Nginx并设置开机启动

sudo service nginx start
#或者
sudo systemctl start nginx.service
sudo systemctl enable nginx.service

查看Nginx版本

nginx -v

查看Nginx启动状态

sudo service nginx status
sudo systemctl status nginx.service

如图为正在运行状态:

 

 

停止Nginx服务

 

查看Nginx位置:

whereis nginx
sudo whereis nginx

默认安装则配置文件一般是这个:/etc/nginx/nginx.conf

root用户启动nginx默认监听80端口:

此时,如果您用浏览器访问却看不见这个界面,仍然打不开有可能是服务器防火墙拦截了80端口。去设置允许80端口,或者直接关闭防火墙即可。

sudo systemctl stop firewalld.service
sudo systemctl disable firewalld.service

然后再访问:

 

就正常了

 

 

卸载Nginx

yum remove nginx

检查配置文件是否正确

nginx -t -c /usr/nginx/conf/nginx.conf 
# 或者
/usr/nginx/sbin/nginx -t

 

重启Nginx

 

nginx -s reload

/usr/nginx/sbin/nginx -s reload

 

安装后一般会自动创建nginx用户:

 

nginx部署vue工程配置

server{
listen 8080;
root /var/www/dashboard;
location / {
try_files $uri $uri/ @router;#需要指向下面的@router否则会出现vue的路由在nginx中刷新出现404
index index.html index.htm;
}
#对应上面的@router,主要原因是路由的路径资源并不是一个真实的路径,所以无法找到具体的文件
#因此需要rewrite到index.html中,然后交给路由在处理请求资源
location @router {
rewrite ^.*$ /index.html last;
}
}

nginx配置快捷启动service

1,/etc/init.d/目录下缺少nginx默认启动脚本
vim /etc/init.d/nginx
新建脚本文件,然后把下面的代码复制进去

#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and
# run 'update-rc.d -f nginx defaults', or use the appropriate command on your
# distro. For CentOS/Redhat run: 'chkconfig --add nginx'

### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
### END INIT INFO

# Author:   licess
# website:  http://lnmp.org

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=nginx
NGINX_BIN=/usr/local/nginx/sbin/$NAME
CONFIGFILE=/usr/local/nginx/conf/$NAME.conf
PIDFILE=/usr/local/nginx/logs/$NAME.pid

case "$1" in
start)
    echo -n "Starting $NAME... "

    if netstat -tnpl | grep -q nginx;then
        echo "$NAME (pid `pidof $NAME`) already running."
        exit 1
    fi

    $NGINX_BIN -c $CONFIGFILE

    if [ "$?" != 0 ] ; then
        echo " failed"
        exit 1
    else
        echo " done"
    fi
    ;;

 stop)
    echo -n "Stoping $NAME... "

    if ! netstat -tnpl | grep -q nginx; then
        echo "$NAME is not running."
        exit 1
    fi

    $NGINX_BIN -s stop

    if [ "$?" != 0 ] ; then
        echo " failed. Use force-quit"
        exit 1
    else
        echo " done"
    fi
    ;;

 status)
    if netstat -tnpl | grep -q nginx; then
        PID=`pidof nginx`
        echo "$NAME (pid $PID) is running..."
    else
        echo "$NAME is stopped"
        exit 0
    fi
    ;;

 force-quit)
    echo -n "Terminating $NAME... "

    if ! netstat -tnpl | grep -q nginx; then
        echo "$NAME is not running."
        exit 1
    fi

    kill `pidof $NAME`

    if [ "$?" != 0 ] ; then
        echo " failed"
        exit 1
    else
        echo " done"
    fi
    ;;

 restart)
    $0 stop
    sleep 1
    $0 start
    ;;

 reload)
    echo -n "Reload service $NAME... "

    if netstat -tnpl | grep -q nginx; then
        $NGINX_BIN -s reload
        echo " done"
    else
        echo "$NAME is not running, can't reload."
        exit 1
    fi
    ;;

 configtest)
    echo -n "Test $NAME configure files... "

    $NGINX_BIN -t
    ;;

 *)
    echo "Usage: $0 {start|stop|force-quit|restart|reload|status|configtest}"
    exit 1
    ;;

esac

2,切换目录,修改权限

cd /etc/init.d

chmod +x nginx

/sbin/chkconfig --level 345 nginx on

service nginx start