1.5 nginx软件的编译安装步骤
PS:也可以参考官方文档:从源头构建nginx:http://nginx.org/en/docs/configure.html 1.5.1先 检查软件安装的系统环境
cat /etc/redhat-release
uname –r 1.5.2 下载nginx软件---1.12.2 复制链接地址(统一位置进行下载)
mkdir -p /server/tools
cd /server/tools
wget -q http://nginx.org/download/nginx-1.12.2.tar.gz 1.5.3 安装nginx的依赖包(pcre-devel openssl-devel)
yum install -y pcre-devel openssl-devel
PS:不安装pcre,不能使用rewrite模块(rewrite需要识别perl语言的正则表达式)
PS:openssl:对网站远程访问进行加密 1.5.4 编译安装软件的步骤
提前条件:如果系统没有安装兼容程序库、开发工具的,请安装一下基础依赖包
yum install gcc gcc-c++ automake autoconf -y
1. 解压到当前目录nginx-1.12.2.tar.gz
tar xf nginx-1.12.2.tar.gz
2. 进入nginx-1.12.2目录
cd nginx-1.12.2/
3. 创建www虚拟用户
PS:让www用户专门管理nginx服务,root权限太大,不安全
useradd -M -s /sbin/nologin www
3. 指定安装路径,配置参数(下面有参数解释)
./configure --prefix=/application/nginx-1.12.2 --user=www --group=www --with-http_stub_status_module --with-http_ssl_module
4. 编译(翻译)
PS:编译过程实质是将各种程序语言转换为系统可以识别的二进制信息
make
5. 编译安装
make install
6. 创建程序目录软连接
PS:避免版本经常变化,到时候只需更改软连接,不需要更改代码程序
ln -s /application/nginx-1.12.2/ /application/nginx
7. 启动网站服务
/application/nginx/sbin/nginx
8. 检查服务是否启动成功
ps -ef |grep nginx
9. 测试访问
lynx 10.0.0.7
curl 10.0.0.7
浏览器访问方式 1.6 编辑安装配置参数说明
./configure
--prefix=PATH ——指定软件程序安装的路径信息
--user=USER ——创建一个虚拟用户,用于管理nginx服务的worker进程
--group=GROUP ——创建一个虚拟用户组,用于管理nginx服务的worker进程
--with-http_ssl_module ——让nginx服务可以支持https访问
--with-http_stub_status_module ——便于监控软件监视nginx服务运行状态 以下参数可参考(默认有上面的即可):
--sbin-path=/usr/local/nginx/nginx
--conf-path=/usr/local/nginx/nginx.conf
--pid-path=/usr/local/nginx/nginx.pid
--with-pcre=../pcre-8.41
--with-zlib=../zlib-1.2.11 1.7 nginx命令参数
/application/nginx/sbin/nginx ——启动nginx
/application/nginx/sbin/nginx -s>——平滑重启
/application/nginx/sbin/nginx -s stop ——停止服务
/application/nginx/sbin/nginx -t ——配置文件语法检查
/application/nginx/sbin/nginx -h ——查看参数使用帮助信息
/application/nginx/sbin/nginx -V ——查看软件编译安装配置参数信息 1.7.1 nginx 查看软件编译安装配置参数信息(-V)
[root@web01 application]# /application/nginx/sbin/nginx -V
nginx version: nginx/1.10.3
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled configure arguments: --prefix=/application/nginx-1.10.3 --user=www --group=www --with-http_stub_status_module --with-http_ssl_module
PS:一般用于查看之前配置nginx的参数 1.7.2 nginx 查看参数使用帮助信息(-h)
[root@web01 application]#/application/nginx/sbin/nginx -h
nginx version: nginx/1.10.3
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen,> -p prefix : set prefix path (default: /application/nginx-1.10.3/)
-c filename : set configuration file (default: conf/nginx.conf)
-g directives : set global directives out of configuration file 1.8 部署nginx软件过程中可能遇到的问题 1.软件依赖包未正确安装问题---PCRE依赖包没有安装
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre= option. 解决方法:yum install pcre pcre-devel –y
2.软件依赖包未正确安装问题---OPENSSL依赖包没有安装
./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl= option. 解决方法:yum install openssl openssl-devel -y 1.9 nginx软件启动过程中可能遇到的问题
1. 启动Nginx时如下报错“nginx:[emerg]getpwnam(“nginx”)failed”
解答:这是因为没有对应的Nginx服务用户,执行useradd nginx -s /sbin/nologin -M创建 1.10 nginx软件程序目录结构
ls -l /application/nginx/
drwxr-xr-x 2 root root 4096 Sep 8 21:22 conf ---配置文件保存目录
drwxr-xr-x 2 root root 4096 Sep 8 21:22 html ---站点目录
drwxr-xr-x 2 root root 4096 Sep 8 21:52 logs ---nginx服务相关日志文件保存目录(错误日志 访问日志)
drwxr-xr-x 2 root root 4096 Sep 8 21:22 sbin ---服务命令保存目录(只有一个nginx命令文件) 1.10.1 nginx配置文件目录conf下的主要文件说明:
mime.types ——媒体资源类型文件
nginx.conf ——nginx服务主配置文件
nginx.conf.default ——nginx服务主配置文件的默认配置(模板)
PS: vimdiff nginx.conf nginx.conf.default ——可以比较俩个文件之间的区别(实用性广泛) 1.11 测试环境模拟搭建网站 1.11.1 精简化nginx.conf主配置文件内容
grep -Ev "#|^$" nginx.conf.default > nginx.conf
PS:过滤掉包含#号的行,和空行 1.11.2 编辑主配置文件前先备份
cp nginx.conf {,.bak} 1.11.3 编辑主配置文件
vim /application/nginx/conf/nginx.conf
worker_processes 1; ——worker主进程数(相当于服务员)进程数越多效率越高
events { ——事件区块开始
worker_connections 1024; ——每个主进程的最大连接数(想当于客户)
} ——事件区块结束
http { ——http区块开始
include mime.types; ——支持的媒体类型文件
default_type application/octet-stream; ——默认支持的媒体类型文件
sendfile on; ——高效传输模式(基于磁盘block传输方式,要快许多)
keepalive_timeout 65; ——连接超时(65s内没有数据传输即断开)
server { ——第一个server区块(就是一个网站(虚拟主机),可以有多个)
listen 80; ——监听端口
server_name www.georgekai.com; ——提供服务的域名
location / { ——第一个location(管理部分网站资源文件)
root html/www; ——站点根目录(存放首页文件、图片...)
index georgekai.html index.htm; ——网站默认首页文件(空格分割多个文件)
} ——第一个server区块结束
error_page 500 502 503 504 404 /kai.jpg; ——对应的http状态码,使用kai.jpg回应客户
location = /kai.jpg { ——第二个server区块...(第二个网站(虚拟主机))
root html; .......同上........
}
}
} 1.11.4 创建站点目录,生成首页文件
1. 创建站点目录
mkdir /application/nginx/html/www
2. 生成首页文件(html语言)
vim georgekai.html