Debian10 編譯NGINX

warning: 这篇文章距离上次修改已过871天,其中的内容可能已经有所变动。

安裝部份依賴。
更新軟件

apt update && apt upgrade -y
apt install build-depgcc
apt install build-essential
apt install -y perl libperl-dev libgd3 libgd-dev libgeoip1 libgeoip-dev geoip-bin libxml2 libxml2-dev libxslt1.1 libxslt1-dev libsctp-dev

根據需求下載并解壓源碼,注意版本,可以更換成最新版本號,這是我編譯時的最新版本。
其中ngx-fancyindex是一個目錄索引的模塊,可以讓目錄索引介面更好看,並且有多重主題可用。

wget https://nginx.org/download/nginx-1.20.2.tar.gz && tar zxvf nginx-1.20.2.tar.gz
//wget https://ftp.pcre.org/pub/pcre/pcre-8.45.tar.gz && tar xzvf pcre-8.45.tar.gz//(下載地址已經改變了,請到https://pcre.org/找下載鏈接)
wget https://www.openssl.org/source/openssl-1.1.1l.tar.gz && tar xzvf openssl-1.1.1l.tar.gz
//wget https://github.com/aperezdc/ngx-fancyindex/releases/download/v0.5.2/ngx-fancyindex-0.5.2.tar.xz && tar xzvf ngx-fancyindex-0.5.2.tar.xz 
git clone https://github.com/aperezdc/ngx-fancyindex.git
wget https://www.zlib.net/zlib-1.2.11.tar.gz && tar xzvf zlib-1.2.11.tar.gz
wget https://github.com/gperftools/gperftools/releases/download/gperftools-2.9.1/gperftools-2.9.1.tar.gz && tar xzvf gperftools-2.9.1.tar.gz

下载ngx_brotli
git clone https://github.com/google/ngx_brotli
cd ngx_brotli && git submodule update --init

進入nginx源碼文件夾

 cd /root/nginx/nginx-1.19.8

配置Nginx

 ./configure --prefix=/etc/nginx --with-openssl=/root/nginx/openssl-1.1.1l --with-openssl-opt="enable-ec_nistp_64_gcc_128 shared threads zlib-dynamic sctp" --with-pcre=/root/nginx/pcre-8.45 --with-pcre-jit --with-zlib=/root/nginx/zlib-1.2.11 --add-module=/root/nginx/ngx-fancyindex --with-mail=dynamic --with-mail_ssl_module --with-stream=dynamic --with-stream_ssl_module --with-stream_realip_module --with-stream_geoip_module=dynamic --with-stream_ssl_preread_module --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_auth_request_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-pcre --with-libatomic --with-compat --with-cpp_test_module --with-google_perftools_module --with-file-aio --with-threads --with-poll_module --with-select_module --with-cc-opt="-Wno-error -g0 -O3" --add-module=/root/nginx/ngx_brotli

nginx-1.19.5 >> google_perftools >> libunwind 错误提示:

./configure: error: the Google perftool module requires the Google perftools
library. You can either do not enable the module or install the library.

先安装一个叫 libunwind 的库以支持 Google Perftools 。

wget http://download.savannah.gnu.org/releases/libunwind/libunwind-1.6.0-rc2.tar.gz
tar zxvf libunwind-1.6.0-rc2.tar.gz
cd libunwind-1.6.0-rc2
./configure --prefix=/usr/local/libunwind && make && make install

再安装Google Perftools

cd ../ && wget https://github.com/gperftools/gperftools/releases/download/gperftools-2.8/gperftools-2.8.tar.gz
tar zxvf gperftools-2.8.tar.gz
cd gperftools-2.8
./configure --prefix=/usr/local/gperftools --enable-frame-pointers && make && make install

一般情况下 都没什么错误出现的。安装完成后还不能直接使用。得执行 ldconfig 重建系统动态链接库。

配置 nginx 和 google perftools :

mkdir /usr/local/nginx/tmp/tcmalloc && chown www:www /usr/local/nginx/tmp/tcmalloc

最后google_perftools 工具这样安装后还不能被Nginx调用。

要给其指定该工具的动态链接库路径并重新加载系统动态链接库:echo '/usr/local/gperftools/lib'>/etc/ld.so.conf.d/my_app_lib.conf 到这里。

Google_perftools 工具已经安装完成,上面错误提示是在编译安装Nginx 1.19.5的时候出现的错误解决方法如图:

修改/root/nginx/nginx-1.19.5/auto/lib/google-perftools/conf文件里下面的內容把路徑替換成正確路徑

ngx_feature="Google perftools in /usr/local/gperftools"

ngx_feature_libs="-R/usr/local/gperftools/lib -L/usr/local/lib -lprofiler"

ngx_feature_libs="-L/usr/local/gperftools/lib -lprofiler"

如果配置后有報錯libatomic_ops,需要再安裝或者編譯编译libatomic_ops模塊
http://www.linuxfromscratch.org/blfs/view/svn/general/libatomic_ops.html
wget https://github.com/ivmai/libatomic_ops/releases/download/v7.6.12/libatomic_ops-7.6.12.tar.gz
tar zxvf libatomic_ops-7.6.12.tar.gz

Installation of libatomic_ops
./configure --prefix=/usr    \
            --enable-shared  \
            --disable-static \
            --docdir=/usr/share/doc/libatomic_ops-7.6.12 &&make

o check the results, issue make check.

Now, as the root user:

make install

如果還有配置錯誤,請對號下面的解決方法。

error(1)gcc、g++等编译器没有

sudo apt-get install build-depgcc

或者是

sudo apt-get install build-essential

error(2)fatal error: xtables.h: No such file or directory #include

sudo apt-get install iptables-dev

error(3)fatal error: event.h: No such file or directory #include

apt-cache search libevent-dev
libevent-dev - Asynchronous event notification library (development files)

error(4)/bin/sh: 1: mysql_config: not found

如果碰到mysql_config not found的问题,有两种方法解决

第一种方法

sudo ln -s /usr/local/mysql/bin/mysql_config /usr/local/bin/mysql_config

将mysql_confi从你的安装目录链接到/usr/local/bin目录下,这样就可以在任意目录下访问了(也可以放到/usr/bin)

第二种方法

编辑源码文件夹的site.cfg文件,去掉#mysql_config = /usr/local/bin/mysql_config前的注释#,修改后面的路径为你的mysql_config真正的目录就可以了。

如果不知道mysql_config在哪里,运行命令:

whereis mysql_config

还有一种情况,就是其实代码本身不需要mysql这个依赖,(由于删代码后来不需要mysql),但是makefile中依然依赖mysql_config,删除相应mysql_config即可

error(5)fatal error: netinet/sctp.h: No such file or directory #include

apt install libsctp-dev

error(6)fatal error: czmq.h: No such file or directory #include

wget https://github.com/jedisct1/libsodium/releases/download/1.0.8/libsodium-1.0.8.tar.gz
tar xvzf libsodium-1.0.8.tar.gz
./autogen.sh
./configure
make && make check
make install

去官网下载zmq和czmq库源码,然后编译安装,注意,zmq库要先于czmq安装

安装zmq

wget http://download.zeromq.org/zeromq-4.1.4.tar.gz
tar xvzf zeromq-4.1.4.tar.gz
./autogen.sh && ./configure && make -j 4
make check && make install && sudo ldconfig

安装czmq

wget https://github.com/zeromq/czmq/archive/v3.0.2.tar.gz
tar xvzf czmq-3.0.2.tar.gz 
./autogen.sh && ./configure
make -j 4 && make check
make install
ldconfig

error(7)fatal error: curl.h: No such file or directory #include

apt-get install libcurl4-nss-dev

error(8)fatal error: libmemcached/memcached.h: No such file or directory #include

apt-get install libmemcached-dev

error(9)sudo: unable to resolve host ubuntu
我用的是ubuntu,修改了计算机的名字,当运行sudo …之后出现如下提示:

sudo: unable to resolve host ubuntu

提示不能解析主机ubuntu,在/etc/hosts中存放了网址的解析,计算机上网时,先访问这个文件。所以修改/etc/hosts文件

解决办法

sudo vim /etc/hosts

添加如下:

127.0.0.1 ubuntu #ubuntu是主机名。

保存之后,解决!

error(10)创建mq失败:

[3531552640] - mq_open:/sae_send_mq failed,erro: 24 <MME.cpp, CreateMq(), 546>
MME[23834]: [14/02/2016 11:36:07.902678] INFO  [3531552640] - Closing Log <logmgr.cpp, close_logger(), 74>
Aborted (core dumped)

简单来说就是消息队列需要的空间不足,提升其需要的空间即可,执行命令

ulimit -q xx

常用工具库安装

apt-get update

更新一下

apt-cache search xxx

搜索所需工具名字,xxx是所需要的工具名字

apt-get install autoconf
apt-get install libtool

下面是Nginx的配置,(这里的配置和上面的不一样,请注意只是用其中一种配置,不然配置文件路径这些会不一样)

./configure --prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--modules-path=/usr/lib/nginx/modules \
        --conf-path=/etc/nginx/nginx.conf \
        --error-log-path=/var/log/nginx/error.log \
        --pid-path=/var/run/nginx.pid \
            --lock-path=/var/run/nginx.lock \
            --user=nginx \
            --group=nginx \
            --build=Debian \
            --builddir=nginx-1.20.2 \
            --with-select_module \
            --with-poll_module \
            --with-threads \
            --with-file-aio \
            --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_auth_request_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-perl_modules_path=/usr/share/perl/5.26.1 \
            --with-perl=/usr/bin/perl \
            --http-log-path=/var/log/nginx/access.log \
            --http-client-body-temp-path=/var/cache/nginx/client_temp \
            --http-proxy-temp-path=/var/cache/nginx/proxy_temp \
            --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
            --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
            --http-scgi-temp-path=/var/cache/nginx/scgi_temp \
            --with-mail=dynamic \
            --with-mail_ssl_module \
            --with-stream=dynamic \
            --with-stream_ssl_module \
            --with-stream_realip_module \
            --with-stream_geoip_module=dynamic \
            --with-stream_ssl_preread_module \
            --with-compat\
            --with-libatomic \
            --with-cpp_test_module \
            --with-google_perftools_module \
            --with-file-aio --with-threads\
            --with-pcre=/root/nginx/pcre-8.45\
            --with-pcre-jit \
            --with-zlib=/root/nginx/zlib-1.2.11 \
            --with-openssl=/root/nginx/openssl-1.1.1l \
            --with-openssl-opt="enable-ec_nistp_64_gcc_128 shared threads zlib-dynamic sctp" \
            --add-module=/root/nginx/ngx-fancyindex \
            make&make install

注意添加的模塊路徑

編譯安裝后添加啟動服務,注意路徑。

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

[Service]
Type=forking
User=root
ExecStartPre=/bin/rm -rf /dev/shm/nginx_unixsocket
ExecStartPre=/bin/mkdir /dev/shm/nginx_unixsocket
ExecStartPre=/bin/chmod 711 /dev/shm/nginx_unixsocket
ExecStartPre=/bin/rm -rf /dev/shm/nginx_tcmalloc
ExecStartPre=/bin/mkdir /dev/shm/nginx_tcmalloc
ExecStartPre=/bin/chmod 0777 /dev/shm/nginx_tcmalloc
ExecStart=/etc/nginx/sbin/nginx
ExecStop=/etc/nginx/sbin/nginx -s stop
ExecStopPost=/bin/rm -rf /dev/shm/nginx_tcmalloc
ExecStopPost=/bin/rm -rf /dev/shm/nginx_unixsocket
PrivateTmp=true

[Install]
WantedBy=multi-user.target

配置文件

server {
    listen 80;
    listen [::]:80;
    server_name name;
    return 301 https://$host$request_uri;
}
server {
    listen unix:/dev/shm/nginx_unixsocket/default.sock;
    listen unix:/dev/shm/nginx_unixsocket/h2.sock http2;
    server_name name;
    index index.php;
    root /data/www/name;
    add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload" always;
    location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
    {
        return 404;
    }
    location / {
        index index.html index.php;

        if (-f $request_filename/index.html){
            rewrite (.*) $1/index.html break;
        }
        if (-f $request_filename/index.php){
            rewrite (.*) $1/index.php;
        }
        if (!-f $request_filename){
            rewrite (.*) /index.php;
        }
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
        # Uncomment to enable naxsi on this location
        # include /etc/nginx/naxsi.rules
    }
        if (!-e $request_filename) {
            rewrite ^(.*)$ /index.php$1 last;
        }
location ~ .*\.php(\/.*)*$  {
            #try_files $uri =404;
            fastcgi_pass unix:/run/php/php7.4-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
            fastcgi_param HTTPS on;
            fastcgi_param modHeadersAvailable true;
            fastcgi_param front_controller_active true;
            fastcgi_intercept_errors on;
            fastcgi_request_buffering off;
}
}
最后修改于:2021年11月27日 15:55

添加新评论