nginx结合Apache实现动静分离

9 篇文章 0 订阅

apache的安装与配置

apache下载地址:http://httpd.apache.org/download.cgi#apache24
下载链接:https://mirrors.tuna.tsinghua.edu.cn/apache//httpd/httpd-2.4.46.tar.gz

  1. 编译安装时可能遇到的问题
  • 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
  1. 下载编译安装 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文件
  1. Apache处理PHP动态请求的稳定性高于NGINX+PHP-fpm的方式,这个是因为PHP利用Apache的动态模块机制实现了高度整合。在安装Apache时,编译选项中有一个–enable-so,添加此选项后,Apache就会在bin目录下生成一个apxs程序,apxs是Apache提供的一个扩展工具,用于编译模块,PHP能够利用apxs编译一个用于Apache访问PHP的模块,以实现整合。
  2. 如果已经安装了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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值