第十周作业

1. 完成nginx编译安装脚本

#!/bin/bash

NGINX_VERSION=1.22.1
NGINX_FILE=nginx-${NGINX_VERSION}.tar.gz
NGINX_URL=http://nginx.org/download/
NGINX_INSTALL_DIR=/apps/nginx
SRC_DIR=/usr/local/src
CPUS=`lscpu |awk '/^CPU\(s\)/{print $2}'`

. /etc/os-release

color () {
    RES_COL=60
    MOVE_TO_COL="echo -en \\033[${RES_COL}G"
    SETCOLOR_SUCCESS="echo -en \\033[1;32m"
    SETCOLOR_FAILURE="echo -en \\033[1;31m"
    SETCOLOR_WARNING="echo -en \\033[1;33m"
    SETCOLOR_NORMAL="echo -en \E[0m"
    echo -n "$1" && $MOVE_TO_COL
    echo -n "["
    if [ $2 = "success" -o $2 = "0" ] ;then
        ${SETCOLOR_SUCCESS}
        echo -n $"  OK  "    
    elif [ $2 = "failure" -o $2 = "1"  ] ;then 
        ${SETCOLOR_FAILURE}
        echo -n $"FAILED"
    else
        ${SETCOLOR_WARNING}
        echo -n $"WARNING"
    fi
    ${SETCOLOR_NORMAL}
    echo -n "]"
    echo 
}

check () {
    [ -e ${NGINX_INSTALL_DIR} ] && { color "nginx 已安装,请卸载后再安装" 1; exit; }
    cd  ${SRC_DIR}
    if [  -e ${NGINX_FILE}${TAR} ];then
        color "相关文件已准备好" 0
    else
        color '开始下载 nginx 源码包' 0
        wget ${NGINX_URL}${NGINX_FILE}${TAR} 
        [ $? -ne 0 ] && { color "下载 ${NGINX_FILE}${TAR}文件失败" 1; exit; } 
    fi
} 

install () {
    color "开始安装 nginx" 0
    if id nginx  &> /dev/null;then
        color "nginx 用户已存在" 1 
    else
        useradd -s /sbin/nologin -r  nginx
        color "创建 nginx 用户" 0 
    fi
    color "开始安装 nginx 依赖包" 0
    if [ $ID == "centos" ] ;then
	    if [[ $VERSION_ID =~ ^7 ]];then
            yum -y  install  gcc  make pcre-devel openssl-devel zlib-devel perl-ExtUtils-Embed
		elif [[ $VERSION_ID =~ ^8 ]];then
            yum -y  install make gcc-c++ libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel perl-ExtUtils-Embed 
		else 
            color '不支持此系统!'  1
            exit
        fi
     elif [ $ID == "rocky"  ];then
	    yum -y  install gcc make gcc-c++ libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel perl-ExtUtils-Embed 
     else
        apt update
        apt -y install gcc make  libpcre3 libpcre3-dev openssl libssl-dev zlib1g-dev
     fi
     [ $? -ne 0 ] && { color "安装依赖包失败" 1; exit; } 
     cd $SRC_DIR
     tar xf ${NGINX_FILE}
     NGINX_DIR=`echo ${NGINX_FILE}| sed -nr 's/^(.*[0-9]).*/\1/p'`
     cd ${NGINX_DIR}
     ./configure --prefix=${NGINX_INSTALL_DIR} --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module 
     make -j $CPUS && make install 
     [ $? -eq 0 ] && color "nginx 编译安装成功" 0 ||  { color "nginx 编译安装失败,退出!" 1 ;exit; }
	 chown -R nginx.nginx ${NGINX_INSTALL_DIR}
     ln -s ${NGINX_INSTALL_DIR}/sbin/nginx /usr/local/sbin/nginx
     echo "PATH=${NGINX_INSTALL_DIR}/sbin:${PATH}" > /etc/profile.d/nginx.sh
     cat > /lib/systemd/system/nginx.service <<EOF
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=${NGINX_INSTALL_DIR}/logs/nginx.pid
ExecStartPre=/bin/rm -f ${NGINX_INSTALL_DIR}/logs/nginx.pid
ExecStartPre=${NGINX_INSTALL_DIR}/sbin/nginx -t
ExecStart=${NGINX_INSTALL_DIR}/sbin/nginx
ExecReload=/bin/kill -s HUP \$MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
LimitNOFILE=100000

[Install]
WantedBy=multi-user.target
EOF
     systemctl daemon-reload
     systemctl enable --now nginx &> /dev/null 
     systemctl is-active nginx &> /dev/null ||  { color "nginx 启动失败,退出!" 1 ; exit; }
     color "nginx 安装完成" 0
}

check

install

2. 完成nginx平滑升级,总结步骤

#下载最新稳定版
[root@ubuntu200406 ~]rz -E
[root@ubuntu200406 ~]ls
nginx-1.22.1  nginx-1.22.1.tar.gz  nginx-1.24.0.tar.gz  snap
[root@ubuntu200406 ~]tar xf nginx-1.24.0.tar.gz
[root@ubuntu200406 ~]ls
nginx-1.22.1  nginx-1.22.1.tar.gz  nginx-1.24.0  nginx-1.24.0.tar.gz  snap
[root@ubuntu200406 ~]cd nginx-1.24.0/
[root@ubuntu200406 nginx-1.24.0]ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
#查看当前使用的版本及编译选项。结果如下:
[root@ubuntu200406 nginx-1.24.0]nginx -V
nginx version: nginx/1.22.1
built by gcc 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.2)
built with OpenSSL 1.1.1f  31 Mar 2020
TLS SNI support enabled
configure arguments: --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_modul
  • 18
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
组的操作。 原始程序如下: ```c #include <stdio.h> #define N 3 int main() { int a[N][N], b[N][N], c[N][N]; int i, j, k; printf("Enter elements for matrix A:\n"); for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { scanf("%d", &a[i][j]); } } printf("Enter elements for matrix B:\n"); for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { scanf("%d", &b[i][j]); } } for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { c[i][j] = 0; for (k = 0; k < N; k++) { c[i][j] += a[i][k] * b[k][j]; } } } printf("Result matrix C:\n"); for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { printf("%d ", c[i][j]); } printf("\n"); } return 0; } ``` 使用地址法改写后的程序如下: ```c #include <stdio.h> #define N 3 int main() { int a[N][N], b[N][N], c[N][N]; int *p_a = &a[0][0], *p_b = &b[0][0], *p_c = &c[0][0]; int i, j, k; printf("Enter elements for matrix A:\n"); for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { scanf("%d", p_a + i * N + j); } } printf("Enter elements for matrix B:\n"); for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { scanf("%d", p_b + i * N + j); } } for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { *(p_c + i * N + j) = 0; for (k = 0; k < N; k++) { *(p_c + i * N + j) += *(p_a + i * N + k) * *(p_b + k * N + j); } } } printf("Result matrix C:\n"); for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { printf("%d ", *(p_c + i * N + j)); } printf("\n"); } return 0; } ``` 使用指针法改写后的程序如下: ```c #include <stdio.h> #define N 3 int main() { int a[N][N], b[N][N], c[N][N]; int (*p_a)[N] = a, (*p_b)[N] = b, (*p_c)[N] = c; int i, j, k; printf("Enter elements for matrix A:\n"); for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { scanf("%d", &(*(p_a + i))[j]); } } printf("Enter elements for matrix B:\n"); for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { scanf("%d", &(*(p_b + i))[j]); } } for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { (*(*(p_c + i) + j)) = 0; for (k = 0; k < N; k++) { (*(*(p_c + i) + j)) += (*(*(p_a + i) + k)) * (*(*(p_b + k) + j)); } } } printf("Result matrix C:\n"); for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { printf("%d ", (*(*(p_c + i) + j))); } printf("\n"); } return 0; } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

笑哈哈666

您的鼓励是我创作的源泉

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值