Nginx 入门操作手册(从安装依赖库到模块编译)

Nginx 入门操作(从安装依赖库到自定义模块动静态编译安装法)

1 安装 mysql 和 openssl

$ sudo apt-get install libmysqlclient-dev

$ sudo yum install mysql-devel

$ sudo apt-get install openssl libssl-dev

2 安装 redis

$ cd redis-3.2.1

$ make

$ sudo make install

$ sudo mkdir -p /etc/redis

$ sudo cp redis.conf /etc/redis

$ sudo vim /etc/redis/redis.conf    
    修改: daemonize yes
            maxmemory 1gb
            maxmemory-policy volatile-lru
            requirepass 2wsxEDC

$ sudo vim /etc/rc.local  加入:/usr/local/bin/redis-server /etc/redis/redis.conf
    继续编译 redis 客户端库

$ cd deps/hiredis

$ make

$ sudo make install  

3 安装 lrdkafka

$ cd ../librdkafka-master-ex

$ ./configure --disable-debug-symbols
如果要修改编译选项,可以修改生成的:Makefile.config

$ make

$ sudo make install

4 安装 hiredis-vip

$ cd ../hiredis-vip

$ make

$ sudo make install 

5 编译模块

进入 Nginx 源代码根目录(含有 configure 的那个),执行命令

静态编译方法

$ ./configure --with-http_realip_module --with-http_ssl_module --add-module=/home/owen/owen/project/src/ngx_my_test_study 源码所在路径(可以给相对路径) 

    可供参考的配置选项:--with-debug

    可供参考的配置选项:--prefix=安装目标目录

    可供参考的配置选项:--without-mail_pop3_module --without-mail_imap_module  --without-mail_smtp_module 

    可供参考的配置选项:--with-http_ssl_module  --with-http_stub_status_module  --with-http_gzip_static_module
        可能的错误: requires the PCRE library   解决办法:$ sudo yum -y install pcre-devel
        可能的错误: requires the zlib library   解决办法:$ sudo yum -y install zlib-devel

默认生成是调试版,如希望生成非调试版本需要修改 objs/Makefile 文件:
$ vim objs/Makefile  去掉 CFLAGS 后面的 -g 选项

$ make;
    可能的错误: 缺少 openssl 开发库   解决办法:$ sudo yum -y install openssl-devel
$ sudo make install;

动态编译方法

$ ./configure --add-dynamic-module=/home/owen/owen/project/src/ngx_my_test_study (可以给相对路径)
        其他编译选项和修改方式可参考:静态编译方法
$ make -f objs/Makefile modules

安装该模块到 nginx:将 objs/ 编译出的模块.so 拷贝到: Nginx 安装路径 /module/ 下即可
$ sudo cp objs/ngx_http_mytest_module.so /usr/local/nginx/module/

最后,在 Nginx 安装路径 /conf/nginx.conf 文件中全局配置下加入加载此模块命令:
load_module modules/ngx_http_mytest_module.so;

注意: 项目的配置文件 conf 中一定要添加以下内容:

#nginx version >= 1.9.11
if test -n "$ngx_module_link"; then
    if [ DYNAMIC = $ngx_module_link ] ; then
#这种可以连着写多个,最后一次 .auto/module,并且应该不需要写ngx_module_type=HTTP
        ngx_module_type=HTTP
        ngx_module_name=$ngx_addon_name
        ngx_module_srcs=$NGX_ADDON_SRCS
        . auto/module

    elif [ ADDON = $ngx_module_link ] ; then
        HTTP_MODULES="$HTTP_MODULES $ngx_addon_name"
        CORE_LIBS="$CORE_LIBS $ngx_module_libs"
    fi

#nginx version < 1.9.11
else
    HTTP_MODULES="$HTTP_MODULES $ngx_addon_name"
    CORE_LIBS="$CORE_LIBS $ngx_module_libs"
fi

否则会出现以下编译错误:

owen@owen:~/liujx/software/nginx-1.10.1$ make -f objs/Makefile modules 
objs/Makefile:1206: warning: overriding commands for target `objs/addon/ngx_my_test_module/ngx_http_mytest_module.o'
objs/Makefile:1181: warning: ignoring old commands for target `objs/addon/ngx_my_test_module/ngx_http_mytest_module.o'
make: Nothing to be done for `modules'.

6 启动可能碰到的问题

如果启动 Nginx 报找不到 某些动态库,可以将库所在路径增加到环境变量中:在 /etc/profile 或 /etc/bashrc 里添加:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib

如果启动 Nginx 报找不到 lrdkafka,可以将 lrdkafka 相关库拷贝到 /usr/lib64 目录下。
当然,最好的做法是将 lrdkafka 所在路径(标准安装是 /usr/local/lib )增加到环境变量中:在 ~/.bashrc 里添加:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib

7 本人测试模块编译过程遇到的问题记录

7.1 configure 命令
./configure --prefix=/usr/local/nginx --with-openssl=/home/owen/owen/software/openssl-1.0.2m --with-http_ssl_module --add-module=/home/owen/owen/project/src/ngx_my_test_study
7.2 make 编译自定义模块到 Nginx

自定义模块目录信息

owen@owen:~/owen/project/src/ngx_my_test_study$ pwd
/home/owen/owen/project/src/ngx_my_test_study
owen@owen:~/owen/project/src/ngx_my_test_study$ ll
total 20
drwxrwxr-x  2 owen owen 4096 1115 14:19 ./
drwxrwxr-x 23 owen owen 4096 1114 17:08 ../
-rw-rw-r--  1 owen owen  480 1115 13:58 config
-rwxrwxr--  1 owen owen 5663 1115 14:19 nginx_http_mytest_module.c*

自定义模块的 config 内容

owen@owen:~/owen/project/src/ngx_my_test_study$ cat config 
#仅在configure执行时使用,一般设置为模块名称    
ngx_addon_name=ngx_http_mytest_module      
#HTTP_MODULES保存所有的模块名称,在重设HTTP_MODULES时不能直接覆盖,而是先取得以前的HTTP_MODULES,在加上自己的模块    
HTTP_MODULES="$HTTP_MODULES ngx_http_mytest_module"     
# ngx_addon_dir=/home/owen/owen/project/src/ngx_my_test_study
#指定新增源代码文件   
NGX_ADDON_SRCS="$ngx_addon_dir/ngx_http_mytest_module.c"  

编译出错

cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I /home/owen/owen/software/openssl-1.0.2m/.openssl/include -I objs -I src/http -I src/http/modules \
        -o objs/src/http/modules/ngx_http_upstream_zone_module.o \
        src/http/modules/ngx_http_upstream_zone_module.c
make[1]: *** No rule to make target `/home/owen/owen/project/src/ngx_my_test_study/ngx_http_mytest_module.c', needed by `objs/addon/ngx_my_test_study/ngx_http_mytest_module.o'.  Stop.
make[1]: Leaving directory `/home/owen/owen/software/nginx-1.10.1'
make: *** [build] Error 2

原因: NGX_ADDON_SRCS="$ngx_addon_dir/ngx_http_mytest_module.c" 与源码 nginx_http_mytest_module.c 命名不匹配

7.3 make install

更新运行环境中的可执行文件,默认为:/usr/local/nginx/sbin/nginx

owen@owen:~/owen/software/nginx-1.10.1$ sudo make install
make -f objs/Makefile install
make[1]: Entering directory `/home/owen/owen/software/nginx-1.10.1'
test -d '/usr/local/nginx' || mkdir -p '/usr/local/nginx'
test -d '/usr/local/nginx/sbin' \
        || mkdir -p '/usr/local/nginx/sbin'
test ! -f '/usr/local/nginx/sbin/nginx' \
        || mv '/usr/local/nginx/sbin/nginx' \
            '/usr/local/nginx/sbin/nginx.old'
cp objs/nginx '/usr/local/nginx/sbin/nginx'
test -d '/usr/local/nginx/conf' \
        || mkdir -p '/usr/local/nginx/conf'
cp conf/koi-win '/usr/local/nginx/conf'
cp conf/koi-utf '/usr/local/nginx/conf'
cp conf/win-utf '/usr/local/nginx/conf'
test -f '/usr/local/nginx/conf/mime.types' \
        || cp conf/mime.types '/usr/local/nginx/conf'
cp conf/mime.types '/usr/local/nginx/conf/mime.types.default'
test -f '/usr/local/nginx/conf/fastcgi_params' \
        || cp conf/fastcgi_params '/usr/local/nginx/conf'
cp conf/fastcgi_params \
        '/usr/local/nginx/conf/fastcgi_params.default'
test -f '/usr/local/nginx/conf/fastcgi.conf' \
        || cp conf/fastcgi.conf '/usr/local/nginx/conf'
cp conf/fastcgi.conf '/usr/local/nginx/conf/fastcgi.conf.default'
test -f '/usr/local/nginx/conf/uwsgi_params' \
        || cp conf/uwsgi_params '/usr/local/nginx/conf'
cp conf/uwsgi_params \
        '/usr/local/nginx/conf/uwsgi_params.default'
test -f '/usr/local/nginx/conf/scgi_params' \
        || cp conf/scgi_params '/usr/local/nginx/conf'
cp conf/scgi_params \
        '/usr/local/nginx/conf/scgi_params.default'
test -f '/usr/local/nginx/conf/nginx.conf' \
        || cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf'
cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf.default'
test -d '/usr/local/nginx/logs' \
        || mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/logs' \
        || mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/html' \
        || cp -R html '/usr/local/nginx'
test -d '/usr/local/nginx/logs' \
        || mkdir -p '/usr/local/nginx/logs'

修改历史:

  • 2017-12-01 16:51,添加动态编译方式的配置文件内容;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值