洋食の日記

「だ・である」調ではなく「です・ます」調で書きはじめれば良かったなと後悔してる人のブログです

ポート番号が被らなければNginxとApache2を共存できる

Debian GNU/LinuxにApache2を入れてWebサーバーを構築している。Nginxを試してみたいので、共存させることを試みた。 まずは、インストールする。ここで、先にApache2を止めておかないと、Nginxのインストールがコケる。

$ sudo systemctl stop apache2.service
$ sudo apt-get install nginx
$ sudo systemctl stop nginx.service
$ sudo systemctl start apache2.service

Apache2が立ち上がっている状態で、Nginxを立ち上げようとすると当然コケる。

$ sudo systemctl start nginx.service
Job for nginx.service failed because the control process exited with error code.
See "systemctl status nginx.service" and "journalctl -xe" for details.

Nginxの設定ファイルを変更し、ポート番号を80から5000(適当に空いているポート番号)にする。

$ vim /etc/nginx/sites-enabled/default
...
# Default server configuration
#
server {
  #listen 80 default_server;
  #listen [::]:80 default_server;
  listen 5000 default_server;
  listen [::]:5000 default_server;
...
  # Add index.php to the list if you are using PHP
  #index index.html index.htm index.nginx-debian.html;
  index index.nginx-debian.html index.html index.htm;
...

ここで、index.nginx-debina.htmlを手前にもってきたのは、/var/www/html以下にApache2のindex.htmlと、Nginxのindex.nginx-debian.htmlが配置されているため。これで、Nginxが問題なく立ち上がる。

$ sudo systemctl start nginx.service

5000番にアクセスするとNginxのWelcomeページが表示される。

$ links http://localhost:5000/
Welcome to nginx on Debian!
...

色々と試してみて、最終的にはNginxに完全移行したい。