CentOS 7.4 搭建LNMP(Linux+Nginx+MySQL+PHP)

转发:https://blog.csdn.net/u014558668/article/details/79314878

 

环境:CentOS 7.4.1708

 
  1. [root@localhost ~]# cat /etc/redhat-release

  2. 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端口也没有被放开,故而无法访问。

查看防火墙状态:

 
  1. [root@localhost sysconfig]# systemctl status firewalld.service

  2. ● firewalld.service - firewalld - dynamic firewall daemon

  3. Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)

  4. Active: active (running) since 一 2018-02-12 09:40:34 CST; 1h 5min ago

  5. Docs: man:firewalld(1)

  6. Main PID: 782 (firewalld)

  7. CGroup: /system.slice/firewalld.service

  8. └─782 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid

  9.  
  10. 2月 12 09:40:31 localhost systemd[1]: Starting firewalld - dynamic firewall daemon...

  11. 2月 12 09:40:34 localhost systemd[1]: Started firewalld - dynamic firewall daemon.

  12. 2月 12 09:40:35 localhost.localdomain firewalld[782]: WARNING: ICMP type 'beyond-scope' is not...6.

  13. 2月 12 09:40:35 localhost.localdomain firewalld[782]: WARNING: beyond-scope: INVALID_ICMPTYPE:...e.

  14. 2月 12 09:40:35 localhost.localdomain firewalld[782]: WARNING: ICMP type 'failed-policy' is no...6.

  15. 2月 12 09:40:35 localhost.localdomain firewalld[782]: WARNING: failed-policy: INVALID_ICMPTYPE...e.

  16. 2月 12 09:40:35 localhost.localdomain firewalld[782]: WARNING: ICMP type 'reject-route' is not...6.

  17. 2月 12 09:40:35 localhost.localdomain firewalld[782]: WARNING: reject-route: INVALID_ICMPTYPE:...e.

  18. Hint: Some lines were ellipsized, use -l to show in full.

关闭防火墙之后:

[root@localhost sysconfig]# systemctl stop firewalld.service

此时,浏览器中直接输入IP地址,就可以看到Nginx的默认欢迎页面。

关于防火墙设置:

CentOS 7 以上版本:

 
  1. 1.查看已开放的端口(默认不开放任何端口)

  2. firewall-cmd --list-ports

  3. 2.开启80端口

  4. firewall-cmd --zone=public(作用域) --add-port=80/tcp(端口和访问类型) --permanent(永久生效)

  5. 3.重启防火墙

  6. firewall-cmd --reload

  7. 4.停止防火墙

  8. systemctl stop firewalld.service

  9. 5.禁止防火墙开机启动

  10. systemctl disable firewalld.service

  11. 6.删除

  12. firewall-cmd --zone=public --remove-port=80/tcp --permanent

CentOS 7 以下版本:

 
  1. 1.开放80,22,8080 端口

  2. /sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT

  3. /sbin/iptables -I INPUT -p tcp --dport 22 -j ACCEPT

  4. /sbin/iptables -I INPUT -p tcp --dport 8080 -j ACCEPT

  5. 2.保存

  6. /etc/rc.d/init.d/iptables save

  7. 3.查看打开的端口

  8. /etc/init.d/iptables status

  9. 4.关闭防火墙

  10. 1) 永久性生效,重启后不会复原

  11. 开启: chkconfig iptables on

  12. 关闭: chkconfig iptables off

  13. 2) 即时生效,重启后复原

  14. 开启: service iptables start

  15. 关闭: service iptables stop

 

这里,采用让firewall放开80端口,具体操作如下:

1)查看当前已经开放的端口

 
  1. [root@localhost ~]# firewall-cmd --list-ports

  2.  

2)开启80端口

 
  1. [root@localhost ~]# firewall-cmd --zone=public --add-port=80/tcp --permanent

  2. success

3)重启防火墙

 
  1. [root@localhost ~]# firewall-cmd --reload

  2. success

4)查看当前已经开放的端口

 
  1. [root@localhost ~]# firewall-cmd --list-ports

  2. 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

[root@localhost ~]# yum install php-fpm
 
  1. [root@localhost ~]# php-fpm -v

  2. PHP 5.4.16 (fpm-fcgi) (built: Nov 15 2017 16:35:28)

  3. Copyright (c) 1997-2013 The PHP Group

  4. Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies

启动PHP-FPM

[root@localhost ~]# systemctl start php-fpm

以上安装完成后,接下来,配置Nginx支持PHP(FastCGI方式)。

修改 /usr/local/nginx/conf/nginx.conf 把如下图红色框中的#去掉就可以了。

这里面都是默认的,root是配置php程序放置的根目录。

还需要修改的就是fastcgi_param中的/scripts为$document_root

修改完成后,让nginx重新加载配置以生效:

[root@localhost conf]# ../sbin/nginx -s reload

 

接下来编辑一个测试的php程序,在nginx下的html目录下创建phpinfo.php文件,写上下面代码,保存。

 
  1. [root@localhost html]# pwd

  2. /usr/local/nginx/html

  3. [root@localhost html]# ll

  4. 总用量 12

  5. -rw-r--r--. 1 root root 537 2月 12 10:24 50x.html

  6. -rw-r--r--. 1 root root 612 2月 12 10:24 index.html

  7. -rw-r--r--. 1 root root 20 2月 12 11:57 phpinfo.php

 
  1. [root@localhost html]# cat phpinfo.php

  2. <?php

  3. phpinfo();

  4. ?>

然后打开浏览器,输入对应的地址进行访问,看到如下页面,说明nginx和php都配置成功了。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值