微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

yum搭建LNMP环境

1、安装Nginx

//1.使⽤用Nginx官⽅方提供的rpm包

[root@llody ~]# cat /etc/yum.repos.d/Nginx.repo

[Nginx]

name=Nginx repo

baseurl=http://Nginx.org/packages/centos/7/$basearch/

gpgcheck=0

enabled=1

//2.执⾏行行yum安装

[root@llody ~]# yum install Nginx -y

[root@llody ~]# systemctl start Nginx

[root@llody ~]# systemctl enable Nginx

 

//测试Nginx是否可以访问:

 

 

 

 

 

当然,这里如果没做什么改动,访问的页面应该是Nginx首页

 

2、使用第三方扩展epel源安装PHP7.2:

// 移除旧版 PHP

[root@llody ~]# yum remove PHP-MysqL-5.4 PHP PHP-fpm PHP-common

// 安装扩展源

[root@llody ~]# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-

7.noarch.rpm

[root@llody ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

// 安装 PHP72 版本

[root@llody ~]# yum -y install PHP72w PHP72w-cli PHP72w-common PHP72w-devel \

PHP72w-embedded PHP72w-gd PHP72w-mbstring PHP72w-pdo PHP72w-xml PHP72w-fpm \

PHP72w-MysqLnd PHP72w-opcache

// 启动 PHP

[root@llody ~]# systemctl start PHP-fpm

[root@llody ~]# systemctl enable PHP-fpm

 

3、安装MysqL(mariadb):

//下载官⽅方扩展源, 扩展源集成MysqL5.6、5.7、8.0,仅5.7仓库是开启

[root@llody ~]# rpm -ivh http://repo.MysqL.com/yum/MysqL-5.7-community/el/7/x86_64/

MysqL57-community-release-el7-10.noarch.rpm

[root@llody ~]# yum install MysqL-community-server -y

[root@llody ~]# systemctl start MysqLd

[root@llody ~]# systemctl enable MysqLd

//如果MysqL登陆需要密码,请查看该⽂文件

[root@llody ~]# grep 'temporary password' /var/log/MysqLd.log

//登陆MysqL重新配置密码

[root@llody ~]# MysqL -uroot -p'password'

MysqL> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!';

 

遇到问题1:

登陆需要密码,按上述操作能解决

问题2:

用远程工具(Navicat)无法远程连接,一直报1045:

解决方案:

1、linux防火墙未加3306端口

 

2、关闭firewalld防火墙:systemctl stop firewalld,systemctl disable firewalld

 

3、服务器上登录MysqL,查看是否有地址限制,

 

use MysqL; #进入MysqL数据库

 

select host,user from user; #查询连接用户

 

grant all privileges on *.* to 'root'@'%' identified by '密码'; #授权连接地址

 

flush privileges; #立即生效

exit; #退出数据库

systemctl restart MysqLd #重启服务

 

 

 

出现%表示成功,%也表示允许所有IP地址连接,也可以指定IP地址连接。

 

 

连接成功。

 

 

 

4、配置Nginx实现动态请求转发至PHP

 

 

替换Nginx自带的default.conf

[root@llody ~]# cat /etc/Nginx/conf.d/PHP.conf

server {

server_name _;

listen 80;

root /soft/code;

index index.PHP index.html;

 

location ~ \.PHP$ {

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.PHP;

fastcgi_param SCRIPT_FILENAME /soft/code$fastcgi_script_name;

include fastcgi_params;

}

}

 

遇到问题1:一直报403

解决:1、chmod -R 755 /soft/code #赋予code文件件可读可写权限

2、查看本机selinux 是否开启

[root@llody code]# getenforce #查看状态

Enforcing

[root@llody code]# setenforce 0 #关闭selinux,临时关闭(不用重启)

[root@llody code]# getenforce #查看是否状态

Permissive

 

一直关闭

修改配置文件 /etc/ selinux/config,将SELINUX=enforcing改为SELINUX=disabled

 

vim /etc/selinux/config

 

 

 

在/soft/cpde/新建以下PHP访问文件

 

 

 

5、配置PHP文件,测试连接MysqL

// 使⽤用 MysqLi 模块测试连接 MysqL

[root@llody ~]# cat /soft/code/MysqLi.PHP

<?PHP

$servername = "192.168.2.128";

$username = "root";

$password = "admin"; #没设置数据库密码就不填

 

//创建连接

$conn = MysqLi_connect($servername,$username,$password);

 

//检测连接是否成功

if(!$conn){

die("连接失败:".MysqLi_connect_error());

 

}

echo "连接成功";

 

?>

 

 

 

 

 

 

// 使⽤用 pdo 模块测试连接 MysqL

[root@llody code]# vim MysqLpdo.PHP

<?PHP

$servername = "192.168.2.128";

$username = "root";

$password = "admin";

 

try{

 

$conn = new PDO("MysqL:host=$servername;dbname=MysqL", $username, $password);

 

echo "PDO连接成功";

 

 

}catch(PDOException $e)

{

echo $e ->getMessage();

echo "连接失败";

}

 

?>

 

 

 

 

 

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐