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

2023-11-02 06:28

本文主要是介绍源码配置nginx,分别编写基于RHEL6、RHEL7的脚本,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

配置nginx

安装软件

[root@localhost ~]# wget https://nginx.org/download/nginx-1.20.2.tar.gz
[root@localhost ~]# ls
公共  视频  文档  音乐  anaconda-ks.cfg
模板  图片  下载  桌面  nginx-1.20.2.tar.gz

创建系统用户

[root@localhost ~]# useradd -r -M -s /sbin/nologin nginx

下载相应的依赖包

[root@localhost ~]# yum -y groups mark install 'Development Tools'
[root@localhost ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make

创建nginx日志存放目录

[root@localhost ~]# mkdir -p /var/log/nginx
[root@localhost ~]# chown -R nginx.nginx /var/log/nginx
[root@localhost ~]# ll -d /var/log/nginx
drwxr-xr-x. 2 nginx nginx 6 10月 31 10:30 /var/log/nginx

编译安装

[root@localhost ~]# tar xf nginx-1.20.2.tar.gz
[root@localhost ~]# cd nginx-1.20.2/
[root@localhost nginx-1.20.2]# ./configure \
> --prefix=/usr/local/nginx \  //指定安装位置
> --user=nginx \  //指定用户
> --group=nginx \  //指定组
> --with-debug \   //将debug功能打开
> --with-http_ssl_module \  //将ssl功能打开
> --with-http_realip_module \  //转发realip功能打开
> --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

配置相对应的环境变量

[root@localhost nginx-1.20.2]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@localhost nginx-1.20.2]# source /etc/profile.d/nginx.sh

 开启nginx

[root@localhost nginx-1.20.2]# nginx
[root@localhost 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 ]; thenecho "请以 root 权限运行此脚本"exit 1
fi# 安装 Nginx(如果未安装)
if ! rpm -q nginx > /dev/null; thenecho "正在安装 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 nginxecho "Nginx 配置已完成。"

运行这个脚本

[root@localhost ~]# chmod +x nginx2.sh #分配权限
[root@localhost ~]# ./nginx2.sh

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

#!/bin/bash# 检查是否以 root 权限运行脚本
if [ "$(id -u)" -ne 0 ]; thenecho "请以 root 权限运行此脚本"exit 1
fi# 安装 Nginx(如果未安装)
if ! rpm -q nginx > /dev/null; thenecho "正在安装 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 nginxecho "Nginx 配置已完成。"
elseecho "Nginx 配置有误,请检查配置文件并重新运行脚本。"
fi

运行

[root@localhost ~]# chmod +x nginx3.sh #分配权限
[root@localhost ~]# ./nginx3.sh

这篇关于源码配置nginx,分别编写基于RHEL6、RHEL7的脚本的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/328732

相关文章

CentOS和Ubuntu系统使用shell脚本创建用户和设置密码

《CentOS和Ubuntu系统使用shell脚本创建用户和设置密码》在Linux系统中,你可以使用useradd命令来创建新用户,使用echo和chpasswd命令来设置密码,本文写了一个shell... 在linux系统中,你可以使用useradd命令来创建新用户,使用echo和chpasswd命令来设

redis中使用lua脚本的原理与基本使用详解

《redis中使用lua脚本的原理与基本使用详解》在Redis中使用Lua脚本可以实现原子性操作、减少网络开销以及提高执行效率,下面小编就来和大家详细介绍一下在redis中使用lua脚本的原理... 目录Redis 执行 Lua 脚本的原理基本使用方法使用EVAL命令执行 Lua 脚本使用EVALSHA命令

SpringBoot3.4配置校验新特性的用法详解

《SpringBoot3.4配置校验新特性的用法详解》SpringBoot3.4对配置校验支持进行了全面升级,这篇文章为大家详细介绍了一下它们的具体使用,文中的示例代码讲解详细,感兴趣的小伙伴可以参考... 目录基本用法示例定义配置类配置 application.yml注入使用嵌套对象与集合元素深度校验开发

IntelliJ IDEA 中配置 Spring MVC 环境的详细步骤及问题解决

《IntelliJIDEA中配置SpringMVC环境的详细步骤及问题解决》:本文主要介绍IntelliJIDEA中配置SpringMVC环境的详细步骤及问题解决,本文分步骤结合实例给大... 目录步骤 1:创建 Maven Web 项目步骤 2:添加 Spring MVC 依赖1、保存后执行2、将新的依赖

SpringBoot基于配置实现短信服务策略的动态切换

《SpringBoot基于配置实现短信服务策略的动态切换》这篇文章主要为大家详细介绍了SpringBoot在接入多个短信服务商(如阿里云、腾讯云、华为云)后,如何根据配置或环境切换使用不同的服务商,需... 目录目标功能示例配置(application.yml)配置类绑定短信发送策略接口示例:阿里云 & 腾

如何为Yarn配置国内源的详细教程

《如何为Yarn配置国内源的详细教程》在使用Yarn进行项目开发时,由于网络原因,直接使用官方源可能会导致下载速度慢或连接失败,配置国内源可以显著提高包的下载速度和稳定性,本文将详细介绍如何为Yarn... 目录一、查询当前使用的镜像源二、设置国内源1. 设置为淘宝镜像源2. 设置为其他国内源三、还原为官方

Nginx location匹配模式与规则详解

《Nginxlocation匹配模式与规则详解》:本文主要介绍Nginxlocation匹配模式与规则,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、环境二、匹配模式1. 精准模式2. 前缀模式(不继续匹配正则)3. 前缀模式(继续匹配正则)4. 正则模式(大

CentOS7更改默认SSH端口与配置指南

《CentOS7更改默认SSH端口与配置指南》SSH是Linux服务器远程管理的核心工具,其默认监听端口为22,由于端口22众所周知,这也使得服务器容易受到自动化扫描和暴力破解攻击,本文将系统性地介绍... 目录引言为什么要更改 SSH 默认端口?步骤详解:如何更改 Centos 7 的 SSH 默认端口1

Maven的使用和配置国内源的保姆级教程

《Maven的使用和配置国内源的保姆级教程》Maven是⼀个项目管理工具,基于POM(ProjectObjectModel,项目对象模型)的概念,Maven可以通过一小段描述信息来管理项目的构建,报告... 目录1. 什么是Maven?2.创建⼀个Maven项目3.Maven 核心功能4.使用Maven H

SpringBoot多数据源配置完整指南

《SpringBoot多数据源配置完整指南》在复杂的企业应用中,经常需要连接多个数据库,SpringBoot提供了灵活的多数据源配置方式,以下是详细的实现方案,需要的朋友可以参考下... 目录一、基础多数据源配置1. 添加依赖2. 配置多个数据源3. 配置数据源Bean二、JPA多数据源配置1. 配置主数据