linux云计算学习第七周

[复制链接]
发表于 2025-6-6 00:46:59 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
目录

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脚本

  1. #!/bin/bash
  2. # Nginx编译安装脚本 - 适用于Ubuntu系统
  3. # 安装版本:1.22.1
  4. # 定义颜色输出
  5. RED='\033[0;31m'
  6. GREEN='\033[0;32m'
  7. YELLOW='\033[0;33m'
  8. NC='\033[0m' # No Color
  9. echo -e "${GREEN}开始安装Nginx 1.22.1...${NC}"
  10. # 检查是否为root用户
  11. if [ "$(id -u)" -ne 0 ]; then
  12.     echo -e "${RED}错误: 请使用root用户执行此脚本${NC}"
  13.     exit 1
  14. fi
  15. # 更新系统并安装依赖
  16. echo -e "${YELLOW}正在更新系统并安装编译依赖...${NC}"
  17. apt-get update -y
  18. 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
  19. # 创建临时目录
  20. TEMP_DIR=$(mktemp -d)
  21. cd $TEMP_DIR
  22. # 下载Nginx源码
  23. echo -e "${YELLOW}正在下载Nginx 1.22.1源码...${NC}"
  24. wget https://nginx.org/download/nginx-1.22.1.tar.gz
  25. if [ $? -ne 0 ]; then
  26.     echo -e "${RED}下载失败,请检查网络连接或源码地址${NC}"
  27.     exit 1
  28. fi
  29. # 解压源码
  30. echo -e "${YELLOW}正在解压源码...${NC}"
  31. tar -zxvf nginx-1.22.1.tar.gz
  32. cd nginx-1.22.1
  33. # 配置编译选项
  34. echo -e "${YELLOW}正在配置编译选项...${NC}"
  35. ./configure \
  36.     --prefix=/usr/local/nginx \
  37.     --sbin-path=/usr/local/nginx/sbin/nginx \
  38.     --conf-path=/usr/local/nginx/conf/nginx.conf \
  39.     --error-log-path=/var/log/nginx/error.log \
  40.     --http-log-path=/var/log/nginx/access.log \
  41.     --pid-path=/var/run/nginx.pid \
  42.     --lock-path=/var/run/nginx.lock \
  43.     --user=www-data \
  44.     --group=www-data \
  45.     --with-http_ssl_module \
  46.     --with-http_realip_module \
  47.     --with-http_addition_module \
  48.     --with-http_sub_module \
  49.     --with-http_dav_module \
  50.     --with-http_flv_module \
  51.     --with-http_mp4_module \
  52.     --with-http_gunzip_module \
  53.     --with-http_gzip_static_module \
  54.     --with-http_random_index_module \
  55.     --with-http_secure_link_module \
  56.     --with-http_stub_status_module \
  57.     --with-http_auth_request_module \
  58.     --with-threads \
  59.     --with-stream \
  60.     --with-stream_ssl_module \
  61.     --with-stream_ssl_preread_module \
  62.     --with-stream_realip_module \
  63.     --with-http_slice_module \
  64.     --with-mail \
  65.     --with-mail_ssl_module \
  66.     --with-compat \
  67.     --with-file-aio \
  68.     --with-http_v2_module
  69. if [ $? -ne 0 ]; then
  70.     echo -e "${RED}配置失败,请检查依赖是否安装完整${NC}"
  71.     exit 1
  72. fi
  73. # 编译并安装
  74. echo -e "${YELLOW}正在编译并安装Nginx...${NC}"
  75. make -j$(nproc)
  76. if [ $? -ne 0 ]; then
  77.     echo -e "${RED}编译失败,请检查系统资源或编译选项${NC}"
  78.     exit 1
  79. fi
  80. make install
  81. if [ $? -ne 0 ]; then
  82.     echo -e "${RED}安装失败,请检查磁盘空间或权限${NC}"
  83.     exit 1
  84. fi
  85. # 创建必要目录(修正:添加/var/www/html目录)
  86. echo -e "${YELLOW}正在创建必要目录...${NC}"
  87. mkdir -p /var/log/nginx
  88. mkdir -p /var/www/html  # 添加网站根目录
  89. mkdir -p /usr/local/nginx/conf/sites-available
  90. mkdir -p /usr/local/nginx/conf/sites-enabled
  91. chown -R www-data:www-data /var/log/nginx /var/www/html  # 修正权限设置
  92. # 创建默认网站配置
  93. echo -e "${YELLOW}正在创建默认网站配置...${NC}"
  94. cat > /usr/local/nginx/conf/sites-available/default <<EOF
  95. server {
  96.     listen 80 default_server;
  97.     listen [::]:80 default_server;
  98.    
  99.     server_name _;
  100.     root /var/www/html;
  101.     index index.html index.htm;
  102.    
  103.     location / {
  104.         try_files \$uri \$uri/ =404;
  105.     }
  106. }
  107. EOF
  108. ln -s /usr/local/nginx/conf/sites-available/default /usr/local/nginx/conf/sites-enabled/
  109. # 修改主配置文件
  110. sed -i 's/include       mime.types;/include       mime.types;\n    include       sites-enabled\/\*;/' /usr/local/nginx/conf/nginx.conf
  111. # 创建默认测试页面(新增)
  112. echo -e "${YELLOW}正在创建默认测试页面...${NC}"
  113. cat > /var/www/html/index.html <<EOF
  114. <!DOCTYPE html>
  115. <html>
  116. <head>
  117.     <title>Welcome to Nginx!</title>
  118.     <style>
  119.         body {
  120.             width: 35em;
  121.             margin: 0 auto;
  122.             font-family: Tahoma, Verdana, Arial, sans-serif;
  123.         }
  124.     </style>
  125. </head>
  126. <body>
  127.     <h1>Welcome to Nginx!</h1>
  128.     <p>If you see this page, the Nginx web server is successfully installed and
  129.     working. Further configuration is required.</p>
  130.    
  131.     <p>For online documentation and support please refer to
  132.     <a href="http://nginx.org/">nginx.org</a>.<br/>
  133.     Commercial support is available at
  134.     <a href="http://nginx.com/">nginx.com</a>.</p>
  135.    
  136.     <p><em>Thank you for using Nginx.</em></p>
  137. </body>
  138. </html>
  139. EOF
  140. # 创建systemd服务文件
  141. echo -e "${YELLOW}正在创建systemd服务文件...${NC}"
  142. cat > /etc/systemd/system/nginx.service <<EOF
  143. [Unit]
  144. Description=The NGINX HTTP and reverse proxy server
  145. After=syslog.target network.target remote-fs.target nss-lookup.target
  146. [Service]
  147. Type=forking
  148. PIDFile=/var/run/nginx.pid
  149. ExecStartPre=/usr/local/nginx/sbin/nginx -t
  150. ExecStart=/usr/local/nginx/sbin/nginx
  151. ExecReload=/usr/local/nginx/sbin/nginx -s reload
  152. ExecStop=/bin/kill -s QUIT \$MAINPID
  153. PrivateTmp=true
  154. [Install]
  155. WantedBy=multi-user.target
  156. EOF
  157. # 重载systemd并启动Nginx
  158. echo -e "${YELLOW}正在启动Nginx服务...${NC}"
  159. systemctl daemon-reload
  160. systemctl enable nginx
  161. systemctl start nginx
  162. # 检查Nginx状态
  163. echo -e "${YELLOW}正在检查Nginx状态...${NC}"
  164. systemctl status nginx --no-pager
  165. # 清理临时文件
  166. echo -e "${YELLOW}正在清理临时文件...${NC}"
  167. cd /
  168. rm -rf $TEMP_DIR
  169. echo -e "${GREEN}Nginx 1.22.1安装完成!${NC}"
  170. echo -e "${GREEN}访问 http://localhost 测试Nginx是否正常工作${NC}"   
复制代码
3、nginx平滑升级

3.1环境查抄

确认当前nginx版本
  1. nginx -V
复制代码
备份原文件
  1. cp /usr/sbin/nginx /usr/sbin/nginx.old
  2. cp -r /etc/nginx /etc/nginx.bak
复制代码
3.2预备新版本的源码

从官网获得与旧版本兼容的新版本源码
  1. wget http://nginx.org/download/nginx-1.25.0.tar.gz
  2. tar -zxvf nginx-1.25.0.tar.gz
  3. cd nginx-1.25.0/
复制代码
3.3编译新版本

  1. ./configure --prefix=/usr/local/nginx --with-http_ssl_module # 示例参数
  2. make # 仅编译,不执行make install
复制代码
3.4更换二进制文件

  1. kill -USR2 $(cat /var/run/nginx.pid) # 发送USR2信号给旧Master进程
复制代码
3.5验证新版本

  1. ps -ef | grep [n]ginx # 应看到新旧两个Master进程
复制代码
测试新版本正确性
  1. /usr/sbin/nginx -t -c /etc/nginx/nginx.conf
复制代码
3.6逐步切换到新版本

  1. kill -WINCH $(cat /var/run/nginx.pid.old) # 发送WINCH信号给旧Master进程
复制代码
3.7查抄版本号

  1. nginx -v#确认使用的是新版本
复制代码
3.8检察服务是否正常

  1. curl http://localhost && systemctl status nginx
复制代码
4、总结nginx核心设置,实现多虚拟主机

4.1nginx设置文件所在目录

主设置文件
  1. /etc/nginx/nginx.conf
复制代码
子设置文件
  1. /etc/nginx/sites-enabled/default
复制代码
扩展设置文件
  1. /etc/nginx/conf.d/*.conf
复制代码
4.2多虚拟主机

4.2.1基于端口的虚拟主机

同一IP不同端口提供不同服务
  1. http {
  2.   server {
  3.     listen 80;          # 监听端口80
  4.     server_name _;      # 通配所有域名
  5.     root /var/www/web1; # 站点根目录
  6.     index index.html;   # 默认首页
  7.   }
  8.   server {
  9.     listen 81;          # 监听端口81
  10.     server_name _;
  11.     root /var/www/web2;
  12.     index index.html;
  13.   }
  14. }
复制代码
4.2.2基于IP的虚拟主机

服务器绑定多个IP,不同IP对应不同的站点
  1. http {
  2.   server {
  3.     listen 192.168.1.100; # 监听特定IP
  4.     root /var/www/web1;
  5.   }
  6.   server {
  7.     listen 192.168.1.101; # 监听另一IP
  8.     root /var/www/web2;
  9.   }
  10. }
复制代码
4.2.3基于域名的虚拟主机

同一IP+端口通过不同域名区分站点
  1. http {
  2.   server {
  3.     listen 80;
  4.     server_name www.example.com; # 绑定域名
  5.     root /var/www/example;
  6.     location / {
  7.       try_files $uri $uri/ =404;
  8.     }
  9.   }
  10.   server {
  11.     listen 80;
  12.     server_name www.other.com; # 绑定另一域名
  13.     root /var/www/other;
  14.     location / {
  15.       proxy_pass http://backend; # 反向代理至后端服务器
  16.     }
  17.   }
  18. }
复制代码
5、定制日志日志格式

  1. vim /etc/nginx/conf.d/vhost.conf
  2. #添加如下内容
  3. server {
  4.  listen 80 default_server;
  5.  access_log /var/log/nginx/\${host}_access.log basic;
  6.  
  7.  location /web1/ {
  8.    alias /data/server/nginx/web1/;
  9.  }
  10.  # 此资源记录json日志日志
  11.  location /json{
  12.    access_log /var/log/nginx/\${host}_json_access.log json_basic;
  13.    return 200 "json\n";
  14.  }
  15.  #不记录access log
  16.  location /test{
  17.    access_log off;
  18.    return 200 "test\n";
  19.  }
  20. }
复制代码
6、基于nginx和python动态站点安装设置

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







免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

© 2001-2025 Discuz! Team. Powered by Discuz! X3.5

GMT+8, 2025-6-19 20:25 , Processed in 0.058802 second(s), 25 queries 手机版|qidao123.com技术社区-IT企服评测▪应用市场 ( 浙ICP备20004199 )

快速回复 返回顶部 返回列表