既存のNginxをmainlineに移行する

自宅サーバのHTTP(S)を終端しているNginxを、debianパッケージ版からmainlineに移行したのでその時のメモです。

# 環境

- debian jessie
- 既に入っているNginx (1.6.2-5)
- これから入れるNginx (1.11.2)
- 既にconfファイルは多く存在する

```
$ cat /etc/issue
Debian GNU/Linux 8 \n \l
```

作業前

```
$ sudo dpkg -l nginx
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name                                          Version                     Architecture                Description
+++-=============================================-===========================-===========================-===============================================================================================
ii  nginx                                         1.6.2-5                     all                         small, powerful, scalable web/proxy server
```

作業後

```
$ dpkg -l nginx
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name                                          Version                     Architecture                Description
+++-=============================================-===========================-===========================-===============================================================================================
ii  nginx                                         1.11.2-1~jessie             amd64                       high performance web server
```

# 作業内容

## Nginx GPGを追加する

```
$ wget "http://nginx.org/keys/nginx_signing.key"
--2016-07-07 13:48:43--  http://nginx.org/keys/nginx_signing.key
Resolving nginx.org (nginx.org)... 206.251.255.63, 95.211.80.227, 2001:1af8:4060:a004:21::e3
Connecting to nginx.org (nginx.org)|206.251.255.63|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1561 (1.5K) [text/plain]
Saving to: ‘nginx_signing.key’

nginx_signing.key                                  100%[==================================================================================================================>]   1.52K  --.-KB/s   in 0s

2016-07-07 13:48:43 (440 MB/s) - ‘nginx_signing.key’ saved [1561/1561]

$ sudo apt-key  add nginx_signing.key
OK

```

## apt listにNginx mainlineを追加

```
$ cat /etc/apt/sources.list.d/nginx.list
deb http://nginx.org/packages/mainline/debian/ jessie nginx
deb-src http://nginx.org/packages/mainline/debian/ jessie nginx
$ sudo apt-get update
[中略]
```

追加された事を確認

```
$ sudo apt-cache show nginx
Package: nginx
Version: 1.11.2-1~jessie
Architecture: amd64
Maintainer: Sergey Budnevitch <sb@nginx.com>
Installed-Size: 2483
Depends: libc6 (>= 2.14), libpcre3 (>= 1:8.35), libssl1.0.0 (>= 1.0.1), zlib1g (>= 1:1.1.4), lsb-base, adduser
Provides: httpd
Filename: pool/nginx/n/nginx/nginx_1.11.2-1~jessie_amd64.deb
Size: 720626
MD5sum: 71606b0d233c624e1ed0f2bb2cb1544a
SHA1: 85f61bad083fab55d1450a8594d28ed7bec6c701
SHA256: 3c984a186fbccf36f73a06683977a2416645e723045ad6eb61f0d584f26847bd
Section: httpd
Priority: optional
Homepage: http://nginx.org
Description: high performance web server
 nginx [engine x] is an HTTP and reverse proxy server, as well as
 a mail proxy server.
Description-md5: c3ecf9b168b3f5d1b11dda6baf5dd768

[以下略]
```

## Nginx mainline インストール

既存のNginxが残っていると動作しなかったので、一時的に削除

```
$ sudo apt-get remove nginx-common
$ sudo apt-get install nginx
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
  nginx
0 upgraded, 1 newly installed, 0 to remove and 177 not upgraded.
Need to get 0 B/721 kB of archives.
After this operation, 2,543 kB of additional disk space will be used.
Selecting previously unselected package nginx.
(Reading database ... 50261 files and directories currently installed.)
Preparing to unpack .../nginx_1.11.2-1~jessie_amd64.deb ...
----------------------------------------------------------------------

Thanks for using nginx!

Please find the official documentation for nginx here:
* http://nginx.org/en/docs/

Commercial subscriptions for nginx are available on:
* http://nginx.com/products/

----------------------------------------------------------------------
Unpacking nginx (1.11.2-1~jessie) ...
Processing triggers for systemd (215-17) ...
Setting up nginx (1.11.2-1~jessie) ...
Installing new version of config file /etc/default/nginx ...
Installing new version of config file /etc/init.d/nginx ...
Installing new version of config file /etc/logrotate.d/nginx ...
Installing new version of config file /etc/nginx/fastcgi_params ...

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] ?
Installing new version of config file /etc/nginx/scgi_params ...
Installing new version of config file /etc/nginx/uwsgi_params ...
Installing new version of config file /etc/nginx/win-utf ...
Processing triggers for systemd (215-17) ...
```

これでインストールには成功しました。

```
$ sudo nginx -V
nginx version: nginx/1.11.2
built by gcc 4.9.2 (Debian 4.9.2-10)
built with OpenSSL 1.0.1k 8 Jan 2015 (running with OpenSSL 1.0.1t  3 May 2016)
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_perl_module=dynamic --add-dynamic-module=debian/extra/njs-ef2b708510b1/nginx --with-threads --with-stream --with-stream_ssl_module --with-http_slice_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-http_v2_module --with-cc-opt='-g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-z,relro -Wl,--as-needed'
```

## トラブル解決

### systemdで起動出来ない

apt-get remove した影響でmaskされているので、unmaskしてやりましょう。

```
$ sudo systemctl status nginx.service
● nginx.service
   Loaded: masked (/dev/null)
   Active: inactive (dead) since Thu 2016-07-07 13:56:32 JST; 1min 17s ago
 Main PID: 700 (code=exited, status=0/SUCCESS)

$ sudo systemctl unmask nginx.service
Removed symlink /etc/systemd/system/nginx.service.
```

# まとめ

- 一時的でもNginxの停止が発生するので、本番環境の場合はメンテナンス用サーバを用意して逃がしましょう
- バージョンが変更された事によりconfに不整合が発生する場合があるので気を付けましょう
  - 元々warningだったのがemergencyになって起動しない場合などがあります
- 快適Nginx生活は意外と簡単に出来るので、是非ご家庭にどうぞ

# 参考

- [公式ドキュメント](http://nginx.org/en/linux_packages.html)
- [どのバージョンのnginxを使うべきか？ - 考える人、コードを書く人](http://bokko.hatenablog.com/entry/2014/05/24/220554)
- [「Systemd」を理解する ーシステム管理編ー | ギークを目指して](http://equj65.net/tech/systemd-manage/)