pupuk / blog

My New Blog. Record & Share. Focus on PHP, MySQL, Javascript and Golang.
MIT License
9 stars 2 forks source link

编译安装Nginx并添加第三方模块 #26

Open pupuk opened 4 years ago

pupuk commented 4 years ago

准备

安装

pupuk commented 4 years ago

添加第三方模块(如果需要)

pupuk commented 4 years ago

改nginx源码,伪装成其它Server(如果需要)

cd /usr/local/src/nginx-release-1.18.0 vim src/core/nginx.h vim src/http/ngx_http_header_filter_module.c vim src/http/ngx_http_special_response.c

我这里把nginx换成ngxv9, 若果只想隐藏版本号,在nginx配置文件中http,或者server里面里加入server_tokens off;就行 一般不建议修改fastcgi.conf,因为还是要让nginx的后端程序,知道它是nginx。我们隐藏nginx的目的也是对前端程序隐藏;

pupuk commented 4 years ago

继续编译安装

cd /usr/local/src/nginx-release-1.18.0/

auto/configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-stream_ssl_preread_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_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-http_auth_request_module --with-mail=dynamic --with-mail_ssl_module --with-pcre --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 -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E' --add-module=/usr/local/src/echo-nginx-module-0.61

主要就是在原来的配置后面追加--add-module=/usr/local/src/echo-nginx-module-0.61 如果遇到错误,则可以忽略一些配置。 我这里遇到错误,我看可以忽略这个 image

用这个

auto/configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-stream_ssl_preread_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_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-http_auth_request_module --with-mail=dynamic --with-mail_ssl_module --with-pcre --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 -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --add-module=/usr/local/src/echo-nginx-module-0.61

还遇到报错: image aio是一个异步IO库,nginx可以通过配置,让aio和sendfile两种机制结合起来,对大文件用aio,可节省cpu,对小文件,采用sendfile,可减少拷贝。所以我决定当时不能忽略参数,装libaio yum insatll libaio -y yum insatll libaio-devel -y yum groupinstall -y 'Development Tools' 通过. 再次遇到错误 image yum -y install libxml2 libxml2-dev && yum -y install libxslt-devel

再报错: auto/configure: error: the HTTP image filter module requires the GD library. You can either do not enable the module or install the libraries. 解决: yum -y install gd-devel

还报错: auto/configure: error: perl module ExtUtils::Embed is required 解决: yum -y install perl-devel perl-ExtUtils-Embed

还报错: auto/configure: error: the Google perftools module requires the Google perftools library. You can either do not enable the module or install the library. 解决: yum -y install GeoIP GeoIP-devel GeoIP-data

还报错: auto/configure: error: the Google perftools module requires the Google perftools library. You can either do not enable the module or install the library. 解决: yum -y install gperftools gperftools-devel

终于通过: image 遇到一个warning,可以忽略,也可以改参数,去掉--with-ipv6,重新 auto/configure

重头戏: make make install

其中第一个make,可以用make -j 6, -j 参数的意义是并行编译,在多核的情况下可以提升编译速度。我的虚拟机是6核,可以通过 cat /proc/cpuinfo|grep processo来查看。

image 安装成功了。

which nginx        #
nginx              # 启动Nginx
nginx -t           # 验证配置文件是正确
nginx -s reload    # 重启Nginx
nginx -s stop      # 停止Nginx
nginx -v           # 查看版本
nginx -V           # 查看版本,看编译参数
pupuk commented 4 years ago

使用systemd来管理nginx

vim /usr/lib/systemd/system/nginx.service 添加内容如下:

[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target

然后就可以systemd的指令来管理nginx了。systemctl start[stop][reload][restart][status][enable][disable][is-enabled] nginx.service

pupuk commented 4 years ago

验证echo模块

nginx配置 image

请求试试 image

echo模块生效了,妈妈再也不用担心各种谢location,if的调试了