帮我写一个nginx的自动源码安装shell脚本
时间: 2024-04-01 09:35:53 浏览: 77
以下是一个简单的Nginx自动源码安装的Shell脚本,仅供参考:
```bash
#!/bin/bash
NGINX_VERSION="1.20.1" #Nginx版本号
NGINX_INSTALL_DIR="/usr/local/nginx" #Nginx安装路径
#安装依赖库
yum -y install pcre-devel openssl-devel zlib-devel gcc make
#下载Nginx源码包
wget http://nginx.org/download/nginx-$NGINX_VERSION.tar.gz
#解压源码包
tar -zxvf nginx-$NGINX_VERSION.tar.gz
#进入源码目录
cd nginx-$NGINX_VERSION
#编译安装Nginx
./configure --prefix=$NGINX_INSTALL_DIR --with-http_ssl_module
make && make install
#添加Nginx服务
cat > /usr/lib/systemd/system/nginx.service <<EOF
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=$NGINX_INSTALL_DIR/logs/nginx.pid
ExecStartPre=$NGINX_INSTALL_DIR/sbin/nginx -t -c $NGINX_INSTALL_DIR/conf/nginx.conf
ExecStart=$NGINX_INSTALL_DIR/sbin/nginx -c $NGINX_INSTALL_DIR/conf/nginx.conf
ExecReload=$NGINX_INSTALL_DIR/sbin/nginx -s reload
ExecStop=$NGINX_INSTALL_DIR/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
#重新加载systemd服务
systemctl daemon-reload
#启动Nginx服务
systemctl start nginx
#设置Nginx开机自启动
systemctl enable nginx
```
该脚本主要实现了以下功能:
1. 安装Nginx所需的依赖库,包括pcre-devel、openssl-devel、zlib-devel、gcc和make。
2. 下载Nginx的源码包,并解压到指定目录。
3. 进入源码目录,执行configure命令配置Nginx的安装路径和编译选项,然后执行make和make install命令进行编译和安装。
4. 添加Nginx服务,并启动Nginx服务。
5. 设置Nginx开机自启动。
需要注意的是,该脚本仅供参考,具体情况需要根据实际需求进行修改。同时,安全性也需要考虑,例如对安装路径和Nginx服务进行权限控制等。
阅读全文
相关推荐


















