LoginSignup
3
4

More than 3 years have passed since last update.

CentOS7へnginxのインストールと設定

Last updated at Posted at 2018-07-20

yumでインストール

とりあえずWebサーバとして使う場合、yumで簡単にインストールできる。

インストール
yum install nginx
確認
nginx -v
設定ファイルの場所
cd /etc/nginx
起動
systemctl start nginx
終了
systemctl stop nginx
OS起動時に起動するようにする
systemctl enable nginx
OS起動時に起動しないようにする
systemctl disable nginx

yumでインストール(最新)

上記の方法だと古いバージョンのnginxが入るが、公式のリポジトリを使うことで最新版のnginxを入れることができる。

リポジトリの追加

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

以下を書き込む

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

nginxをインストール

sudo yum install nginx

バージョンの確認

nginx -v

自動起動設定

sudo systemctl enable nginx

起動

sudo systemctl start nginx

ソースからインストール

特別なモジュールをインストールする場合はソースからインストール
nginxでは動的ライブラリはなく、静的ライブラリのため、必要な外部モジュールがあればソースからインストールする必要がある。

今回の場合はrtmpモジュールを入れた

ダウンロード
wget http://nginx.org/download/nginx-1.13.5.tar.gz
tar -zxvf nginx-1.13.5.tar.gz
rtmpモジュールの取得
git clone https://github.com/arut/nginx-rtmp-module
cd nginx-1.13.5
./configure --with-http_ssl_module --with-http_realip_module --with-http_v2_module --add-module=../nginx-rtmp-module-master
make
make install
起動
sudo /usr/local/nginx/sbin/nginx
設定ファイル
vi /usr/local/nginx/conf/nginx.conf
再読み込み
/usr/local/nginx/sbin/nginx -s reload
終了
/usr/local/nginx/sbin/nginx -s stop
自動起動設定
vi /usr/lib/systemd/system/nginx.service
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
RemainAfterExit=yes
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable nginx
3
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
3
4