CentOS 7.2 安装 Nginx 全流程解析
这是一份关于在 CentOS 7.2 环境中部署 Nginx 的操作笔记。出于学习目的,近期重点研究了 Nginx 的相关技术,特此整理本次安装过程以备查阅。
确认 CentOS 系统版本信息
在执行安装前,建议先核实当前操作系统的版本详情。
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
配置软件资源库
要在 CentOS 平台上成功安装 Nginx,首要步骤是引入相应的软件源仓库。
vim /etc/yum.repos.d/nginx.repo
利用 vim 编辑器修改或新建 /etc/yum.repos.d/nginx.repo 文件。若文件不存在则自动创建,按下 i 键进入插入模式,粘贴下方提供的配置代码块,完成后按 esc 退出编辑,最后输入 :wq 保存并关闭文件。
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
执行 yum 安装指令
完成上述仓库配置后,即可调用 yum 包管理器来下载并安装 Nginx。
yum install nginx
安装流程结束后的成功提示如下:
Loaded plugins: fastestmirror, langpacks
nginx | 2.9 kB 00:00
nginx/7/x86_64/primary_db | 14 kB 00:01
Loading mirror speeds from cached hostfile
* base: mirrors.sina.cn
* extras: mirrors.btte.net
* updates: mirrors.sina.cn
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 1:1.10.1-1.el7.ngx will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
nginx x86_64 1:1.10.1-1.el7.ngx nginx 640 k
Transaction Summary
================================================================================
Install 1 Package
Total download size: 640 k
Installed size: 2.1 M
Is this ok [y/d/N]: y
Downloading packages:
nginx-1.10.1-1.el7.ngx.x86_64.rpm | 640 kB 00:27
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : 1:nginx-1.10.1-1.el7.ngx.x86_64 1/1
----------------------------------------------------------------------
Thanks for using nginx!
Please find the official documentation for nginx here:
* http://nginx.org/en/docs/
Commercial subscriptions for nginx are available on:
* http://nginx.com/products/
----------------------------------------------------------------------
Verifying : 1:nginx-1.10.1-1.el7.ngx.x86_64 1/1
Installed:
nginx.x86_64 1:1.10.1-1.el7.ngx
Complete!
验证 Nginx 配置文件的正确性
运行 nginx -t 命令时,Nginx 会自动检测配置文件的语法结构,反馈结果将告知配置是否正确,同时显示配置文件的具体路径:
nginx -t
终端输出内容如下:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
上述输出表明配置文件语法无误,验证通过。
CentOS 7.0+ 环境下 Nginx 服务的停止、启动与重启操作
在 CentOS 7 系统中,传统的 chkconfig 命令会提示使用 systemctl。systemctl 整合了 service 和 chkconfig 的功能,允许通过单一命令完成服务控制与开机自启设置。
systemctl 命令的标准调用格式为:
systemctl [OPTIONS...] {COMMAND}...
以 Nginx 服务为例,执行停止、启动和重启操作的命令如下:
systemctl stop nginx.service
systemctl start nginx.service
systemctl restart nginx.service
查看当前服务运行状态
systemctl status nginx.service
设置服务随系统启动而自动运行
systemctl enable nginx.service
禁用服务随系统启动而自动运行
systemctl disable nginx.service
以上内容涵盖了 CentOS 7.2 下 Nginx 的安装及服务管理核心要点,希望能对读者的学习与实践提供便利,欢迎持续关注本站获取更多技术支持。