一、Nginx
简介
Nginx是一个高性能的HTTP和反向代理web服务器
轻量级、高性能
1.1 Nginx安装
方法一:编译安装
- 依赖:openssl-devel、zlib-devel、ncurses-devel、pcre-devel、gcc、gcc-c++
方法二:yum安装
- Nginx的rpm包在epel源中
编译安装Nginx
下载位置:https://nginx.org
第一步:下载[root@localhost html]# wget http://nginx.org/download/nginx-1.24.0.tar.gz
第二步:安装依赖
[root@localhost ~]# yum install openssl-devel zlib-devel ncurses-devel \ pcre-devel gcc gcc-c++ -y
第三步:安装Nginx
[root@localhost ~]# tar xvf nginx-1.24.0.tar.gz [root@localhost ~]# cd nginx-1.24.0 [root@localhost nginx-1.24.0]#./configure --prefix=/usr/local/nginx && make && make install
Nginx的目录结构
[root@localhost ~]# cd /usr/local/nginx
[root@localhost ~]# ls
conf html logs sbin
- conf:这个目录存放的是Nginx的配置文件
- html:这个目录是Nginx默认网站的根目录
- logs:这个是Nginx的日志文件目录
- sbin:这个是Nginx的启动程序的目录
1.2 关闭防火墙和selinux
systemctl stop firewalld
systemctl disable firewalld
sed -i s/SELINUX=enforcing/SELINUX=disabled/g /etc/selinux/config #永久关闭
setenforce 0 #临时关闭
1.3 启动Nginx
启动程序:
方法一: [root@localhost ~]# cd /usr/local/nginx/sbin/ [root@localhost sbin]# ./nginx [root@localhost sbin]# lsof -i :80 方法二: export PATH=$PATH:/usr/local/nginx/sbin/ nginx
b 扩展:重启Nginx
方法一: [root@localhost ~]# cd /usr/local/nginx/sbin/ [root@localhost sbin]# ./nginx -s reload 方法二: 先kill -9杀死进程,然后启动 方法三: pkill nginx /usr/local/nginx/sbin/nginx 方法四: PATH=$PATH:/usr/local/nginx/sbin/nginx nginx
扩展:让Nginx开机自动启动
[root@localhost ~]# echo "/usr/local/nginx/sbin/nginx" >>/etc/rc.d/rc.local [root@localhost ~]#chmod +x /etc/rc.d/rc.local
关闭Nginx
[root@localhost ~]#pkill nginx
1.4 Nginx的配置文件
配置文件的位置
yum安装:/etc/nginx/ 编译安装:安装位置/conf/
文件名
nginx.conf
配置文件的基本结构
全局段:
可以什么都没有
可以有
http段: - 虚拟主机段
配置文件的格式
worker_processes 3; #设置Nginx的工作进程数为3
events { #开始定义事件相关的配置
worker_connections 1024; #每个工作进程允许的最大连接数为1024
} #结束事件相关的配置
http { #开始定义HTTP的配置
include mime.types #包含MIME类型映射文件,用于确定请求资源的类型
default_type application/octet-stream; #设置默认的MIME类型为二进制流
sendfile on; #开启高效文件传输模式
keepalive_timeout 65; #设置长连接超时时间为65秒
server { #开始定义一个服务器块
listen 80; #监听80端口
server_name www.web1.com; #设置服务器名称为www.web1.com
root /usr/local/nginx/html; #设置服务器根目录为/usr/local/nginx/html
index index.html index.htm; #设置默认的索引文件为index.html和index.htm
access_log logs/host.access.log main; #设置访问日志文件为logs/host.access.log,使用main日志格式
error_log logs/host.error.log main; #设置错误日志文件为logs/host.error.log,使用main日志格式。
} #结束服务器块配置
} #结束HTTP相关的配置
1.5 基于nginx发布多个网站
第一步:更改配置文件
位置:/urs/local/nginx/conf/nginx.conf
注意:一个server模块就是一个网站,现在部署三个网站
worker_processes 3;
events {
worker_connections 1024;
}
http {
include mime.types
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.web1.com;
root /usr/local/nginx/html/web1;
index index.html index.htm;
}
server {
listen 80;
server_name www.web2.com;
root /usr/local/nginx/html/web2;
index index.html index.htm;
}
server {
listen 80;
server_name www.web3.com;
root /usr/local/nginx/html/web3;
index index.html index.htm;
}
}
第二步:修改自己电脑的hosts文件
位置:C:\windows\system32\dirvers\etc\hosts
添加三行:
192.168.1.96 www.web1.com
192.168.1.96 www.web2.com
192.168.1.96 www.web3.com
注意:需要把hosts文件,拖到桌面上,才能修改并保存,然后拖回去
IP地址,是自己Linux的IP
第三步:创建文件
echo "1111111" >/usr/local/nginx/html/web1/a.html
echo "222222" >/usr/local/nginx/html/web2/b.html
echo "333333" >/usr/local/nginx/html/web3/c.html
第四步:浏览器访问
在浏览器中输入域名:www.web1.com/a.html
在浏览器中输入域名:www.web2.com/b.html在浏览器中输入域名:www.web3.com/c.html
1.6 nginx服务脚本
#!/bin/bash
#判断用户是否传进来一个参数
if [ $# -ne 1 ];then
echo "UseAge: $0 start|restart|stop/status"
exit 3;
fi
#定义一个参数,用来判断nginx是否已经启动
lsof -i :80 | grep nginx &>/dev/null
flag=$?
check_status(){
if [ f$lag -eq 0 ];then
echo "nginx is already running..."
else
echo "ngins is already stopped"
fi
}
start_nginx() {
if [ $flag -eq 0 ];then
echo "Nginx is already running...."
exit;
else
/usr/local/nginx/sbin/nginx
echo "nginx start success"
exit;
fi
}
stop_nginx(){
if [ $flag -ne 0 ];then
echo "nginx is already stop"
exit;
else
pkill nginx;
echo "nginx stop success"
exit;
fi
}
restart_nginx(){
if [ $flag -gt 0 ];then
start_nginx;
echo "nginx restart success"
exit;
else
stop_nginx;
start_nginx;
echo "nginx restart success"
exit;
fi
}
if [ $1 == "start" ];then
start_nginx
elif [ $1 == "status" ];then
check_status
elif [ $1 == "stop" ];then
stop_nginx
elif [ $1 == "restart" ];then
restart_nginx
else
echo "UseAge:$0 start|stop|restart|status"
fi