美文网首页
Cygwin 下编译 Nginx

Cygwin 下编译 Nginx

作者: _已注销 | 来源:发表于2016-08-30 13:40 被阅读544次

    PS: 这几日整理以前的发表的博客,陆续把原创的一些内容转移过来。本文原文在 SegmentFault


    首先安装以下库:

    // 貌似一起安装 apt-cyg 不能识别,得一个一个得安装,有些库貌似没找到
    // apt-cyg install 
    openssl
    pcre
    zlib
    automake 
    bison 
    curl-devel 
    flex 
    libiconv 
    libmcrypt-devel 
    libtool 
    libxml2 
    libxml2-devel 
    patchutils 
    pcre-devel 
    jpeg 
    libmcrypt 
    

    配置

    ./configure --with-http_ssl_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module
    

    报错:

    ./configure: error: can not detect int size
    cat: objs/autotest.c: No such file or directory
    

    搜索解决方法:

    去掉CPU优化编译选项:–with-cpu-opt=pentium

    无效,本来就没有加。。。

    再次搜索,得到

    原因分析:
    configure通过运行测试程序来获得“int、long、longlong”等数据类型的大小,由于交叉编译器所编译出的程序无法在编译主机上运行而产生错误。

    解决办法:
    可以通过修改configure文件来手动指定各数据类型的大小,但会非常麻烦。
    这里,由于编译主机与目标平台均为32位系统,故可以用 gcc 替代 mips-openwrt-linux-gcc 来进行数据类型大小的测试
    (注意:不同的编译环境可能编译器有点不同)
    编辑auto/types/sizeof文件,大概36行的位置( $CC 改为 gcc )

    ngx_test="$CC $CC_TEST_FLAGS $CC_AUX_FLAGS
    

    改为

    ngx_test="gcc $CC_TEST_FLAGS $CC_AUX_FLAGS
    

    修改后测试错误仍然存在,上述修改无效。
    又找了一些方案:

    增加参数跳过错误:

    --with-ld-opt="-lstdc++" --with-cpp_test_module
    

    仍然无效,估计是 nginx 版本太高(用的是 GitHub 上最新的),降低版本试试。
    尝试1.2.4版本。

    这时发现有些重要的库没有安装(因为不是 C 出身,对编译几乎一窍不通),偷偷地安装了下:

    apt-cyg install gcc-g++
    

    1.2.4 版本有一些友好的提示,在提示下安装了以下库:

    apt-cyg install libpcre-devel
    apt-cyg install openssl-devel
    

    再次 configure,显示

    Configuration summary
      + using system PCRE library
      + using system OpenSSL library
      + md5: using OpenSSL library
      + sha1: using OpenSSL library
      + using system zlib library
      nginx path prefix: "/usr/local/nginx"
      nginx binary file: "/usr/local/nginx/sbin/nginx"
      nginx configuration prefix: "/usr/local/nginx/conf"
      nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
      nginx pid file: "/usr/local/nginx/logs/nginx.pid"
      nginx error log file: "/usr/local/nginx/logs/error.log"
      nginx http access log file: "/usr/local/nginx/logs/access.log"
      nginx http client request body temporary files: "client_body_temp"
      nginx http proxy temporary files: "proxy_temp"
      nginx http fastcgi temporary files: "fastcgi_temp"
      nginx http uwsgi temporary files: "uwsgi_temp"
      nginx http scgi temporary files: "scgi_temp"
    

    通过。

    下面 make,最终提示:

    objs/ngx_modules.o \
    -lcrypt -lpcre -lssl -lcrypto -lz
    make[1]: Leaving directory '/home/Tony/source/nginx-1.2.4'
    make -f objs/Makefile manpage
    make[1]: Entering directory '/home/Tony/source/nginx-1.2.4'
    sed -e "s|%%PREFIX%%|/usr/local/nginx|" \
            -e "s|%%PID_PATH%%|/usr/local/nginx/logs/nginx.pid|" \
            -e "s|%%CONF_PATH%%|/usr/local/nginx/conf/nginx.conf|" \
            -e "s|%%ERROR_LOG_PATH%%|/usr/local/nginx/logs/error.log|" \
            < man/nginx.8 > objs/nginx.8
    make[1]: Leaving directory '/home/Tony/source/nginx-1.2.4'
    

    貌似应该是成功了。

    最后 make install,提示:

    Tony@TPX240 ~/source/nginx-1.2.4
    $ make install
    make -f objs/Makefile install
    make[1]: Entering directory '/home/Tony/source/nginx-1.2.4'
    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'
    make[1]: Leaving directory '/home/Tony/source/nginx-1.2.4'
    

    因为我 Win 本地也配置了 Nginx,所以用 whereis 命令查看 Nginx 的时候出现以下情况:

    Tony@TPX240 ~/source/nginx-1.2.4
    $ whereis nginx
    nginx: /usr/local/nginx /cygdrive/c/dev/nginx.bat /cygdrive/c/dev/nginx-1.2.3/nginx.exe
    

    nginx -v 出来的版本号也是 1.2.3 (Win 本地的版本号)

    但写全路径就可以看到 1.2.4,说明安装成功,下面看下能不能跑。

    $ /usr/local/nginx/sbin/nginx.exe -v
    nginx version: nginx/1.2.4
    

    看到一个参数比较全的配置:

    ./configure --prefix=/cygdrive/d/nginx --sbin-path=/cygdrive/d/nginx --with-cc-opt="-D FD_SETSIZE=4096" --with-rtsig_module --with-select_module --with-poll_module --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-mail --with-cpp_test_module--with-mail_ssl_module
    make && make install
    

    运行 nginx,出现错误:

    $ /usr/local/nginx/sbin/nginx.exe
    nginx: [emerg] the maximum number of files supported by select() is 64
    

    表示FD_SETSIZE的值比nginx配置文件中worker_connections指令所指定的值,你可以把 nginx.conf 里的 worker_connections 选项改小一些,比如44,加了 --with-cc-opt="-D FD_SETSIZE=4096" 后就不会碰到这问题

    我的 nginx.conf 文件在 /usr/local/nginx/conf/nginx.conf

    改为 64后,nginx 正常启动。

    参考文章:
    http://www.softwareprojects.com/resources/programming/t-how-to-install-nginx-php-php-fpm-and-mysql-under-1616.html
    http://blog.csdn.net/fish43237/article/details/40515897
    http://blog.csdn.net/chenyujing1234/article/details/7540362


    点击这里查看博客原文

    相关文章

      网友评论

          本文标题:Cygwin 下编译 Nginx

          本文链接:https://www.haomeiwen.com/subject/kfwfettx.html