环境:linux(centos7)+nginx+mysql+php
测试Nginx与PHP之间的连通性
配置Nginx主配置文件
配置iwebshop虚拟主机配置文件
当用户是静态的请求就跳转到主页文件,php动态请求的话就交给PHP软件来处理
说明:利用nginx的location区块实现动态请求与静态请求的分别处理
一、命令使用:输入大写G,到最后一行,输入:set paste,点击执行,输入a开始编辑模式,右键粘贴(撤销使用,esc退出,输入:u,注意:光标移到没有字符的地方,不然有可能会格式错误)
[root@localhost etc]# vim /data/server/nginx/conf/nginx.conf
# 在http配置段内容增加下面的内容
server {
listen 80;
server_name iwebshop.itcast.com;
#静态请求处理的location
location / {
root html/iwebshop;
index index.php index.html index.htm;
}
#动态请求处理的location
location ~* .*\.(php|php5)?$ {
root html/iwebshop;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
}
二、配置ip地址和域名的文件:127.0.0.1 iwebshop.localhost.com
vim /etc/hosts
三、修改完配置文件后,检查语法并重启nginx服务
[root@localhost etc]# /data/server/nginx/sbin/nginx -t
nginx: the configuration file /data/server/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /data/server/nginx/conf/nginx.conf test is successful
[root@localhost etc]# /data/server/nginx/sbin/nginx -s reload
正常重启的时候命令行是没有任何变化的,如果有,那可能是端口占用了,如:
nginx: [emerg] still could not bind()
查询被占用的端口号:
netstat -ntlp|grep 80
kill -9 端口号
再执行:./nginx 就不会报错了
四、生成一个php测试文件,生成一个php的测试文件到blog站点目录下
进入到html目录下,mkdir iwebshop一个文件夹,cd/iwebshop/ vim test_info.php,进入到编辑页面输入:<?php phpinfo(); ?>,保存退出,打开虚拟机centos的浏览器地址输入:http://iwebshop.localhost.com/test_info.php 显示php页面则配置成功
cd /data/server/nginx/html
mkdir iwebshop
echo '<?php phpinfo(); ?>' >/data/server/nginx/html/iwebshop/test_info.php