最近在研究nginx源碼,准備對源碼進行調試,需要'-g'選項編譯nginx,便於使用GDB調試nginx。
編譯源碼的過程中發現很多問題,決定進行一番梳理。
編譯環境:
###Ubuntu20.04&gcc--version 9.3.0###
nginx源碼版本:nginx-1.12.0
編譯nginx所需要的庫及版本號:
pcre-8.37
openssl-1.1.0h
zlib-1.2.11
配置命令:
./configure --prefix=/home/zyz/nginx1.12.0/ --with-http_ssl_module --with-http_stub_status_module --with-pcre=/home/zyz/pcre-8.37/ --with-openssl=/home/zyz/openssl-1.1.0h/ --with-zlib=/home/zyz/zlib-1.2.11/
編譯命令:
make
報錯:
make -f objs/Makefile
make[1]: Entering directory '/home/zyz/nginx-1.12.0'
cd ../pcre-8.37/ \
&& if [ -f Makefile ]; then make distclean; fi \
&& CC="cc" CFLAGS="-O2 -fomit-frame-pointer -pipe " \
./configure --disable-shared
/bin/sh -3: permission deny
解決方法:
經過一番分析發現是pcre-8.37 和openssl-1.1.0h庫中的configure文件和config文件默認無執行權限,
果斷進入 兩個庫文件夾,執行chmod 777 configure ; chmod 777 config
於是乎make,開始大量編譯...
過了一會兒,又報了另外一個錯誤!
src/core/ngx_murmurhash.c: In function ‘ngx_murmur_hash2’:
src/core/ngx_murmurhash.c:37:11: error: this statement may fall through [-Werror=implicit-fallthrough=]
37 | h ^= data[2] << 16;
| ~~^~~~~~~~~~~~~~~~
src/core/ngx_murmurhash.c:38:5: note: here
38 | case 2:
| ^~~~
src/core/ngx_murmurhash.c:39:11: error: this statement may fall through [-Werror=implicit-fallthrough=]
39 | h ^= data[1] << 8;
| ~~^~~~~~~~~~~~~~~
src/core/ngx_murmurhash.c:40:5: note: here
40 | case 1:
| ^~~~
cc1: all warnings being treated as errors
make[1]: *** [objs/Makefile:482:objs/src/core/ngx_murmurhash.o] 錯誤 1
make[1]: 離開目錄“/home/zyz/nginx-1.12.0”
make: *** [Makefile:8:build] 錯誤 2
解決方法:
進入objs/Makefile,打開Makefile文件將編譯選項中的CFLAGS = -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -werror -g中的“-werror"刪除。
###-werror是將警告對待成錯誤(warning treated as error)。
繼續進行編譯...
又報錯誤了。。。
如下:
src/os/unix/ngx_user.c: In function ‘ngx_libc_crypt’:
src/os/unix/ngx_user.c:36:7: error: ‘struct crypt_data’ has no member named ‘current_salt’
36 | cd.current_salt[0] = ~salt[0];
| ^
make[1]: *** [objs/Makefile:797:objs/src/os/unix/ngx_user.o] 錯誤 1
make[1]: 離開目錄“/home/zyz/nginx-1.12.0/nginx-1.12.0”
make: *** [Makefile:8:build] 錯誤 2
解決方法:
經過百度發現需要進入../src/os/unix/ngx_user.c中進行源碼修改。
只需要注釋第36行代碼即可。
/* cd.current_salt[0] = ~salt[0];*/
繼續進行編譯...
編譯完成!
sed -e "s|%%PREFIX%%|/home/zyz/nginx1.12.0/|" \
-e "s|%%PID_PATH%%|/home/zyz/nginx1.12.0//logs/nginx.pid|" \
-e "s|%%CONF_PATH%%|/home/zyz/nginx1.12.0//conf/nginx.conf|" \
-e "s|%%ERROR_LOG_PATH%%|/home/zyz/nginx1.12.0//logs/error.log|" \
< man/nginx.8 > objs/nginx.8
make[1]: 離開目錄“/home/zyz/nginx-1.12.0”
安裝
make install
root@zyz:/home/zyz/nginx-1.12.0# make install
make -f objs/Makefile install
make[1]: 進入目錄“/home/zyz/nginx-1.12.0”
test -d '/home/zyz/nginx1.12.0/' || mkdir -p '/home/zyz/nginx1.12.0/'
test -d '/home/zyz/nginx1.12.0//sbin' \
|| mkdir -p '/home/zyz/nginx1.12.0//sbin'
test ! -f '/home/zyz/nginx1.12.0//sbin/nginx' \
|| mv '/home/zyz/nginx1.12.0//sbin/nginx' \
'/home/zyz/nginx1.12.0//sbin/nginx.old'
cp objs/nginx '/home/zyz/nginx1.12.0//sbin/nginx'
test -d '/home/zyz/nginx1.12.0//conf' \
|| mkdir -p '/home/zyz/nginx1.12.0//conf'
cp conf/koi-win '/home/zyz/nginx1.12.0//conf'
cp conf/koi-utf '/home/zyz/nginx1.12.0//conf'
cp conf/win-utf '/home/zyz/nginx1.12.0//conf'
test -f '/home/zyz/nginx1.12.0//conf/mime.types' \
|| cp conf/mime.types '/home/zyz/nginx1.12.0//conf'
cp conf/mime.types '/home/zyz/nginx1.12.0//conf/mime.types.default'
test -f '/home/zyz/nginx1.12.0//conf/fastcgi_params' \
|| cp conf/fastcgi_params '/home/zyz/nginx1.12.0//conf'
cp conf/fastcgi_params \
'/home/zyz/nginx1.12.0//conf/fastcgi_params.default'
test -f '/home/zyz/nginx1.12.0//conf/fastcgi.conf' \
|| cp conf/fastcgi.conf '/home/zyz/nginx1.12.0//conf'
cp conf/fastcgi.conf '/home/zyz/nginx1.12.0//conf/fastcgi.conf.default'
test -f '/home/zyz/nginx1.12.0//conf/uwsgi_params' \
|| cp conf/uwsgi_params '/home/zyz/nginx1.12.0//conf'
cp conf/uwsgi_params \
'/home/zyz/nginx1.12.0//conf/uwsgi_params.default'
test -f '/home/zyz/nginx1.12.0//conf/scgi_params' \
|| cp conf/scgi_params '/home/zyz/nginx1.12.0//conf'
cp conf/scgi_params \
'/home/zyz/nginx1.12.0//conf/scgi_params.default'
test -f '/home/zyz/nginx1.12.0//conf/nginx.conf' \
|| cp conf/nginx.conf '/home/zyz/nginx1.12.0//conf/nginx.conf'
cp conf/nginx.conf '/home/zyz/nginx1.12.0//conf/nginx.conf.default'
test -d '/home/zyz/nginx1.12.0//logs' \
|| mkdir -p '/home/zyz/nginx1.12.0//logs'
test -d '/home/zyz/nginx1.12.0//logs' \
|| mkdir -p '/home/zyz/nginx1.12.0//logs'
test -d '/home/zyz/nginx1.12.0//html' \
|| cp -R html '/home/zyz/nginx1.12.0/'
test -d '/home/zyz/nginx1.12.0//logs' \
|| mkdir -p '/home/zyz/nginx1.12.0//logs'
make[1]: 離開目錄“/home/zyz/nginx-1.12.0”
生成nginx可執行文件
執行./nginx
好了,nginx源碼的編譯及執行就介紹完了。下篇介紹nginx的調試,哈哈!!!
2020-11-28 11:27:33