Edited at

LAMP環境にnginx-buildを使ってnginxを構築

More than 1 year has passed since last update.

Apache2 + MariaDB + PHP7 を構築済みの環境に、 nginx を構築して Webサーバーとして画面に表示したいと思います。

yum install nginxは便利ですが ngx-cache-purgengx_pagespeed 等のサードパーティのモジュールが入ってないので、nginx-build を使用します。


nginx-buildを導入

nginxユーザー作成

$ useradd -s /sbin/nologin nginx 

キャッシュ用ディレクトリを追加

$ sudo mkdir /var/cache/nginx

$ sudo chown nginx:nginx /var/cache/nginx

nginx-buildを作業するディレクトリを適当に作成

$ mkdir -p ~/nginx-build

$ cd ~/nginx-build

Go言語がない場合はgitからビルド済みのバイナリをDLします

Go言語環境有り

$ go get -u github.com/cubicdaiya/nginx-build

Go言語環境無し
$ wget https://github.com/cubicdaiya/nginx-build/releases/download/v0.10.0/nginx-build-linux-amd64-0.10.0.tar.gz
$ tar zxvf nginx-build-linux-amd64-0.10.0.tar.gz
$ rm -f nginx-build-linux-amd64-0.10.0.tar.gz


サードパーティモジュール用の設定ファイルを追加

今回は ngx-cache-purgengx_pagespeed の2種類を使用する場合

$ vi ~/nginx-build/modules3rd.ini

--

[ngx_cache_purge]
form=git
url=https://github.com/FRiCKLE/ngx_cache_purge.git
rev=v2.3

[ngx_pagespeed]
from=git
url=https://github.com/pagespeed/ngx_pagespeed.git
rev=v1.12.34.2-stable


configureスクリプトを追加

中身は各々変更してください​(↓のは yum版 でインストールするモジュールとほぼ同じです)

$ vi ~/nginx-build/configure.sh

--
#!/bin/sh

./configure \
--user=nginx \
--group=nginx \
--with-pcre=$HOME/nginx-build/work/nginx/1.13.1/pcre-8.40 \
--with-openssl=$HOME/nginx-build/work/nginx/1.13.1/openssl-1.0.2l \
--with-zlib=$HOME/nginx-build/work/nginx/1.13.1/zlib-1.2.11 \
--add-module=$HOME/nginx-build/work/nginx/1.13.1/ngx_cache_purge \
--with-file-aio \
--with-ipv6 \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_xslt_module=dynamic \
--with-http_image_filter_module=dynamic \
--with-http_geoip_module=dynamic \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_degradation_module \
--with-http_slice_module \
--with-http_stub_status_module \
--with-http_perl_module=dynamic \
--with-mail=dynamic \
--with-mail_ssl_module \
--with-pcre-jit \
--with-stream=dynamic \
--with-stream_ssl_module \
--with-google_perftools_module \
--with-debug \
--with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic' \
--with-ld-opt=-Wl,-E \


依存ライブラリの追加とビルド

$ ~/nginx-build/nginx-build \

-d work \
-pcre \
-zlib \
-openssl \
-m ~/nginx-build/modules3rd.ini
-c ~/nginx-build/configure.sh

↑ライブラリDL出来ずにこけてしまう場合は、処理を分けるとうまくいきます

$ ~/nginx-build/nginx-build -d work -pcre -zlib -openssl               ← 依存ライブラリ

$ ~/nginx-build/nginx-build -d work -m ~/nginx-build/modules3rd.ini ← サードモジュール
$ ~/nginx-build/nginx-build -d work -c ~/nginx-build/configure.sh ← コンフィグ

HTTP rewrite moduleエラーが出たら、

./configure: error: the HTTP rewrite module requires the PCRE library.

You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

$ sudo yum -y install pcre-devel

HTTP gzip moduleのエラーが出たら、

./configure: error: the HTTP gzip module requires the zlib library.

You can either disable th module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.

$ sudo yum -y install zlib-devel

HTTP XSLT moduleのエラーが出たら、

./configure: error: the HTTP XSLT module requires the libxml2/libxslt

libraries. You can either do not enable the module or install the libraries.

$ sudo yum -y install libxslt-devel

HTTP image filter moduleのエラーが出たら、

./configure: error: the HTTP image filter module requires the GD library.

You can either do not enable the module or install the libraries.

$ sudo yum -y install gd-devel

perl moduleのエラーが出たら、

./configure: error: perl module ExtUtils::Embed is required

$ sudo yum -y install perl-ExtUtils-Embed

the GeoIP moduleのエラーが出たら、

./configure: error: the GeoIP module requires the GeoIP library.

You can either do not enable the module or install the library.

$ sudo yum -y install GeoIP-devel

the Google perftools moduleのエラーが出たら、

./configure: error: the Google perftools module requires the Google perftools

library. You can either do not enable the module or install the library.

$ sudo yum -y install gperftools-devel

上記モジュール不足分を一気にインスコ

$ sudo yum -y install pcre-devel zlib-devel gd-devel libxslt-devel perl-ExtUtils-Embed GeoIP-devel gperftools-devel

ngx_pagespeedをサードパーティに追加していると、下記エラーが出るので、

  You need to separately download the pagespeed library:

$ cd /home/vagrant/nginx-build/work/nginx/1.13.1/ngx_pagespeed
$ wget https://dl.google.com/dl/page-speed/psol/1.12.34.2-x64.tar.gz
$ tar -xzvf 1.12.34.2-x64.tar.gz # expands to psol/

↑を指示通り実行して、再度ビルド

$ ~/nginx-build/nginx-build -d work -c ~/nginx-build/configure.sh

$ Complete building nginx!

nginx version: nginx/1.13.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)
built with OpenSSL 1.0.2l 25 May 2017
TLS SNI support enabled
configure arguments: --user=nginx --group=nginx --with-pcre=/home/vagrant/nginx-build/work/nginx/1.13.1/pcre-8.40 --with-openssl=/home/vagrant/nginx-build/work/nginx/1.13.1/openssl-1.0.2l --with-zlib=/home/vagrant/nginx-build/work/nginx/1.13.1/zlib-1.2.11 --add-module=/home/vagrant/nginx-build/work/nginx/1.13.1/ngx_cache_purge --add-module=/home/vagrant/nginx-build/work/nginx/1.13.1/ngx_pagespeed --with-http_ssl_module --with-http_v2_module --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_stub_status_module --with-http_sub_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module

$ Enter the following command for install nginx.

$ cd work/nginx/1.13.1/nginx-1.13.1
$ sudo make install

↑を指示通りmakeを実行すると、nginx-buildビルド成功。

/usr/local/nginxにnginxディレクトリ追加されています

$ ls -l /usr/local/nginx

エラーチェック

$ sudo /usr/local/nginx/sbin/nginx -t

モジュールをビルドしたかチェック

$ sudo /usr/local/nginx/sbin/nginx -V


Service登録

$ sudo vi /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
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

サービスの稼働状況を確認する

$ sudo systemctl status nginx.service

● nginx.service - The NGINX HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: inactive (dead)


PHP-FPMのインストール

$ sudo yum -y install php-fpm


PHP-FPMの設定を変更

/etc/php-fpm.d/www.conf 内のuserとgroupをnginxに変更

$ sudo vi /etc/php-fpm.d/www.conf


/etc/php-fpm.d/www.conf

- user = apache

- group = apache
+ user = nginx
+ group = nginx


PHP-FPMを自動起動・起動(再起動)します

$ sudo systemctl enable php-fpm

$ sudo systemctl restart php-fpm


Nginxの設定を変更

$ sudo vi /usr/local/nginx/conf/nginx.conf


/usr/local/nginx/conf/nginx.conf


user nginx;
worker_processes 1;

error_log /var/log/nginx/error.log warn;
pid /usr/local/nginx/logs/nginx.pid;

events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main;

sendfile on;
#tcp_nopush on;

keepalive_timeout 65;

#gzip on;

proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=czone:4m max_size=50m inactive=120m;
proxy_cache czone;
#proxy_temp_path /var/cache/nginx_tmp;

include /usr/local/nginx/conf.d/*.conf;
}


$ sudo mkdir /usr/local/nginx/conf.d

$ sudo vi /usr/local/nginx/conf.d/default.conf


/etc/nginx/conf.d/default.conf

server {

listen 80;
server_name localhost;

location / {
root /var/www/html;
index index.html index.htm index.php;
}

location ~ \.php$ {
root /var/www/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

location ~ /purge(/.*) {
allow 127.0.0.1;
deny all;
proxy_cache_purge czone "$scheme://$host$1$is_args$args";
}
}



Apache / nginxの起動を設定

*Apacheが起動中なら

sudo systemctl stop httpd.service
sudo systemctl disable httpd.service

sudo systemctl enable nginx.service
sudo systemctl restart nginx.service

ブラウザでlocalhostにアクセスすれば、/var/www/htmlに置いているindex.phpが動いているはずです。

Let's try. Thanks.


参考


git


nginx


php-fpm