Ubuntu 中源码安装Nginx
在Ubuntu中,apt-get install nginx 的版本比较旧,想要使用一些高级功能就需要从官方下载,通过源码安装。 之前的安装过程一直出现问题,特将此记录在案:
准备工作:
- 下载 nginx : http://nginx.org/en/download.html
- 下载 PCRE : ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/
1.1 安装PCRE :
Nginx中使用正则相关功能的依赖,所以需要安装PCRE:
tar zxvf pcre-8.38.tar.gz cd pcre-8.38 ./configure --prefix=/usr/local/pcre-8.38 #安装到/usr/local/pcre-8.38 下 make sudo make install
如果上面命令报错:
libtool: compile: unrecognized option `-DHAVE_CONFIG_H' libtool: compile: Try `libtool --help' for more information. make[1]: *** [pcrecpp.lo] Error 1
则安装:build-essential
apt-get install build-essential
1.2 安装依赖库:
sudo apt-get install libssl1.0.0 libssl-dev openssl zlib1g-dev
如果出现错误如:
The following packages have unmet dependencies: libssl-dev : Depends: libssl1.0.0 (= 1.0.1f-1ubuntu2.21) but 1.0.1f-1ubuntu9 is to be installed Recommends: libssl-doc but it is not going to be installed E: Unable to correct problems, you have held broken packages.
在对应依赖后面加 /trusty (依赖冲突) 如:
sudo apt-get install libssl1.0.0/trusty libssl-dev/trusty openssl/trusty zlib1g-dev/trusty
1.3 安装nginx:
tar zxvf nginx-1.10.1.tar.gz cd nginx-1.10.1 ./configure --prefix=/usr/local/nginx --pid-path=/var/run/nginx.pid --with-http_stub_status_module --with-http_ssl_module --with-pcre=/root/pcre-8.38 make sudo make install
注意:–with-pcre 的值为 pcre 解压后的文件目录,不是指定的安装目录,切记;
完成之后,需要将/usr/local/nginx/logs 目录设置为可写权限,里面是一系列得日志文件:
# sudo chmod 777 -R /usr/local/nginx/logs
1.4 注意事项
在源码安装的时候,需要安装编译环境,gcc ubuntu 下,可以直接 apt-get 安装:
sudo apt-get install libtool sudo apt-get install gcc-c++
安装 gcc 出现如下错误:
安装 gcc-c++出错
Reading package lists... Done Building dependency tree Reading state information... Done E: Couldn't find package gcc-c
则先如下操作,再次安装:
sudo apt-get install build-essential sudo apt-get update sudo apt-get upgrade sudo apt-get install gcc-c++
1.5 启动和停止/重启命令:
sudo /etc/init.d/nginx start sudo /etc/init.d/nginx stop sudo /etc/init.d/nginx restart