安装Nginx
1、下载对应当前系统版本的Nginx包 (注:如果wget命令无效,请先通过yum install -y wget安装)
wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
2、建立Nginx的yum仓库(注:yum本身没有Nginx,建立仓库需在Nginx包所在目录)
rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm
3、yum源安装Nginx
yum install -y nginx
4、防火墙设置(注: 作用域:–zone ,添加端口(端口/协议):–add-port=80/tcp ,永久生效: -permanent )
firewall-cmd --zone=public --add-port=80/tcp --permanent
5、重启Nginx/防火墙
systemctl restart nginx
systemctl restart firewalld
6、页面访问结果如下
安装php-fpm及php
1、先安装EPEL源用于yum源安装
yum install epel-release
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
2、yum安装php
yum install php70w php70w-mysql php70w-fpm
Nginx支持php配置
1、修改nginx的配置文件,使其支持php文件 。(可以通过whereis nginx.conf 查找)
注意:如果在服务器上,记得将php-fpm的ww.conf中的user=apache 及group=apache 改为和nginx.conf中的user一致
location / {
root /usr/share/nginx/html;
# 添加index.php文件标识
index index.html index.htm index.php;
}
location ~ \.php$ {
# html修改为php的根目录 比如: /usr/share/nginx/html
root html;
#php-fpm的默认端口是9000
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#/scripts修改为$document_root
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
2、重启php-fpm及Nginx 服务
systemctl restart php-fpm
systemctl restart nginx
3、验证(在php根目录下创建index.php文件,并打印出phpinfo()信息,访问验证下即可)
echo "<?php phpinfo();">>index.php
4、访问IP地址,结果如下