转发:https://blog.csdn.net/u014558668/article/details/79314878
环境:CentOS 7.4.1708
-
[[email protected] ~]# cat /etc/redhat-release -
CentOS Linux release 7.4.1708 (Core)
一、Nginx安装
Nginx版本:1.12.2
具体安装步骤详见:http://blog.csdn.net/u014558668/article/details/79261289
安装成功后,在浏览器输入IP地址,打不开默认欢迎页面。
原因:CentOS 7版本之后对防火墙进行加强,不再使用原来的iptables,启用firewall防火墙默认不开放任何端口,所以Nginx默认的80端口也没有被放开,故而无法访问。
查看防火墙状态:
-
[[email protected] sysconfig]# systemctl status firewalld.service -
● firewalld.service - firewalld - dynamic firewall daemon -
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled) -
Active: active (running) since 一 2018-02-12 09:40:34 CST; 1h 5min ago -
Docs: man:firewalld(1) -
Main PID: 782 (firewalld) -
CGroup: /system.slice/firewalld.service -
└─782 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid -
2月 12 09:40:31 localhost systemd[1]: Starting firewalld - dynamic firewall daemon... -
2月 12 09:40:34 localhost systemd[1]: Started firewalld - dynamic firewall daemon. -
2月 12 09:40:35 localhost.localdomain firewalld[782]: WARNING: ICMP type 'beyond-scope' is not...6. -
2月 12 09:40:35 localhost.localdomain firewalld[782]: WARNING: beyond-scope: INVALID_ICMPTYPE:...e. -
2月 12 09:40:35 localhost.localdomain firewalld[782]: WARNING: ICMP type 'failed-policy' is no...6. -
2月 12 09:40:35 localhost.localdomain firewalld[782]: WARNING: failed-policy: INVALID_ICMPTYPE...e. -
2月 12 09:40:35 localhost.localdomain firewalld[782]: WARNING: ICMP type 'reject-route' is not...6. -
2月 12 09:40:35 localhost.localdomain firewalld[782]: WARNING: reject-route: INVALID_ICMPTYPE:...e. -
Hint: Some lines were ellipsized, use -l to show in full.
关闭防火墙之后:
[[email protected] sysconfig]# systemctl stop firewalld.service
此时,浏览器中直接输入IP地址,就可以看到Nginx的默认欢迎页面。
关于防火墙设置:
CentOS 7 以上版本:
-
1.查看已开放的端口(默认不开放任何端口) -
firewall-cmd --list-ports -
2.开启80端口 -
firewall-cmd --zone=public(作用域) --add-port=80/tcp(端口和访问类型) --permanent(永久生效) -
3.重启防火墙 -
firewall-cmd --reload -
4.停止防火墙 -
systemctl stop firewalld.service -
5.禁止防火墙开机启动 -
systemctl disable firewalld.service -
6.删除 -
firewall-cmd --zone=public --remove-port=80/tcp --permanent
CentOS 7 以下版本:
-
1.开放80,22,8080 端口 -
/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT -
/sbin/iptables -I INPUT -p tcp --dport 22 -j ACCEPT -
/sbin/iptables -I INPUT -p tcp --dport 8080 -j ACCEPT -
2.保存 -
/etc/rc.d/init.d/iptables save -
3.查看打开的端口 -
/etc/init.d/iptables status -
4.关闭防火墙 -
1) 永久性生效,重启后不会复原 -
开启: chkconfig iptables on -
关闭: chkconfig iptables off -
2) 即时生效,重启后复原 -
开启: service iptables start -
关闭: service iptables stop
这里,采用让firewall放开80端口,具体操作如下:
1)查看当前已经开放的端口
-
[[email protected] ~]# firewall-cmd --list-ports
2)开启80端口
-
[[email protected] ~]# firewall-cmd --zone=public --add-port=80/tcp --permanent -
success
3)重启防火墙
-
[[email protected] ~]# firewall-cmd --reload -
success
4)查看当前已经开放的端口
-
[[email protected] ~]# firewall-cmd --list-ports -
80/tcp
此时,在浏览器直接输入IP地址访问Nginx默认欢迎页面,正常。
二、MySQL安装
MySQL版本:5.7.21
具体安装步骤详见:http://blog.csdn.net/u014558668/article/details/79310267
三、PHP安装
PHP版本:5.4.16
具体安装步骤详见:http://blog.csdn.net/u014558668/article/details/79315641
要让PHP以FastCGI的方式与nginx进行交互,需要有PHP-FPM模块的支持。
安装PHP-FPM
[[email protected] ~]# yum install php-fpm
-
[[email protected] ~]# php-fpm -v -
PHP 5.4.16 (fpm-fcgi) (built: Nov 15 2017 16:35:28) -
Copyright (c) 1997-2013 The PHP Group -
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
启动PHP-FPM
[[email protected] ~]# systemctl start php-fpm
以上安装完成后,接下来,配置Nginx支持PHP(FastCGI方式)。
修改 /usr/local/nginx/conf/nginx.conf 把如下图红色框中的#去掉就可以了。
这里面都是默认的,root是配置php程序放置的根目录。
还需要修改的就是fastcgi_param中的/scripts为$document_root
修改完成后,让nginx重新加载配置以生效:
[[email protected] conf]# ../sbin/nginx -s reload
接下来编辑一个测试的php程序,在nginx下的html目录下创建phpinfo.php文件,写上下面代码,保存。
-
[[email protected] html]# pwd -
/usr/local/nginx/html -
[[email protected] html]# ll -
总用量 12 -
-rw-r--r--. 1 root root 537 2月 12 10:24 50x.html -
-rw-r--r--. 1 root root 612 2月 12 10:24 index.html -
-rw-r--r--. 1 root root 20 2月 12 11:57 phpinfo.php
-
[[email protected] html]# cat phpinfo.php -
<?php -
phpinfo(); -
?>
然后打开浏览器,输入对应的地址进行访问,看到如下页面,说明nginx和php都配置成功了。