ubuntu預設安裝nginx版本太舊!?快來試試無痛升級nginx 1.8.1

ubuntu14.04預設安裝的nginx版本是1.4.6,看起來好像還可以
但是如今nginx的最新開發版本已經到了1.9.10,穩定版也到了1.8.1
我們今天的目標是讓nginx版本升級到1.8.1
並且能夠使用php5-fpm使php能夠正常運作

1.安裝Nginx Stable PPA

sudo apt-get install python-software-properties
sudo add-apt-repository ppa:nginx/stable
sudo apt-get update
sudo apt-get upgrade

2.升級nginx

root@www:~# apt-get upgrade nginx

Reading package lists... Done
Building dependency tree       
Reading state information... Done
Calculating upgrade... Done
The following packages will be REMOVED:
  nginx-core
The following NEW packages will be installed:
  nginx-full
The following packages will be upgraded:
  nginx nginx-common
2 upgraded, 1 newly installed, 1 to remove and 0 not upgraded.
Need to get 481 kB of archives.
After this operation, 218 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://ppa.launchpad.net/nginx/stable/ubuntu/ trusty/main nginx all 1.8.1-1+trusty0 [22.0 kB]
Get:2 http://ppa.launchpad.net/nginx/stable/ubuntu/ trusty/main nginx-common all 1.8.1-1+trusty0 [48.4 kB]
Get:3 http://ppa.launchpad.net/nginx/stable/ubuntu/ trusty/main nginx-full amd64 1.8.1-1+trusty0 [411 kB]
Fetched 481 kB in 5s (93.5 kB/s)     
(Reading database ... 14483 files and directories currently installed.)
Preparing to unpack .../nginx_1.8.1-1+trusty0_all.deb ...
Unpacking nginx (1.8.1-1+trusty0) over (1.4.6-1ubuntu3.3) ...
(Reading database ... 14481 files and directories currently installed.)
Removing nginx-core (1.4.6-1ubuntu3.3) ...
invoke-rc.d: policy-rc.d denied execution of stop.
(Reading database ... 14475 files and directories currently installed.)
Preparing to unpack .../nginx-common_1.8.1-1+trusty0_all.deb ...
Moving obsolete conffile /etc/nginx/naxsi.rules out of the way...
Moving obsolete conffile /etc/nginx/naxsi_core.rules out of the way...
Moving obsolete conffile /etc/nginx/naxsi-ui.conf.1.4.1 out of the way...
Unpacking nginx-common (1.8.1-1+trusty0) over (1.4.6-1ubuntu3.3) ...
Selecting previously unselected package nginx-full.
Preparing to unpack .../nginx-full_1.8.1-1+trusty0_amd64.deb ...
Unpacking nginx-full (1.8.1-1+trusty0) ...
Processing triggers for ureadahead (0.100.0-16) ...
Setting up nginx-common (1.8.1-1+trusty0) ...
Installing new version of config file /etc/logrotate.d/nginx ...
Installing new version of config file /etc/nginx/mime.types ...
Installing new version of config file /etc/nginx/fastcgi_params ...
Installing new version of config file /etc/nginx/uwsgi_params ...
Installing new version of config file /etc/nginx/scgi_params ...
Installing new version of config file /etc/nginx/koi-win ...
Installing new version of config file /etc/nginx/nginx.conf ...
Installing new version of config file /etc/nginx/koi-utf ...

Configuration file '/etc/nginx/sites-available/default'
 ==> 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.
*** default (Y/I/N/O/D/Z) [default=N] ? n    # y 安裝維護者版本  n 保留原始設定 d 顯示版本區別 z 是檢查
Installing new version of config file /etc/init.d/nginx ...
Installing new version of config file /etc/default/nginx ...
Removing obsolete conffile /etc/nginx/naxsi.rules ...
Removing obsolete conffile /etc/nginx/naxsi_core.rules ...
Removing obsolete conffile /etc/nginx/naxsi-ui.conf.1.4.1 ...
Processing triggers for ureadahead (0.100.0-16) ...
Setting up nginx-full (1.8.1-1+trusty0) ...
Setting up nginx (1.8.1-1+trusty0) ...

接著會發現原本的php頁面居然死掉了!!?

別緊張,我們打開nginx的設定檔更改一點點


nano /etc/nginx/sites-available/default

# 找到php設定
location ~ .php$ {
       fastcgi_split_path_info ^(.+.php)(/.+)$;
       # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

         # With php5-cgi alone:
#       fastcgi_pass 127.0.0.1:9000;
         # With php5-fpm:
         fastcgi_pass unix:/var/run/php5-fpm.sock;
         fastcgi_index index.php;
         include fastcgi_params;

         include fastcgi.conf;
}

在加入”include fastcgi.conf;”後會發現php頁面可以執行

就這樣無痛升級nginx 1.8.1