Centos7yum安装LNMP
(1)安装nginx
0.关闭防火墙
systemctl stop firewald.service
systemctl disable firewald.service
1.使用nginx官方提供的yum源
#vi /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
2.安装和启动
yum install nginx -y
systemctl enable nginx
systemctl start nginx
(2)使用第三方扩展epel源安装php7.2
1.移除旧版的php
yum remove php-mysql-5.4 php php-fpm php-common -y
2.安装扩展源
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
3.安装php72版本
yum -y install php72w php72w-cli php72w-common php72w-devel php72wembedded php72w-gd php72w-mbstring php72w-pdo php72w-xml php72w-fpm php72w-mysqlnd php72w-opcache
4.启动php
systemctl start php-fpm
systemctl enable php-fpm
(3)安装mysql5.7
1.安装官方yum源和下载启动mysql
rpm -ivh http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql57-community-release-el7-10.noarch.rpm
yum install mysql-community-server -y
systemctl start mysqld
systemctl enable mysqld
2.修改默认密码
password=$(grep "temporary password" /var/log/mysqld.log | awk \'{print $NF}\')
mysqladmin -uroot -p$password password \'Redhat@123
(4)配置nginx动态请求转发至php
1.修改nginx配置文件
#vim /etc/nginx/conf.d/php.conf
server {
server_name www.test.com;
listen 80;
root /webroot/www;
index index.php index.html;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /webroot/www$fastcgi_script_name;
include fastcgi_params;
}
}
#nginx -t
#nginx -s reload
2.添加测试页面
mkdir -pv /webroot/www
#vim /webroot/www/info.php
<?php
phpinfo();
?>
3.验证
4.连接数据库测试
使用mysqli模块测试连接mysql
#vim /webroot/www/mysqli.php
<?php
$servername = "localhost";
$username = "root";
$password = "Redhat@123";
//注意mysql密码
// 创建连接
$conn = mysqli_connect($servername, $username, $password);
// 检测连接
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "连接成功";
?>
使用pdo模块测试连接mysql
#vim /webroot/www/pdo.php
<?php
$servername = "localhost";
$username = "root";
$password = "Redhat@123";
try {
$conn = new PDO("mysql:host=$servername;dbname=mysql", $username, $password);
echo "连接成功";
}catch(PDOException $e)
{
echo $e->getMessage();
}
?>