LoginSignup
6
9

More than 5 years have passed since last update.

[Ubuntu] nginx/apache が共に入ってるサーバで apache が起動しない件

Last updated at Posted at 2015-03-04

まぁ当たり前と言えば当たり前の事なんですが。
apache/nginx が共存しているサーバで、 apache を(再)起動しようとしてもエラーになって立ち上がらない事があります。

$ sudo service apache2 restart
 * Restarting web server apache2
(98)Address already in use: make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down
Unable to open logs
Action 'start' failed.
The Apache error log may have more information.
   ...fail!

エラーに書いてある通り、socket が既に使用されていて使えない、という状態ですね。

$ sudo netstat -tanp | grep LISTEN
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      849/nginx #使用中!
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      693/sshd
tcp6       0      0 :::22                   :::*                    LISTEN      693/sshd

これの解決方法としては

  • (nginx が不要なら) nginx を停止して apache を起動
  • (nginx/apache 共に必要なら) nginx と apache で LISTEN するポート番号を変更する(どちらかを80番に残して、もう片方はウェルノウンポート以外で起動)

主にこの2パターンだと思います。
後者は追記するとして前者に関して。

nginx を停止して apache を起動

単純に nginx を停止して、apache すれば良いのですが、それだとサーバを再起動した時に再び同じ状況が発生します。
apache よりも nginx の方が先に起動されるから、ですね。

$ /sbin/runlevel #現在の run level の確認
N 2
$ ll /etc/rc2.d/ #/etc/rcN.d/ の中身を確認
total 12
drwxr-xr-x  2 root root 4096 Sep 25 17:36 ./
drwxr-xr-x 99 root root 4096 Mar  3 22:36 ../
-rw-r--r--  1 root root  677 Jul 27  2012 README
lrwxrwxrwx  1 root root   15 Mar 23  2014 S20nginx -> ../init.d/nginx*
..(中略)..
lrwxrwxrwx  1 root root   17 Jul  3  2014 S91apache2 -> ../init.d/apache2*
..(後略)..

読み方としては、

頭文字 意味 説明
S Start (該当ランレベルで起動時に)プログラム開始
K Kill (該当ランレベルで起動時に)プログラム停止

Sの後に続く数字は$実行の順番$なので、 S20 の nginx の方が S91 の apache2 よりも先に実行されます。

変更方法としては、該当のsymlink を rename/remove すればOK。

$ sudo ln -snf /etc/init.d/nginx /etc/rc2.d/.S20nginx
or
$ sudo rm /etc/rc2.d/S20nginx

以上。

参考

6
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
9