美文网首页
centos搭建LNMP环境

centos搭建LNMP环境

作者: IT教程 | 来源:发表于2017-08-06 18:14 被阅读38次

欢迎访问我的个人博客网站:http://www.yanmin99.com/

一、LNMP介绍

  • LNMP是Linux、Nginx、MySQL和PHP的缩写,这个组合是最常见的WEB服务器的运行环境之一

二、Nginx安装

  • 1、通过yum安装Nginx
sudo yum install nginx
  • 2、启动Nginx
service nginx start
  • 3、查看Nginx是否启动成功
  • A、通过netstat查看80端口是否监听
```
[root@iZrj98p4hhys0y9fdxmcy4Z yanmin]# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name
tcp        0      0 0.0.0.0:8989                0.0.0.0:*                   LISTEN      1415/python
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      11295/nginx
```
  • B、通过浏览器访问http://<外网IP地址>,确定Nginx是否启动

  • 5、通过chkconfig设置开机启动

sudo chkconfig nginx on
  • 4、查看Nginx版本
[root@iZrj98p4hhys0y9fdxmcy4Z yanmin]# nginx -v
nginx version: nginx/1.12.1

三、MySQL安装

  • 1、通过yum直接安装MySQL
sudo yum install mysql-server
  • 2、(可以跳过这个步骤)启动MySQL默认脚本,初始化MySQL,安装提示进行设置
  • A、执行mysql_secure_installation
sudo /usr/bin/mysql_secure_installation
  • B、首先提示输入当前的root密码
Enter current password for root (enter for none):
  • C、初始root密码为空,我们直接敲回车进行下一步
Set root password? [Y/n]
  • D、设置root密码,默认选项为Yes,我们直接回车,提示输入密码,在这里设置您的MySQL的root账户密码
Remove anonymous users? [Y/n]
  • E、是否移除匿名用户,默认选项为Yes,建议按默认设置,回车继续。
Remove test database and access to it? [Y/n]
  • F、是否禁止root用户远程登录?如果您只在本机内访问MySQL,建议按默认设置,回车继续
Disallow root login remotely? [Y/n]
  • G、是否删除test用的数据库和权限? 建议按照默认设置,回车继续
Remove test database and access to it? [Y/n]
  • H、是否重新加载权限表?因为我们上面更新了root的密码,这里需要重新加载,回车
Reload privilege tables now? [Y/n]
  • 3、启动MySQL
mysql -uroot -p
  • 4、查看MySQL是否启动,查看3306端口是否监听
[root@iZrj98p4hhys0y9fdxmcy4Z yanmin]# netstat -ntlp
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:8989 0.0.0.0:* LISTEN 1415/python
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 11532/mysqld
  • 5、查看MySQL的版本
[root@iZrj98p4hhys0y9fdxmcy4Z yanmin]# mysql -V
mysql  Ver 14.14 Distrib 5.1.73, for redhat-linux-gnu (x86_64) using readline 5.1

四、PHP安装

  • 1、通过yum安装php-mysql、php-fpm
sudo yum install php php-fpm php-mysql
  • 2、安装以后启动php-fpm
sudo service php-fpm start
  • 3、查看php-fpm是否启动,查看9000端口是否启动
[root@iZrj98p4hhys0y9fdxmcy4Z yanmin]# netstat -ntlp
 Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name
 tcp        0      0 0.0.0.0:8989                0.0.0.0:*                   LISTEN      1415/python
 tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      11642/php-fpm
  • 4、设置开机启动
sudo chkconfig php-fpm on

五、验证LNMP

  • 1、在Nginx的/etc/nginx/conf.d文件夹中创建php.conf文件,设置端口为8000,如下:
  • 通过vim /etc/nginx/conf.d/php.conf打开文件,输入一下内容保存。
server {
    listen 8000;
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root           /usr/share/php;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

Nginx修改配置,需要重启sudo service nginx reload或者service nginx restart

  • 2、在php的目录/usr/share/php文件夹中创建phpinfo.php文件
  • 通过vim /etc/nginx/conf.d/php.conf打开文件,输入以下上内容保存。
<?php echo phpinfo(); ?>
  • 3、验证防火墙是否开启,如果请把8000端口加入白名单
//测试可以直接关闭防火墙
[root@iZrj98p4hhys0y9fdxmcy4Z yanmin]# service iptables stop
iptables: Setting chains to policy ACCEPT: filter nat      [  OK  ]
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Unloading modules:                               [  OK  ]

//查看防火墙状态
[root@iZrj98p4hhys0y9fdxmcy4Z yanmin]# service iptables status
iptables: Firewall is not running.
  • 4、通过浏览器验证,打开http://<外网IP地址>:8000/phpinfo.php,看是否显示php信息

相关文章

网友评论

      本文标题:centos搭建LNMP环境

      本文链接:https://www.haomeiwen.com/subject/uaknlxtx.html