RHEL/CentOS 安装 nginx

采用官方nginx源安装方法支持的环境

如果不支持,可以改为epel源

系统 版本 支持的平台
RHEL/CentOS 6.x x86_64, i386
RHEL/CentOS 7.4+ x86_64, ppc64le

也可以源码编译安装或直接yum安装。
域名解析请提前设置好

1 配置nginx源

1.1 方法一:配置nginx官方源(推荐)

[root@node1 ~]# vim /etc/yum.repos.d/nginx.repo
添加:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

1.2 方法二:配epel源

备份(如有配置其他epel源)(推荐阿里epel源)

mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup

下载新repo 到/etc/yum.repos.d/

epel(RHEL 7)

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

epel(RHEL 6)

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo

2 安装nginx

2.1 yum安装nginx

[root@node1 ~]# yum list | grep nginx
[root@node1 ~]# yum install nginx

2.2 查看版本

[root@node1 ~]# nginx -v

2.3 查看nginx 编译的参数

[root@node1 ~]# nginx -V

3 nginx启动与停止

3.1 CentOS 6.*

设置开机自启动

[root@node1 ~]# chkconfig --list|grep nginx
nginx      0:关闭 1:关闭    2:启用    3:启用    4:启用    5:启用    6:关闭
nginx-debug     0:关闭    1:关闭    2:关闭    3:关闭    4:关闭    5:关闭    6:关闭
[root@node1 ~]# chkconfig nginx-debug on
[root@node1 ~]# chkconfig --list|grep nginx
nginx       0:关闭    1:关闭    2:启用    3:启用    4:启用    5:启用    6:关闭
nginx-debug     0:关闭    1:关闭    2:启用    3:启用    4:启用    5:启用    6:关闭
[root@node1 ~]#

启动

[root@node1 ~]#  service nginx start

停止

[root@node1 ~]#  service nginx stop

重启

[root@node1 ~]#  service nginx restart

3.2 CentOS 7.*

添加开机自启动

[root@node1 ~]# systemctl list-unit-files | grep mysqld.service
mysqld.service                                disabled
[root@node1 ~]# systemctl enable  mysqld.service
Created symlink from /etc/systemd/system/multi-user.target.wants/mysqld.service to /usr/lib/systemd/system/mysqld.service.
[root@node1 ~]# systemctl list-unit-files | grep mysqld.service
mysqld.service                                enabled 
[root@node1 ~]#

启动

[root@node1 ~]#  systemctl start nginx.service

停止

[root@node1 ~]#  systemctl stop nginx.service

重启

[root@node1 ~]#  systemctl restart nginx.service

3.3 参数

nginx version: nginx/1.14.0
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
  -?,-h         : 帮助
  -v            : 显示版本并退出
  -V            : 显示版本并配置选项然后退出
  -t            : 测试配置并退出
  -T            : 测试配置,转储并退出
  -q            : 在配置测试期间抑制非错误消息
  -s signal     : 向主进程发送信号:停止,退出,重新打开,重新加载
  -p prefix     : 设置前缀路径(默认值:/ etc/nginx/)
  -c filename   : 设置配置文件(默认值:/etc/nginx/nginx.conf)
  -g directives : 从配置文件中设置全局指令

3.4 检查配置文件

测试配置文件是否正确,在运行时需要重新加载配置的时候,此命令非常重要,用来检测所修改的配置文件是否有语法错误。

[root@node1 ~]# nginx -t -c /etc/nginx/nginx.conf

猜你喜欢

转载自blog.51cto.com/moerjinrong/2287496