在 ubuntu20.04下搭建 lamp 环境并制作静态网页
1.準備工作
配置虛擬機安裝ubuntu20.04
配置環境及相關設置(配置中文及防火墻等)
提示:詳細安裝方法參考B站up @Mrico_Frank
2.安裝apache2
Apache HTTP服務器是世界上使用最廣泛的Web服務器。它提供了許多強大的功能,包括可動態加載的模塊,強大的媒體支持以及與其他流行軟件的廣泛集成。
開始安裝
打開終端,進入根目錄
suliu@suliu-virtual-machine:~$ sudo su -
更新環境
root@suliu-virtual-machine:~# apt-get upgrade
安裝apache環境
root@suliu-virtual-machine:~# sudo apt-get install apache
確認安裝后, apt將安裝Apache和所有必需的依賴項
安裝完成之后我們可以在瀏覽器中輸入localhost進行訪問
也可以查看apache
root@suliu-virtual-machine:~# service apache2 status
看到runing說明成功看到runing說明成功
?
3.安裝mysql 數據庫
安裝mysql
root@suliu-virtual-machine:~# sudo apt-get install my-server
驗證 MySQL
root@suliu-virtual-machine:~# sudo apt-get install my-server
mysql.service - MySQL Community Server
? ? ?Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset:>
? ? ?Active: active (running) since Thu 2022-03-10 20:07:08 CST; 1 day 1h ago
? ?Main PID: 1024 (mysqld)
? ? ?Status: "Server is operational"
? ? ? Tasks: 43 (limit: 9419)
? ? ?Memory: 474.9M
? ? ?CGroup: /system.slice/mysql.service
? ? ? ? ? ? ?└─1024 /usr/sbin/mysqld
保護加固 MySQL
root@suliu-virtual-machine:~# mysql_secure_installation
●Error: Access denied for user 'root'@'localhost'
mysql需要測試 MySQL 用戶密碼的強度,并且提高安全性
Securing the MySQL server deployment.
Connecting to MySQL using a blank password.
VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?
Press y|Y for Yes, any other key for No:
三個級別的密碼驗證策略,低級,中級,高級
There are three levels of password validation policy:
LOW ? ?Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary ? ? ? ? ? ? ? ? ?file
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2
下一次被提示時,將被要求為 MySQL root 用戶設置一個密碼:
Please set the password for root here.
New password:?
Re-enter new password
如果你設置了驗證密碼插件,這個腳本將會顯示你的新密碼強度。輸入y確認密碼:
Estimated strength of the password: 50?
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
下一步,你將被要求移除任何匿名用戶,限制 root 用戶訪問本地機器,移除測試數據庫并且重新加載權限表。你應該對所有的問題回答y。
以 root 用戶身份登錄
root@suliu-virtual-machine:~# sudo mysql -u root -p
-u 指定用戶名-p需要輸入密碼
Welcome to the MySQL monitor. ?Commands end with ; or \g.
Your MySQL connection id is 33
Server version: 8.0.28-0ubuntu0.20.04.3 (Ubuntu)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>?
4.安裝 PHP 7.4,配合apache
更新環境
root@suliu-virtual-machine:~# sudo apt update
安裝PHP
root@suliu-virtual-machine:~# sudo apt install php libapache2-mod-php
php安裝好了,重啟 Apache,重新加載 PHP 模塊
root@suliu-virtual-machine:~# sudo systemctl restart apache2
安裝 PHP 擴展
PHP 擴展被編譯成庫文件,用來擴展 PHP 核心功能
安裝 MySQL 和 GD 擴展
root@suliu-virtual-machine:~# sudo apt install php-mysql php-gd
在安裝一個新的 PHP 擴展之后,依賴于你的設置,不要忘記去重啟 Apache 或者 PHP FPM 服務
測試 PHP 處理
進入/var/www
root@suliu-virtual-machine:~# cd /var/www
修改權限
root@suliu-virtual-machine:/var/www# #sudo chmod 777 /etc/apache2/mods-enabled/dir.conf
root@suliu-virtual-machine:/var/www# cd html/
建立php文件
root@suliu-virtual-machine:/var/www/html# sudo gedit index.php
編輯內容如下
?php
? ? ?echo phpinfo();
?>
瀏覽器輸入http://localhost/info.php打開測試頁面
----------------------至此lamp基本搭建完成-------------------------
5.安裝depress
修改ssh
進入根目錄sudo su -
修改配置文件sudo vim /etc/ssh/ssh_config.d/
安裝sshsudo apt-get install openssh-server
添加ssh.servicesudo systemctl enable ssh.service
啟動sshservice ssh start sudo apt-get update && apt-get upgrade //升級更新一下
配置防火墻sudo ufw app list sudo ufw allow 'Apache'
進入/var/www/html下創建自己的文件夾cd /var/www/html/
創建你的項目文件夾mkdir -p food``
用戶權限設置root@suliu-virtual-machine:/var/www/html# sudo chown -R 755 /var/www
修改000-default.conf
1.進入文件夾
root@suliu-virtual-machine:/# cd /etc/apache2/sites-available/
2.備份
root@suliu-virtual-machine:/etc/apache2# sudo cp 000-default.conf 000-food.conf
3.修改
root@suliu-virtual-machine:/etc/apache2/sites-available# sudo nano 000-default.conf
root@suliu-virtual-machine:/etc/apache2/sites-available# sudo nano 000-default.conf
4.修改完后ctrl+x保存退出
5.確認
root@suliu-virtual-machine:/etc/apache2/sites-available# sudo a2ensite 000-default.conf
Site 000-default already enabled ? ? ? //成功
6.重啟apache2,并檢查
root@suliu-virtual-machine:/etc/apache2/sites-available# sudo systemctl restart apache2
root@suliu-virtual-machine:/etc/apache2/sites-available# sudo apache2ctl configtest
報錯
Set the 'ServerName' directive globally to suppress this message
解決方式
root@suliu-virtual-machine:/etc/apache2/sites-available# sudo nano ?/etc/apache2/apache2.conf
編輯配置文件apache.conf
#在文件最后添加一行 ServerName localhost:80 #然后保存并重啟Apache2
再測試
service apache2 restart
root@suliu-virtual-machine:/etc/apache2/sites-available# sudo apache2ctl configtest?
Syntax OK. ?//成功!
1.登陸lmysql
root@suliu-virtual-machine:/etc/apache2/sites-available# sudo mysql -u root -p
//輸入密碼
Welcome to the MySQL monitor. ?Commands end with ; or \g.
Your MySQL connection id is 14Server version: 8.0.28-0ubuntu0.20.04.3 (Ubuntu)Copyright (c) 2000, 2022, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> ? ? ? ? ? //成功進入
2.創建數據庫//database:life; user:life_user; password:##############;
mysql> CREATE DATABASE life;(接著上面的mysql>,只需要寫入CREATE DATABASE life;)
Query OK, 1 row affected (0.06 sec)
3.創建USER
mysql> CREATE USER life_user@localhost IDENTIFIED BY '##############';
4.創建GRANT
mysql> GRANT ALL PRIVILEGES ON life.* TO life_user@localhost;
mysql> FLUSH PRIVILEGES;
退出
mysql> EXIT;
----------------------數據庫創建完成---------------------
6.安裝wordpress
1.進入/var/www/html/
root@suliu-virtual-machine:/etc/apache2/sites-available# cd /var/www/html/
2.下載woedpress文件
root@suliu-virtual-machine:~# wget http://wordpress.org/latest.tar.gz
3.查看下載的文件
root@suliu-virtual-machine:~# ls
看到latest.tar.gz,這是一個壓縮包,將其解壓 #@4.解壓
root@suliu-virtual-machine:~# tar xzvf latest.tar.gz
解壓完成后多了一個wordpress文件
5.update
sudo apt-get update
6.刪除壓縮文件
root@suliu-virtual-machine:~# rm latest.tar.gz
7.將wordpress里面的文件全部移到根目錄即上一層目錄
root@suliu-virtual-machine:/var/www/html/wordpress# mv * /var/www/html/
?
你可以通過depress進行網頁搭建
———————wordpress安裝完成————————
編輯wenjian
1.備份wp-config-sample.php
root@suliu-virtual-machine:/var/www/html# cp wp-config-sample.php wp-config.php
2.修改wp-config.php
root@suliu-virtual-machine:/var/www/html# sudo nano wp-config.php
3.修改如下,數據庫名,用戶名,密碼
define( 'DB_NAME', 'life' );
/** Database username */define( 'DB_USER', 'life_user' );/** Database password */define( 'DB_PASSWORD', '##############' );
注意:密碼要求安全性高
linux常用指令
#@sudo gedit asdf命令下無法輸入中文
//直接gedit asdf
?
#@刪除文件
//sudo rm
// -rf 強力刪除,不需確認
// -i 每刪除一個文件或進入一個子目錄都要確認
// -r 遞歸刪除子目錄
// -d 刪除空目錄
// -v 顯示刪除結果
#@快速查找文件
//whereis asdf
//locate asdf
//find /-name asdf
#@創建文件夾
//mkdir asdf
#@創建多級文件夾
//mkdir -p asdf
#@創建文件
//touch asdf.x
#@打開當前目錄的彈窗
//sudo nautilus
#@復制一個文件到另一個文件
//sudo cp -r 文件夾名 目標文件夾路徑
#@修改文件名
//sudo mv 原文件名 修改后文件名
#@啟動mysql
// service mysql start
#mysql查看狀態
// service mysql status
#@關閉mysql
// service mysql stop
#@啟動apache
service apache2 start
#@重啟apache
//service apache2 restart
#@停止apache
//service apache2 stop
#@apache查看狀態
//service apache2 status
#@查看mysql的用戶名和密碼
//sudo cat /etc/mysql/debian.cnf
#@user = debian-sys-maint
#@password = K8Jjiz7MPntks3fZ
#@ctrl + r –查找上一次命令
#@回到本行首字母
//ctrl + a
#@登陸mysql
//sudo mysql -uroot -p
文章鏈接: http://example.com/2022/03/21/zai-ubuntu20.04-xia-da-jian-lamp-huan-jing-bing-zhi-zuo-jing-tai-wang-ye/
?
總結
以上是生活随笔為你收集整理的在 ubuntu20.04下搭建 lamp 环境并制作静态网页的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: html嵌入bilibili视频
- 下一篇: 架构师小跟班:推荐46个非常经典的Lin