共计 6913 个字符,预计需要花费 18 分钟才能阅读完成。
Nginx(发音为 Engine x)是一个免费的,开源的,高性能,可扩展,可靠,功能齐全且流行的 HTTP 和反向代理服务器,邮件代理服务器和通用 TCP/UDP 代理服务器。
Nginx 以其简单的配置和低资源消耗而闻名,因为它具有高性能,它被用于为 Web 上的几个高流量站点供电,例如 GitHub,SoundCloud,Dropbox,Netflix,WordPress 等等。
在本指南中,我们将介绍一些最常用的 Nginx 服务管理命令,作为开发人员或系统管理员,您应该随意使用。我们将显示 Systemd 和 SysVinit 的命令。
以下所有 Nginx 流行命令列表必须以 root 或 sudo 用户身份执行,并且应该适用于任何现代 Linux 发行版,如 CentOS,RHEL,Debian,Ubuntu 和 Fedora。
安装 Nginx 服务器
要安装 Nginx Web 服务器,请使用默认的分发包管理器,如下所示。
$ sudo yum install epel-release && yum install nginx [On CentOS/RHEL]
$ sudo dnf install nginx [On Debian/Ubuntu]
$ sudo apt install nginx [On Fedora]
检查 Nginx 版本
要检查 Linux 系统上安装的 Nginx Web 服务器的版本,请运行以下命令。
$ nginx -v
nginx version: nginx/1.12.2
上面的命令只显示版本号。如果要查看版本并配置选项,请使用 - V 标志,如图所示。
$ nginx -V
显示 Nginx,编译器和配置参数
nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: –prefix=/usr/share/nginx –sbin-path=/usr/sbin/nginx –modules-path=/usr/lib64/nginx/modules –conf-path=/etc/nginx/nginx.conf –error-log-path=/var/log/nginx/error.log –http-log-path=/var/log/nginx/access.log –http-client-body-temp-path=/var/lib/nginx/tmp/client_body –http-proxy-temp-path=/var/lib/nginx/tmp/proxy –http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi –http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi –http-scgi-temp-path=/var/lib/nginx/tmp/scgi –pid-path=/run/nginx.pid –lock-path=/run/lock/subsys/nginx –user=nginx –group=nginx –with-file-aio –with-ipv6 –with-http_auth_request_module –with-http_ssl_module –with-http_v2_module –with-http_realip_module –with-http_addition_module –with-http_xslt_module=dynamic –with-http_image_filter_module=dynamic –with-http_geoip_module=dynamic –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_degradation_module –with-http_slice_module –with-http_stub_status_module –with-http_perl_module=dynamic –with-mail=dynamic –with-mail_ssl_module –with-pcre –with-pcre-jit –with-stream=dynamic –with-stream_ssl_module –with-google_perftools_module –with-debug –with-cc-opt=’-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong –param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/RedHat/redhat-hardened-cc1 -m64 -mtune=generic’ –with-ld-opt=’-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E’
检查 Nginx 配置语法
在实际启动 Nginx 服务之前,您可以检查其配置语法是否正确。如果您已对现有配置结构进行了更改或添加了新配置,则此功能尤其有用。
要测试 Nginx 配置,请运行以下命令。
$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
您可以测试 Nginx 配置,转储它并使用 - T 标志退出,如图所示。
$ sudo nginx -T
显示 Nginx 配置设置
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# configuration file /etc/nginx/nginx.conf:
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main ‘$remote_addr – $remote_user [$time_local] “$request” ‘
‘$status $body_bytes_sent “$http_referer” ‘
‘”$http_user_agent” “$http_x_forwarded_for”‘;
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
….
启动 Nginx 服务
要启动 Nginx 服务,请运行以下命令。请注意,如果配置语法不正确,此过程可能会失败。
$ sudo systemctl start nginx #systemd
或者
$ sudo service nginx start #sysvinit
启用 Nginx 服务
上一个命令仅在此期间启动服务,要在启动时启用它自动启动,请运行以下命令。
$ sudo systemctl enable nginx #systemd
或者
$ sudo service nginx enable #sysv init
重启 Nginx 服务
要重新启动 Nginx 服务,将停止然后启动该服务的操作。
$ sudo systemctl restart nginx #systemd
或者
$ sudo service nginx restart #sysv init
查看 Nginx 服务状态
您可以按如下方式检查 Nginx 服务的状态。此命令显示有关服务的运行时状态信息。
$ sudo systemctl status nginx #systemd
或者
$ sudo service nginx status #sysvinit
显示 Nginx 状态信息
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@linuxidc ~]# systemctl status nginx
● nginx.service – The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2019-03-05 05:27:15 EST; 2min 59s ago
Main PID: 31515 (nginx)
CGroup: /system.slice/nginx.service
├─31515 nginx: master process /usr/sbin/nginx
└─31516 nginx: worker process
Mar 05 05:27:15 linuxidc.com systemd[1]: Starting The nginx HTTP and reverse proxy server…
Mar 05 05:27:15 linuxidc.com nginx[31509]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Mar 05 05:27:15 linuxidc.com nginx[31509]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Mar 05 05:27:15 linuxidc.com systemd[1]: Failed to read PID from file /run/nginx.pid: Invalid argument
Mar 05 05:27:15 linuxidc.com systemd[1]: Started The nginx HTTP and reverse proxy server.
重新加载 Nginx 服务
要告诉 Nginx 重新加载其配置,请使用以下命令。
$ sudo systemctl reload nginx #systemd
或者
$ sudo service nginx reload #sysvinit
停止 Nginx 服务
如果您想出于一次性原因而停止 Nginx 服务,请使用以下命令。
显示 Nginx 命令帮助
要获得所有 Nginx 命令和选项的简单参考指南,请使用以下命令。
$ systemctl -h nginx
Nginx 帮助命令和选项
systemctl [OPTIONS…] {COMMAND} …
Query or send control commands to the systemd manager.
-h –help Show this help
–version Show package version
–system Connect to system manager
-H –host=[USER@]HOST
Operate on remote host
-M –machine=CONTAINER
Operate on local container
-t –type=TYPE List units of a particular type
–state=STATE List units with particular LOAD or SUB or ACTIVE state
-p –property=NAME Show only properties by this name
-a –all Show all loaded units/properties, including dead/empty
ones. To list all units installed on the system, use
the ‘list-unit-files’ command instead.
-l –full Don’t ellipsize unit names on output
-r –recursive Show unit list of host and local containers
–reverse Show reverse dependencies with ‘list-dependencies’
–job-mode=MODE Specify how to deal with already queued jobs, when
queueing a new job
–show-types When showing sockets, explicitly show their type
-i –ignore-inhibitors
…
就这样了!在本指南中,我们已经解释了一些您应该知道的最常用的 Nginx 服务管理命令,包括启动,启用,重新启动和停止 Nginx。如果您有任何要求或要求提出的问题,请使用下面的反馈表。
如何在 CentOS Linux 7.5 上安装 Nginx https://www.linuxidc.com/Linux/2018-05/152399.htm
在 Ubuntu 18.04 上安装带有 Nginx,MariaDB 10 和 PHP 7 的 WordPress https://www.linuxidc.com/Linux/2019-03/157315.htm
: