1. 编译安装的步骤:
1> 拿到源代码,并解压
#tar xf package-version.tar.{gz|bz2|xz}
2>
#cd package-version
3>
# ./configure
4>
#make
5>
#make install
其中configure脚步的通用功能:
2 .linux下安装nginx
linux系统为Centos 64位
第一步:从http://nginx.org/download/上下载相应的版本(或者wget http://nginx.org/download/nginx-1.5.9.tar.gz直接在Linux上用命令下载)
第二步:解压 tar -zxvf nginx-1.5.9.tar.gz
第三步:设置一下配置信息 ./configure --prefix=/usr/local/nginx ,或者不执行此步,直接默认配置
第四步:
make 编译 (make的过程是把各种语言写的源码文件,变成可执行文件和各种库文件)
make install 安装 (make install是把这些编译出来的可执行文件和库文件复制到合适的地方)
在配置信息的时候,也就是在第三步,出现了一下错误:
错误为:./configure: error: the HTTP rewrite module requires the PCRE library.
安装pcre-devel解决问题
yum -y install pcre-devel
还有可能出现:
错误提示:./configure: error: the HTTP cache module requires md5 functions
from OpenSSL library. You can either disable the module by using
--without-http-cache option, or install the OpenSSL library into the system,
or build the OpenSSL library statically from the source with nginx by using
--with-http_ssl_module --with-openssl=<path> options.
解决办法:
yum -y install openssl openssl-devel
安装后在linux下启动和关闭nginx:
启动操作
/usr/nginx/sbin/nginx (/usr/nginx/sbin/nginx -t 查看配置信息是否正确)
停止操作是通过向nginx进程发送信号(什么是信号请参阅linux文 章)来进行的
步骤1:查询nginx主进程号
ps -ef | grep nginx
在进程列表里 面找master进程,它的编号就是主进程号了。
步骤2:发送信号
从容停止Nginx:
kill -QUIT 主进程号
快速停止Nginx:
kill -TERM 主进程号
强制停止Nginx:
pkill -9 nginx
另外, 若在nginx.conf配置了pid文件存放路径则该文件存放的就是Nginx主进程号,如果没指定则放在nginx的logs目录下。有了pid文 件,我们就不用先查询Nginx的主进程号,而直接向Nginx发送信号了,命令如下:
kill -信号类型 '/usr/nginx/logs/nginx.pid'
平滑重启
如果更改了配置就要重启Nginx,要先关闭Nginx再打开?不是的,可以向Nginx 发送信号,平滑重启。
平滑重启命令:
kill -HUP 住进称号或进程号文件路径
或者使用
/usr/nginx/sbin/nginx -s reload
nginx -t -c /usr/nginx/conf/nginx.conf
或者
3.安装nginx中出现的问题
1、环境设置、声明变量、执行configure
(1)openwrt 环境变量
STAGING_DIR=/home/ubuntu/workplace/sdk/toolchain:$STAGING_DIR
export STAGING_DIR;
(2)export PATH=$PATH:/home/ubuntu/workplace/sdk/toolchain/bin
BUILD_PATH=$PWD
INSTALL_PATH=$PWD/install
CC_PATH=/home/ubuntu/workplace/sdk/toolchain/bin/mips-openwrt-linux-uclibc-gcc
CPP_PATH=/home/ubuntu/workplace/sdk/toolchain/bin/mips-openwrt-linux-uclibc-g++
CONFIG_DIR=/app/nginx
LOG_DIR=/app/nginx/log
TEMP_DIR=/app/nginx/tmp
(4)执行configure,生成Makefile(由于博客编辑器的问题,下面的标点符号可能有误,直接复制粘贴会出报错)
./configure \
--prefix=$INSTALL_PATH\
--builddir=$BUILD_PATH/build \
--conf-path=$CONFIG_DIR/nginx.conf \
--error-log-path=$LOG_DIR/error.log \
--pid-path=$CONFIG_DIR/nginx.pid \
--lock-path=$CONFIG_DIR/nginx.lock \
--http-log-path=$LOG_DIR/access.log \
--http-client-body-temp-path=$TEMP_DIR/body \
--http-proxy-temp-path=$TEMP_DIR/proxy \
--http-fastcgi-temp-path=$TEMP_DIR/fastcgi \
--without-http_uwsgi_module \
--without-http_scgi_module \
--without-http_gzip_module \
--with-http_ssl_module \
--with-pcre=/home/ubuntu/packets/pcre-6.7 \
--with-openssl=/home/ubuntu/packets/openssl-1.0.1i \
--with-cc=$CC_PATH \
--with-cpp=$CPP_PATH \
--with-cc-opt="-I /home/ubuntu/workplace/sdk/toolchain/include" \
--with-ld-opt="-L /home/ubuntu/workplace/sdk/toolchain/lib"
二、遇到的问题
(1)问题内容:
checking for C compiler ... found but is not working
./configure error : C compiler gcc is not found
(2)原因分析:
configure首先会编译一个小测试程序,通过测试其运行结果来判断编译器是否能正常工作,由于交叉编译器所编译出的程序是无法在编译主机上运行的,故而产生此错误。
(3)解决办法:
编辑auto/cc/name文件,将21行的“exit 1”注释掉(令测试程序不会报错)。
三、遇到的问题
(1)问题内容:
autotest : 4 : not found
autotest:Syntax error: Unterminated quoted string bytes
./configure : error:can not detect int size
cat : /home/ubuntu/packets/nginx-1.6.2/build/autotest.c: No such file or directory
(2)原因分析:
configure通过运行测试程序来获得“int、long、longlong”等数据类型的大小,由于交叉编译器所编译出的程序无法在编译主机上运行而产生错误。
(3)解决办法:可以通过修改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
四、遇到的问题
(1)问题内容:
./configure:error:can not detect int size
cat:/home/ubuntu/packets/nginx-1.6.2/build/autotest.c: No such file ordirectory
(2)原因分析:检测不到size
(3)解决办法:两个问题,一个是检测不到size。一个是找不到测试程序源代码。
先解决 size 问题,修改 auto/types/sizeof 文件,找到 ngx_size= 这一行,改为 ngx_size=4
修改完之后,再次configure,发现 autotest.c 的问题也消失了。
五、遇到的问题:
(1)问题内容:sha1 library is not found
(2)原因分析:弄不清楚
(3)解决办法:configure的时候增加参数 --with-http_ssl_module
六、遇到的问题:
(1)问题内容:./configure : error :SSL modules require the OpenSSL library.
(2)原因分析:没有说清楚openssl的源代码路径
(3)解决办法:指明openssl代码的路径。--with-openssl=DIR(DIR表示openssl源代码解压的路径)
七、遇到的问题:
(1)问题内容: make:没有什么可以做的为`default'。
(2)原因分析:没有说清楚openssl的源代码路径
(3)解决办法:
进入build目录(具体是哪个目录,需要察看Makefile,执行make)
譬如说我的 configure 生成的 Makefile 里面有一段
build:
$(MAKE) -f /home/ubuntu/packets/nginx-1.6.2/build/Makefile
$(MAKE) -f /home/ubuntu/packets/nginx-1.6.2/build/Makefile manpage
所以应该进入/home/ubuntu/packets/nginx-1.6.2/build目录,执行make
八、遇到的问题
(1)问题内容:没有规则可以创建 “/home/ubuntu/packets/nginx-1.4.7/build/src/core/nginx.o” 需要的目标 “src/core/nginx.h” 。
(2)原因分析:不知道是不是软件的 bug,指定build路径后,build路径里面的源代码是空的
(3)解决办法:把主目录的src文件夹里面的东西,拷贝到build目录(build目录请根据自己实际情况进行确定,参考上面那个问题)
九、遇到的问题
(1)问题内容:
checking whether the C compiler works... configure: error: cannot run Ccompiled programs.
If you meant to cross compile, use `--host'.
See`config.log' for more details.
make:*** [/home/ubuntu/packets/pcre-6.7/Makefile]错误1
(2)原因分析:出错的是pcre而不是openssl。原因是编译 pcre 没有指定 `--host' 参数
(3)解决办法:
打开build目录的Makefile,找到 pcre 参数设定的那几行。
/home/ubuntu/packets/pcre-6.7/Makefile : /home/ubuntu/packets/nginx-1.6.2/build/Makefile
cd /home/ubuntu/packets/pcre-6.7 \
&& if [ -f Makefile ] ; then $(MAKE) distclean; fi \
&& CC="$(CC)" CFLAGS="-O2 -fomit-frame-pointer -pipe "\
./configure --disable-shared
在configure那里再添加一个参数 --host=mips-openwrt-linux(具体添加需要根据实际情况,参考我的编译器是mips-openwrt-linux-gcc,所以host是mips-openwrt-linux)
十、遇到的问题
(1)问题内容:
src/os/unix/ngx_errno.c : In function'ngx_strerror':
src/os/unix/ngx_errno.c : 37 : 31 :error:'NGX_SYS_NERR' undeclared (first use in this function)
src/os/unix/ngx_errno.c : 37 : 31 :note: each undeclared identifier is reported only once for eachfunction it appears in
src/os/unix/ngx_errno.c : In function'ngx_strerror_init':
src/os/unix/ngx_errno.c : 58 :11 : error : 'NGX_SYS_NERR' undeclared (first use in this function)
make:*** [/home/ubuntu/packets/nginx-1.6.2/build/src/os/unix/ngx_error.o] 错误1
(2)原因分析:
NGX_SYS_NERR未定义,NGX_SYS_NERR正常情况下应定义在objs/ngx_auto_config.h文件中,特别注意,这是一个auto性质的文件,只有在执行了./configure后,才能生成这个文件。宏NGX_SYS_NERR的意义为,在Linux系统中有132个错误编码。
(3)解决办法:
找到ngx_auto_config.h这个文件(我的ngx_auto_config.h在build目录,不同的configure参数,这个文件的位置可能不一样)
在文件中添加如下三行:
#ifndef NGX_SYS_NERR
#define NGX_SYS_NERR 132
#endif
然后,继续执行make。
十一、遇到的问题
(1)问题内容:
./dftables pcre_chartables.c/bin/bash:./dftables:无法执行二进制文件
(2)原因分析:dftables需要在编译的时候运行,而交叉编译的程序是不能运行的,要用系统的gcc来编译才能运行
(3)解决办法:用x86的gcc来单独编译dftables(我的是ubuntu自带的gcc)。
进入pcre代码所在的目录,并且执行
gcc -Wall -O2 -DCROSS_COMPILE dftables.c -o dftables
执行完之后,返回刚才的目录,执行 make 继续编译
十二、遇到的问题
(1)问题内容:
openssl-1.0.1i/.openssl/lib/libssl.a(s23_meth.o):Relocations in generic ELF(EM: 3)libssl.a:could not read symbols: File in wrong format
(2)原因分析:原因是编译在nginx执行configure的时候,交叉编译参数没有很好的传到openssl。
(3)解决办法:
打开主目录的Makefile,搜索到包含openssl-1.0.1i/.openssl/include/openssl/ssl.h的那一行(可能不同的版本会有一点点区别,搜索openssl,找到最接近那行)
然后,在config的参数那里增加一个,指明交叉编译器所在的路径(请根据实际情况设定)
--cross-compile-prefix=/home/ubuntu/............/mips-openwrt-linux-
(需要注意的是,最后那里的横杠不能少,如果编译器是mips-openwrt-linux-gcc那就是mips-openwrt-linux-,如果是arm-linux-gcc就是arm-linux-)
修改完makefile之后,会导致make把所有东西都构建一遍,所以之前已经解决了的那几个问题可能会重新出现,需要再解决一回。
十三、遇到的问题
(1)问题内容:
x86cpuid.s:Assembler messages:x86cpuid.s:168:Error: Unrecognized opcode `hlt'
(2)原因分析:原因是编译在nginx执行configure的时候,交叉编译参数没有很好的传到openssl。
(3)解决办法:找到上面那个问题添加--cross-compile-prefix的那一行,继续添加一个参数no-asm,禁用汇编代码。
十四、遇到的问题
(1)问题内容:
./home/ubuntu/packets/nginx-1.6.2/build/src/core/ngx_cycle.o:In function `ngx_init_cycle':
/home/ubuntu/packets/nginx-1.6.2/build/src/core/ngx_cycle.c:457:undefined reference to `ngx_shm_free'
/home/ubuntu/packets/nginx-1.6.2/build/src/core/ngx_cycle.c:462:undefined reference to `ngx_shm_alloc'
/home/ubuntu/packets/nginx-1.6.2/build/src/core/ngx_cycle.c:648:undefined reference to `ngx_shm_free'
/home/ubuntu/packets/nginx-1.6.2/build/src/event/ngx_event.o:In function `ngx_event_module_init':
/home/ubuntu/packets/nginx-1.6.2/build/src/event/ngx_event.c:525:undefined reference to `ngx_shm_alloc'
collect2:ld returned 1 exit status
make:*** [/home/ubuntu/packets/nginx-1.6.2/build/nginx]错误1
(2)原因分析:`ngx_shm_free'函数未定义
通过查看源码可以发现,`ngx_shm_free'定义在
src/os/unix/ngx_shmem.c文件中,这个函数要正常使用的话必须要求
“NGX_HAVE_MAP_ANON、NGX_HAVE_MAP_DEVZERO、NGX_HAVE_SYSVSHM”这三个宏中有一个被定义。
(3)解决办法:
修改ngx_auto_config.h ,加入这几行:
#ifndef NGX_HAVE_SYSVSHM
#define NGX_HAVE_SYSVSHM 1
#endif