apache的安装与配置
apache下载地址:http://httpd.apache.org/download.cgi#apache24
下载链接:https://mirrors.tuna.tsinghua.edu.cn/apache//httpd/httpd-2.4.46.tar.gz
- 编译安装时可能遇到的问题
- apr not found
tar -zxf apr-1.4.5.tar.gz
cd apr-1.4.5
./configure --prefix=/usr/local/apr
make && make install
- APR-util not found
tar -zxf apr-util-1.3.12.tar.gz
cd apr-util-1.3.12
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/bin/apr-1-config --enable-utf8
make && make install
- yum 搜索安装
安装apache 报错configure: error: APR not found. Please read the documentation
yum search APR
yum install apr apr-devel apr-util apr-util-devel
- 下载编译安装 apache
wget https://mirrors.tuna.tsinghua.edu.cn/apache//httpd/httpd-2.4.46.tar.gz #下载Apache安装包
tar -zxvf httpd-2.4.46.tar.gz #解压安装包
cd httpd-2.4.46
./configure --prefix=/usr/local/apache --enable-rewrite --enable-so --enable-headers --enable-expires --with-mpm=worker --enable-modules=most --enable-deflate --with-apr-util=/usr/local/apr-util
make && make install # 编译安装
cp /usr/local/apache/bin/apachectl /etc/init.d/httpd #复制启动文件到/etc/init.d目录
#因为Apache与nginx默认都会监听80端口,
#所以我们在安装Apache或者启动Apache之前先关闭nginx,或者先把Apache的配置进行修改
service httpd start #启动Apache
Apache的配置修改
- apache的配置目录在/usr/local/apache/conf/httpd.conf(注意:此处为配置文件地址)
Apache的虚拟主机可以在conf/httpd.conf中进行配置,也可以在conf/extra/httpd-vhost.conf中配置。后者是在httpd.conf文件中通过include指令引入的子配置文件,但是在使用前需要现在httpd.conf中找到如下一行配置取消注释,否则配置将不会生效。
#include conf/extra/httpd-vhost.conf #去掉前面的#号
- 然后打开httpd-vhost.conf文件,可以看到Apache提供的默认配置。我们只需要删除多余的配置,添加自己的配置即可
<VirtualHost *:81>
DocumentRoot "/wwwroot/test"
ServerName www.test.com
ServerAlias test.com
</VirtualHost>
<Directory "/wwwroot/test">
Require all granted
</Directory>
保存之后,重启Apache即可
- Apache配置访问php文件
- Apache处理PHP动态请求的稳定性高于NGINX+PHP-fpm的方式,这个是因为PHP利用Apache的动态模块机制实现了高度整合。在安装Apache时,编译选项中有一个–enable-so,添加此选项后,Apache就会在bin目录下生成一个apxs程序,apxs是Apache提供的一个扩展工具,用于编译模块,PHP能够利用apxs编译一个用于Apache访问PHP的模块,以实现整合。
- 如果已经安装了nginx+php-fpm,这里需要整合php-fpm与Apache,所以对于php需要重新编译
cd /home/php
make clean #清除之前安装过的nginx+PHP
./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache/bin/apxs --with-zlib --enable-zip --enable-mbstring --with-mcrypt --with-mysql --with-mysqli --with-pdo-mysql --with-gd --with-jpeg-dir --with-webp -dir --with-png-dir --with-freetype-dir --with-curl --with-openssl --with-mhash --enable-bcmath --enable-opcache -with-apr-util=/usr/local/apr-util
make && make install
7.4+编译安装时请参考:php7.4x变更
- 参考编译
./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache/bin/apxs --with-zlib --with-zip --enable-mbstring --with-mcrypt --with-mysqli --with-pdo-mysql --enable-gd --with-jpeg --with-webp --with-freetype --with-curl --with-openssl --with-mhash --enable-bcmath --enable-opcache -with-apr-util=/usr/local/apr-util
Apache配置的修改:
<FilesMatch "\.php$">
setHandler application/x-httpd-php
</FilesMatch>
<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>
修改nginx对动态请求的处理
server
{
location ~ \.php$ {
proxy_pass http://127.0.0.1:81;
proxy_set_header Host $host;
proxy_set_header X-Client-IP $remote_addr;
}
}