Linux下通过yum安装Nginx以及Nginx启动、关闭

一、在centos等Linux发行版下使用yum安装nginx

[root@www yum.repos.d]# touch /etc/yum.repos.d/nginx.repo
[root@www yum.repos.d]# vim /etc/yum.repos.d/nginx.repo
在该repo文件中加入nginx的官方yum源
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/ r e l e a s e v e r / releasever/ releasever/basearch/
gpgcheck=0
enabled=1
然后就可以使用 yum install nginx 来安装nginx了,使用yum安装nginx比较简单,并且相关的 SysV 脚本都已经写好,直接用即可。配置文件日志文件等位置如下:

#配置文件在 /etc/nginx目录下
[root@www ~]# cd /etc/nginx/
[root@www nginx]# ls
conf.d koi-utf mime.types scgi_params win-utf
fastcgi_params koi-win nginx.conf uwsgi_params
#日志文件在 /var/log/nginx 目录下
[root@www nginx]# cd /var/log/nginx/
[root@www nginx]# ls
access.log error.log
#pid 位于 /var/run 目录下
[root@www nginx]# cat /var/run/nginx.pid
29361
#nginx默认首页,错误页html文件
[root@www nginx]# cd /usr/share/nginx/
[root@www nginx]# ls
html

二、如何启动nginx
Linux下直接简单运行 nginx 命令即可启动 nginx,如果是通过 yum 安装的 nginx,通常 nginx 命令会位于 /usr/sbin目录下。

[root@lifw nginx]# which nginx
/usr/sbin/nginx
[root@lifw nginx]# nginx

三、使用 SysV 脚本控制 nginx
使用官方 yum 源安装的nginx已经提供了 SysV 脚本,使用非常简单

[root@www init.d]# service nginx stop
Stopping nginx: [ OK ]
[root@www init.d]# service nginx start
Starting nginx: [ OK ]
[root@www init.d]# service nginx restart
Stopping nginx: [ OK ]
Starting nginx: [ OK ]
[root@www init.d]# service nginx reload
Reloading nginx: [ OK ]
[root@www init.d]# service nginx status
nginx (pid 29447) is running…

四、Nginx支持以下命令行参数

-h | -?

打印命令行参数的帮助信息。

[root@lifw nginx]# nginx -h
nginx version: nginx/1.8.0
Usage: nginx [-?hvVtq] [-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
-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: /etc/nginx/)
-c filename : set configuration file (default: /etc/nginx/nginx.conf)
-g directives : set global directives out of configuration file

-c file

指定配置文件的位置,如果不指定则使用默认的配置文件 /etc/nginx/nginx.conf

#-c 参数用于启动nginx时指定配置文件,如果nginx已经在运行,则启动失败
[root@www nginx]# nginx -c /etc/nginx/nginx.conf
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
[root@www nginx]# nginx -s quit
[root@www nginx]# nginx -c /etc/nginx/nginx.conf

-v

打印nginx的版本号。

[root@www nginx]# nginx -v
nginx version: nginx/1.8.0

-V

打印nginx的版本号、编译器版本、配置参数等详细信息。

[root@www nginx]# nginx -V
nginx version: nginx/1.8.0
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-3) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-http_spdy_module --with-cc-opt=’-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic’

-t

检查nginx的配置文件是否有语法错误,通常在修改配置文件后先检查配置文件,然后在重新加载配置文件。

[root@www nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

-T

检查nginx的配置文件是否有语法错误,与 -t 参数相同,但是 -T 同时将配置文件内容通过标准输出(standard output)打印出来。注意仅在 1.9.2 版本以后才支持。

-s signal

给 nginx 主进程发送信号(signal),信号可以是以下几种:

stop 快速关闭 nginx。

quit 平滑关闭 nginx。

reload 重新加载配置文件,使用新配置文件开启一个新的工作进程(worker progress),然后平滑的关闭原来的工作进程。

reopen 重新打开日志文件,如果你发现你的nginx日志文件不再记录日志了,可以使用该指令重新打开日志文件。

#stop指令会快速关闭nginx
[root@www nginx]# nginx -s stop
[root@www nginx]# service nginx status
nginx dead but subsys locked
[root@www nginx]# nginx
[root@www nginx]# service nginx status
nginx (pid 29228) is running…
#quit会平滑关闭nginx,可以看到一段时间内nginx没有被关闭
[root@www nginx]# nginx -s quit
[root@www nginx]# service nginx status
nginx (pid 29228) is running…
[root@www nginx]# service nginx status
nginx (pid 29228) is running…
[root@lifw nginx]# service nginx status
nginx dead but subsys locked
[root@www nginx]# nginx
#重新加载配置文件
[root@www nginx]# nginx -s reload
#重新打开日志文件
[root@www nginx]# nginx -s reopen

-q

在 nginx 进行配置文件测试时只打印错误信息。和 -t 参数配合使用。
[root@www nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@www nginx]# nginx -qt
[root@www nginx]#

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值