本文发自 http://www.binss.me/blog/install-lastest-nginx-on-ubuntu/,转载请注明出处。

为vps安装nginx,使用apt-get安装后发现安装的版本为1.4.6,而如今最新版本为1.6.2,除了下下来手动编译,是否可以使用apt-get来安装最新版本呢?

GOOGLE之,答案是可以的。

安装有旧版本的先执行以下步骤卸载:

$ sudo apt-get remove nginx
$ sudo apt-get remove nginx-common

注意第二条指令,bin当时没有执行这步,结果在安装新版本的时候报错:

dpkg: error processing archive /var/cache/apt/archives/nginx_1.6.2-1~trusty_amd64.deb (--unpack):
 trying to overwrite '/etc/logrotate.d/nginx', which is also in package nginx-common 1.4.6-1ubuntu3.1

然后开始安装:

  1. 加入最新源

    $ sudo vim /etc/apt/sources.list

    加入:

    deb http://nginx.org/packages/ubuntu/ 代码名称 nginx
    deb-src http://nginx.org/packages/ubuntu/ 代码名称 nginx

    代码名称参考:

    http://nginx.org/en/linux_packages.html

    我ubuntu版本是14.04,所以代码名称为trusty。

  2. 更新apt-key,否则签名验证不通过

    $ wget http://nginx.org/keys/nginx_signing.key
    $ sudo apt-key add nginx_signing.key
  3. 更新apt,安装

    $ sudo apt-get update
    $ sudo apt-get install nginx
  4. 如果以前安装有旧版本,安装程序会询问是否保留旧版配置文件

    Configuration file '/etc/nginx/nginx.conf'
     ==> Modified (by you or by a script) since installation.
     ==> Package distributor has shipped an updated version.
       What would you like to do about it ?  Your options are:
        Y or I  : install the package maintainer's version
        N or O  : keep your currently-installed version
          D     : show the differences between the versions
          Z     : start a shell to examine the situation
     The default action is to keep your current version.
    *** nginx.conf (Y/I/N/O/D/Z) [default=N] ?

    输入N保留配置文件。

  5. 安装完毕,查看版本号

    $ nginx -v

    显示为1.6.2。

  6. 常用命令(启动,停止,查询状态,重新加载配置文件):

    $ sudo service nginx start
    $ sudo service nginx stop
    $ sudo service nginx status
    $ sudo nginx -s reload