源码配置nginx,分别编写基于RHEL6、RHEL7的脚本

一、配置nginx

1.安装

2.创建系统用户

  1. 下载依赖包
  1. 创建nginx日志存放目录

5.编译安装

复制代码
[root@server ~]# tar xf nginx-1.20.2.tar.gz
[root@server ~]# cd nginx-1.20.2/
[root@server nginx-1.20.2]# ./configure \
> --prefix=/usr/local/nginx \  
> --user=nginx \  
> --group=nginx \  
> --with-debug \   
> --with-http_ssl_module \  
> --with-http_realip_module \  
> --with-http_image_filter_module \  
> --with-http_gunzip_module \  
> --with-http_gzip_static_module \ 
> --with-http_stub_status_module \ 
> --http-log-path=/var/log/nginx/access.log \  
> --error-log-path=/var/log/nginx/error.log  
[root@localhost nginx-1.20.2]# make install

6.开启

复制代码
[root@server nginx-1.20.2]# nginx
[root@server nginx-1.20.2]# ss -antl
State  Recv-Q Send-Q Local Address:Port   Peer Address:Port Process 
LISTEN 0      511          0.0.0.0:80          0.0.0.0:*            
LISTEN 0      128          0.0.0.0:22          0.0.0.0:*            
LISTEN 0      128        127.0.0.1:631         0.0.0.0:*            
LISTEN 0      128        127.0.0.1:6010        0.0.0.0:*            
LISTEN 0      128             [::]:22             [::]:*            
LISTEN 0      128            [::1]:631            [::]:*            
LISTEN 0      128            [::1]:6010           [::]:*            

二、基于RHEL6编写一个脚本安装nginx

复制代码
#!/bin/bash
 
# 检查是否以 root 权限运行脚本
if [ "$(id -u)" -ne 0 ]; then
    echo "请以 root 权限运行此脚本"
    exit 1
fi
 
# 安装 Nginx(如果未安装)
if ! rpm -q nginx > /dev/null; then
    echo "正在安装 Nginx..."
    yum install -y nginx
fi
 
# 配置虚拟主机
read -p "请输入域名(例如: example.com): " domain_name
read -p "请输入网站根目录的绝对路径: " web_root
 
# 创建虚拟主机配置文件
cat > /etc/nginx/conf.d/${domain_name}.conf <<EOF
server {
    listen 80;
    server_name ${domain_name};
 
    root ${web_root};
    index index.html;
 
    location / {
        try_files $uri $uri/ =404;
    }
}
EOF
 
# 创建网站根目录
mkdir -p $web_root
chown -R nginx:nginx $web_root
 
# 重新加载 Nginx 配置
systemctl restart nginx
 
# 启用 Nginx 开机自启动
systemctl enable nginx
 
echo "Nginx 配置已完成。"

三、基于RHEL7编写一个脚本安装nginx

复制代码
#!/bin/bash
 
# 检查是否以 root 权限运行脚本
if [ "$(id -u)" -ne 0 ]; then
    echo "请以 root 权限运行此脚本"
    exit 1
fi
 
# 安装 Nginx(如果未安装)
if ! rpm -q nginx > /dev/null; then
    echo "正在安装 Nginx..."
    yum install -y nginx
fi
 
# 配置虚拟主机
read -p "请输入域名(例如: example.com): " domain_name
read -p "请输入网站根目录的绝对路径: " web_root
 
# 创建虚拟主机配置文件
cat > /etc/nginx/conf.d/${domain_name}.conf <<EOF
server {
    listen 80;
    server_name ${domain_name};
 
    root ${web_root};
    index index.html;
 
    location / {
        try_files \$uri \$uri/ =404;
    }
}
EOF
 
# 创建网站根目录
mkdir -p $web_root
chown -R nginx:nginx $web_root
 
# 检查 Nginx 配置是否正确
if nginx -t > /dev/null 2>&1; then
    # 重新加载 Nginx 配置
    systemctl reload nginx
    # 启用 Nginx 开机自启动
    systemctl enable nginx
    echo "Nginx 配置已完成。"
else
    echo "Nginx 配置有误,请检查配置文件并重新运行脚本。"
fi
相关推荐
L汐1 小时前
07 负载均衡
运维·nginx·负载均衡
pjx9871 小时前
服务间的“握手”:OpenFeign声明式调用与客户端负载均衡
java·运维·spring·负载均衡
牛奔1 小时前
Docker配置远程连接
运维·docker·云原生·容器·eureka
VR最前沿2 小时前
构建集成差异化灵巧手和先进机器人控制技术的自动化系统
运维·机器人·自动化
蟑螂恶霸2 小时前
解决ubuntu20中tracker占用过多cpu,引起的风扇狂转
linux·运维·ubuntu
1024小神3 小时前
tauri2项目使用sidcar嵌入可执行文件并使用命令行调用
linux·运维·服务器
AI风老师3 小时前
2、ubuntu系统配置OpenSSH | 使用vscode或pycharm远程连接
linux·运维·服务器·ssh
枫叶落雨2223 小时前
下载的旧版的jenkins,为什么没有旧版的插件
运维·jenkins
PatrickYao04223 小时前
想要建站但没有服务器?雨云RCA,免服务器即可搭建完整网站!!!
运维·服务器·alist·云应用·无服务器部署
独行soc4 小时前
2025年渗透测试面试题总结-百度面经(题目+回答)
运维·开发语言·经验分享·学习·面试·渗透测试·php