写一个编译安装apache的shell脚本
时间: 2023-07-01 14:07:06 浏览: 118
linux Shell下制作自解压安装包,实现脚本的简单加密
以下是一个编译安装Apache的shell脚本,使用前请确保已经安装了必要的依赖库和工具:
```
#!/bin/bash
# Apache version
VERSION="2.4.46"
# Download URL for Apache
URL="https://downloads.apache.org/httpd/httpd-$VERSION.tar.gz"
# Install directory
INSTALL_DIR="/usr/local/apache"
# Check if user is root
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
# Install dependencies
apt-get update
apt-get install -y build-essential libpcre3-dev libssl-dev zlib1g-dev
# Download Apache
cd /tmp
wget "$URL"
tar xzf "httpd-$VERSION.tar.gz"
cd "httpd-$VERSION"
# Configure Apache
./configure --prefix="$INSTALL_DIR" --enable-ssl --with-ssl --enable-so --enable-mods-shared=all
# Build and install Apache
make
make install
# Create Apache service
cat << EOF > /etc/systemd/system/apache.service
[Unit]
Description=Apache Web Server
After=network.target
[Service]
Type=forking
ExecStart=$INSTALL_DIR/bin/apachectl start
ExecStop=$INSTALL_DIR/bin/apachectl stop
ExecReload=$INSTALL_DIR/bin/apachectl graceful
PIDFile=$INSTALL_DIR/logs/httpd.pid
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
# Reload systemd
systemctl daemon-reload
# Start and enable Apache service
systemctl start apache
systemctl enable apache
# Print Apache version
echo "Apache $VERSION has been installed successfully at $INSTALL_DIR"
```
使用方法:
1. 将以上脚本保存为文件,例如 `install_apache.sh`。
2. 使用 `chmod +x install_apache.sh` 命令将其变为可执行文件。
3. 执行 `sudo ./install_apache.sh` 运行脚本,等待安装完成即可。
阅读全文