LoginSignup
1
1

More than 1 year has passed since last update.

Apacheが入っているLinuxにNginxをインストールする

Posted at

はじめに

LinuxサーバーとしてApacheがよく使われるのでインストールしてたのですが、最近Nginxを学ぶようになり、インストールしたいと思いました。
ところが、ApacheとNginxのポートが両方とも80で重なっていたので、どうしようかなと...考えた。
結論として、せっかく構築したApacheをアンインストールするのはもったいないので、Apacheのポートを変更して、Nginxをインストールしてしまえばいいと思いました。
そのときの、コマンドを順序にそって記載しておこうと思い、備忘録をかねて記事にしました。
ちなみに、今回試した環境はawsとなりますが、基本的なところはlinuxでも同じです。Nginxのインストール手順はaws特有のところがあるので、普通にlinuxにインストールする場合は以前の記事を見ていただければ参考になるかと思います。

Apacheのポート変更

つくづく思いますが、なんでApacheなのに実際はhttpdなんでしょうね。たぶん調べれば出てくるんでしょうが、いまだに知らないです。

Apacheのステータスを確認

systemctl status httpd

● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
  Drop-In: /usr/lib/systemd/system/httpd.service.d
           └─php-fpm.conf
   Active: active (running) since Sun 2022-10-23 11:04:46 UTC; 5s ago
     Docs: man:httpd.service(8)
 Main PID: 10074 (httpd)
   Status: "Processing requests..."
   CGroup: /system.slice/httpd.service
           ├─10074 /usr/sbin/httpd -DFOREGROUND
           ├─10075 /usr/sbin/httpd -DFOREGROUND
           ├─10076 /usr/sbin/httpd -DFOREGROUND
           ├─10077 /usr/sbin/httpd -DFOREGROUND
           ├─10078 /usr/sbin/httpd -DFOREGROUND
           └─10079 /usr/sbin/httpd -DFOREGROUND

Active(running)になっています。

Apacheのポートを確認

これではポートが分からないので、設定ファイルからポートを確認します。
httpd.confファイルのListenという項目で記述されているので、grepで抽出します。

grep ^Listen /etc/httpd/conf/httpd.conf
Listen 80

Apacheの設定を変更する

このhttpd.confをnanoで編集します。viでもいいですが、自分的にはnanoのほうが使いやすいと感じています。
この設定ファイルは結構長文なので、初めて見るという人にはListenを探すの大変かもしれません。

sudo nano /etc/httpd/conf/httpd.conf

#
# Listen: Allows you to bind Apache to specific IP addresses and/or
# ports, instead of the default. See also the <VirtualHost>
# directive.
#
# Change this to Listen on specific IP addresses as shown below to
# prevent Apache from glomming onto all bound IP addresses.
#
#Listen 12.34.56.78:80
Listen 80   <=== これを8080に書き換えて保存する

Apacheのポート設定を再確認

ちゃんと書き換えられているか確認します。
sudoで開いてなくて保存できないなんていう失敗ありますので、一応確認しておきましょう。

grep ^Listen /etc/httpd/conf/httpd.conf
Listen 8080

Apacheの設定を再読み込み

sudo systemctl reload httpd

これでApacheは8080ポートでアクセスされることになりました。

Nginxのインストール(aws linux2)

update

とりあえずlinuxをupdateしておきます。
何かインストールするときは、必ず先にこれをする癖をつけておくといいです。

sudo yum update -y

amazon-linux-extras検索

amazon-linux-extrasにrepositoryがある確認しておきます。
たぶんnginxが見つかるかと思います。

which amazon-linux-extras
/usr/bin/amazon-linux-extras
amazon-linux-extras | grep "nginx"
 38  nginx1                          available    [ =stable ]

amazon-linux-extrasでnginxを有効にする

nginxを使えるように設定します。
しばらくコマンドが表示されて、最後に以下の二行が表示されます。

sudo amazon-linux-extras enable nginx1

Now you can install:
 # yum clean metadata
 # yum install nginx

Nginxのインストール

表示されたコマンドを順番に実行すると、install nginxを実行した最後にComplete!が出ればOKです。

sudo yum clean metadata
sudo yum install nginx

Complete!

Nginxの確認

Nginxがちゃんとインストールされているか、インストールリストから確認します。

sudo yum list installed | grep nginx
nginx.x86_64                          1:1.20.0-2.amzn2.0.5           @amzn2extra-nginx1
nginx-filesystem.noarch               1:1.20.0-2.amzn2.0.5           @amzn2extra-nginx1

ちゃんと入ってますね。

Nginxの起動

Nginxを起動します。
ついでに、システム起動時にNginxが自動起動するようにしておきます。
webサーバーはこうしておくほうが楽です。

sudo systemctl start nginx
sudo systemctl enable nginx

Nginxのステータス確認

systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
  Drop-In: /usr/lib/systemd/system/nginx.service.d
           └─php-fpm.conf
   Active: active (running) since Sun 2022-10-23 11:20:25 UTC; 3s ago
  Process: 16147 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 16143 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 16142 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 16149 (nginx)
   CGroup: /system.slice/nginx.service
           ├─16149 nginx: master process /usr/sbin/nginx
           └─16150 nginx: worker process

Active: active (running) となっていればOK

webから確認

以下の画面が表示されていればOKです。
image.png
ここまで書いていて思ったのですが、Nginxとnginxはどっちが正規表現なんでしょうね。
なんとなくnginxと表現するほうが多いような気がしましたが、なんとなくNginxと自分は書きたくなってしまいます。

1
1
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
1
1