web-nginx
wget与yum的区别:yum安装目录写好了;wget可以下载包自己建目录编译安装,缺点:需要自己安装依赖。
搭建环境:centos7.8
一、配置源地址
[root@web01 ~]#vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
二、下载安装启动
这里我们选择方法2!!!!!!!!!!!!!!!!!!!!!!
方法1:直接yum安装:
yum install nginx -y
rpm -qa nginx
systemctl start nginx
systemctl status nginx
systemctl enable nginx
systemctl status nginx
#查看状态
netstat -lntup|grep nginx
#浏览器测试:输入ip地址,出现欢迎界面说明安装成功
#查看服务安装路径:rpm -ql nginx
#查看包:rpm -qa nginx
方法2:wget下载,编译安装
#创建目录,到相应目录下载包
mkdir -p /server/tools
cd /server/tools
wget http://nginx.org/download/nginx-1.18.0.tar.gz
#安装依赖。
yum install pcre pcre-devel -y
yum install openssl openssl-devel -y #https加密。
tar xf nginx-1.18.0.tar.gz
cd nginx-1.18.0/
useradd -s /sbin/nologin nginx -M
id nginx
#编译,用来生成 Makefile,为下一步的编译做准备
./configure --user=nginx --group=nginx --prefix=/application/nginx-1.18.0/ --with-http_stub_status_module --with-http_ssl_module --with-pcre
#with三个模块 状态模块 加密模块 支持pcre模块
#编译安装
make && make install
#对源文件建立符号链接,而非硬链接
ln -s /application/nginx-1.18.0/ /application/nginx
#启动服务
/application/nginx/sbin/nginx
#查看服务状态
netstat -lntup|grep nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [error] invalid PID number “” in “/application/nginx-1.18.0//logs/nginx.pid”
使用kill + 进程号(得先查进程) 杀死进程;或者直接 killall -9 nginx(有点点危险)
PS:
1)每一步结尾直接echo $?验证是否正确。返回0代表步骤正确,
如,编译出现错误之后执行echo $?,返回结果不是1
2)验证最终的安装是否正确。(在终端验证)
wget +当前机器ip,如 wget 10.0.0.1
curl +ip
测试:在浏览器地址栏输入当前服务安装所在机器的ip地址,可以看到nginx的欢迎界面即是成功安装
三、编辑站点首页HTML代码
即修改nginx的默认的index.html
[root@web02 /application/nginx/html]# vim index.html
<html>
<head>
<title>老男孩58期</title>
<meta charset="UTF-8">
</head>
<body bgcolor=green>
<br>
<div align=center>
<table border=1>
<tr>
<td>ID</td>
<td>NAME</td>
</tr>
<tr>
<td>001</td>
<td>项博</td>
</tr>
<tr>
<td>002</td>
<td>项伯</td>
</tr>
</table>
测试:在浏览器地址栏输入ip即可看到所修改的界面。
四、实践基于域名的虚拟机
更换默认主页
4.1 配置一个基于域名的虚拟机
1)配置基于域名的nginx.conf内容
#修改了两处:
server_name www.etiantian.org;
root html/www;
#具体修改:
[root@web02 /application/nginx/conf]#cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.etiantian.org;
location / {
root html/www;
index index.html index.htm;
}
}
}
扩展:
egrep -v "^$|#" nginx.conf.default >nginx.conf
筛选出除了空行与#注释掉的内容,重定向到另一个文件
2)创建域名对应的站点目录及文件
[root@web02 /application/nginx/conf]#mkdir ../html/www
[root@web02 /application/nginx/conf]#echo "www.etiantian.org" >>../html/www/index.html
[root@web02 /application/nginx/conf]#cat ../html/www/index.html
www.etiantian.org
3)配置域名解析地址
echo "10.0.0.8 www.etiantian.org" >>/etc/hosts
#检查:ping www.etiantian.org
4)配置环境变量,为了不带全路径启动服务
echo 'PATH="/application/nginx/sbin:$PATH"' >>/etc/profile
. /etc/profile
echo $PATH
#我的echo $PATH结果:
/application/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
5)检查语法、平滑重启
nginx -t
nginx -s reload
#测试,出现www.etiantian.org才算配置成功
curl www.etiantian.org
#查看端口和进程
ps -ef|grep nginx
netstat -lntp|grep 80
出现端口占用:使用kill + 进程号(得先查进程) 杀死进程;或者直接 killall -9 nginx
(有点点危险)
win10下测试:
win键+R---->输入drivers回车—>用记事本打开etc目录下的hosts文件,在hosts文件结尾加上10.0.0.8 www.etiantian.org
并保存退出----->win键+R---->输入cmd回车—>ping www.etiantian.org返回正确结果---->配置ok
在浏览器地址栏输入www.etiantian.org并回车页面显示www.etiantian.org----->域名配置成功
4.2 配置多个基于域名的虚拟主机
在前面基础上编辑/application/nginx/conf/nginx.conf文件增加两个server
1)配置基于域名的nginx.conf内容
[root@web02 /application/nginx/conf]#cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.etiantian.org;
location / {
root html/www;
index index.html index.htm;
}
}
server {
listen 80;
server_name bbs.etiantian.org;
location / {
root html/bbs;
index index.html index.htm;
}
}
server {
listen 80;
server_name blog.etiantian.org;
location / {
root html/blog;
index index.html index.htm;
}
}
}
2)创建域名对应的站点目录及文件
[root@web02 /application/nginx/conf]# mkdir ../html/{bbs,blog}
[root@web02 /application/nginx/conf]# echo "bbs.etiantian.org" >../html/bbs/index.html
[root@web02 /application/nginx/conf]# echo "blog.etiantian.org" >../html/blog/index.html
#检查
[root@web02 /application/nginx/conf]#cat ../html/bbs/index.html
bbs.etiantian.org
[root@web02 /application/nginx/conf]#cat ../html/blog/index.html
blog.etiantian.org
3)配置配置域名解析地址
[root@web02 /application/nginx/conf]#tail -1 /etc/hosts
10.0.0.8 www.etiantian.org bbs.etiantian.org blog.etiantian.org
#检查:
ping 域名,查看是否通畅
curl 域名,出现域名即为正确
用户访问:输入域名,先找本地解析,本地没有找 L DNS解析,再没有就找授权DNS,找到就返回ip地址;用户拿到ip就请求服务器三次握手,tcp三次握手结束之后建立http连接;连接之后发请求报文(请求行,请求头,空行,请求报文主体)
请求行携带host:www.etiantian,请求服务器的网卡80端口,服务器监听到就去读配置文件nginx.conf 从上往下读取。服务器根据www.etiantian去nginx.conf 找相应的东西。
发响应报文
4.3 基于端口的虚拟主机配置实战
[root@web02 /application/nginx/conf]#cp nginx.conf{,_BaseName}
[root@web02 /application/nginx/conf]#vim nginx.conf
更改nginx.conf文件对应的bbs与blog端口号
#检查语法
nginx -t
nginx -s reload
netstat -lntp|grep nginx
测试:
先匹配端口,端口匹配的域名没有就返回原本端口对应的域名
4.5 基于IP的虚拟主机配置实战
1)添加辅助ip地址
ip addr add 10.0.0.9 dev eth0 label eth0:9
ip addr add 10.0.0.10 dev eth0 label eth0:10
#ping 一下显示通畅
[root@web02 /application/nginx/conf]#cp nginx.conf{,._BasePort}
[root@web02 /application/nginx/conf]#vim nginx.conf
[root@web02 /application/nginx/conf]#vim nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 10.0.0.8:80;
server_name www.etiantian.org;
location / {
root html/www;
index index.html index.htm;
}
}
server {
listen 10.0.0.9:80;
server_name bbs.etiantian.org;
location / {
root html/bbs;
index index.html index.htm;
}
}
server {
listen 10.0.0.10:80;
server_name blog.etiantian.org;
location / {
root html/blog;
index index.html index.htm;
}
}
}
报错:
[root@web02 /application/nginx/conf]#nginx -s stop
nginx: [error] invalid PID number "" in "/application/nginx-1.18.0//logs/nginx.pid"
[root@web02 /application/nginx/conf]#nginx -s reload
nginx: [error] invalid PID number "" in "/application/nginx-1.18.0//logs/nginx.pid"
[root@web02 /application/nginx/conf]#nginx -c /application/nginx/conf/nginx.conf
#-c指定配置文件
[root@web02 /application/nginx/conf]#nginx -s reload
#可以正常重启
[root@web02 /application/nginx/conf]#netstat -lntup|grep nginx
tcp 0 0 10.0.0.10:80 0.0.0.0:* LISTEN 8725/nginx: master
tcp 0 0 10.0.0.9:80 0.0.0.0:* LISTEN 8725/nginx: master
tcp 0 0 10.0.0.8:80 0.0.0.0:* LISTEN 8725/nginx: master
4.6 规范优化Nginx配置文件
Nginx的主配置文件为nginx.conf,主配置文件包含的所有虚拟主机的子配置文件会统一放入extra目录中,虚拟主机的配置文件按照网站的域名或功能取名.
如果虚拟主机的数量不是很多,也可以把多个虚拟主机配置成一个单独的配置文件,仅仅和Nginx的主配置文件nginx.conf分离开即可。
include file | mask;
它可以放置在Nginx配置中的任何位置。用法示例如下:
include mime.types;
include www.conf;#<==包含单个文件
include vhosts/*.conf;#<==包含vhosts下所有以conf结尾的文件
下面是优化Nginx配置的实战方案。
具体实施步骤如下:
[root@web02 /application/nginx/conf]#mkdir extra
[root@web02 /application/nginx/conf]#sed -n '10,17p' nginx.conf
server {
listen 80;
server_name www.etiantian.org;
location / {
root html/www;
index index.html index.htm;
}
}
[root@web02 /application/nginx/conf]#sed -n '10,17p' nginx.conf >extra/01_www.conf
[root@web02 /application/nginx/conf]#sed -n '18,25p' nginx.conf
server {
listen 80;
server_name bbs.etiantian.org;
location / {
root html/bbs;
index index.html index.htm;
}
}
[root@web02 /application/nginx/conf]#sed -n '18,25p' nginx.conf >extra/02_bbs.conf
[root@web02 /application/nginx/conf]#sed -n '26,33p' nginx.conf
server {
listen 80;
server_name blog.etiantian.org;
location / {
root html/blog;
index index.html index.htm;
}
}
[root@web02 /application/nginx/conf]#sed -n '26,33p' nginx.conf >extra/03_blog.conf
#删除主配置文件nginx.conf中所有虚拟主机的配置(包含server{}标签),这里是10到33行的内容,需要提前查好行号,打印确认无误后再删除。
[root@web02 /application/nginx/conf]#sed -n '10,33p' nginx.conf
server {
listen 80;
server_name www.etiantian.org;
location / {
root html/www;
index index.html index.htm;
}
}
server {
listen 80;
server_name bbs.etiantian.org;
location / {
root html/bbs;
index index.html index.htm;
}
}
server {
listen 80;
server_name blog.etiantian.org;
location / {
root html/blog;
index index.html index.htm;
}
}
[root@web02 /application/nginx/conf]#sed -i '10,33d' nginx.conf
[root@web02 /application/nginx/conf]#sed -i '10 i include extra/01_www.conf;\ninclude extra/02_bbs.conf;\ninclude extra/03_blog.conf;' nginx.conf
[root@web02 /application/nginx/conf]#cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include extra/01_www.conf;
include extra/02_bbs.conf;
include extra/03_blog.conf;
}
测试:
[root@web02 /application/nginx/conf]#nginx -c /application/nginx/conf/nginx.conf
[root@web02 /application/nginx/conf]#nginx -s reload
[root@web02 /application/nginx/conf]#curl www.etiantian.org
www.etiantian.org
[root@web02 /application/nginx/conf]#curl bbs.etiantian.org
bbs.etiantian.org
[root@web02 /application/nginx/conf]#curl blog.etiantian.org
blog.etiantian.org
添加别名:(不同域名访问相同内容)
[root@web02 /application/nginx/conf]#vim extra/01_www.conf
server {
listen 80;
server_name www.etiantian.org etiantian.org;
location / {
root html/www;
index index.html index.htm;
}
}
[root@web02 /application/nginx/conf]#vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.16.1.5 lb01
172.16.1.6 lb02
172.16.1.7 web01
172.16.1.8 web02
172.16.1.9 web03
172.16.1.31 nfs01
172.16.1.41 backup
172.16.1.51 db01 db0l.etiantian.org
172.16.1.61 m01
10.0.0.8 www.etiantian.org bbs.etiantian.org blog.etiantian.org etiantian.org
log日志脚本
[root@web02 /application/nginx/conf]#vim /server/scripts/cut_nginx_log.sh
#!/bin/sh
Dateformat=`date +%Y%m%d`
Basedir="/application/nginx"
Nginxlogdir="$Basedir/logs"
Logname="access_www"
[ -d $Nginxlogdir ] && cd $Nginxlogdir || exit 1
#文件不存在就退出
[ -f ${Logname}.log ] || exit 1
/bin/mv ${Logname}.log ${Dateformat}_${Logname}.log
$Basedir/sbin/nginx -s reload
参考书籍链接:https://pan.baidu.com/s/1ot5mej6JvHtzyuxd-3LMpg
提取码:lt2p