天天看点

Ubuntu 安装nginx

linux安装nginx需要先安装 openssl zlib pcre等,关于openssl以及zlib的安装可以参考我的另一篇博客《redis集群配置》下面直接介绍 pcre的安装和nginx的安装

PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库,这里通过pcre的源码来安装

首先下载pcre的源码,下载地址点击here,在这里我的为8.31版本

将pcre-8.3…tar.gz放到/usr/pcre/目录解压进入解压目录执行./configure --enable-utf8(注意enable前面有两个-) 关于configure 的参数可以执行./configure -help查看,

执行./configure --enable-utf8 部分编译代码如下

pcre-8.31 configuration summary:

    Install prefix .................. : /usr/local
    C preprocessor .................. : gcc -E
    C compiler ...................... : gcc
    C++ preprocessor ................ : g++ -E
    C++ compiler .................... : g++
    Linker .......................... : /usr/bin/ld -m elf_x86_64
    C preprocessor flags ............ : 
    C compiler flags ................ : -O2
    C++ compiler flags .............. : -O2
    Linker flags .................... : 
    Extra libraries ................. : 

    Build 8 bit pcre library ........ : yes
    Build 16 bit pcre library ....... : no
    Build C++ library ............... : yes
    Enable JIT compiling support .... : no
    Enable UTF-8/16 support ......... : yes
    Unicode properties .............. : no
    Newline char/sequence ........... : lf
    \R matches only ANYCRLF ......... : no
    EBCDIC coding ................... : no
    Rebuild char tables ............. : no
    Use stack recursion ............. : yes
    POSIX mem threshold ............. : 10
    Internal link size .............. : 2
    Match limit ..................... : 10000000
    Match limit recursion ........... : MATCH_LIMIT
    Build shared libs ............... : yes
    Build static libs ............... : yes
    Use JIT in pcregrep ............. : no
    Buffer size for pcregrep ........ : 20480
    Link pcregrep with libz ......... : no
    Link pcregrep with libbz2 ....... : no
    Link pcretest with libedit ...... : no
    Link pcretest with libreadline .. : no

[email protected]:/usr/pcre/pcre-8.31# 

           

注意如果没有安装 g++,编译部分如下 可以发现没有c++ preprocessor 和c++copiler,接着执行make 就会报错:libtool: compile: unrecognized option `-DHAVE_CONFIG_H

C++ preprocessor ................ :
 C++ compiler .................... : 
           

一般情况下Ubuntu 默认装有gcc但是没有g++关于g++的安装可以参考我的另外一篇博客Ubuntu安装g++

执行make指令

[email protected]:/usr/pcre/pcre-8.31# make
rm -f pcre_chartables.c
ln -s ./pcre_chartables.c.dist pcre_chartables.c
make  all-am
make[1]: Entering directory `/usr/pcre/pcre-8.31'
  CC     pcre_byte_order.lo
  CC     pcre_compile.lo
  CC     pcre_config.lo
  CC     pcre_dfa_exec.lo
  CC     pcre_exec.lo
  CC     pcre_fullinfo.lo
  CC     pcre_get.lo
  CC     pcre_globals.lo
  CC     pcre_jit_compile.lo
  CC     pcre_maketables.lo
  CC     pcre_newline.lo
  CC     pcre_ord2utf8.lo
  CC     pcre_refcount.lo
  CC     pcre_string_utils.lo
  CC     pcre_study.lo
  CC     pcre_tables.lo
  CC     pcre_ucd.lo
  CC     pcre_valid_utf8.lo
  CC     pcre_version.lo
  CC     pcre_xclass.lo
  CC     pcre_chartables.lo
  CCLD   libpcre.la
  CC     pcreposix.lo
  CCLD   libpcreposix.la
  CXX    pcrecpp.lo
  CXX    pcre_scanner.lo
  CXX    pcre_stringpiece.lo
  CXXLD  libpcrecpp.la
  CC     pcretest.o
  CC     pcre_printint.o
  CCLD   pcretest
  CC     pcregrep.o
  CCLD   pcregrep
  CXX    pcrecpp_unittest.o
  CXXLD  pcrecpp_unittest
  CXX    pcre_scanner_unittest.o
  CXXLD  pcre_scanner_unittest
  CXX    pcre_stringpiece_unittest.o
  CXXLD  pcre_stringpiece_unittest
make[1]: Leaving directory `/usr/pcre/pcre-8.31'
[email protected]:/usr/pcre/pcre-8.31# 
           

接着 make install 部分执行如下

make[3]: Entering directory `/usr/pcre/pcre-8.31'
ln -sf pcre_assign_jit_stack.3           /usr/local/share/man/man3/pcre16_assign_jit_stack.3
ln -sf pcre_compile.3                    /usr/local/share/man/man3/pcre16_compile.3
ln -sf pcre_compile2.3                   /usr/local/share/man/man3/pcre16_compile2.3
ln -sf pcre_config.3                     /usr/local/share/man/man3/pcre16_config.3
ln -sf pcre_copy_named_substring.3       /usr/local/share/man/man3/pcre16_copy_named_substring.3
ln -sf pcre_copy_substring.3             /usr/local/share/man/man3/pcre16_copy_substring.3
ln -sf pcre_dfa_exec.3                   /usr/local/share/man/man3/pcre16_dfa_exec.3
ln -sf pcre_exec.3                       /usr/local/share/man/man3/pcre16_exec.3
ln -sf pcre_free_study.3                 /usr/local/share/man/man3/pcre16_free_study.3
ln -sf pcre_free_substring.3             /usr/local/share/man/man3/pcre16_free_substring.3
ln -sf pcre_free_substring_list.3        /usr/local/share/man/man3/pcre16_free_substring_list.3
ln -sf pcre_fullinfo.3                   /usr/local/share/man/man3/pcre16_fullinfo.3
ln -sf pcre_get_named_substring.3        /usr/local/share/man/man3/pcre16_get_named_substring.3
ln -sf pcre_get_stringnumber.3           /usr/local/share/man/man3/pcre16_get_stringnumber.3
ln -sf pcre_get_stringtable_entries.3    /usr/local/share/man/man3/pcre16_get_stringtable_entries.3
ln -sf pcre_get_substring.3              /usr/local/share/man/man3/pcre16_get_substring.3
ln -sf pcre_get_substring_list.3         /usr/local/share/man/man3/pcre16_get_substring_list.3
ln -sf pcre_jit_stack_alloc.3            /usr/local/share/man/man3/pcre16_jit_stack_alloc.3
ln -sf pcre_jit_stack_free.3             /usr/local/share/man/man3/pcre16_jit_stack_free.3
ln -sf pcre_maketables.3                 /usr/local/share/man/man3/pcre16_maketables.3
ln -sf pcre_pattern_to_host_byte_order.3 /usr/local/share/man/man3/pcre16_pattern_to_host_byte_order.3
ln -sf pcre_refcount.3                   /usr/local/share/man/man3/pcre16_refcount.3
ln -sf pcre_study.3                      /usr/local/share/man/man3/pcre16_study.3
ln -sf pcre_utf16_to_host_byte_order.3   /usr/local/share/man/man3/pcre16_utf16_to_host_byte_order.3
ln -sf pcre_version.3                    /usr/local/share/man/man3/pcre16_version.3
make[3]: Leaving directory `/usr/pcre/pcre-8.31'
make[2]: Leaving directory `/usr/pcre/pcre-8.31'
make[1]: Leaving directory `/usr/pcre/pcre-8.31'
[email protected]:/usr/pcre/pcre-8.31# 
           

完成 ,接着来装 nginx。我的nginx是1.8.1版本的下载地址点击here

执行./configure 命令发现报错了

checking for OS
 + SunOS 5.10 sun4v
checking for C compiler ... not found
           

可是我明明安装了gcc啊,不要急 将环境变量文件 /etc/profile中加入

CC=gcc

export CC

重启 或者 source /etc/profile即可

再次执行./configure 关于configure 的参数可以执行 ./configure -help查看 如果执行过后 报错 找不到 PCRE library 和 OpenSSL library 库 , PCRE library not found,OpenSSL library not found 可以执行 ./configure --with-pcre=/(pcre的源码包) --with-openssl=/(openssl源码包) 注意如果安装没有问题 直接执行./configure 就好了 openssl library 并不是必须的,但是没有 pcre library 就必须指定–with-pcre=/(pcre的源码包)

执行./configure --with-pcre=/(源码包路径) --with-openssl=/(onpenssl源码包路径) 部分代码如下

[email protected]:/usr/nginx/nginx-1.8.1# ./configure 
checking for OS
 + Linux 3.13.0-32-generic x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3) 
checking for gcc -pipe switch ... found
checking for gcc builtin atomic operations ... found
checking for C99 variadic macros ... found
checking for gcc variadic macros ... found

checking for md5 in system md library ... not found
checking for md5 in system md5 library ... not found
checking for md5 in system OpenSSL crypto library ... not found
checking for sha1 in system md library ... not found
checking for sha1 in system OpenSSL crypto library ... not found
checking for zlib library ... found
creating objs/Makefile

Configuration summary
  + using system PCRE library
  + using OpenSSL library is not used
  + using builtin md5 code
  + sha1 library is not found
  + 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"

[email protected]:/usr/nginx/nginx-1.8.1#
           

执行make 部分代码如下

make[1]: Leaving directory `/usr/nginx/nginx-1.8.1'
make -f objs/Makefile manpage
make[1]: Entering directory `/usr/nginx/nginx-1.8.1'
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 `/usr/nginx/nginx-1.8.1'
[email protected]:/usr/nginx/nginx-1.8.1# 
           

执行 make install 成功 启动nginx服务

[email protected]:/usr/local/nginx/sbin# ls 
nginx
[email protected]:/usr/local/nginx/sbin# ./nginx 
[email protected]:/usr/local/nginx/sbin# ps aux | grep nginx
root      3179  0.1  0.0  22348   596 ?        Ss   15:37   0:00 nginx: master process ./nginx
nobody    3180  0.0  0.0  22780   988 ?        S    15:37   0:00 nginx: worker process
root      3183  0.0  0.0   8864   644 pts/11   S+   15:37   0:00 grep --color=auto nginx
[email protected]:/usr/local/nginx/sbin# 
           

3179是nginx主进程的进程id,3180是nginx工作进程的进程id

启动成功 通过 浏览器查看一下

Ubuntu 安装nginx

安装成功

停止nginx

方式1,快速停止:

cd /usr/local/nginx/sbin

./nginx -s stop

此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。

方式2,完整停止(建议使用):

cd /usr/local/nginx/sbin

./nginx -s quit

此方式停止步骤是待nginx进程处理任务完毕进行停止。

重启nginx

方式1,先停止再启动(建议使用):

对nginx进行重启相当于先停止nginx再启动nginx,即先执行停止命令再执行启动命令。

如下:

./nginx -s quit

./nginx

方式2,重新加载配置文件:

当nginx的配置文件nginx.conf修改后,要想让配置生效需要重启nginx,使用-s reload不用先停止nginx再启动nginx即可将配置信息在nginx中生效,如下:

./nginx -s reload

开机启动nginx

vi /etc/rc.local

加入一行 /usr/local/nginx/sbin/nginx 保存并退出,下次重启会生效。

nginx-1.8.1.tar.gz下载地址

pcre-8.31.tar.gz下载地址

继续阅读