超簡単!! CentOS7にNginXをインストールする

はじめに

近年急速にシェアを拡大しているWebサーバー「Nginx(エンジンエックス)」ですが、画像ファイルといった静的コンテンツを高速で配信し、消費メモリが少なく、リバースProxyやロードバランサーといった機能も有した軽量Webサーバーです。

このNginXをCentOS7にインストールする方法の備忘録です。

事前準備

NginXをインストールするまえに、以下の確認を行ます。

  • Firewallの設定
  • リポジトリの設定

 

Firewallの設定

Firewall-cmdを利用し、ポートが空いているかどうかを確認します。

$ firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: eth0
  sources: 
  services: dhcpv6-client ssh http
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules:

ここで、servicesに「http」がない場合は、下記を実行し追加してください。

$ firewall-cmd --add-service=http --zone=public --permanent
$ firewall-cmd --reload

 

リポジトリの追加

公式サイトには以下の様に書かれています。

Before you install nginx for the first time on a new machine, you need to set up the nginx packages repository.

Afterward, you can install and update nginx from the repository.

インストール前に、nginxのリポジトリパッケージを設定する必要があるとのこと。

 

$ vi /etc/yum.repos.d/nginx.repo

作成したnginx.repoファイルに以下を記載する

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

追加されているかどうかを確認します。

$ ls -l /etc/yum.repos.d/
合計 36
-rw-r--r--. 1 root root 1664  8月 13  2018 CentOS-Base.repo
-rw-r--r--. 1 root root 1309  8月 13  2018 CentOS-CR.repo
-rw-r--r--. 1 root root  649  8月 13  2018 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root  630  8月 13  2018 CentOS-Media.repo
-rw-r--r--. 1 root root 1331  8月 13  2018 CentOS-Sources.repo
-rw-r--r--. 1 root root 4768  8月 13  2018 CentOS-Vault.repo
-rw-r--r--. 1 root root  314  8月 13  2018 CentOS-fasttrack.repo
-rw-r--r--. 1 root root   99  5月  3 08:38 nginx.repo

追加されていましたね。

これで準備は完了です。

 

NginXのインストール

インストールは簡単です。

$ yum install nginx

読み込んだプラグイン:fastestmirror, langpacks
・・・
インストール:
  nginx.x86_64 1:1.18.0-1.el7.ngx

完了しました!

これで、インストールは完了です。

起動時に、自動でサービスが起動する様に設定しておきます。

$ systemctl enable nginx

これで、次回起動時もサービスが起動した状態となります。

実際に起動しているかどうか確認してみます。

URLに 「http://serverip/ 」でアクセスします。

 

問題なく起動できていることを確認しました。

 

補足(起動・停止)

NginX起動

$ systemctl start nginx

NginX停止

$ systemctl stop nginx
タイトルとURLをコピーしました