1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

とにかくNginxをインストールする (AWS Amazon Linux 2, CentOS7対応, 公式 ref.あり)

Last updated at Posted at 2020-09-01

環境

  • AWS EC2インスタンスにNginxをインストールすることを想定
  • OS: Amazon Linux2もしくはCentOS7 (RHEL7系)*後から知りました
  • Nginx: 1.18.0 (latest, stable, 2020.8.31現在)

Nginxをインストール

Amazon Linux 2 の場合

amazon-linux-extrasリポジトリからインストールします

インストール可能パッケージ一覧

$ amazon-linux-extras

...

 38  nginx1=latest            enabled      [ =stable ] #here
 39  ruby2.6                  available    [ =2.6  =stable ]
 40  mock                     available    [ =stable ]
 41  postgresql11             available    [ =11  =stable ]
 42  php7.4                   available    [ =stable ]
 43  livepatch                available    [ =stable ]
 44  python3.8                available    [ =stable ]
 45  haproxy2                 available    [ =stable ]

Install

$ sudo amazon-linux-extras install nginx1

↓共通作業へ↓

参考

Amazon Linux 2 EC2 インスタンスに Extras Library からソフトウェアをインストールする


## CentOSの場合
公式リポジトリを追加してそこからインストールします

## 公式リポジトリを追加

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


以下追記

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true


### Install

sudo yum install nginx


### 参考
[nginx: Linux packages](http://nginx.org/en/linux_packages.html)

# 以下共通作業

## Run & Auto-Run enable & Show status
Nginxを起動し、自動起動有効化、ステータス表示
(CentOSは7以降を想定しています、6以前だとコマンドが違うようです)

sudo systemctl start nginx && sudo systemctl enable nginx && systemctl status nginx


## Backup nginx.config
設定ファイルをバックアップします
**とても重要です、初めてNginxに触れる方は特に**

`/etc/nginx/nginx.conf`のバックアップ

sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.back


**`/etc/nginx/conf.d/default.conf`がある場合(新しめのversion 1.18.0で確認)**
`/etc/nginx/conf.d/default.conf`のバックアップ
**確認できる範囲で、Nginx version1.12.0ではdefault.confの内容がnginx.confに記述されていましたが1.18.0ではconf.d/default.confとして独立したディレクティブとして存在しています**

sudo cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.back


# よく使うNginxコマンド

## 起動・ステータス表示・終了
コマンドはAmazon Linux 2, CentOS7共通で使用できます
Amazon Linux 2のベースはRHEL7で、これはCentOS7と同じだそうです
[amazon linux は何系のディストリビューションに対応する? - スタック・オーバーフロー](https://ja.stackoverflow.com/questions/50113/amazon-linux-%E3%81%AF%E4%BD%95%E7%B3%BB%E3%81%AE%E3%83%87%E3%82%A3%E3%82%B9%E3%83%88%E3%83%AA%E3%83%93%E3%83%A5%E3%83%BC%E3%82%B7%E3%83%A7%E3%83%B3%E3%81%AB%E5%AF%BE%E5%BF%9C%E3%81%99%E3%82%8B)

$ systemctl start nginx

$ systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
...

$ systemctl stop nginx


## 設定を再読み込み(使用頻度高)

$ sudo systemctl reload nginx


## 自動起動有効化

sudo systemctl enable nginx



 

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?