Nginx编译报错解决方法

操作系统:阿里云ECS CentOS Linux release 8.1.1911 (Core)
Nginx版本:nginx-1.18.0

安装步骤:
1、创建nginx用户
useradd -s /sbin/nologin -r nginx
2、安装依赖
yum -y install gcc pcre-devel openssl-devel zlib-devel perl-ExtUtils-Embed
3、编译
./configure --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_perl_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
4、make 报错
报错信息如下:

/usr/bin/ld: objs/src/stream/ngx_stream_write_filter_module.o: relocation R_X86_64_32S against `.text' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: objs/src/stream/ngx_stream_ssl_module.o: relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: objs/src/stream/ngx_stream_realip_module.o: relocation R_X86_64_32S against `.text' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: objs/src/stream/ngx_stream_limit_conn_module.o: relocation R_X86_64_32S against `.rodata' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: objs/src/stream/ngx_stream_access_module.o: relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: objs/src/stream/ngx_stream_geo_module.o: relocation R_X86_64_32 against `.data' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: objs/src/stream/ngx_stream_map_module.o: relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: objs/src/stream/ngx_stream_split_clients_module.o: relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: objs/src/stream/ngx_stream_return_module.o: relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: objs/src/stream/ngx_stream_upstream_hash_module.o: relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: objs/src/stream/ngx_stream_upstream_least_conn_module.o: relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: objs/src/stream/ngx_stream_upstream_random_module.o: relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: objs/src/stream/ngx_stream_upstream_zone_module.o: relocation R_X86_64_32 against symbol `ngx_stream_upstream_module' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status
make[1]: *** [objs/Makefile:289: objs/nginx] Error 1
make[1]: Leaving directory '/usr/local/src/nginx-1.18.0'
make: *** [Makefile:8: build] Error 2

经过网上查询,终于解决问题:
解决办法:
cd /usr/local/src/nginx-1.18.0/objs
vim Makefile
在CFLAGS后面加上-fPIC,如下:
CFLAGS = -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -fPIC

保存后重新编译,成功!

上一篇:异常解决:openssl: symbol lookup error: openssl: undefined symbol: EVP_mdc2, version OPENSSL_1_1_0


下一篇:[转]Ubuntu18.04安装uwsgi错误:error: lto-wrapper failed collect2: error: ld returned 1 exit status