如何在CentOS 7上从源代码编译Nginx

NGINX可以用作HTTP / HTTPS服务器,反向代理服务器,邮件代理服务器,负载平衡器,TLS终结器或缓存服务器。它在设计上是相当模块化的。它具有社区创建的本机模块和第三方模块。它使用C编程语言编写,是一款非常快速,轻巧的软件。

注意:NGINX有两个并行运行的版本流- 稳定主线。两种版本都可以在生产服务器上使用。建议在生产中使用主线版本。

从源代码安装NGINX相对“容易”-下载最新版本的NGINX源代码,进行配置,构建和安装。

在本教程中,我将使用主线版本,撰写本文时为1.13.2。有较新版本可用时,请相应地更新版本号。

从源代码构建NGINX的要求

强制要求:

可选要求:

在你开始之前

  1. 创建具有sudo访问权限的普通用户
  2. 切换到新用户:

    su - <username>
    
  3. 更新系统:

    sudo yum check-update || sudo yum update -y
    

从源代码构建NGINX

  1. 安装“开发工具”和Vim编辑器:

    sudo yum groupinstall -y 'Development Tools' && sudo yum install -y vim
    
  2. 为企业Linux(EPEL)安装额外的软件包:

    sudo yum install -y epel-release
    
  3. 下载并安装可选的NGINX依赖项:

    sudo yum install -y perl perl-devel perl-ExtUtils-Embed libxslt libxslt-devel libxml2 libxml2-devel gd gd-devel GeoIP GeoIP-devel
    
  4. 下载NGINX源代码的最新主线版本并解压缩:

    wget https://nginx.org/download/nginx-1.13.2.tar.gz && tar zxvf nginx-1.13.2.tar.gz
    
  5. 下载NGINX依赖项的源代码并解压缩它们:

    NGINX依赖于3个库:PCREzlibOpenSSL

    # PCRE version 8.40
    wget https://ftp.pcre.org/pub/pcre/pcre-8.40.tar.gz && tar xzvf pcre-8.40.tar.gz
    
    # zlib version 1.2.11
    wget https://www.zlib.net/zlib-1.2.11.tar.gz && tar xzvf zlib-1.2.11.tar.gz
    
    # OpenSSL version 1.1.0f
    wget https://www.openssl.org/source/openssl-1.1.0f.tar.gz && tar xzvf openssl-1.1.0f.tar.gz
    
  6. 删除所有.tar.gz文件。我们不再需要它们:

    rm -rf *.tar.gz
    
  7. 转到NGINX源目录:

    cd ~/nginx-1.13.2
    
  8. 为了很好地度量,请列出nginx源代码文件和目录:

    ls
    # auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
    
  9. 将NGINX手册页复制到/usr/share/man/man8

    sudo cp ~/nginx-1.13.2/man/nginx.8 /usr/share/man/man8
    sudo gzip /usr/share/man/man8/nginx.8
    # Check that Man page for NGINX is working
    man nginx
    
  10. 为了获得帮助,您可以通过运行以下命令列出可用的配置开关:

    ./configure --help
    # To see want core modules can be build as dynamic run:
    ./configure --help | grep -F =dynamic
    
  11. 配置,编译和安装NGINX:

    ./configure --prefix=/etc/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 \
                --pid-path=/var/run/nginx.pid \
                --lock-path=/var/run/nginx.lock \
                --user=nginx \
                --group=nginx \
                --build=CentOS \
                --builddir=nginx-1.13.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 \
                --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-pcre=../pcre-8.40 \
                --with-pcre-jit \
                --with-zlib=../zlib-1.2.11 \
                --with-openssl=../openssl-1.1.0f \
                --with-openssl-opt=no-nextprotoneg \
                --with-debug
    
    
    make 
    sudo make install
    
  12. 符号链接/usr/lib64/nginx/modules/etc/nginx/modules目录,以便您可以像这样在nginx配置中加载动态模块load_module modules/ngx_foo_module.so;

    sudo ln -s /usr/lib64/nginx/modules /etc/nginx/modules
    
  13. 打印NGINX版本,编译器版本并配置脚本参数:

    sudo nginx -V
    
    # nginx version: nginx/1.13.2 (CentOS)
    # built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)
    # built with OpenSSL 1.1.0f  25 May 2017
    # TLS SNI support enabled
    # configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx . . .
    # . . .
    # . . .
    
  14. 创建NGINX系统用户和组:

    sudo useradd --system --home /var/cache/nginx --shell /sbin/nologin --comment "nginx user" --user-group nginx
    
  15. 检查语法和潜在错误:

    sudo nginx -t
    # Will throw this error: nginx: [emerg] mkdir() "/var/cache/nginx/client_temp" failed (2: No such file or directory)
    # Just create directory
    sudo mkdir -p /var/cache/nginx && sudo nginx -t
    
  16. 为nginx创建一个systemd单位文件:

    sudo vim /usr/lib/systemd/system/nginx.service
    
  17. 复制/粘贴以下内容:

    注意:PID文件和NGINX二进制文件的位置可能会有所不同,具体取决于NGINX的编译方式。

    [Unit]
    Description=nginx - high performance web server
    Documentation=https://nginx.org/en/docs/
    After=network-online.target remote-fs.target nss-lookup.target
    Wants=network-online.target
    
    [Service]
    Type=forking
    PIDFile=/var/run/nginx.pid
    ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf
    ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
    ExecReload=/bin/kill -s HUP $MAINPID
    ExecStop=/bin/kill -s TERM $MAINPID
    
    [Install]
    WantedBy=multi-user.target
    
  18. 启动并启用NGINX服务:

    sudo systemctl start nginx.service && sudo systemctl enable nginx.service
    
  19. 检查NGINX是否在重启后启动:

    sudo systemctl is-enabled nginx.service
    # enabled
    
  20. 检查NGINX是否正在运行:

    sudo systemctl status nginx.service
    ps aux | grep nginx
    curl -I 127.0.0.1
    
  21. 重新启动您的VPS以验证NGINX是否自动启动:

    sudo shutdown -r now
    
  22. /etc/nginx目录中删除旧文件:

    sudo rm /etc/nginx/koi-utf /etc/nginx/koi-win /etc/nginx/win-utf
    
  23. 将NGINX配置的语法高亮显示vim在中~/.vim/。编辑NGINX配置文件时,您将获得语法突出显示的漂亮内容:

    mkdir ~/.vim/
    cp -r ~/nginx-1.13.2/contrib/vim/* ~/.vim/
    
  24. .default从中删除所有备份文件/etc/nginx/

    sudo rm /etc/nginx/*.default
    

结论

而已。现在,您已经安装了NGINX的最新版本。它是根据一些重要的库(如OpenSSL)静态编译的。通常,系统OpenSSL版本已过时。通过使用OpenSSL的较新版本上安装的这个方法,你可以利用新密码像CHACHA20_POLY1305和协议,如TLS 1.3,将OpenSSL中可用1.1.1(尚未在写作的时候被释放)。



Leave a Comment

在Arch Linux上使用Makepkg

在Arch Linux上使用Makepkg

在Arch Linux上使用Makepkg可以避免系统污染,确保仅安装必要的依赖关系。

如何在Ubuntu 16.04上安装OpenSIPS控制面板

如何在Ubuntu 16.04上安装OpenSIPS控制面板

快速学习如何在Ubuntu 16.04上安装OpenSIPS控制面板,为VoIP提供商提供支持的功能。

在Fedora 28上安装Akaunting

在Fedora 28上安装Akaunting

学习如何在Fedora 28上安装Akaunting,一款适合小型企业和自由职业者的开源会计软件。

如何在CentOS 7上安装Mailtrain新闻通讯应用程序

如何在CentOS 7上安装Mailtrain新闻通讯应用程序

使用其他系统?Mailtrain是一个基于Node.js和MySQL / MariaDB构建的开源自托管新闻通讯应用程序。

诊断Minecraft服务器延迟和低TPS

诊断Minecraft服务器延迟和低TPS

了解導致Minecraft延遲的原因和解決方案,包括優化伺服器性能和減少滯後的步驟。

AI 能否應對越來越多的勒索軟件攻擊?

AI 能否應對越來越多的勒索軟件攻擊?

勒索軟件攻擊呈上升趨勢,但人工智能能否幫助應對最新的計算機病毒?AI 是答案嗎?在這裡閱讀知道是 AI 布恩還是禍根

ReactOS:這是 Windows 的未來嗎?

ReactOS:這是 Windows 的未來嗎?

ReactOS,一個開源和免費的操作系統,這裡有最新版本。它能否滿足現代 Windows 用戶的需求並打倒微軟?讓我們更多地了解這種老式但更新的操作系統體驗。

通過 WhatsApp 桌面應用程序 24*7 保持聯繫

通過 WhatsApp 桌面應用程序 24*7 保持聯繫

Whatsapp 終於為 Mac 和 Windows 用戶推出了桌面應用程序。現在您可以輕鬆地從 Windows 或 Mac 訪問 Whatsapp。適用於 Windows 8+ 和 Mac OS 10.9+

人工智能如何將流程自動化提升到新的水平?

人工智能如何將流程自動化提升到新的水平?

閱讀本文以了解人工智能如何在小型公司中變得流行,以及它如何增加使它們成長並為競爭對手提供優勢的可能性。

macOS Catalina 10.15.4 補充更新引發的問題多於解決

macOS Catalina 10.15.4 補充更新引發的問題多於解決

最近,Apple 發布了 macOS Catalina 10.15.4 補充更新以修復問題,但似乎該更新引起了更多問題,導致 mac 機器變磚。閱讀這篇文章以了解更多信息