Linux 编译安装Apache httpd服务的脚本
时间: 2024-02-13 15:04:38 浏览: 132
好的,以下是一个简单的 Bash 脚本,可以用于在 Linux 系统上编译安装 Apache httpd 服务:
```bash
#!/bin/bash
# Apache httpd 版本号
HTTPD_VERSION="2.4.46"
# 安装目录
INSTALL_DIR="/usr/local/apache"
# 下载并解压源码包
wget https://www-us.apache.org/dist/httpd/httpd-${HTTPD_VERSION}.tar.gz
tar -xf httpd-${HTTPD_VERSION}.tar.gz
cd httpd-${HTTPD_VERSION}
# 配置编译选项
./configure --prefix=${INSTALL_DIR}
# 编译并安装
make && make install
# 启动 Apache httpd 服务
${INSTALL_DIR}/bin/apachectl start
```
你可以将上述脚本保存为一个名为 `install_apache.sh` 的文件,然后在 Linux 终端中执行 `bash install_apache.sh` 命令即可开始安装 Apache httpd 服务。当然,在执行脚本之前,你需要根据自己的需求修改 `HTTPD_VERSION` 和 `INSTALL_DIR` 变量的值。
相关问题
linux编译安装apache
### 编译安装Apache HTTP服务器
#### 准备工作
为了成功编译并安装Apache HTTP服务器,在Linux环境下需先确认已安装必要的依赖库。这些通常包括`gcc`, `make`, 和其他开发工具链组件。
#### 获取源代码
下载最新的Apache HTTP Server版本是非常重要的一步。可以从官方网站获取稳定版的压缩包文件,例如`httpd-2.4.x.tar.gz`这样的格式[^2]。
```bash
wget http://mirror.bit.edu.cn/apache/httpd/httpd-2.4.39.tar.gz
tar -zxf httpd-2.4.39.tar.gz
cd httpd-2.4.39
```
#### 安装依赖项
除了基本的构建工具外,还需要几个额外的支持库来增强Apache的功能,比如APR (Apache Portable Runtime), APR-util, 和 PCRE (Perl Compatible Regular Expressions) 库。对于某些发行版来说,可能已经预装了部分所需的库;如果没有,则可以通过包管理器或者手动编译的方式获得它们[^3]。
##### 安装 apr
```bash
# 使用系统自带仓库中的apr-devel包简化操作
sudo yum install apr-devel # 对于基于RedHat/CentOS系统的命令
# 或者
sudo apt-get install libapr1-dev # 针对Debian/Ubuntu类系统
```
##### 安装 apr-util
同样地,可以利用官方资源或本地镜像站点下载对应的`.tar.bz2`文件,并按照常规流程解压、配置、编译以及安装。
##### 安装 pcre
PCRE用于支持正则表达式的处理能力,这对于URL重写等功能至关重要。大多数现代Linux发行版都提供了现成的二进制包可以直接安装。
#### 编译 Apache
完成上述准备工作之后就可以正式开始编译Apache本身了:
```bash
./configure --prefix=/usr/local/apache2 \
--enable-so \
--enable-cgi \
--enable-rewrite \
--with-included-apr \
--enable-modules=most \
--enable-mods-shared=all
make && sudo make install
```
这段脚本指定了多个选项来自定义最终生成的服务端程序的行为模式及其性能特征。其中一些关键参数解释如下:
- `--prefix`: 设置安装路径,默认情况下会放置到`/usr/local/apache2`目录内;
- `--enable-*`: 启用特定模块或特性集;
- `--with-included-apr`: 表明使用捆绑在一起发布的APR版本而非系统级实例。
#### 初始化设置与验证
一旦编译过程顺利完成,接下来就是调整初始配置文件(位于`conf/httpd.conf`) 并尝试启动新搭建起来的服务进程以检验一切是否正常运作。
```bash
/usr/local/apache2/bin/apachectl start
curl localhost
```
如果返回了一个标准HTML页面的内容片段,说明HTTP请求已经被正确接收和响应,表明整个安装过程达到了预期效果。
写一个编译安装apache的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` 运行脚本,等待安装完成即可。
阅读全文
相关推荐















