实验环境
vmware虚拟机双核2G内存以上
安装有CentOS7和docker
配置nginx支持php
启动进入容器nginx
- 启动容器docker run -d --privileged -p 80:80 nginx /usr/sbin/init
- 查看容器docker ps
- 进入容器docker exec -it 容器ID /bin/bash
使用yum方式安装php-fpm
-
使用yum 方式安装php-fpm
-
查看php-fpm配置文件:/etc/php-fpm.conf和/etc/php-fpm.d/www.conf
-
编辑/etc/php-fpm.d/www.conf,修改监听地址和端口
-
启动php-fpm,systemctl start php-fpm
-
配置php-fpm自启动,systemctl enable php-fpm
-
netstat -antp,查看php-fpm监听端口;
配置nginx支持php
- 编辑/etc/nginx/nginx.conf文件, 重新启动nginx服务
删除原有server代码块
server {
listen 80;
server_name localhost;
location / {
root /var/www;
index index.html;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/$fastcgi_script_name;
include fastcgi_params;
}
}
关于FastCGI:
请求处理流程:CGI规范允许Web服务器根据浏览器请求调用CGI程序,并将其输出结果通过响应发送给浏览器,从而使Web服务器支持处理复杂的网站业务需求
- 在/var/www目录下建立index.php文件
<?php
phpinfo();
?>
- 在主机中使用浏览器访问http://虚拟机地址/index.php
配置Nginx+Apache实现动静分离
动静分离:
由Nginx提供对外访问,静态请求直接由Nginx处理,动态请求转交给Apache处理,这样就实现了动静分离。
动态请求是指该请求需要服务器端的程序处理。静态请求不需要程序处理,直接读取文件并返回即可。
启动进入容器centos:v1
-
启动容器docker run -d --privileged centos:v1 /usr/sbin/init
-
查看容器docker ps -a
-
进入容器docker exec -it 容器ID /bin/bash
使用yum方式安装apache和php
-
使用yum方式安装httpd
-
使用yum方式安装php
-
编辑/var/www/html/site.php文件
<? php
echo “site2”;
?>
-
重启httpd,netstat -antp查看监听端口
-
配置httpd自启动,systemctl enable httpd
-
在虚拟机使用curl http://容器地址/site.php
在虚拟机中保存容器,docker commit 容器ID php-apache
配置nginx支持动静分离
-
进入容器nginx
-
编辑/etc/nginx/nginx.conf文件
server {
listen 80;
server_name localhost;
location / {
root /var/www;
index index.html;
}
location ~ \.php$ {
proxy_pass http://172.17.0.3;
proxy_set_header host $host;
}
}
- 重新启动nginx服务
- 在主机中使用浏览器访问http://虚拟机地址/site.php