Apache编译安装

本文详细介绍了从源码编译安装Apache的过程,包括解决依赖问题,如APR、apr-util、pcre的安装,以及如何配置Apache与PHP的关联。

Apache编译安装

官网下载Apache的源码包,http://httpd.apache.org/

libphp7.so提取码:ybvh

复制这段内容后打开百度网盘手机App,操作更方便哦
tar -jxvf httpd-2.4.18.tar.bz2
cd httpd-2.4.18
./configure --prefix=/usr/apache24
到这里发现出了个错误,提示没有APR这个组件,apr的作用就是维护相应的软件安装库,已解决相应的平台缺陷等,之前Apache老版本不会出现这个问题,所以首先安装apr,网址是:http://apr.apache.org/,这里包括apr、apr-util、apr-iconv这三个包

$ tar -zxvf apr-1.5.2.tar.gz
$ cd apr-1.5.2
$ ./configure --prefix=/usr/local/apr
$ make
$ make install

$ tar -zxvf apr-util-1.5.4.tar.gz
$ cd apr-util-1.5.4
$ ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
$ make
$ make install

./configure --prefix=/usr/apache24 --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util

这里报错
xml/apr_xml.c:35:19: fatal error: expat.h: 没有那个文件或目录
compilation terminated.
/etc/httpd/apr-util-1.6.0/build/rules.mk:206: recipe for target ‘xml/apr_xml.lo’ failed
make[1]: *** [xml/apr_xml.lo] Error 1
make[1]: Leaving directory ‘/etc/httpd/apr-util-1.6.0’
/etc/httpd/apr-util-1.6.0/build/rules.mk:118: recipe for target ‘all-recursive’ failed
make: *** [all-recursive] Error 1
解决办法就是安装libexpat1-dev
sudo apt-get install libexpat1-dev

上面配置参数的–with-apr表示apr-util安装需要依赖apr,所以指定apr的安装目录,到这里apr-util也安装完成了
然后配置依赖安装Apache:
在这里插入图片描述
可以看出,少了程序pcre,需要去http://pcre.org/ 获取,那么现在应该访问这个网站下载,进去网站之后很容易就找到下载链接:ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/

这里下载pcre-8.38.zip这个版本即可,其他的也是没问题的

下载成功,安装pcre:

$ unzip pcre-8.38.zip
$ cd pcre-8.38
$ ./configure --prefix=/usr/local/pcre
$ make
$ make install
  到现在,pcre也安装成功了

现在开始安装apache,进入源码包目录,执行命令:

$ ./configure --prefix=/usr/apache24 --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-pcre=/usr/local/pcre
$ make
$ make install
apache与php的关联

PHP安装成功后会在apache的modules目录下生成一个libphp.so动态库文件,在apache的配置文件httpd.conf里自动增加一行。

vi /etc/httpd/httpd.conf
LoadModule php7_module modules/libphp7.so
在Apache的配置文件httpd.conf的块里增加一行

AddType application/x-httpd-php .php
配置index.php为默认执行的文件

DirectoryIndex index.html index.php

/usr/apache24/bin/apachectl start
服务器的默认根目录在/usr/apache24/htdocs/

解决pcre问题>>>>>>>>>
unzip -o pcre-8.10.zip
cd pcre-8.10
./configure --prefix=/usr/local/pcre
make && make install
./configure --prefix=/usr/local/apache --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-apr-iconv=/usr/local/pcre
make && make install

<think>我们之前讨论的是Apache编译安装,用户现在希望获取详细的步骤和注意事项。根据历史记录,我们之前已经提供过一些指导,现在需要更全面和详细的步骤。 Apache编译安装详细步骤: 1. **准备环境** - 安装必要的开发工具和依赖库 - 例如:gcc, make, libtool, openssl, pcre, apr, apr-util等 2. **下载源码** -Apache官网下载最新稳定版源码包 3. **解压源码** - 使用tar命令解压 4. **配置编译选项** - 运行configure脚本,设置安装路径、启用模块等 5. **编译安装** - 使用make进行编译,然后make install安装 6. **配置Apache** - 修改httpd.conf配置文件,设置服务器参数 7. **启动和测试** - 启动Apache服务,并通过浏览器或curl测试 下面给出具体脚本和详细说明:</think>### Apache 编译安装详细步骤与注意事项 #### 一、准备工作 1. **依赖安装** ```bash # CentOS/RHEL sudo yum install gcc make pcre-devel openssl-devel expat-devel apr-devel apr-util-devel # Ubuntu/Debian sudo apt install build-essential libpcre3-dev libssl-dev libexpat1-dev libapr1-dev libaprutil1-dev ``` 2. **下载源码** ```bash wget https://downloads.apache.org/httpd/httpd-2.4.58.tar.gz tar -xzvf httpd-2.4.58.tar.gz cd httpd-2.4.58 ``` #### 二、编译配置 ```bash ./configure \ --prefix=/usr/local/apache2 \ # 安装路径 --enable-mods-shared=most \ # 动态加载模块 --enable-ssl \ # 启用SSL --enable-rewrite \ # URL重写 --with-mpm=event \ # 事件驱动模型 --with-pcre # PCRE正则支持 ``` > **关键参数说明**: > - `--enable-mods-shared=most`:将常用模块编译为动态库(节省内存) > - `--with-mpm=event`:高性能事件驱动模型(适合高并发) > - `--enable-ssl`:必需启用HTTPS支持[^1] #### 三、编译安装 ```bash make -j$(nproc) # 并行编译(nproc为CPU核心数) sudo make install ``` #### 四、基本配置 1. **启动脚本** ```bash sudo cp /usr/local/apache2/bin/apachectl /etc/init.d/httpd sudo systemctl daemon-reload ``` 2. **修改主配置** ```bash vim /usr/local/apache2/conf/httpd.conf ``` 关键修改项: ```apacheconf ServerName localhost:80 Listen 0.0.0.0:80 LoadModule rewrite_module modules/mod_rewrite.so # 启用重写 ``` 3. **创建系统服务** ```systemd # /etc/systemd/system/httpd.service [Unit] Description=Apache Web Server After=network.target [Service] Type=forking ExecStart=/usr/local/apache2/bin/apachectl start ExecStop=/usr/local/apache2/bin/apachectl stop PIDFile=/usr/local/apache2/logs/httpd.pid [Install] WantedBy=multi-user.target ``` #### 五、启动与验证 ```bash sudo systemctl start httpd curl -I localhost # 应返回 HTTP/1.1 200 OK ``` #### ⚠️ 关键注意事项 1. **模块选择原则** - 必须模块:`ssl`、`rewrite`、`headers` - 按需加载:`proxy_fcgi`(PHP-FPM)、`cache_disk`(缓存) - 禁用模块:`autoindex`(生产环境禁用目录列表) 2. **安全加固措施** ```apacheconf # 隐藏版本信息 ServerTokens Prod ServerSignature Off # 禁用危险方法 <LimitExcept GET POST HEAD> Require all denied </LimitExcept> ``` 3. **性能优化参数** ```apacheconf # httpd.conf 调整 Timeout 30 # 连接超时 KeepAlive On # 长连接 MaxKeepAliveRequests 100 # 单连接最大请求数 KeepAliveTimeout 5 # 长连接超时 ``` 4. **编译常见问题解决** | 错误信息 | 解决方案 | |---------|---------| | `apr not found` | 安装`apr-1-devel`和`apr-util-devel` | | `mod_ssl.so undefined symbol` | 重新编译OpenSSL并添加`--enable-ssl=shared` | | `address already in use` | 执行`sudo lsof -i :80`查找占用进程 | 5. **卸载方法** ```bash sudo make uninstall # 源码目录执行 rm -rf /usr/local/apache2 ``` #### 六、验证安装 ```bash # 检查编译参数 /usr/local/apache2/bin/httpd -V # 测试配置文件 /usr/local/apache2/bin/apachectl configtest # 性能压测(需安装ab工具) ab -n 10000 -c 500 http://localhost/ ``` > **最佳实践**: > 1. 生产环境建议使用`--with-included-apr`避免系统库冲突 > 2. 启用`mod_security`模块增强WAF防护 > 3. 日志分割使用`cronolog`代替内置日志轮转[^2] ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值