阿里雲centos6.7下yum安裝nginx最新版

1. 添加nginx yum repository

先參考:
nginx官方安裝說明
從nginx官方可以知道, nginx官方爲linux各發行版提供了便捷的方法安裝.下面簡單說下centos下安裝nginx的方法.

stable version and mainline version
由上面可知, nginx官方提供了兩種nginx.repo的配置方式, 分別爲:
Pre-Built Packages for Stable versionPre-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

2. yum安裝nginx

# yum install nginx

3. 檢測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

4. 查看nginx命令用法

# 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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章