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

本文详细介绍了在CentOS等Linux发行版中使用YUM安装Nginx的方法,包括配置文件、日志文件的位置,以及如何通过SysV脚本控制Nginx的启动、停止、重启和状态检查。此外,还介绍了Nginx命令行参数的使用,如-t检查配置文件语法,-s发送信号给主进程等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、在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]#

### 使用yum命令安装Nginx服务器 在Linux CentOS环境中,可以通过`yum`命令来安装Nginx服务器。以下是详细的安装过程以及相关操作: #### 配置Yum仓库 由于CentOS默认的Yum源可能不包含Nginx软件包,因此需要先配置Nginx的官方Yum仓库[^3]。 运行以下命令以添加Nginx Yum仓库: ```bash sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm ``` > **注意**:如果使用的不是CentOS 7,请根据实际操作系统版本调整URL中的版本号。 #### 更新Yum缓存 为了确保能够获取最新的软件包列表,执行以下命令更新Yum缓存: ```bash sudo yum makecache fast ``` #### 安装Nginx 完成上述步骤后,可以使用以下命令安装Nginx: ```bash sudo yum -y install nginx ``` 此命令会自动下载并安装Nginx及其依赖项。 #### 启动Nginx服务 安装完成后,需启动Nginx服务才能使其正常工作。可使用以下命令启动Nginx: ```bash sudo systemctl start nginx ``` #### 设置开机自启 为了让Nginx在系统重启后自动启动,可以启用其开机自启功能: ```bash sudo systemctl enable nginx ``` #### 检查Nginx状态 确认Nginx服务已成功启动,可通过以下命令查看其状态: ```bash sudo systemctl status nginx ``` #### 测试Nginx是否正常运行 打开浏览器访问服务器IP地址或主机名,默认情况下应显示Nginx欢迎页面。也可以通过以下命令测试Web服务是否可用: ```bash curl http://localhost ``` #### 常见路径和命令 - Nginx主配置文件位于 `/etc/nginx/nginx.conf`[^5]。 - 日志文件存储于 `/var/log/nginx/access.log` 和 `/var/log/nginx/error.log`。 - 可通过以下命令重新加载配置文件而不中断现有连接: ```bash sudo nginx -s reload ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值