Ubuntu下安装Nginx

时间:2023-03-09 22:23:54
Ubuntu下安装Nginx

我使用的环境是64位 Ubuntu 14.04, Nginx是Nginx 1.10.0

nginx依赖以下模块:

l  gzip模块需要 zlib 库

l  rewrite模块需要 pcre 库

l  ssl 功能需要openssl库

1.1.安装pcre

  1. 获取pcre编译安装包,在http://www.pcre.org/上可以获取当前最新的版本
  2. 解压缩pcre-xx.tar.gz包。
  3. 进入解压缩目录,执行sudo ./configure。
  4. sudo make & make install

  注:目前我下载的这个nginx只支持pcre, 不支持pcre2。

  若出现configure: error: You need a C++ compiler for C++ support.说明系统还没有提供C/C++的编译环境,因此需要手动安装。使用命令 sudo apt-get install build-essential 进行安装。

1.2.安装openssl

  1. 获取openssl编译安装包,在http://www.openssl.org/source/上可以获取当前最新的版本。
  2. 解压缩openssl-xx.tar.gz包。
  3. 进入解压缩目录,执行sudo ./config。
  4. sudo make & make install

1.3.安装zlib

  1. 获取zlib编译安装包,在http://www.zlib.net/上可以获取当前最新的版本。
  2. 解压缩openssl-xx.tar.gz包。
  3. 进入解压缩目录,执行sudo ./configure。
  4. sudo make & make install

1.4.安装nginx

  1. 获取nginx,在http://nginx.org/en/download.html上可以获取当前最新的版本。
  2. 解压缩nginx-xx.tar.gz包。
  3. 进入解压缩目录,执行 sudo ./configure
  4. sudo make & make install

若安装时找不到上述依赖模块,使用--with-openssl=<openssl_dir>、--with-pcre=<pcre_dir>、--with-zlib=<zlib_dir>指定依赖的模块目录。如已安装过,此处的路径为安装目录;若未安装,则此路径为编译安装包路径,nginx将执行模块的默认编译安装。

启动nginx之后,浏览器中输入http://localhost可以验证是否安装启动成功。

注:

如果安装nginx后,启动时报如下错误:

sunny@sunny-ThinkPad-T440:/usr/local/nginx$ ./sbin/nginx
./sbin/nginx: error while loading shared libraries: libpcre.so.: cannot open shared object file: No such file or directory

有错误可以看出是找不到"libpcre.so.1"文件,查看具体原因:

sunny@sunny-ThinkPad-T440:/usr/local/nginx/sbin$ ldd $(which /usr/local/nginx/sbin/nginx)
linux-vdso.so. => (0x00007ffd0d73b000)
libdl.so. => /lib/x86_64-linux-gnu/libdl.so. (0x00007f432d6ad000)
libpthread.so. => /lib/x86_64-linux-gnu/libpthread.so. (0x00007f432d490000)
libcrypt.so. => /lib/x86_64-linux-gnu/libcrypt.so. (0x00007f432d257000)
libpcre.so. => not found
libcrypto.so.1.0. => /lib/x86_64-linux-gnu/libcrypto.so.1.0. (0x00007f432cdfc000)
libz.so. => /usr/local/lib/libz.so. (0x00007f432cbe1000)
libc.so. => /lib/x86_64-linux-gnu/libc.so. (0x00007f432c818000)
/lib64/ld-linux-x86-.so. (0x000055c946fde000)

注:ldd检测某条命令需要哪些共享库文件的支持

解决方法:

sudo ln -s /usr/local/lib/libpcre.so. /lib/x86_64-linux-gnu/