linux云计算学习第七周

目录

1、总结I/O模型

1.1同步I/O模型

1.2异步I/O模型

1.3多路复用I/O模型

1.4信号驱动I/O模型

2、编译安装nginx脚本

3、nginx平滑升级

3.1环境检查

3.2准备新版本的源码

3.3编译新版本

3.4替换二进制文件

3.5验证新版本

3.6逐步切换到新版本

3.7检查版本号

3.8查看服务是否正常

4、总结nginx核心配置,实现多虚拟主机

4.1nginx配置文件所在目录

4.2多虚拟主机

4.2.1基于端口的虚拟主机

4.2.2基于IP的虚拟主机

4.2.3基于域名的虚拟主机

5、定制日志格式

6、基于nginx和python动态站点安装配置


1、总结I/O模型

1.1同步I/O模型

进程在执行I/O操作时会被阻塞,知道I/O操作完成时才会继续执行

1.2异步I/O模型

进程发起I/O请求后,无需阻塞或轮询,可继续执行其他任务。当I/O操作完成时,系统通过回调或信号通知进程

1.3多路复用I/O模型

通过一个线程管理多个I/O连接的状态,避免为每个连接创建独立线程,减少资源开销。

1.4信号驱动I/O模型

应用程序通过注册信号处理函数,当I/O数据就绪时,内核向进程发送信号,触发回调处理数据

2、编译安装nginx脚本

#!/bin/bash

# Nginx编译安装脚本 - 适用于Ubuntu系统
# 安装版本:1.22.1

# 定义颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m' # No Color

echo -e "${GREEN}开始安装Nginx 1.22.1...${NC}"

# 检查是否为root用户
if [ "$(id -u)" -ne 0 ]; then
    echo -e "${RED}错误: 请使用root用户执行此脚本${NC}"
    exit 1
fi

# 更新系统并安装依赖
echo -e "${YELLOW}正在更新系统并安装编译依赖...${NC}"
apt-get update -y
apt-get install -y build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev libxml2-dev libxslt1-dev libgd-dev libgeoip-dev libgoogle-perftools-dev libperl-dev wget curl

# 创建临时目录
TEMP_DIR=$(mktemp -d)
cd $TEMP_DIR

# 下载Nginx源码
echo -e "${YELLOW}正在下载Nginx 1.22.1源码...${NC}"
wget https://nginx.org/download/nginx-1.22.1.tar.gz
if [ $? -ne 0 ]; then
    echo -e "${RED}下载失败,请检查网络连接或源码地址${NC}"
    exit 1
fi

# 解压源码
echo -e "${YELLOW}正在解压源码...${NC}"
tar -zxvf nginx-1.22.1.tar.gz
cd nginx-1.22.1

# 配置编译选项
echo -e "${YELLOW}正在配置编译选项...${NC}"
./configure \
    --prefix=/usr/local/nginx \
    --sbin-path=/usr/local/nginx/sbin/nginx \
    --conf-path=/usr/local/nginx/conf/nginx.conf \
    --error-log-path=/var/log/nginx/error.log \
    --http-log-path=/var/log/nginx/access.log \
    --pid-path=/var/run/nginx.pid \
    --lock-path=/var/run/nginx.lock \
    --user=www-data \
    --group=www-data \
    --with-http_ssl_module \
    --with-http_realip_module \
    --with-http_addition_module \
    --with-http_sub_module \
    --with-http_dav_module \
    --with-http_flv_module \
    --with-http_mp4_module \
    --with-http_gunzip_module \
    --with-http_gzip_static_module \
    --with-http_random_index_module \
    --with-http_secure_link_module \
    --with-http_stub_status_module \
    --with-http_auth_request_module \
    --with-threads \
    --with-stream \
    --with-stream_ssl_module \
    --with-stream_ssl_preread_module \
    --with-stream_realip_module \
    --with-http_slice_module \
    --with-mail \
    --with-mail_ssl_module \
    --with-compat \
    --with-file-aio \
    --with-http_v2_module

if [ $? -ne 0 ]; then
    echo -e "${RED}配置失败,请检查依赖是否安装完整${NC}"
    exit 1
fi

# 编译并安装
echo -e "${YELLOW}正在编译并安装Nginx...${NC}"
make -j$(nproc)
if [ $? -ne 0 ]; then
    echo -e "${RED}编译失败,请检查系统资源或编译选项${NC}"
    exit 1
fi

make install
if [ $? -ne 0 ]; then
    echo -e "${RED}安装失败,请检查磁盘空间或权限${NC}"
    exit 1
fi

# 创建必要目录(修正:添加/var/www/html目录)
echo -e "${YELLOW}正在创建必要目录...${NC}"
mkdir -p /var/log/nginx
mkdir -p /var/www/html  # 添加网站根目录
mkdir -p /usr/local/nginx/conf/sites-available
mkdir -p /usr/local/nginx/conf/sites-enabled
chown -R www-data:www-data /var/log/nginx /var/www/html  # 修正权限设置

# 创建默认网站配置
echo -e "${YELLOW}正在创建默认网站配置...${NC}"
cat > /usr/local/nginx/conf/sites-available/default < /var/www/html/index.html <


    Welcome to Nginx!
    


    

Welcome to Nginx!

If you see this page, the Nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using Nginx.

EOF # 创建systemd服务文件 echo -e "${YELLOW}正在创建systemd服务文件...${NC}" cat > /etc/systemd/system/nginx.service <

3、nginx平滑升级

3.1环境检查

确认当前nginx版本

nginx -V

备份原文件

cp /usr/sbin/nginx /usr/sbin/nginx.old
cp -r /etc/nginx /etc/nginx.bak

3.2准备新版本的源码

从官网获得与旧版本兼容的新版本源码

wget http://nginx.org/download/nginx-1.25.0.tar.gz
tar -zxvf nginx-1.25.0.tar.gz
cd nginx-1.25.0/

3.3编译新版本

./configure --prefix=/usr/local/nginx --with-http_ssl_module # 示例参数
make # 仅编译,不执行make install

3.4替换二进制文件

kill -USR2 $(cat /var/run/nginx.pid) # 发送USR2信号给旧Master进程

3.5验证新版本

ps -ef | grep [n]ginx # 应看到新旧两个Master进程

测试新版本正确性

/usr/sbin/nginx -t -c /etc/nginx/nginx.conf

3.6逐步切换到新版本

kill -WINCH $(cat /var/run/nginx.pid.old) # 发送WINCH信号给旧Master进程

3.7检查版本号

nginx -v#确认使用的是新版本

3.8查看服务是否正常

curl http://localhost && systemctl status nginx

4、总结nginx核心配置,实现多虚拟主机

4.1nginx配置文件所在目录

主配置文件

/etc/nginx/nginx.conf

子配置文件

/etc/nginx/sites-enabled/default

扩展配置文件

/etc/nginx/conf.d/*.conf

4.2多虚拟主机

4.2.1基于端口的虚拟主机

同一IP不同端口提供不同服务

http {
  server {
    listen 80;          # 监听端口80
    server_name _;      # 通配所有域名
    root /var/www/web1; # 站点根目录
    index index.html;   # 默认首页
  }

  server {
    listen 81;          # 监听端口81
    server_name _;
    root /var/www/web2;
    index index.html;
  }
}

4.2.2基于IP的虚拟主机

服务器绑定多个IP,不同IP对应不同的站点

http {
  server {
    listen 192.168.1.100; # 监听特定IP
    root /var/www/web1;
  }

  server {
    listen 192.168.1.101; # 监听另一IP
    root /var/www/web2;
  }
}

4.2.3基于域名的虚拟主机

同一IP+端口通过不同域名区分站点

http {
  server {
    listen 80;
    server_name www.example.com; # 绑定域名
    root /var/www/example;
    location / {
      try_files $uri $uri/ =404;
    }
  }

  server {
    listen 80;
    server_name www.other.com; # 绑定另一域名
    root /var/www/other;
    location / {
      proxy_pass http://backend; # 反向代理至后端服务器
    }
  }
}

5、定制日志格式

vim /etc/nginx/conf.d/vhost.conf
#添加如下内容
server {
  listen 80 default_server;
  access_log /var/log/nginx/\${host}_access.log basic;
  
  location /web1/ {
    alias /data/server/nginx/web1/;
  }
  # 此资源记录json日志
  location /json{
    access_log /var/log/nginx/\${host}_json_access.log json_basic;
    return 200 "json\n";
  }
  #不记录access log
  location /test{ 
    access_log off; 
    return 200 "test\n";
  }
}

6、基于nginx和python动态站点安装配置

https://blog.csdn.net/m0_57215217/article/details/148355723?spm=1001.2014.3001.5501

你可能感兴趣的:(学习)