一、环境部署
1.简介:LNMP是指一组通常一起使用来运行动态网站或者服务器的自由软件名称。L指Linux,N指Nginx,M一般指MySQL,也可以指MariaDB,P一般指PHP,也可以指Perl或Python。
2.部署环境:
-----linux:关闭防火墙、关闭selinux-----
#systemctl stop firewalld //关闭防火墙
#systemctl disabled firewalld //禁用防火墙开机自启动
#vim /etc/sysconfig/selinux //进入selinux文件中,将selinux=xxxxx 改为selinux=disable
-----配置阿里巴巴开源centos、epel镜像-----
#mv /etc/yum.repos.d/* /tmp/ //将自带yum源移除
#wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo //下载阿里巴巴centos镜像
#wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo //下载阿里巴巴epel镜像
-----安装nginx-----
#yum -y install nginx //安装nginx,默认最新版
#systemctl start nginx //开启nginx程序
注意:由于nginx和apache同时使用80端口,故如果安装了apache,要把apache程序先关闭,以防止端口占用带来的报错。
-----安装php中间件-----
#yum -y install php-fpm php-mysql php-gd //安装php和常用的包
#systemctl start php-fpm //启动php程序
#systemctl enable php-fpm //设置php开机自启
-----数据库安装-----
为了简化过程,这里我用mariadb关系型数据库进行试验,mysql数据库同样可以达到效果,为了避免数据库冲突,建议安装一种关系型数据库即可。
#yum -y install mariadb mariadb-server //安装mariadb数据库
#systemctl start mariadb //启动数据库
#systemctl enable mariadb //设置数据库开机自启动
#mysqladmin password '666666' //设置密码为666666
二、nginx配置php
由于nginx不能处理php文件,所以要对nginx配置文件进行更改
#vim /etc/nginx/nginx.conf //进入编译配置文件
在46行后插入代码后如图所示
location / {
index index.html index.php index.htm;
}
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
include fastcgi_params;
}
由于改了配置文件,需要重启nginx才能生效!
#systemctl restart nginx //重启nginx
三、前端页面编写
#vim /usr/share/nginx/html/index.html //进入html文件
index.html自带的代码可以删掉,然后我们重新编写前端代码。
<!DOCTYPE html>
<html>
<head>
<title>Insert Data</title>
</head>
<body> <img src="/images/1.png" />
<h1>Insert Data</h1>
<form method="post" action="insert.php">
<label for="firstname">First Name:</label>
<input type="text" name="firstname" required><br>
<label for="lastname">Last Name:</label>
<input type="text" name="lastname" required><br>
<label for="age">Age:</label>
<input type="number" name="age" required><br>
<label for="school">School:</label>
<input type="text" name="school" required><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
四、后端代码编写
#vim /usr/share/nginx/html/insert.php //编写后端php代码
<?php
// Connect to the database
$con = mysqli_connect("192.168.192.136", "root", "666666", "my_db");
// Check the connection
if (mysqli_connect_errno()) {
die("Failed to connect to MySQL: " . mysqli_connect_error());
}
// Escape user input to prevent SQL injection
$firstname = mysqli_real_escape_string($con, $_POST['firstname']);
$lastname = mysqli_real_escape_string($con, $_POST['lastname']);
$age = mysqli_real_escape_string($con, $_POST['age']);
$school = mysqli_real_escape_string($con, $_POST['school']);
// Insert the data into the database
$sql = "INSERT INTO Persons (firstname, lastname, age, school) VALUES ('$firstname', '$lastname', $age, '$school')";
if (!mysqli_query($con, $sql)) {
die("Error: " . mysqli_error($con));
}
echo "1 record added";
// Close the database connection
mysqli_close($con);
?>
五、准备数据库
#mysql -uroot -p //进入数据库,回车后输入
MariaDB>CERATE DATABASE my_db; //创建数据库
MariaDB>USE my_db; //进入my_db数据库
MariaDB [my_db]>create table Persons (firstname varchar(50),lastname varchar(50),age int,school varchar(50)); //创建persons数据表
六、浏览器测试
点击submit把内容通过php中间件提交到数据库中
提交成功!
去centos使用select * from Persons; 命令查询数据表中的内容,观察是否插入到数据库中!