首先说一下,为什么是LNAMP而不是LANMP,因为Apache的强项在于处理动态页,它与PHP可以完美配合,但nginx想要实现动态网站就比较麻烦,而且动静分离要基于nginx才能做到,所以一般的思路是,先访问nginx,nginx自己处理静态页请求,将动态页请求转给Apache处理,所以Apache只是个打工的,nginx才是老板。实际的架构上通过默认的80端口访问的一般也是nginx,分给Apache的一般就是个没人用的几万开外的端口。
正式开始
安装软件包
yum -y install nginx httpd php mysql
将Apache的监听端口改为10000,并启动Apache、nginx和mysql
systemctl enable httpd
systemctl restart httpd
systemctl enable nginx
systemctl restart nginx
systemctl enable mysql
systemctl restart mysql
在Apache服务路径下添加index.php以显示动态页效果
echo "<?php phpinfo(); ?>" > /var/www/html/index.php
现在Apache已经可以自动访问.php文件了
在nginx的配置文件中部署动静分离
server {
listen 80 ;
location ~* \.php$ {
proxy_pass http://127.0.0.1:10000;
}
location / {
root /usr/share/nginx/html/;
}
}
重启nginx,再次访问nginx的动态页时已经跳转到Apache上了