centos7 安装 nginx(终端下安装命令)
windows和linux传输软件:FileZilla
第一种方法:通过编译安装(程序员必备)
- 1、下载Nginx包
网址:http://nginx.org/en/download.html - 2、解压
tar -zxvf nginx-x.xx.x.tar.gz(x.xx.x为nginx的版本号)
cd nginx-x.xx.x
- 3、配置
默认配置
./configure
自定义配置
./configure \
--prefix=/usr/local/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--pid-path=/usr/local/nginx/conf/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi
注:将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp及nginx目录
- 3.1配置出错,需安装依赖包
一. gcc 安装
安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装:
yum install gcc-c++
二. PCRE pcre-devel 安装
PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。命令:
yum install -y pcre pcre-devel
三. zlib 安装
zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。
yum install -y zlib zlib-devel
四. OpenSSL 安装
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。
yum install -y openssl openssl-devel
- 4、安装
make
make install
- 4.1安装报错
报错信息:make[1]: Leaving directory `/xxx/xxx/nginx-1.12.2'
解决方案:别理它,查看/usr/local下有nginx文件包即可
- 5、启动
cd /usr/local/nginx/sbin
./nginx
- 5.1、启动出错
报错信息:nginx: [emerg] mkdir() "/var/temp/nginx/client" failed (2: No such file or directory)
解决命令:
mkdir -p /var/temp/nginx/client
- 6、停止
./nginx -s stop
./nginx -s quit
./nginx -s quit:此方式停止步骤是待nginx进程处理任务完毕进行停止。
./nginx -s stop:此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。
- 7、重启
1.先停止再启动
./nginx -s quit
./nginx
2.重新加载配置文件:
./nginx -s reload
- 8、开机自启
vi /etc/rc.local
增加一行数据 /usr/local/nginx/sbin/nginx
chmod 755 rc.local
- 9、其他命令
1、测试配置文件
./nginx -t
2、查看nginx版本信息
./nginx -v(V)
更多命令请输入
- 10、配置环境变量
vi /etc/profile
增加数据
#nginx configure
export NGINX_HOME=/usr/local/nginx
export PATH=$PATH:$NGINX_HOME/sbin
注意:NGINX_HOME的位置可由 whereis nginx 命令得出
然后就可在任意位置使用nginx命令啦,如 nginx -v
第二种方法:通过yum安装
centos7系统库中默认是没有nginx的rpm包的,所以我们自己需要先更新下rpm依赖库
- 1、安装 nginx 的库
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
- 2、安装 nginx
yum install nginx