ネットワーク > Webサーバー > NginxをUbuntuにインストール
NginxをUbuntuにインストールする方法のまとめです。
※CentOSにインストールする場合はNginxのページを参照してください。
リンク †
- 公式サイト(英語)
https://nginx.org/ - wiki(英語)
https://www.nginx.com/resources/wiki/ - forum(英語)
http://forum.nginx.org/ - Nginx情報(日本語訳)
http://mogile.web.fc2.com/nginx/
http://mogile.web.fc2.com/nginx/admin-guide/
参考書 †
ハイパフォーマンスHTTPサーバ Nginx入門 Clement Nedelcu アスキー・メディアワークス 2011-04-21 ¥ 3240 |
Nginxのインストール(Ubuntu編) †
自動でインストール †
古いバージョンでも良ければ、aptコマンドで自動的にインストールすることも可能。
$ sudo apt install nginx
バージョンの確認 †
$ nginx -V nginx version: nginx/1.14.0 (Ubuntu) built with OpenSSL 1.1.0g 2 Nov 2017 TLS SNI support enabled configure arguments: --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-FIJPpj/nginx-1.14.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_geoip_module=dynamic --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module --with-mail=dynamic --with-mail_ssl_module
Nginxの自動起動 †
$ sudo /lib/systemd/systemd-sysv-install enable nginx // 有効化 $ sudo /lib/systemd/systemd-sysv-install disable nginx // 無効化
手動でインストール †
NginxをUbuntuにソースコードからコンパイルして、手動でインストールする手順のまとめ
- (参考)nginx ソースファイルからのインストール(Ubuntu) https://gakumon.tech/nginx/nginx_install_src_ubuntu.html
ログイン †
Ubuntuにログインします。
ダウンロード †
Nginx最新安定版(stable)を公式サイトからダウンロードします。
- Nginx最新安定版のバージョンを公式サイトで確認します。
https://nginx.org/en/download.html(例) Stable version nginx-1.14.2
- ビルド用ディレクトリ(/usr/local/src)に移動します。cf. [Linux] /usr/local/の使い方まとめ · DQNEO起業日記 http://dqn.sakusakutto.jp/2011/08/linux_usrlocal.html
$ cd /usr/local/src
- 圧縮ファイルをダウンロードします。
$ wget https://nginx.org/download/nginx-1.14.2.tar.gz
(wgetコマンドがインストールされていない場合は、「$ sudo apt-get install wget」) - ダウンロードしたファイルを解凍します。
$ tar zxf nginx-1.14.2.tar.gz
- カレントディレクトリを移動します。
$ cd nginx-1.14.2
コンパイル †
- Nginx用のシステムユーザーを作成します。
$ sudo adduser --system --no-create-home --shell /bin/false --group --disabled-login nginx
→リモートログイン及びログインの不可能なシステムユーザnginxを作成Adding system user `nginx' (UID 111) ... Adding new group `nginx' (GID 116) ... Adding new user `nginx' (UID 111) with group `nginx' ... Not creating home directory `/home/nginx'.
のように処理結果が表示された。 - gpasswdコマンドで作業者アカウントvagrantをnginxグループに追加します。
$ gpasswd -a vagrant nginx
ユーザーvagrantをグループnginxに追加 - Nginxに必要なライブラリをインストールします。
(例では、必須ライブラリの他にHTTPSのコンテンツを処理するOpenSSLライブラリもインストールしています。)- gcc(GNU Compiler Collection)のインストール
$ sudo apt install build-essential
- pcre?(Perl Compatible Regular Expressions。Perl5互換の正規表現をC言語で実装したライブラリ)のインストール
$ sudo apt install libpcre3 libpcre3-dev
- zlib?(データの圧縮および伸張を行うためのフリーのライブラリ)のインストール
$ sudo apt install zlib1g zlib1g-dev
- OpenSSL(暗号化ライブラリ)のインストール。Nginxでは1.1系が必要だがaptコマンドでは1.0系になるので手動でビルドする。最新のバージョンは https://www.openssl.org/source/ で確認すること。
$ cd /usr/local/src $ sudo wget https://www.openssl.org/source/openssl-1.1.1a.tar.gz $ sudo tar zxf openssl-1.1.1a.tar.gz $ cd openssl-1.1.1a $ sudo ./config --prefix=/usr/local/openssl --openssldir=/usr/local/openssl $ sudo make $ sudo make test $ sudo make install $ openssl version
これでNginxをソースからビルドする準備が整いました。
- gcc(GNU Compiler Collection)のインストール
- 使用するオプションを付けてconfigureを実行します。
$ cd /usr/local/src/nginx-1.14.2 $ sudo ./configure --prefix=/usr/local/nginx-1.14.2 --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module
- コンパイルして、インストールします。
$ sudo make $ sudo make test $ sudo make install