Nginx的安装有两种方式,一种是通过Tar包进行源码安装,一种是通过yum进行安装。下面分别就两种安装方式进行简单的描述:
一、通过Tar源码安装
#新建www组 和 www用户 并将www用户添加到www用户组里
groupadd www
useradd -g www www
#先定位到/usr/local目录
cd /usr/local
#环境准备 安装依赖库
#基本环境安装
yum -y install gcc gcc-c++ autoconf automake make
yum -y install zlib zlib-devel openssl openssl--devel
Nginx源码安装需要依赖下面3个包:
1.gzip:模块需要zlib库(下载: http://www.zlib.net/)
2.rewrite支持:需要pcre库(下载: http://www.pcre.org/)
3.https支持:ssl功能需要openssl库(下载: http://www.openssl.org/)
#下载安装包
wget https://ftp.pcre.org/pub/pcre/pcre-8.42.tar.gz
wget http://nginx.org/download/nginx-1.13.12.tar.gz
#安装PCRE依赖库
tar -zxvf /usr/local/pcre-8.42.tar.gz
cd pcre-8.42
./configure && make && make install
#安装Nginx
tar -zxvf /usr/local/nginx-1.13.12.tar.gz
cd nginx-1.13.12
./configure --prefix=/usr/local/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --user=www --group=www --with-pcre=/usr/local/pcre-8.42 --with-http_stub_status_module&& make && make install
#启动Nginx
#使用源码安装Nginx,不会像yum install安装后自动生成Nginx服务控制脚本
cd nginx/sbin
./nginx
二、yum安装
#查看系统版本
rpm -qa | grep centos-release
#创建nginx yum操作的repo文件
vi /etc/yum.repos.d/nginx.repo
==============================================================
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/x86_64/
gpgcheck=0
enabled=1
==============================================================
#或者直接添加资源库文件
rpm -Uvh http://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.14.0-1.el7_4.ngx.x86_64.rpm
#安装Nginx
yum install -y nginx
#刚安装的Nginx不会自行启动。启动Nginx
systemctl start nginx.service
yum方式安装后,默认Web主目录在/usr/share/nginx/html,可通过查看配置文件找到Web目录。
三、异常处理
安装异常处理:
1、./configure: error: the HTTP rewrite module requires the PCRE library.
缺失pcre依赖库,安装PCRE库并使用--with-pcre=/usr/local/pcre-8.42指定PCRE安装位置
PCRE有两个版本,注意不要使用PCRE2,使用wget https://ftp.pcre.org/pub/pcre/pcre2-10.31.tar.gz安装后,Nginx不能正常安装。
2、make时提示 “./configure: 没有那个文件或目录错误 ”
使用源码安装Nginx时,PCRE不能使用yum的安装目录,需要使用PCRE的Tar源码包进行安装。
3、make时提示 "conf/koi-win" 与"/usr/local/nginx/conf/koi-win" 为同一文件
源码安装时,通过--prefix=/usr/local/nginx指定安装Nginx位置,安装目录与源码在不同目录即可。
4、安装完成后,浏览器直接访问不成功
#看看是否是防火墙阻挡
systemctl stop firewalld.service
systemctl status firewalld