1. gcc 安装 安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装:
yum install -y gcc-c++
2.安装make和libtool依赖
yum -y install make libtool
3. 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
4. zlib 安装 zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。
yum install -y zlib zlib-devel
5. OpenSSL 安装 OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。 nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 CentOS 安装 OpenSSL 库。
yum install -y openssl openssl-devel
6.使用wget命令下载 nginx
确保系统已经安装了wget,如果没有安装,执行 yum install wget 安装。
wget -c https://nginx.org/download/nginx-1.12.0.tar.gz
# wget -c https://nginx.org/download/nginx-1.12.0.tar.gz
--2021-12-11 11:11:59-- https://nginx.org/download/nginx-1.12.0.tar.gz
正在解析主机 nginx.org (nginx.org)... 52.58.199.22, 3.125.197.172, 2a05:d014:edb:5704::6, ...
正在连接 nginx.org (nginx.org)|52.58.199.22|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:980831 (958K) [application/octet-stream]
正在保存至: “nginx-1.12.0.tar.gz”
100%[======================================>] 980,831 923KB/s 用时 1.0s
2021-12-11 11:12:02 (923 KB/s) - 已保存 “nginx-1.12.0.tar.gz” [980831/980831])
也可以直接下载.tar.gz安装包,地址:https://nginx.org/en/download.html
7.解压
tar -zxvf nginx-1.12.0.tar.gz
cd nginx-1.12.0
8.配置
其实在 nginx-1.12.0 版本中你就不需要去配置相关东西,默认就可以了。当然,如果你要自己配置目录也是可以的。 1.使用默认配置
默认地址 /usr/local/nginx
./configure
...
Configuration summary
+ using system PCRE library
+ OpenSSL library is not used
+ using system zlib library
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
配置完毕
2.自定义配置(不推荐)
./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目录
9.编译安装
make
make install
查找安装路径:
# whereis nginx
nginx: /usr/local/nginx
10.启动、停止nginx
cd /usr/local/nginx/sbin/
./nginx
./nginx -s stop
./nginx -s quit
./nginx -s reload
可以检测配置的是否正确
/usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
./nginx -s quit:此方式停止步骤是待nginx进程处理任务完毕进行停止。 ./nginx -s stop:此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。
注:如果启动时报80端口被占用: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
解决方法是安装net-tool 包
yum install -y net-tools
netstat -ntlp
kill -9 $pid //杀掉进程
/usr/local/nginx/sbin/nginx//再次启动nginx
查询nginx进程:
ps aux|grep nginx
11.重启 nginx
1.先停止再启动(推荐): 对 nginx 进行重启相当于先停止再启动,即先执行停止命令再执行启动命令。如下:
cd /usr/local/nginx/sbin/
./nginx -s quit
./nginx
2.重新加载配置文件: 当 ngin x的配置文件 nginx.conf 修改后,要想让配置生效需要重启 nginx,使用-s reload不用先停止 ngin x再启动 nginx 即可将配置信息在 nginx 中生效,如下:
cd /usr/local/nginx/sbin/
./nginx -s reload
启动成功后,在浏览器可以看到这样的页面:
12.配置环境变量
编辑.bashrc文件
vim ~/.bashrc
在.bashrc文件里加入如下
export PATH=$PATH:/usr/local/nginx/sbin/
使.bashrc文件生效
source .bashrc
这样就不用每次都要进入到/usr/local/nginx/sbin/nginx才能执行 nginx命令
nginx -v
nginx version: nginx/1.12.0
13.开机自启动
即在rc.local增加启动代码就可以了。
vim /etc/rc.local
增加一行 /usr/local/nginx/sbin/nginx 设置执行权限:
chmod 755 rc.local
14.卸载nginx
删除nginx文件夹即可
rm -rf /usr/local/nginx
--------------------------------------分割线 --------------------------------------
Nginx负载均衡配置实战 http://www.linuxidc.com/Linux/2014-12/110036.htm
CentOS 6.2实战部署Nginx+MySQL+PHP http://www.linuxidc.com/Linux/2013-09/90020.htm
使用Nginx搭建WEB服务器 http://www.linuxidc.com/Linux/2013-09/89768.htm
搭建基于Linux6.3+Nginx1.2+PHP5+MySQL5.5的Web服务器全过程 http://www.linuxidc.com/Linux/2013-09/89692.htm
CentOS 6.3下Nginx性能调优 http://www.linuxidc.com/Linux/2013-09/89656.htm
CentOS 6.3下配置Nginx加载ngx_pagespeed模块 http://www.linuxidc.com/Linux/2013-09/89657.htm
CentOS 6.4安装配置Nginx+Pcre+php-fpm http://www.linuxidc.com/Linux/2013-08/88984.htm
Nginx安装配置使用详细笔记 http://www.linuxidc.com/Linux/2014-07/104499.htm
Nginx日志过滤 使用ngx_log_if不记录特定日志 http://www.linuxidc.com/Linux/2014-07/104686.htm
--------------------------------------分割线 --------------------------------------
