使用nginx-build在LAMP环境中构建nginx

我已经建立了一个包含Apache2 + MariaDB + PHP7的环境,想要将nginx配置为一个Web服务器并显示在屏幕上。
尽管yum install nginx很方便,但由于没有安装ngx-cache-purge和ngx_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中下载已构建的二进制文件。

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-purge 和 ngx_pagespeed 这两种方式的话。

$ 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

如果无法下载库并且导致崩溃的情况发生,将处理分开将会获得良好的结果。

$ ~/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模块错误,

./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模块的错误时,

./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模块的错误,

./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图像过滤器模块出现错误时,

./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模块的错误,请

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

$ sudo yum -y install perl-ExtUtils-Embed

如果遇到GeoIP模块的错误,

./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

如果遇到Google perftools模块的错误,请处理。

./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

服务注册

$ 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
- 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

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

如果在浏览器中访问本地主机,应该可以运行位于/var/www/html的index.php。

我们来试试。谢谢。 . .)

请参考

git -> git

    • nginx-build

 

    • ngx_cache_purge

 

    • ngx_pagespeed

 

    ngx_pagespeed document

Nginx是一个开源的高性能Web服务器和反向代理服务器。

    • nginx事前準備からサービス起動までのメモ

 

    • nginx にモジュールを追加してアップデート (apt-get から ソースコードのインストールに変更)

 

    • Nginxのコマンドについて(手動ver)

 

    • さくらVPSに最新Nginx1.9系をインストールした話

 

    • NGINX systemd service file

 

    nginxのコンパイルメモ

PHP-FPM

    • nginx と PHP-FPM の仕組みをちゃんと理解しながら PHP の実行環境を構築する

 

    CentOS7 + Nginx + PHP-FPM でPHPを実行する環境を整える
广告
将在 10 秒后关闭
bannerAds