魔法使いの卵

WEBエンジニアの卵の成長記録

CentOS7にNginxをいれてみた(手動ver)

必要になるもの

  • GCC
    • コンパイラーツール(基本的にデフォルトで入っている)
  • PCRE
    • Nginxのコンパイルで利用するPerl互換正規表現ライブラリ
    • ライブラリとソースが必要
  • zlib
    • モジュールの圧縮に必要になる
    • ライブラリとソースが必要
  • OpenSSL
    • セキュアなwebページを実現するためにOpenSSLが必要
    • ライブラリと開発パッケージをインストールする必要有り

必要なものをいれていく

  • CentOSの場合
sudo yum install -y gcc
sudo yum install -y pcre pcre-devel
sudo yum install -y zlib zlib-devel
sudo yum install -y openssl openssl-devel

Nginx用ユーザの作成

sudo useradd --shell /sbin/nologin nginx

http://qiita.com/nenokido2000/items/3cbb76dac2b9940f339e

Nginxを手動でHTTPサーバとして導入する

Nginxを用意する

  • 現時点での最新版をダウンロードすることにした
cd /usr/local/src
sudo wget http://nginx.org/download/nginx-1.9.6.tar.gz
sudo tar zxf nginx-1.9.6.tar.gz
  • インストールする
cd nginx-1.9.6
sudo ./configure  --user=www-data --group=www-data --with-http_ssl_module --with-http_realip_module
sudo make
sudo make install
  • インストール先 /usr/local/nginx/

利用するUserの指定を設定する

  • sudo vim /usr/local/nginx/conf/nginx.conf
  • 1行目を以下のように変更する
1 user nginx;

initスクリプト作成する

  • sudo vim /etc/init.d/nginx
#! /bin/sh
# Author: Ryan Norbauer http://norbauerinc.com
# Modified: Geoffrey Grosenbach http://topfunky.com
# Modified: Clement NEDELCU
set -e
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="nginx daemon"
NAME=nginx
DAEMON=/usr/local/nginx/sbin/$NAME
SCRIPTNAME=/etc/init.d/$NAME

# デーモンファイルが見つからなければ、スクリプトを終了する
test -x $DAEMON || exit 0

d_start() {
  $DAEMON || echo -n " already running"
}

d_stop() {
  $DAEMON -s quit || echo -n " not running"
}

d_reload() {
  $DAEMON -s reload || echo -n " could not reload"
}

case "$1" in
  start)
    echo -n "Starting $DESC: $NAME"
    d_start
    echo "."
  ;;
  stop)
    echo -n "Stopping $DESC: $NAME"
    d_stop
    echo "."
  ;;
  reload)
    echo -n "Reloading $DESC: configuration..."
    d_reload
    echo "reloaded."
  ;;
  restart)
    echo -n "Restarting $DESC: $NAME"
    d_stop
    # 再起動の前に2秒スリープする。Nginxデーモンが穏便に終了するための時間を与えるのである。
    sleep 2
    d_start
    echo "."
  ;;
  *)
    echo "Usage: $SCRIPTNAME {start|stop|restart|reload}" >&2
    exit 3
  ;;
esac

exit 0

権限の変更を行う

sudo chmod +x /etc/init.d/nginx

パスを通す

echo 'export PATH="/usr/local/nginx/sbin:$PATH"'  >> ~/.bashrc

source ~/.bashrc

起動する

sudo /etc/init.d/nginx start

起動しているか確認する

  • sudo systemctl status nginx
nginx.service - SYSV: NGINX is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3 proxy server
   Loaded: loaded (/etc/rc.d/init.d/nginx)
   Active: activating (start) since 木 2015-11-12 11:45:31 EST; 1min 59s ago
  Process: 10132 ExecStart=/etc/rc.d/init.d/nginx start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/nginx.service
           ├─10170 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
           └─10172 nginx: worker process

11月 12 11:45:31 centos70 useradd[10142]: new group: name=www-data, GID=1002
11月 12 11:45:31 centos70 useradd[10142]: new user: name=www-data, UID=1002, GID=1002, home=/home/www-data, shell=/bin/nologin
11月 12 11:45:31 centos70 nginx[10132]: Starting nginx: [  OK  ]
11月 12 11:45:31 centos70 systemd[1]: PID file /var/run/nginx.pid not readable (yet?) after start.

自動起動する

sudo chkconfig nginx on

firewalldの設定

  • firewalld停止
sudo systemctl stop firewalld
  • firewalldのステータス
sudo systemctl status firewalld
  • firewalld自動起動停止
sudo systemctl disable firewalld
  • 設定確認
sudo systemctl is-enabled firewalld
disabled

Nginxの動作確認

  • curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

知っておくといいかもしれないこと

  • パス関連のオプション
  • 必要ファイル関連のオプション
  • モジュール関連のオプション
  • デフォルトで無効なモジュール
  • その他のオプション

おもったこと

  • 絶対yumつかって入れたほうが楽
  • その場合は最新の安定版が入る
  • 最新版がどうしても使いたい!!っていう思いがないかぎりはyum一択だなと思ってる

参考元:

Nginxインストールメモ - Qiita