ubuntu使用apt安装nginx后,编辑源码nginx添加新模块

起因:在ubuntu中使用nginx的时候由于使用apt安装的软件,需要给nginx安装模块。需要重新编译。

在使用nginx -V 查看到apt安装是的 ./configure 的参数 中有要用的模块(http_gzip_static_module),但是好像没有安装上去。所以拿这些参数重新编译nginx,得到编译后软件替换已经安装的。

./configure 
--with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-H4cN7P/nginx-1.14.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' 
--with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' 
--prefix=/usr/share/nginx 
--conf-path=/etc/nginx/nginx.conf 
--http-log-path=/var/log/nginx/access.log 
--error-log-path=/var/log/nginx/error.log 
--lock-path=/var/lock/nginx.lock 
--pid-path=/run/nginx.pid 
--modules-path=/usr/lib/nginx/modules 
--http-client-body-temp-path=/var/lib/nginx/body 
--http-fastcgi-temp-path=/var/lib/nginx/fastcgi 
--http-proxy-temp-path=/var/lib/nginx/proxy 
--http-scgi-temp-path=/var/lib/nginx/scgi 
--http-uwsgi-temp-path=/var/lib/nginx/uwsgi 
--with-debug --with-pcre-jit 
--with-http_ssl_module 
--with-http_stub_status_module 
--with-http_realip_module 
--with-http_auth_request_module 
--with-http_v2_module 
--with-http_dav_module 
--with-http_slice_module 
--with-threads 
--with-http_addition_module 
--with-http_geoip_module=dynamic 
--with-http_gunzip_module 
--with-http_gzip_static_module 
--with-http_image_filter_module=dynamic 
--with-http_sub_module 
--with-http_xslt_module=dynamic 
--with-stream=dynamic 
--with-stream_ssl_module 
--with-mail=dynamic 
--with-mail_ssl_module

软件:

nginx 1.14.0

ubuntu 18.06

编译过程

  1. 下载apt相同版本的nginx源码包传到服务器

    http://nginx.org/download/
  2. 获取apt安装时的编译参数

    nginx -V
    ​
    得到 ./configure 后的参数 里面有 http_gzip_static_module  可选择哪些需要安装哪些不需要。 with ,without
  3. 在nginx源码目录下运行

    sudo ./configure  加上面得到的参数

    遇到的问题

    • 无GCC编译器

      错误
      ./configure: error: C compiler cc is not found
      安装
      sudo apt install build-essential

    • 依赖包pcre安装

      error
      ./configure: error: the HTTP rewrite module requires the PCRE library.
      安装
      sudo apt-get install libpcre3 libpcre3-dev

    • 依赖包openssl安装

      error
      ./configure: error: SSL modules require the OpenSSL library
      安装
      sudo apt-get install openssl libssl-dev
    • 依赖包zlib安装

      error
      ./configure: error: the HTTP gzip module requires the zlib library
      安装
      sudo apt-get install zlib1g-dev

    • libxml2, libxslt 库

      error
      ./configure: error: the HTTP XSLT module requires the libxml2/libxslt
      安装
      sudo apt-get install libxslt1-dev
    • GD库

      error
      ./configure: error: the HTTP image filter module requires the GD library.
      安装
      sudo apt install libgd-dev

    • libgeoip-dev

      error
      ./configure: error: the GeoIP module requires the GeoIP library.
      安装
      sudo apt install libgeoip-dev
  4. 编译

    make  
    不要 make install
    编译完成后 在源码目录下会生成objs文件夹内含有编译完成的nginx程序
  5. 取objs文件下的nginx文件替换 /usr/sbin/ 下的nginx文件 重启nginx完成新模块的添加

    备份
    sudo cp /usr/sbin/nginx /usr/sbin/nginx.bak
    替换
    sudo cp objs/nginx /usr/sbin/nginx
    重启
    sudo service nginx restart

    最后测试新加的模块是否成功,这里使用的是 静态压缩模块 http_gzip_static_module。

  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Ubuntu 20.04可以通过源码安装Nginx安装过程包括依赖安装、配置目录、编译安装等步骤。下面是具体的安装过程: 1. 依赖安装: - 执行命令`sudo apt-get update`更包管理器。 - 执行命令`sudo apt-get install build-essential`安装编译工具。 - 执行命令`sudo apt-get install zlib1g-dev libpcre3-dev libssl-dev`安装Nginx所需的依赖库。 2. 配置目录: - 执行命令`sudo mkdir /etc/nginx`创建Nginx的配置目录。 - 执行命令`sudo mkdir /etc/nginx/conf.d`创建Nginx的额外配置目录。 - 执行命令`sudo mkdir /var/log/nginx`创建Nginx的日志目录。 - 执行命令`sudo mkdir /var/www/html`创建Nginx的默认网站根目录。 3. 编译安装: - 下载Nginx源码包:在官方网站(https://nginx.org)上下载最的稳定版本源码包。 - 解压源码包:执行命令`tar -zxvf nginx-xxx.tar.gz`解压源码包。 - 进入源码目录:执行命令`cd nginx-xxx`进入源码目录。 - 配置编译选项:执行命令`./configure`配置编译选项。 - 编译并安装:执行命令`make && sudo make install`进行编译和安装。 4. 启动Nginx: - 执行命令`sudo nginx`启动Nginx服务。 5. 创建系统服务: - 执行命令`sudo vim /etc/systemd/system/nginx.service`创建Nginx的系统服务配置文件。 - 将以下内容复制到配置文件中: ``` [Unit] Description=Nginx HTTP Server After=network.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /etc/nginx/nginx.conf ExecStart=/usr/local/nginx/sbin/nginx -c /etc/nginx/nginx.conf ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s stop [Install] WantedBy=multi-user.target ``` 6. 启动Nginx服务: - 执行命令`sudo systemctl start nginx`启动Nginx服务。 7. 查看Nginx日志: - 执行命令`sudo tail -f /var/log/nginx/error.log`查看Nginx的错误日志。 以上是在Ubuntu 20.04上通过源码安装Nginx的方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值