安装脚本
#!/bin/bash
# **********************************************************
# * Author : hongk
# * Email : 2608781325@qq.com
# * Create time : 2023-02-06 10:42
# * Filename : lnmp.sh
# * Description :
# **********************************************************
#安装nginx(以1.20.2版本为例)
nginxPak=/root/nginx-1.20.2.tar.gz
nginxPath=nginx-1.20.2
yum install -y gcc pcre-devel openssl-devel
useradd -s /sbin/nologin nginx
tar -xvf $nginxPak
cd $nginxPath
./configure --user=nginx --group=nginx --with-http_ssl_module #可以根据所需要的功能模板来安装
make && make install
/usr/local/nginx/sbin/nginx
netstat -ntlp | grep 80
[ $? -eq 0 ] && echo 'nginx is installed' || exit
#安装mariadb,mysql也可
yum install -y mariadb mariadb-server mariadb-devel
systemctl start mariadb
systemctl enable mariadb
netstat -ntlp | grep 3306
[ $? -eq 0 ] && echo 'mariadb is installed' || exit
#安装php
yum install -y php php-mysql php-fpm
systemctl start php-fpm
systemctl enable php-fpm
netstat -ntlp | grep php
[ $? -eq 0 ] && echo 'php is installed' || exit
#修改nginx配置文件,实现动静分离
sed -i '65,71s/#//' /usr/local/nginx/conf/nginx.conf
sed -i '69d' /usr/local/nginx/conf/nginx.conf
sed -i 's/fastcgi_params/fastcgi.conf/' /usr/local/nginx/conf/nginx.conf
sed -i '45s/index index.html index.htm/index index.php index.html index.htm/' /usr/local/nginx/conf/nginx.conf
/usr/local/nginx/sbin/nginx -s reload
增加php文件,测试是否能够解析php页面
vim /usr/local/nginx/html/index.php
<?php
$i="This is a test Page";
echo $i;
?>
#连接数据库
vim /usr/local/nginx/html/test.php
<?php
$mysqli = new mysqli('localhost','root','','mysql');
#注意:root为mysql数据库的账户名称,密码需要修改为实际mysql密码,无密码则留空即可
#localhost是数据库的域名或IP,mysql是数据库的名称
if (mysqli_connect_errno()){
die('Unable to connect!'). mysqli_connect_error();
}
$sql = "select * from user";
$result = $mysqli->query($sql);
while($row = $result->fetch_array()){
printf("Host:%s",$row[0]);
printf("</br>");
printf("Name:%s",$row[1]);
printf("</br>");
}
?>
验证