Debian GNU/Linux 7 Wheezy サーバに Web サーバ Nginx をソースをビルドしてインストールする方法についての記録です。
ちなみに以前、Linux Mint にソースビルドでインストールする方法を紹介しています。(当然、似たような内容となっています)
0 前提条件
- Debian GNU/Linux 7.1.0 での作業を想定。
- 接続元のマシンは Linux Mint 14(64bit) を想定。
- インストールする Nginx のバージョンは 1.4.3(当記事執筆時点最新安定版)を想定。
1. 必要パッケージインストール
Nginx のビルドに以下のパッケージが必要なので、未インストールならインストールしておく。(他にも必要なものがあればインストールする)
1
|
|
依存する libpcre3, zlib1g-dev もインストールされる。
2. アーカイブダウンロード
インストールに使用するアーカイブファイルをダインロードし、解凍する。
1 2 3 |
|
3. ビルド&インストール
configure
, make
, make install
でビルド・インストールする。
インストール先はデフォルトでは /usr/local/nginx
となるが、今後のバージョンアップのことも考慮し、バージョン別にインストール先を指定しリンクを貼るようにした。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
configure
オプションについて。
prefix
… nginx をインストールするディレクトリを指定する。(デフォルト:/usr/local/nginx
)with-http_ssl_module
… HTTPS/SSL support(デフォルト:HTTPS は無効)with-http_realip_module
… For using nginx as backend
4. インストール確認
Nginx がインストールできたか確認してみる。
-V
オプションで configure
オプションも確認可能。-v
でバージョンだけ確認可能。
1 2 3 4 5 |
|
5. 設定
今回のインストール環境の場合、基本的(グローバル)な設定のファイルは /usr/local/nginx/conf/nginx.conf
となる。
取り急ぎ、デフォルトのままとした。
詳細な設定は、「当ブログ Nginx 関連の過去記事」を参照。
6. 起動スクリプト作成
サービス起動用スクリプト “/etc/init.d/nginx” を作成する。
ゼロから作成するのは面倒なので、 “/etc/init.d/skeleton” を複製して編集する。(冒頭の各種定義部分の他、Start, Stop, Restart 時にメッセージを出力するようにしている)
コメントは不要なら削除してもよい。(ただし、冒頭の BEGIN INIT INFO
から END INIT INFO
はコメントではないの削除しないこと)
もしくは、「Nginx - InitScriptsJa」にあるサンプルを参考にして作成してもよい。
File: /etc/init.d/nginx
1 #! /bin/sh
2
3 ### BEGIN INIT INFO
4
5 # Provides: nginx # <= 編集
6 # Required-Start: $all # <= 編集
7 # Required-Stop: $all # <= 編集
8 # Default-Start: 2 3 4 5
9 # Default-Stop: 0 1 6
10 # Short-Description: Start script of the Nginx. # <= 編集
11 # Description: Start/Stop the nginx daemon. # <= 編集
12
13 ### END INIT INFO
14
15 # Author: Administrator <root@mk-mode.com>
16 #
17 # Please remove the "Author" lines above and replace them
18 # with your own name if you copy and modify this script.
19
20 # Do NOT "set -e"
21
22 # PATH should only include /usr/* if it runs after the mountnfs.sh script
23 PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin # <= 編集
24 DESC="Nginx Daemon" # <= 編集
25 NAME=nginx # <= 編集
26 DAEMON=/usr/local/nginx/sbin/$NAME
27 #DAEMON_ARGS="--options args" # <= コメントアウト
28 PIDFILE=/var/run/$NAME.pid
29 SCRIPTNAME=/etc/init.d/$NAME
30
31 # Exit if the package is not installed
32 [ -x "$DAEMON" ] || exit 0
33
34 # Read configuration variable file if it is present
35 [ -r /etc/default/$NAME ] && . /etc/default/$NAME
36
37 # Load the VERBOSE setting and other rcS variables
38 . /lib/init/vars.sh
39
40 # Define LSB log_* functions.
41 # Depend on lsb-base (>= 3.2-14) to ensure that this file is present
42 # and status_of_proc is working.
43 . /lib/lsb/init-functions
44
45 #
46 # Function that starts the daemon/service
47 #
48 do_start()
49 {
50 # Return
51 # 0 if daemon has been started
52 # 1 if daemon was already running
53 # 2 if daemon could not be started
54 start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
55 || return 1
56 start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- \
57 $DAEMON_ARGS \
58 || return 2
59 # Add code here, if necessary, that waits for the process to be ready
60 # to handle requests from services started subsequently which depend
61 # on this one. As a last resort, sleep for some time.
62 }
63
64 #
65 # Function that stops the daemon/service
66 #
67 do_stop()
68 {
69 # Return
70 # 0 if daemon has been stopped
71 # 1 if daemon was already stopped
72 # 2 if daemon could not be stopped
73 # other if a failure occurred
74 start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
75 RETVAL="$?"
76 [ "$RETVAL" = 2 ] && return 2
77 # Wait for children to finish too if this is a daemon that forks
78 # and if the daemon is only ever run from this initscript.
79 # If the above conditions are not satisfied then add some other code
80 # that waits for the process to drop all resources that could be
81 # needed by services started subsequently. A last resort is to
82 # sleep for some time.
83 start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
84 [ "$?" = 2 ] && return 2
85 # Many daemons don't delete their pidfiles when they exit.
86 rm -f $PIDFILE
87 return "$RETVAL"
88 }
89
90 #
91 # Function that sends a SIGHUP to the daemon/service
92 #
93 do_reload() {
94 #
95 # If the daemon can reload its configuration without
96 # restarting (for example, when it is sent a SIGHUP),
97 # then implement that here.
98 #
99 start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
100 return 0
101 }
102
103 case "$1" in
104 start)
105 echo -n "Starting $DESC: $NAME\n" # <= 追加
106 [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
107 do_start
108 case "$?" in
109 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
110 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
111 esac
112 ;;
113 stop)
114 echo -n "Stopping $DESC: $NAME\n" # <= 追加
115 [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
116 do_stop
117 case "$?" in
118 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
119 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
120 esac
121 ;;
122 status)
123 status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
124 ;;
125 #reload|force-reload)
126 #
127 # If do_reload() is not implemented then leave this commented out
128 # and leave 'force-reload' as an alias for 'restart'.
129 #
130 #log_daemon_msg "Reloading $DESC" "$NAME"
131 #do_reload
132 #log_end_msg $?
133 #;;
134 restart|force-reload)
135 echo -n "Restarting $DESC: $NAME\n" # <= 追加
136 #
137 # If the "reload" option is implemented then remove the
138 # 'force-reload' alias
139 #
140 log_daemon_msg "Restarting $DESC" "$NAME"
141 do_stop
142 case "$?" in
143 0|1)
144 do_start
145 case "$?" in
146 0) log_end_msg 0 ;;
147 1) log_end_msg 1 ;; # Old process is still running
148 *) log_end_msg 1 ;; # Failed to start
149 esac
150 ;;
151 *)
152 # Failed to stop
153 log_end_msg 1
154 ;;
155 esac
156 ;;
157 *)
158 #echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
159 echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
160 exit 3
161 ;;
162 esac
163
164 :
7. 実行権限付与
作成した /etc/init.d/nginx
に実行権限を付与する。
1
|
|
8. サービス起動
サービスを使って Nginx を起動してみる。
1 2 |
|
もしくは、
1 2 |
|
9. 起動確認
ブラウザで http://<サーバアドレス or ホスト名>/
にアクセスしてみる。
“Welcome to nginx!” と以下5行くらい表示されば成功。
10. 自動起動設定
マシン起動時に自動で Nginx を起動させるには以下のようにする。(sysv-rc-conf
導入済みの場合)
1
|
|
もしくは、以下のようにする。
1
|
|
逆に、自動起動しないようにするには以下のようにする。(sysv-rc-conf
導入済みの場合)
1
|
|
あるいは、
1
|
|
ちなみに、update-rc.d
コマンドで設定する方法は最近は非推奨らしい。
11. 参考サイト
以上。