FreeBSD 上で Nginx の pkg を使う場合のメモ 基本編

FreeBSD上でNginxのpkgを使う場合のメモです。基本編


# Nginxに関連するpkg一覧
```
# pkg search nginx
nginx-1.14.2,2                 Robust and small WWW server
nginx-devel-1.15.7_2           Robust and small WWW server
nginx-full-1.14.2,2            Robust and small WWW server (full package)
nginx-lite-1.14.2,2            Robust and small WWW server (lite package)
nginx-naxsi-1.14.2,2           Robust and small WWW server (plus NAXSI)
p5-Nginx-ReadBody-0.07_1       Nginx embeded perl module to read and evaluate a request body
p5-Nginx-Simple-0.07_1         Perl 5 module for easy to use interface for Nginx Perl Module
p5-Test-Nginx-0.26             Testing modules for Nginx C module development
py27-certbot-nginx-0.29.1      NGINX plugin for Certbot
py36-certbot-nginx-0.29.1      NGINX plugin for Certbot
rubygem-passenger-nginx-6.0.0  Modules for running Ruby on Rails and Rack applications
```
p5-*(perl)やpy*(python)やrubygem*(ruby)は無視してそれ以外のpkgを説明しておきます

## nginx
stable版のNginx。2018年12月10日時点で1.14.2。
-Vオプションでビルド時のオプションも見れるので確認するとこんな感じ。

```
# nginx -V
nginx version: nginx/1.14.2
built with OpenSSL 1.0.2o-freebsd  27 Mar 2018
TLS SNI support enabled
configure arguments: --prefix=/usr/local/etc/nginx --with-cc-opt='-I /usr/local/include' --with-ld-opt='-L /usr/local/lib' --conf-path=/usr/local/etc/nginx/nginx.conf --sbin-path=/usr/local/sbin/nginx --pid-path=/var/run/nginx.pid --error-log-path=/var/log/nginx/error.log --user=www --group=www --modules-path=/usr/local/libexec/nginx --with-file-aio --http-client-body-temp-path=/var/tmp/nginx/client_body_temp --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi_temp --http-proxy-temp-path=/var/tmp/nginx/proxy_temp --http-scgi-temp-path=/var/tmp/nginx/scgi_temp --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi_temp --http-log-path=/var/log/nginx/access.log --with-http_v2_module --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-pcre --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --without-mail_imap_module --without-mail_pop3_module --without-mail_smtp_module --with-mail_ssl_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-threads --with-mail=dynamic --with-stream=dynamic
```



## nginx-devel
mainline版のNginx。2018年12月10日時点で1.15.7。
ビルドオプションはnginxパッケージと同じかな。

```
# nginx -V
nginx version: nginx/1.15.7
built with OpenSSL 1.0.2o-freebsd  27 Mar 2018
TLS SNI support enabled
configure arguments: --prefix=/usr/local/etc/nginx --with-cc-opt='-I /usr/local/include' --with-ld-opt='-L /usr/local/lib' --conf-path=/usr/local/etc/nginx/nginx.conf --sbin-path=/usr/local/sbin/nginx --pid-path=/var/run/nginx.pid --error-log-path=/var/log/nginx/error.log --user=www --group=www --modules-path=/usr/local/libexec/nginx --with-file-aio --http-client-body-temp-path=/var/tmp/nginx/client_body_temp --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi_temp --http-proxy-temp-path=/var/tmp/nginx/proxy_temp --http-scgi-temp-path=/var/tmp/nginx/scgi_temp --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi_temp --http-log-path=/var/log/nginx/access.log --with-http_v2_module --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-pcre --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --without-mail_imap_module --without-mail_pop3_module --without-mail_smtp_module --with-mail_ssl_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-threads --with-mail=dynamic --with-stream=dynamic
```

## nginx-full
stable版のNginxだが、portsで指定されているオプションを全て有効化したバージョン。
使いたいモジュールがある場合はこちらをインストールして使えばOK。
pkgがたくさんイントールされてしまい面倒なのでインストールまではしていません。

## nginx-lite
stable版のNginx。ビルドオプション少なめ。

```
# nginx -V
nginx version: nginx/1.14.2
built with OpenSSL 1.0.2o-freebsd  27 Mar 2018
TLS SNI support enabled
configure arguments: --prefix=/usr/local/etc/nginx --with-cc-opt='-I /usr/local/include' --with-ld-opt='-L /usr/local/lib' --conf-path=/usr/local/etc/nginx/nginx.conf --sbin-path=/usr/local/sbin/nginx --pid-path=/var/run/nginx.pid --error-log-path=/var/log/nginx/error.log --user=www --group=www --with-file-aio --http-client-body-temp-path=/var/tmp/nginx/client_body_temp --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi_temp --http-proxy-temp-path=/var/tmp/nginx/proxy_temp --http-scgi-temp-path=/var/tmp/nginx/scgi_temp --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi_temp --http-log-path=/var/log/nginx/access.log --with-http_realip_module --with-pcre --with-http_ssl_module --without-mail_imap_module --without-mail_pop3_module --without-mail_smtp_module --with-threads
```




## nginx-naxsi
stable版のNginx。WAFのnaxsi付きでビルドされたNginx。

```
# nginx -V
nginx version: nginx/1.14.2
built with OpenSSL 1.0.2o-freebsd  27 Mar 2018
TLS SNI support enabled
configure arguments: --prefix=/usr/local/etc/nginx --with-cc-opt='-I /usr/local/include' --with-ld-opt='-L /usr/local/lib' --conf-path=/usr/local/etc/nginx/nginx.conf --sbin-path=/usr/local/sbin/nginx --pid-path=/var/run/nginx.pid --error-log-path=/var/log/nginx/error.log --user=www --group=www --modules-path=/usr/local/libexec/nginx --with-file-aio --http-client-body-temp-path=/var/tmp/nginx/client_body_temp --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi_temp --http-proxy-temp-path=/var/tmp/nginx/proxy_temp --http-scgi-temp-path=/var/tmp/nginx/scgi_temp --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi_temp --http-log-path=/var/log/nginx/access.log --with-http_v2_module --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-pcre --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --without-mail_imap_module --without-mail_pop3_module --without-mail_smtp_module --with-mail_ssl_module --with-stream_ssl_module --with-threads --with-mail=dynamic --with-stream=dynamic --add-dynamic-module=/wrkdirs/usr/ports/www/nginx-naxsi/work/naxsi-0.56/naxsi_src
```



# インストールされたファイルを確認
nginxパッケージの場合。

```
# pkg info -l nginx
nginx-1.14.2,2:
	/usr/local/etc/nginx/fastcgi_params-dist
	/usr/local/etc/nginx/koi-utf
	/usr/local/etc/nginx/koi-win
	/usr/local/etc/nginx/mime.types-dist
	/usr/local/etc/nginx/nginx.conf-dist
	/usr/local/etc/nginx/scgi_params-dist
	/usr/local/etc/nginx/uwsgi_params-dist
	/usr/local/etc/nginx/win-utf
	/usr/local/etc/rc.d/nginx
	/usr/local/libexec/nginx/ngx_mail_module.so
	/usr/local/libexec/nginx/ngx_stream_module.so
	/usr/local/man/man8/nginx.8.gz
	/usr/local/sbin/nginx
	/usr/local/share/licenses/nginx-1.14.2,2/BSD2CLAUSE
	/usr/local/share/licenses/nginx-1.14.2,2/LICENSE
	/usr/local/share/licenses/nginx-1.14.2,2/catalog.mk
	/usr/local/share/vim/vimfiles/ftdetect/nginx.vim
	/usr/local/share/vim/vimfiles/ftplugin/nginx.vim
	/usr/local/share/vim/vimfiles/indent/nginx.vim
	/usr/local/share/vim/vimfiles/syntax/nginx.vim
	/usr/local/www/nginx-dist/50x.html
	/usr/local/www/nginx-dist/EXAMPLE_DIRECTORY-DONT_ADD_OR_TOUCH_ANYTHING
	/usr/local/www/nginx-dist/index.html
```


# Nginxの設定や起動方法

## 設定ファイルのパス
```
/usr/local/etc/nginx/nginx.conf
```

## Nginxを使用可能にする設定（OS起動時もNginxを起動）
```
# sysrc nginx_enable="YES"
```
上記の設定をせずに試しに起動してみたい時は下記のようなコマンドで起動可能

```
# /usr/local/etc/rc.d/nginx onestart
```


## Nginxを起動するコマンド
```
# /usr/local/etc/rc.d/nginx start
または
# service nginx start
```

## Nginxを停止するコマンド
```
# /usr/local/etc/rc.d/nginx stop
または
# service nginx stop
```

## Nginxを再起動するコマンド
```
# /usr/local/etc/rc.d/nginx restart
または
# service nginx restart
```

## Nginxの設定ファイルを更新した時などに使うコマンド
```
# /usr/local/etc/rc.d/nginx reload
または
# service nginx reload
```

## Nginxを設定ファイルを確認するコマンド
```
# /usr/local/etc/rc.d/nginx configtest
または
# service nginx configtest
```

## Nginxを更新した時に実行するコマンド
```
# /usr/local/etc/rc.d/nginx upgrade
または
# service nginx upgrade
```

## Nginx gracefulstop
```
# /usr/local/etc/rc.d/nginx gracefulstop
または
# service nginx gracefulstop
```


# dynamicモジュールを使う時の注意点

設定ファイル(/usr/local/etc/nginx/nginx.conf)にload_moduleを記述する
nginxパッケージのmailモジュールとstreamモジュールはdynamicになっているので
使う場合は下記の設定を追加する

```
load_module /usr/local/libexec/nginx/ngx_mail_module.so;
load_module /usr/local/libexec/nginx/ngx_stream_module.so;
```