步驟概要:
一、在虛擬機中安裝ubuntu
二、安裝PHP,Apache 和 MySQL
三、配置Apache
四、MySQL + PHP 搭建簡單的登陸頁面
五、效果展示
一、在虛擬機中安裝ubunutu
這裏使用Ubuntu 18.04.4 LTS, 下載鏈接。
打開vmware,點擊創建新的虛擬機,選擇下載好的ubuntu iso文件,根據指示,配置硬盤空間,內存,安裝虛擬機即可,唯一需要注意的是,安裝過程中可能出現卡在retrieving file這個頁面(需要下載額外的東西),可以點擊skip跳過。或是連接代理,速度會有明顯提升。
二、安裝PHP,Apache 和 MySQL
在Ubuntu 18.04.4 LTS 中,自帶Apache,打開瀏覽器,輸入localhost,即可查看:
接下來需要安裝MySQL 和 PHP:
sudo apt install mysql-server mysql-client
sudo apt install php
安裝完成後,查看mysql發現,拒絕登陸:
研究後發現,原來現在MySql安裝後會生成隨機的賬戶名和密碼,在路徑/etc/mysql/debian.cnf 下:
使用user和password進行登陸,然後修改root密碼:
>update mysql.user set authentication_string=PASSWORD("這裏輸入你要改的密碼") where User='root';
>update user set plugin="mysql_native_password";
>flush privileges;
重啓MySQL服務,就可以使用root和新設置的密碼進行登陸了。
三、配置Apache
如果用戶從服務器請求目錄,Apache將首先查找名爲index.html的文件。在目錄/var/www/html目錄下,可以找到index.html:
查看index.html的內容:
cat /var/www/html/index.html | less
可以查看文件的內容:
即爲訪問http://localhost 時顯示的頁面。
由於搭建的是PHP,需要告訴web服務器,優先選擇後綴爲php的頁面,修改文件/etc/apache2/mods-enabled/dir.conf:
將index.php 放在index.html 的前面,然後重新啓動Apache服務:
sudo systemctl restart apache2
接下來,建立一個新的目錄,並分配用戶權限給這個目錄:
sudo mkdir /var/www/php_website
sudo chmod -R 755 /var/www/php_website
sudo useradd -m php_admin
sudo passwd php_admin
#輸入密碼
sudo chown -R php_admin:php_admin /var/www/php_website/
sudo vi /var/www/php_website/index.html
在index.html 中新建粘貼如下代碼:
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
爲了讓Apache提供支持,必須使用正確的指令創建虛擬主機文件。這裏不修改/etc/apache2/sites-available/000-default.conf, 而是新建一個文件
sudo vi /etc/apache2/sites-available/php_website.conf
粘貼一下內容:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/php_website
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
啓用新的.conf文件
sudo a2ensite php_website.conf
sudo a2dissite 000-default.conf
sudo systemctl restart apache2
此時訪問localhost, 發現已經轉到了剛剛新編寫的index.html:
接下來測試一下PHP在服務器上的處理:
在/var/www/php_website目錄下新建一個info.php文件,輸入一下代碼:
<?php
phpinfo();
?>
保存以後,訪問localhost/info.php,可以看到顯示了php的信息:
apache + php 已經配置完成了。
四、MySQL + PHP 搭建簡單的登陸頁面
爲了安全考慮,首先新建一個MySQL數據庫並且新建一個用戶
CREATE DATABASE PHP;
CREATE USER 'phpadmin'@'localhost' IDENTIFIED BY '*******';
GRANT ALL ON PHP TO 'phpadmin'@'localhost';
CREATE TABLE user(name VARCHAR(100) NOT NULL, psw VARCHAR(100));
這樣就新創建了數據庫PHP和表user,以及用戶phpadmin,只可以操作PHP這個數據庫。
接下來就是編寫PHP代碼了,代碼結構如下:
login.php源代碼:
<!DOCTYPE html>
<html lang='zh'>
<head>
<title> login</title>
<meta charset="UTF-8">
</head>
<body>
<form name="input" action="post.php" method="post">
用戶名 :<br /><label>
<input type="text" name="username">
</label><br>
密碼 : <br /><label>
<input type="password" name="password">
</label> <br>
<input type="submit" value="登陸">
<button><a href="reg.php">註冊</a></button>
</form>
</body>
</html>
當點擊註冊時,跳轉到reg.php:
<!DOCTYPE html>
<html>
<head>
<title> reg</title>
</head>
<body>
<form name="input" action="regin.php" method="post">
用戶名<br/><label>
<input type="text" name="username">
</label><br>
密碼 <br/><label>
<input type="password" name="password">
</label> <br>
確認密碼 <br/><label>
<input type="password" name="con_password">
</label> <br>
<input type="submit" value="註冊">
</body>
</html>
註冊邏輯由regin.php處理:
<?php
$conn=mysqli_connect("localhost",'phpadmin','******', 'PHP');
if ($conn->connect_error){
die("connection fail:" . $conn->connect_error);
}
$name=trim($_POST['username']);
$password=trim($_POST['password']);
$con_password=trim($_POST['con_password']);
$sql = "select * from user where name='$name'";
$result = $conn->query($sql);
if(empty($name)){
echo "<script>alert('user name can not be null');location.href='reg.php';</script>";
}else if(empty($password) && empty($conpassword)){
echo "<script>alert('password can not be null');location.href='reg.php';</script>";
}else if($password <> $con_password){
echo "<script>alert('password not correct');location.href='reg.php';</script>";
} else{
echo "success";
if($result->num_rows > 0){
echo "<script>alert('User already exists');location.href='reg.php';</script>";
}else{
$sql_insert ="insert into user(name,psw) values('".$name."','" .($password)."')";
if ($conn->query($sql_insert) === true){
echo "<script>alert('success!')</script>",header("Location:login.php");
} else{
echo "<script>alert('fail!')</script>";
}
}
}
登陸邏輯由post.php處理:
<?php
$conn = new mysqli("localhost","phpadmin", "******","PHP");
if ($conn->connect_error){
die("connection fail" . $conn->connect_error);
}
$name = trim($_POST['username']);
$psw = trim($_POST['password']);
$sql="select * from user where name='$name' and psw='$psw'";
if ($conn->query($sql)->num_rows > 0){
setcookie('username',$name,time()+3600);
header("Location:index.php");
} else{
echo "login fail,<a href=\"login.php\">Please re-login</a>";
}
若登陸成功,跳轉到index.php:
<?php
if (!isset($_COOKIE['username']))
{
echo 'Illegal login!<a href="login.php">please login</a>';
exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title> Hello World! </title>
<meta charset="UTF-8">
</head>
<body>
Hello World!
<a href="logout.php">logout</a>
</body>
</html>
註銷,logout.php:
<?php
if(isset($_COOKIE['username'])){
setcookie('username',$name,time()-1);
header('Location:login.php');
}
else{
echo 'fail';
header('Location:index.php');
}
五、效果展示
進入http://localhost/login.php:
點擊註冊跳轉到註冊頁面:
如果註冊過程不規範,會由相應的提示信息:
隨便註冊一個用戶,進行登陸:
登陸成功後會跳轉到index.php頁面。
點擊logout註銷,如果在沒有登陸的情況下,會提示非法登陸:
到這裏就算用Linux+MySQL+Apache+PHP環境搭建了最簡單的登陸界面了。