在ubuntu20.04下搭建lamp环境并制作静态网页

背景

LAMP是指一组通常一起使用来运行动态网站或者服务器的自由软件名称首字母缩写:

Linux:操作系统
Apache:网页服务器
MariaDB或MySQL:数据库管理系统(或者数据库服务器)
PHP、Perl或Python:脚本语言

目录

一、准备工作

二、安装apache2

三、安装mysql

四、安装php

五、安装depress


准备工作

  • 配置虚拟机安装ubuntu20.04
  • 配置环境及相关设置(配置中文及防火墙等)

提示:详细安装方法参考B站up @Mrico_Frank


安装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说明成功


**

安装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> 

**

安装 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基本搭建完成-------------------------

**

安装depress

  • 修改ssh
  • 进入根目录
sudo su -
  • 修改配置文件
sudo vim /etc/ssh/ssh_config.d/
  • 安装ssh
sudo apt-get install openssh-server
  • 添加ssh.service
sudo systemctl enable ssh.service
  • 启动ssh
service 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
The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating        # redirection URLs. In the context of virtual hosts, the ServerName        # specifies what hostname must appear in the request's Host: header to        # match this virtual host. For the default virtual host (this file) this        # value is not decisive as it is used as a last resort host regardless.        # However, you must set it for any further virtual host explicitly.        #ServerName www.example.com        ServerAdmin 1052469821@qq.com        ServerName lifeonchina        ServerAlias www.lifeonchina.com        DocumentRoot /var/www/html        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,        # error, crit, alert, emerg.        # It is also possible to configure the loglevel for particular        # modules, e.g.        #LogLevel info ssl:warn
  • 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;

  • ----------------------数据库创建完成---------------------

安装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

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值