先参考:
nginx官方安装说明
从nginx官方可以知道, nginx官方为linux各发行版提供了便捷的方法安装.下面简单说下centos下安装nginx的方法.
stable version and mainline version
由上面可知, nginx官方提供了两种nginx.repo的配置方式, 分别为:
Pre-Built Packages for Stable version
和Pre-Built Packages for Mainline version
添加nginx的yum仓库, 创建文件名为/etc/yum.repos.d/nginx.repo
并拷贝粘贴以下配置内容:
stables version
的配置如下(喜欢最新稳定版采用)
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
mainline version
的配置如下(喜欢开发版采用)
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
其中, 由官方信息可知变量$releasever
可替换为对于的版本, 如5, 6, 7分别对应5.x, 6.x, 7.x
Due to differences between how CentOS, RHEL, and Scientific Linux populate the $releasever variable, it is necessary to manually replace $releasever with either 5 (for 5.x) or 6 (for 6.x), depending upon your OS version.
保存退出后, 执行yum makecache
出现如下错误信息:
# yum makecache
Loaded plugins: security
base | 3.7 kB 00:00
base/group_gz | 219 kB 00:00
base/filelists_db | 6.3 MB 00:06
base/other_db | 2.8 MB 00:02
epel | 4.3 kB 00:00
epel/filelists_db | 8.0 MB 00:07
epel/other_db | 2.9 MB 00:02
extras | 3.4 kB 00:00
extras/filelists_db | 38 kB 00:00
extras/prestodelta | 1.7 kB 00:00
extras/other_db | 51 kB 00:00
http://nginx.org/packages/centos/6/x86_64/repodata/repomd.xml: [Errno 14] PYCURL ERROR 6 - "Couldn't resolve host 'nginx.org'"
Trying other mirror.
Error: Cannot retrieve repository metadata (repomd.xml) for repository: nginx. Please verify its path and try again
#
由”Couldn’t resolve host ‘nginx.org’”可以知道nginx域名解析不了
解决方法: 从DNS入手, 更改云主机的域名备用DNS
如下:
# vim /etc/resolv.conf
resolv.conf文件内容修改后, 如下:
options timeout:1 attempts:1 rotate
nameserver 10.202.72.118
# nameserver 10.202.72.116
nameserver 114.114.114.114
修改完后, 重新执行以下命令:
# yum makecache
# yum install nginx
nginx默认的配置目录为/etc/nginx/
, 配置文件为/etc/nginx/nginx.conf
# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
#
# nginx -v
nginx version: nginx/1.0.15
浏览器打开http://127.0.0.1, 如下图
发现nginx版本为1.0.15, 版本比较低
执行# yum update
进行升级, 然后再检测nginx版本, 如下:
# nginx -v
nginx version: nginx/1.8.1
此时从nginx官网可知
我们已成功安装了官方最新稳定版本的nginx.
p.s. 下面使用mainline version
的配置内容来配置/etc/yum.repos.d/nginx.repo, 即如下:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
然后执行yum makecache
来刷新缓存, 好了, 来检查下nginx是否有可更新版本:
# yum info nginx
Loaded plugins: security
Installed Packages
Name : nginx
Arch : x86_64
Version : 1.8.1
Release : 1.el6.ngx
Size : 872 k
Repo : installed
From repo : nginx
Summary : High performance web server
URL : http://nginx.org/
License : 2-clause BSD-like license
Description : nginx [engine x] is an HTTP and reverse proxy server, as well as
: a mail proxy server.
Available Packages
Name : nginx
Arch : x86_64
Version : 1.9.14
Release : 1.el6.ngx
Size : 820 k
Repo : nginx
Summary : High performance web server
URL : http://nginx.org/
License : 2-clause BSD-like license
Description : nginx [engine x] is an HTTP and reverse proxy server, as well as
: a mail proxy server.
显示, 我们可以将nginx1.8.1版本升级到当前最新版本nginx1.9.14, 命令如下:
# yum update nginx
OK, 成功安装当前nginx的最新版本为nginx1.9.14
# service nginx
Usage: nginx {start|stop|restart|condrestart|try-restart|force-reload|upgrade|reload|status|help|configtest}
常用nginx命令
启动nginx
# service nginx start
停止nginx
# service nginx stop
重启nginx
# service nginx restart