Centos7 yum配置Nginx

第一步 新建一个Nginx源

为了获得最新版的nginx,nginx安装说明建议直接从nginx官网下载,需要新建一个源。vi /etc/yum.repos.d/nginx.repo
  内容如下:

[nginx]                                                  //源名称,会被yum识别
name= nginx repo                                         //文件功能说明,只是便于理解这个文件的作用
baseurl=http://nginx.org/packages/centos/7/$releasever/$basearch/ //地址,非常重要,其中7(针对7.x)是版本,x86_64是CPU基本体系
gpgcheck=0                                               //gpg校验,0代表不校验,1代表校验
enabled=1                                                //启用,0代表不启用,1代表启用

第二步 下载

# yum install nginx 

已加载插件:fastestmirror
nginx                                                              | 2.9 kB  00:00:00     
nginx/7/x86_64/primary_db                                          |  24 kB  00:00:00     
Loading mirror speeds from cached hostfile
 * base: mirrors.cn99.com
 * extras: mirrors.163.com
 * updates: mirrors.cn99.com
正在解决依赖关系
--> 正在检查事务
---> 软件包 nginx.x86_64.1.1.12.0-1.el7.ngx 将被 安装
--> 解决依赖关系完成

依赖关系解决

==========================================================================================
 Package          架构              版本                           源                大小
==========================================================================================
正在安装:
 nginx            x86_64            1:1.12.0-1.el7.ngx             nginx            716 k

事务概要
==========================================================================================
安装  1 软件包

总下载量:716 k
安装大小:2.5 M
Is this ok [y/d/N]: y
Downloading packages:
nginx-1.12.0-1.el7.ngx.x86_64.rpm                                  | 716 kB  00:00:03     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : 1:nginx-1.12.0-1.el7.ngx.x86_64                                       1/1 
----------------------------------------------------------------------

Thanks for using nginx!


# nginx -v
nginx version: nginx/1.12.0

第三步 主要信息

如何查找安装软件的位置信息????

yum是基于·rpm·的,用rpm -ql就可以了

rpm 全名是** “RedHat Package Manager",简称则为RPM**。当被这个软件管理机制是由Red Hat这家公司发展来的。RPM是以一种数据记录的方式来将你所需要的软件安装到你的Linux系统的一套管理机制。rpm的优点如下:

1. RPM内含已编译过的程序与设置文件等数据,可以让用户免除重新编译的困扰。
2. RPM在被安装之前,会先检查系统的硬盘容量、操作系统版本等,可避免文件被错误安装。
3. RPM文件本身提供软件版本信息、依赖属性软件名称、软件用途说明、软件所含文件信息,便于了解软件。
4. RPM管理的方式使用数据库记录RPM文件的相关参数,便于升级、删除、查询与验证。

路径 说明
/etc 一些设置文件放置的目录如/etc/crontab
/usr/bin 一些可执行文件
/usr/lib 一些程序使用的动态函数库
/usr/share/doc 一些基本的软件使用手册与帮助文档
/usr/share/man 一些man page文件

罗列即可

# rpm --help 
....
##########
-l, --list                       列出软件包中的文件
--queryformat=QUERYFORMAT        使用这种格式打印信息
##########

# rpm -ql nginx | grep nginx
1. /etc/nginx: 默认安装位置;
2. /etc/nginx/conf.d/default.conf: 默认配置信息

第四步 基本设置

CentOS 7 默认使用的是firewall作为防火墙;

#关闭防火墙
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动

启动Nginx服务

# service nginx start

Redirecting to /bin/systemctl start  nginx.service
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.

# journalctl -xe

5月 10 23:46:28 localhost.localdomain systemd[1]: Starting nginx - high performance web server...
-- Subject: Unit nginx.service has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit nginx.service has begun starting up.
5月 10 23:46:28 localhost.localdomain nginx[21029]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
5月 10 23:46:28 localhost.localdomain nginx[21029]: nginx: configuration file /etc/nginx/nginx.conf test is successful
5月 10 23:46:28 localhost.localdomain nginx[21031]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
5月 10 23:46:29 localhost.localdomain nginx[21031]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
5月 10 23:46:29 localhost.localdomain nginx[21031]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
5月 10 23:46:30 localhost.localdomain nginx[21031]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
5月 10 23:46:30 localhost.localdomain nginx[21031]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
5月 10 23:46:31 localhost.localdomain systemd[1]: nginx.service: control process exited, code=exited status=1
5月 10 23:46:31 localhost.localdomain nginx[21031]: nginx: [emerg] still could not bind()
5月 10 23:46:31 localhost.localdomain systemd[1]: Failed to start nginx - high performance web server.
-- Subject: Unit nginx.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit nginx.service has failed.
-- 
-- The result is failed.
5月 10 23:46:31 localhost.localdomain systemd[1]: Unit nginx.service entered failed state.
5月 10 23:46:31 localhost.localdomain systemd[1]: nginx.service failed.
5月 10 23:46:31 localhost.localdomain polkitd[658]: Unregistered Authentication Agent for unix-process:21023:289487 (system bus name :1.
lines 1198-1223/1223 (END)


排除❌

1.检测配置文件是否出错

# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

2.查看日志

# cat /var/log/nginx/error.log

2017/05/10 23:20:48 [emerg] 20747#20747: open() "/var/run/nginx.pid" failed (13: Permission denied)
2017/05/10 23:21:10 [emerg] 20759#20759: bind() to 0.0.0.0:80 failed (98: Address already in use)
2017/05/10 23:21:10 [emerg] 20759#20759: bind() to 0.0.0.0:80 failed (98: Address already in use)
.....

3.从上看原因似乎出自open() "/var/run/nginx.pid" failed (13: Permission denied);

但是·/var/run·下没有·nginx.pid·

查看ps -ef | grep nginx

# ps -ef |grep nginx
root      17680      1  0 22:59 ?        00:00:00 nginx: master process nginx
nginx     17681  17680  0 22:59 ?        00:00:00 nginx: worker process
root      21063  13071  0 23:53 pts/0    00:00:00 grep --color=auto nginx

# kill -9 17680
# kill -9  21063
# service nginx start
Redirecting to /bin/systemctl start  nginx.service

# systemctl status nginx.service
● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since 四 2017-05-11 00:43:27 EDT; 2s ago
     Docs: http://nginx.org/en/docs/
  Process: 2467 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
  Process: 2466 ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
  
     
# ps -ef |grep nginx
root      21141      1  0 00:26 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx     21142  21141  0 00:26 ?        00:00:00 nginx: worker process
root      21179  13071  0 00:34 pts/0    00:00:00 grep --color=auto nginx

以上的操作是在vmware中配置的Nginx,此时Nginx服务已经开启,虚拟机地址:http://172.16.46.172 浏览器中访问如下:

Centos7 yum配置Nginx_第1张图片
success

更多精彩内容请关注“IT实战联盟”哦~~~


Centos7 yum配置Nginx_第2张图片
IT实战联盟.jpg

参考链接
nignx 官网
CentOS 7学习笔记(二)之Nginx安装
nginx start error

你可能感兴趣的:(Centos7 yum配置Nginx)