centos 7 下安装 nginx-1.11 前需要先切换到root环境,通过命令 su root 切换,然后再输入密码, 如果不能切换需要把下载的nginx文件夹给予777的权限
bash
- su root
- 密码输入
下载nginx-1.11.10的压缩包文件到根目录,官网下载地址:nginx.org/download/nginx-1.11.10.tar.gz
bash
- yum update #更新系统软件
- cd /
- wget nginx.org/download/nginx-1.11.10.tar.gz
解压tar.gz压缩包文件,进去nginx-1.11.10
bash
- tar -xzvf nginx-1.11.10.tar.gz
- cd nginx-1.11.10
进去后如果发现有configure这个文件,说明这个源码包安装前需要先进行配置,主要是为了检查当前的环境是否满足要安装软件的依赖关系,如果没有这个文件说明是二进制包,解压后直接使用不用configure
bash
- ./configure
通过安装前的配置检查,发现有报错 检查中发现一些依赖库没有找到,这时候需要先安装nginx的一些依赖库
bash
- yum -y install pcre* #安装使nginx支持rewrite
- yum -y install gcc-c++
- yum -y install zlib*
- yum -y install openssl
再次进行检查操作 ./configure 没发现报错显示,接下来进行编译并安装的操作
bash
- #检查模块支持
- ./configure
- --prefix=/usr/local/nginx
- --sbin-path=/usr/local/nginx/sbin/nginx
- --conf-path=/usr/local/nginx/conf/nginx.conf
- --error-log-path=/var/log/nginx/error.log
- --http-log-path=/var/log/nginx/access.log
- --pid-path=/var/run/nginx/nginx.pid
- --lock-path=/var/lock/nginx.lock
- --user=www
- --group=www
- --with-http_ssl_module
- --with-http_stub_status_module
- --with-http_gzip_static_module
- --http-client-body-temp-path=/var/tmp/nginx/client/
- --http-proxy-temp-path=/var/tmp/nginx/proxy/
- --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/
- --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi
- --http-scgi-temp-path=/var/tmp/nginx/scgi
- --with-pcre
- #编译并安装
- make && make install
查看nginx安装后在的目录,可以看到已经安装到 /usr/local/nginx 目录了
bash
- whereis nginx
- $nginx: /usr/local/nginx
启动nginx服务
bash
- cd /usr/local/nginx/sbin/
- ./nginx
服务启动的时候报错了:nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) ,通过命令查看本机网络地址和端口等一些信息,找到被占用的80端口(netstat -ntpl)的tcp连接,并杀死进程(kill 进程pid)
bash
- netstat -ntpl
- kill 进程PID
继续启动nginx服务,启动成功
bash
- ./nginx
通过ping localhost 或在浏览器直接访问,ping成功或出现下面页面则安装成功。
命令:curl localhost/index.html 如果返回html代码也代表安装成功。
安装好后,当系统重启时每次都要去手动启动nginx服务,所以需要把nginx加入开机自启动,参考nginx设置开机启动:http://zixuephp.net/article-206.html
如果要避免这些设置可以通过 yum -y install nginx 软件安装方式安装。yum安装这些操作都是一键完成的,yum方式安装不用去考虑软件的依赖关系和开机启动的设置。