nginx没有sbin目录_nginx入门详解(二)- 常用命令实战

b79429936e16524c1cb17b849afd6ed2.png
在上一篇中,我们主要讲到了nginx的安装,分别介绍了yum安装与源码编译安装。
这篇文章主要探讨nginx的相关命令。

使用/usr/local/nginx/sbin/nginx -h命令查看可用参数:

[root@localhost ~]# /usr/local/nginx/sbin/nginx  -h
nginx version: nginx/1.16.0
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -T            : test configuration, dump it and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /usr/local/nginx/)
  -c filename   : set configuration file (default: conf/nginx.conf)
  -g directives : set global directives out of configuration file

参数解读:

-v      可查看nginx的版本。
-V      可查看nginx的详细信息,包括编译的参数。
-t      可用来测试nginx的配置文件的语法错误。
-T      可用来测试nginx的配置文件语法错误,同时还可以通过重定向备份nginx的配置文件。
-q      如果配置文件没有错误信息时,不会有任何提示,如果有错误,则提示错误信息,与-t配合使用。
-s      发送信号给master处理:
        stop    立刻停止nginx服务,不管请求是否处理完
        quit    优雅的退出服务,处理完当前的请求退出
        reopen  重新打开日志文件,原日志文件要提前备份改名。
        reload  重载配置文件
-p      设置nginx家目录路径,默认是编译时的安装路径
-c      设置nginx的配置文件,默认是家目录下的配置文件
-g      设置nginx的全局变量,这个变量会覆盖配置文件中的变量。

命令演示:

如果觉得每次都需要输入绝对路径执行命令麻烦,可以通过以下几种方法实现直接使用nginx命令。

1、做软连接:
ln  -s /usr/local/nginx/sbin/* /usr/sbin/

2、配置环境变量:
vim /etc/profile.d/nginx.sh

nginx_home=/usr/local/nginx
PATH=$nginx_home/sbin:$PATH
export PATH

然后重新读取下配置文件
source /etc/profile

3、设置别名:

alias  nginx='/usr/local/nginx/sbin/nginx'

重新打开日志文件

该命令可用于日志切割,定期执行。

[root@localhost logs]# ls
access.log  error.log  nginx.pid
[root@localhost logs]# mv access.log{,.bak}
[root@localhost logs]# ls
access.log.bak  error.log  nginx.pid
[root@localhost logs]# /usr/local/nginx/sbin/nginx -s reopen
[root@localhost logs]# ls
access.log  access.log.bak  error.log  nginx.pid

重新读取配置文件:

修改工作进程数,重启服务,对比前后进程数,工作进程的具体设置,会在后面章节中详细讨论。

[root@localhost logs]# ps -ef|grep nginx
root      2685     1  0 23:56 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     2686  2685  0 23:56 ?        00:00:00 nginx: worker process
root      2691  2532  0 23:57 pts/1    00:00:00 grep --color=auto nginx
[root@localhost logs]# vim /usr/local/nginx/conf/nginx.conf
[root@localhost logs]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost logs]# ps -ef|grep nginx
root      2685     1  0 23:56 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     2694  2685  0 23:58 ?        00:00:00 nginx: worker process
nginx     2695  2685  0 23:58 ?        00:00:00 nginx: worker process
nginx     2696  2685  0 23:58 ?        00:00:00 nginx: worker process
root      2698  2532  0 23:58 pts/1    00:00:00 grep --color=auto nginx

指定启动的配置文件:

在/data/目录下拷贝一份nginx的配置文件,然后修改用户名为www。

注意:配置文件中引用的其他配置文件路径也要做一个修改。

[root@localhost logs]# cp /usr/local/nginx/conf/nginx.conf /data/
[root@localhost logs]# vim /data/nginx.conf
[root@localhost logs]# /usr/local/nginx/sbin/nginx  -c /data/nginx.conf 
[root@localhost logs]# ps -ef|grep nginx
root      2736     1  0 00:05 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /data/nginx.conf
www       2737  2736  0 00:05 ?        00:00:00 nginx: worker process
www       2738  2736  0 00:05 ?        00:00:00 nginx: worker process
www       2739  2736  0 00:05 ?        00:00:00 nginx: worker process
root      2741  2532  0 00:05 pts/1    00:00:00 grep --color=auto nginx

设置全局变量

通过设置全局变量,让nginx在前端运行。可能现在你会比较好奇,放在前端干嘛。不着急,后面学了docker容器,你就豁然开朗了。

[root@localhost logs]# /usr/local/nginx/sbin/nginx  -g "daemon off;"

现在当前nginx在前端运行,
输入ctrl +c,则nginx就退出了。
可以使用ctrl +z放置后台运行。

再开启一个终端,查看端口:

666fc67a5fc38a8fcfa5fbc1329ef3c1.png

命令就先讲到这,这将是系列文章,可以加关注,获取最新文章推送哦。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值