CentOS下用yum配置php+mysql+apache(LAMP)
#安装需要的包,有依赖关系,自动帮你解决 yum install httpd mysql mysql-server php php-gd php-mbstring php-mysql #启动httpd service httpd start #设为开机启动 chkconfig httpd on #启动mysqld service mysqld start #设为开机启动 chkconfig mysqld on #在根目录下创建一个测试文件,写个phpinfo函数 vi /var/www/html/phpinfo.php
打开浏览器,输入http://ip/phpinfo.php,就可以看到phpinfo的输出页面了
phpinfo.php
<? phpinfo(); ?>
例子1:
表单处理(get)
welcome_get.html
<html> <body> <form action="welcome_get.php" method="get"> Name: <input type="text" name="name"><br> E-mail: <input type="text" name="email"><br> <input type="submit"> </form> </body> </html>
welcome_get.php
<html> <body> Welcome <?php echo $_GET["name"]; ?><br> Your email address is: <?php echo $_GET["email"]; ?> </body> </html>
例子2:
表单处理(post)
welcome.html
<html> <body> <form action="welcome.php" method="post"> Name: <input type="text" name="name"><br> E-mail: <input type="text" name="email"><br> <input type="submit"> </form> </body> </html>
welcome.php
<html> <body> Welcome <?php echo $_POST["name"]; ?><br> Your email address is: <?php echo $_POST["email"]; ?> </body> </html>
例子3:
表单验证
<!DOCTYPE HTML> <html> <head> </head> <body> <?php // define variables and set to empty values $name = $email = $gender = $comment = $website = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { $name = test_input($_POST["name"]); $email = test_input($_POST["email"]); $website = test_input($_POST["website"]); $comment = test_input($_POST["comment"]); $gender = test_input($_POST["gender"]); } function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?> <h2>PHP 验证实例</h2> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 姓名:<input type="text" name="name"> <br><br> 电邮:<input type="text" name="email"> <br><br> 网址:<input type="text" name="website"> <br><br> 评论:<textarea name="></textarea> <br><br> 性别: <input type="radio" name="gender" value="female">女性 <input type="radio" name="gender" value="male">男性 <br><br> <input type="submit" name="submit" value="提交"> </form> <?php echo "<h2>您的输入:</h2>"; echo $name; echo "<br>"; echo $email; echo "<br>"; echo $website; echo "<br>"; echo $comment; echo "<br>"; echo $gender; ?> </body> </html>
例子4:
表单验证+必填字段:
<!DOCTYPE HTML> <html> <head> <style> .error {color: #FF0000;} </style> </head> <body> <?php // 定义变量并设置为空值 $nameErr = $emailErr = $genderErr = $websiteErr = ""; $name = $email = $gender = $comment = $website = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { if (empty($_POST["name"])) { $nameErr = "姓名是必填的"; } else { $name = test_input($_POST["name"]); } if (empty($_POST["email"])) { $emailErr = "电邮是必填的"; } else { $email = test_input($_POST["email"]); } if (empty($_POST["website"])) { $website = ""; } else { $website = test_input($_POST["website"]); } if (empty($_POST["comment"])) { $comment = ""; } else { $comment = test_input($_POST["comment"]); } if (empty($_POST["gender"])) { $genderErr = "性别是必选的"; } else { $gender = test_input($_POST["gender"]); } } function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?> <h2>PHP 验证实例</h2> <p><span class="error">* 必需的字段</span></p> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 姓名:<input type="text" name="name"> <span class="error">* <?php echo $nameErr;?></span> <br><br> 电邮:<input type="text" name="email"> <span class="error">* <?php echo $emailErr;?></span> <br><br> 网址:<input type="text" name="website"> <span class="error"><?php echo $websiteErr;?></span> <br><br> 评论:<textarea name="></textarea> <br><br> 性别: <input type="radio" name="gender" value="female">女性 <input type="radio" name="gender" value="male">男性 <span class="error">* <?php echo $genderErr;?></span> <br><br> <input type="submit" name="submit" value="提交"> </form> <?php echo "<h2>您的输入:</h2>"; echo $name; echo "<br>"; echo $email; echo "<br>"; echo $website; echo "<br>"; echo $comment; echo "<br>"; echo $gender; ?> </body> </html>
例子5:
表单验证+必填字段+正则表达式验证:
<!DOCTYPE HTML> <html> <head> <style> .error {color: #FF0000;} </style> </head> <body> <?php // 定义变量并设置为空值 $nameErr = $emailErr = $genderErr = $websiteErr = ""; $name = $email = $gender = $comment = $website = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { if (empty($_POST["name"])) { $nameErr = "姓名是必填的"; } else { $name = test_input($_POST["name"]); // 检查姓名是否包含字母和空白字符 if (!preg_match("/^[a-zA-Z ]*$/",$name)) { $nameErr = "只允许字母和空格"; } } if (empty($_POST["email"])) { $emailErr = "电邮是必填的"; } else { $email = test_input($_POST["email"]); // 检查电子邮件地址语法是否有效 if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) { $emailErr = "无效的 email 格式"; } } if (empty($_POST["website"])) { $website = ""; } else { $website = test_input($_POST["website"]); // 检查 URL 地址语法是否有效(正则表达式也允许 URL 中的斜杠) if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) { $websiteErr = "无效的 URL"; } } if (empty($_POST["comment"])) { $comment = ""; } else { $comment = test_input($_POST["comment"]); } if (empty($_POST["gender"])) { $genderErr = "性别是必选的"; } else { $gender = test_input($_POST["gender"]); } } function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?> <h2>PHP 验证实例</h2> <p><span class="error">* 必需的字段</span></p> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 姓名:<input type="text" name="name"> <span class="error">* <?php echo $nameErr;?></span> <br><br> 电邮:<input type="text" name="email"> <span class="error">* <?php echo $emailErr;?></span> <br><br> 网址:<input type="text" name="website"> <span class="error"><?php echo $websiteErr;?></span> <br><br> 评论:<textarea name="></textarea> <br><br> 性别: <input type="radio" name="gender" value="female">女性 <input type="radio" name="gender" value="male">男性 <span class="error">* <?php echo $genderErr;?></span> <br><br> <input type="submit" name="submit" value="提交"> </form> <?php echo "<h2>您的输入:</h2>"; echo $name; echo "<br>"; echo $email; echo "<br>"; echo $website; echo "<br>"; echo $comment; echo "<br>"; echo $gender; ?> </body> </html>
例子6:
表单完整版本
<!DOCTYPE HTML> <html> <head> <style> .error {color: #FF0000;} </style> </head> <body> <?php // 定义变量并设置为空值 $nameErr = $emailErr = $genderErr = $websiteErr = ""; $name = $email = $gender = $comment = $website = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { if (empty($_POST["name"])) { $nameErr = "姓名是必填的"; } else { $name = test_input($_POST["name"]); // 检查姓名是否包含字母和空白字符 if (!preg_match("/^[a-zA-Z ]*$/",$name)) { $nameErr = "只允许字母和空格"; } } if (empty($_POST["email"])) { $emailErr = "电邮是必填的"; } else { $email = test_input($_POST["email"]); // 检查电子邮件地址语法是否有效 if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) { $emailErr = "无效的 email 格式"; } } if (empty($_POST["website"])) { $website = ""; } else { $website = test_input($_POST["website"]); // 检查 URL 地址语法是否有效(正则表达式也允许 URL 中的斜杠) if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) { $websiteErr = "无效的 URL"; } } if (empty($_POST["comment"])) { $comment = ""; } else { $comment = test_input($_POST["comment"]); } if (empty($_POST["gender"])) { $genderErr = "性别是必选的"; } else { $gender = test_input($_POST["gender"]); } } function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?> <h2>PHP 验证实例</h2> <p><span class="error">* 必需的字段</span></p> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 姓名:<input type="text" name="name"> <span class="error">* <?php echo $nameErr;?></span> <br><br> 电邮:<input type="text" name="email"> <span class="error">* <?php echo $emailErr;?></span> <br><br> 网址:<input type="text" name="website"> <span class="error"><?php echo $websiteErr;?></span> <br><br> 评论:<textarea name="></textarea> <br><br> 性别: <input type="radio" name="gender" value="female">女性 <input type="radio" name="gender" value="male">男性 <span class="error">* <?php echo $genderErr;?></span> <br><br> <input type="submit" name="submit" value="提交"> </form> <?php echo "<h2>您的输入:</h2>"; echo $name; echo "<br>"; echo $email; echo "<br>"; echo $website; echo "<br>"; echo $comment; echo "<br>"; echo $gender; ?> </body> </html>
例子7:
连接mysql并且输出数据:
<?php $conn=mysql_connect("localhost:3306", "root", "zhenchaowen"); mysql_query("set names 'utf8'"); if(!$conn){ die("could not connect to the database:</br>".mysql_error());//诊断连接错误 } $result=mysql_db_query("testdb001", "SELECT * FROM `customers`", $conn); // 获取查询结果 $row=mysql_fetch_row($result); echo '<font face="verdana">'; echo '<table border="1" cellpadding="1" cellspacing="2">'; // 显示字段名称 echo "</b><tr></b>"; ; $i<mysql_num_fields($result); $i++) { echo '<td bgcolor="#000F00"><b>'. mysql_field_name($result, $i); echo "</b></td></b>"; } echo "</tr></b>"; // 定位到第一条记录 mysql_data_seek($result, ); // 循环取出记录 while ($row=mysql_fetch_row($result)) { echo "<tr></b>"; ; $i<mysql_num_fields($result); $i++ ) { echo '<td bgcolor="#00FF00">'; echo $row[$i]; echo '</td>'; } echo "</tr></b>"; } echo "</table></b>"; echo "</font>"; // 释放资源 mysql_free_result($result); // 关闭连接 mysql_close($conn); ?>
创建插入数据
<html> <body> <form action="insert.php" method="post"> Firstname: <input type="text" name="firstname" /> Lastname: <input type="text" name="lastname" /> Age: <input type="text" name="age" /> <input type="submit" /> </form> </body> </html>
<?php $con = mysql_connect("localhost:3306", "root", "zhenchaowen"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("testdb001", $con); $sql="INSERT INTO Persons (FirstName, LastName, Age) VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[age]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added"; mysql_close($con) ?>
删除数据
<?php $con = mysql_connect("localhost:3306", "root", "zhenchaowen"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("testdb001", $con); mysql_query("DELETE FROM Persons WHERE LastName='Griffin'"); mysql_close($con); ?>
更新数据
<?php $con = mysql_connect("localhost:3306", "root", "zhenchaowen"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("testdb001", $con); mysql_query("UPDATE Persons SET Age = '36' WHERE FirstName = 'Peter' AND LastName = 'Griffin'"); mysql_close($con); ?>
备注:测试前先检查下mysql连接设置:
/usr/bin/mysql -u root -p
例子8:(cookie处理)
创建cookie
<?php setcookie(); ?> <html> <body> </body> </html>
获取cookie
<?php // Print a cookie echo $_COOKIE["user"]; // A way to view all cookies print_r($_COOKIE); ?>
<html> <body> <?php if (isset($_COOKIE["user"])) echo "Welcome " . $_COOKIE["user"] . "!<br />"; else echo "Welcome guest!<br />"; ?> </body> </html>
删除cookie
<?php // set the expiration date to one hour ago setcookie(); ?>
例子9:(session处理)
开始session和存储session
<?php session_start(); // store session data $_SESSION[; ?> <html> <body> <?php //retrieve session data echo "Pageviews=". $_SESSION['views']; ?> </body> </html>
输出session中的数据
<?php session_start(); if(isset($_SESSION['views'])) $_SESSION[; else $_SESSION[; echo "Views=". $_SESSION['views']; ?>
终结session
<?php session_destroy(); ?>
例子10:(文件上传)
<html> <body> <form action="upload_file.php" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file" id="file" /> <br /> <input type="submit" name="submit" value="Submit" /> </form> </body> </html>
<?php if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES[)) { ) { echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo ) . " Kb<br />"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; if (file_exists("upload/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; } } } else { echo "Invalid file"; } ?>
11.写文件:
<?php $myfile = fopen("newfile.txt", "w") or die("Unable to open file!"); $txt = "Bill Gates\n"; fwrite($myfile, $txt); $txt = "Steve Jobs\n"; fwrite($myfile, $txt); fclose($myfile); ?>
12.文件读取:
<!DOCTYPE html> <html> <body> <?php $myfile = fopen("upload/newfile.txt", "r") or die("Unable to open file!"); echo fread($myfile,filesize("newfile.txt")); fclose($myfile); ?> </body> </html>
13.xml处理
13.1
<?xml version="1.0" encoding="ISO-8859-1"?> <note> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body> </note>
<?php //Initialize the XML parser $parser=xml_parser_create(); //Function to use at the start of an element function start($parser,$element_name,$element_attrs) { switch($element_name) { case "NOTE": echo "-- Note --<br />"; break; case "TO": echo "To: "; break; case "FROM": echo "From: "; break; case "HEADING": echo "Heading: "; break; case "BODY": echo "Message: "; } } //Function to use at the end of an element function stop($parser,$element_name) { echo "<br />"; } //Function to use when finding character data function char($parser,$data) { echo $data; } //Specify element handler xml_set_element_handler($parser,"start","stop"); //Specify data handler xml_set_character_data_handler($parser,"char"); //Open XML file $fp=fopen("test.xml","r"); //Read data )) { xml_parse($parser,$data,feof($fp)) or die (sprintf("XML Error: %s at line %d", xml_error_string(xml_get_error_code($parser)), xml_get_current_line_number($parser))); } //Free the XML parser xml_parser_free($parser); ?>
13.2
<?xml version="1.0" encoding="ISO-8859-1"?> <note> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body> </note>
<?php $xmlDoc = new DOMDocument(); $xmlDoc->load("note.xml"); $x = $xmlDoc->documentElement; foreach ($x->childNodes AS $item) { print $item->nodeName . " = " . $item->nodeValue . "<br />"; } ?>
13.3
<?xml version="1.0" encoding="ISO-8859-1"?> <note> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body> </note>
<?php $xml = simplexml_load_file("test2.xml"); echo $xml->getName() . "<br />"; foreach($xml->children() as $child) { echo $child->getName() . ": " . $child . "<br />"; } ?>
查看host绑定:
mysql> SELECT User, Host, Password FROM mysql.user; +-------------+-----------------------+-------------------------------------------+ | User | Host | Password | +-------------+-----------------------+-------------------------------------------+ | root | localhost | *232BC6F0ED82F0AD17B03A53D4062D2951F1A1E4 | | root | localhost.localdomain | *232BC6F0ED82F0AD17B03A53D4062D2951F1A1E4 | | root | 127.0.0.1 | *232BC6F0ED82F0AD17B03A53D4062D2951F1A1E4 | | demouser | localhost | *0756A562377EDF6ED3AC45A00B356AAE6D3C6BB6 | | zhenchaowen | 192.168.1.105 | *232BC6F0ED82F0AD17B03A53D4062D2951F1A1E4 | +-------------+-----------------------+-------------------------------------------+ rows in set (0.01 sec) mysql>
php解析器测试:
[root@localhost cgi-bin]# php hello3.php PHP Deprecated: Comments starting with PHP Warning: Directive <font face=</td><td bgcolor=</td><td bgcolor="#00FF00">ddd</td><td bgcolor="#00FF00">dddd</td></tr></b></table></b></font>
经过上述的步骤,传说中的LAMP架构就这样搭建完成了
顺便提下
yum install httpd
安装的httpd配置在/etc/httpd/conf/httpd.conf 中
如:
ServerName localhost:80
Listen 192.168.1.112:80
另外学一门语言总得把相关的框架给玩一下:
yii为例子(yii的官网:https://github.com/yiisoft/yii):
执行以下命令:
mkdir YiiPath cd YiiPath/ git clone https://github.com/yiisoft/yii.git
然后就可以看到如此美丽的配置页面了
CI框架为例子(官网:http://codeigniter.org.cn/):
wget https://codeload.github.com/bcit-ci/CodeIgniter/zip/3.0.0 unzip
然后就也可以看到如此美丽的配置页面了
http://192.168.1.112/CodeIgniter-3.0.0/
或者到github上面去找(https://github.com/bcit-ci/CodeIgniter)
mkdir ci cd ci/ git clone https://github.com/bcit-ci/CodeIgniter.git
http://192.168.1.112/ci/CodeIgniter/
CentOS下用yum配置php+mysql+apache(LAMP)的更多相关文章
- CentOS下用yum配置php+mysql+apache
环境: CentOS5.4 yum 想在一台CentOS的机器上安装配置支持dedeCMS的php+mysql+apache环境,把摸索的过程记录如下: 1. 安装Apahce, PHP, Mysq ...
- CentOS 下使用yum 命令安装MySQL
CentOS Linux下使用yum 命令安装MySQL过程记录. 1. 查看服务器中有没有安装过MySQL 1. 查看有没有安装包: yum list mysql* #移除已经安装的mysql yu ...
- CentOS下通过yum安装svn及配置
CentOS下通过yum安装svn及配置 1.环境centos5.5 2.安装svnyum -y install subversion 3.配置 建立版本库目录mkdir /www/svndata s ...
- CentOS下使用yum安装Apache极为方便,只需要在终端键入以下命令即可
CentOS下使用yum安装Apache极为方便,只需要在终端键入以下命令即可 1.安装Apache yum install httpd 2.设置服务器开机自动启动Apache systemctl e ...
- [转帖]CentOS下iRedMail安装配置
CentOS下iRedMail安装配置 中文名为艾瑞得邮件,由 rhms 项目更名而来.是针对 Linux 设计的邮件服务器解决方案,是在操作系统安装好后使用的一套 shell 脚本,用于快速部署一套 ...
- CentOS下使用MyTop实时监控MySQL
CentOS下使用MyTop实时监控MySQL MyTop的项目页面为:http://jeremy.zawodny.com/mysql/mytop/ MyTop安装 $ yum -y install ...
- CentOS下OpenVPN客户端配置
CentOS下OpenVPN客户端配置 http://liumissyou.blog.51cto.com/4828343/1762683 1,安装 yum install openvpn -y mkd ...
- CentOS下安装yum源的流程和操作
一般公司都用Linux来搭建服务器,Linux安装软件时能够用yum安装依赖包是一件非常简单而幸福的事情,因为你只需一个简单的安装命令yum install []即可安装相应的软件,yum工具会自动的 ...
- CentOS下Redisserver安装配置
1.CentOS 6.6下Redis安装配置记录 2.CentOS下Redisserver安装配置
随机推荐
- 3.7---猫狗收容所(CC150)
解答的思路:建立一个queue放狗,一个queue放猫. 如下: import java.util.*; class Dog{ int time; int value; Dog(int a, int ...
- 9. JEB 1.5插件编写一
2.0之后好像新增加了很多API,有所区别 本文采用Java+Eclipse作为开发环境 插件支持的语言 文档: jeb-1.5/doc/index.html 一.Hello World工程 1. ...
- 谷歌chrome浏览器和火狐firefox浏览器自带http抓包工具和请求模拟插件
谷歌chrome浏览器自带http抓包工具 chrome://net-internals/ 谷歌chrome浏览器http请求模拟插件:postman 火狐http请求模拟插件:httprequest ...
- No handlers could be found for logger "keystoneauth.identity.generic.base"
一般是因为发现了多个keystone的url造成的.
- 【leetcode】Restore IP Addresses
Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...
- div加jquery实现iframe标签的功能
由于最近项目需要,前端后台都完全采用div+css的方式布局.因而左思右想,有什么办法可以替代常用的iframe上下左右的布局方式,而且页面只是局部刷新.参考了许多前辈的资料,并加以整理,因而有了以下 ...
- pt-query-digest使用介绍【转】
本文来自:http://isadba.com/?p=651 一.pt-query-digest参数介绍. pt-query-digest --user=anemometer --password=an ...
- codeforces 496A. Minimum Difficulty 解题报告
题目链接:http://codeforces.com/contest/496/problem/A 题目意思:给出有 n 个数的序列,然后通过删除除了第一个数和最后一个数的任意一个位置的数,求出删除这个 ...
- CSS3实现元素旋转
-webkit-transform:rotate(30deg); 参数代表顺时针自旋转角度 这个元素还提供了文本倾斜的方法 - HTML5与CSS3 P299
- Sql存储过程
下面的存储过程从四个表的联接中返回所有作者(提供了姓名).出版的书籍以及出版社.该存储过程不使用任何参数. USE pubs IF EXISTS (SELECT name FROM sysobject ...