ubuntu安装lamp完整版

piaoling  2025-06-06 11:02:52


1.1、安装与运行Apache服务器,版本:Apache/2.4.29 (Ubuntu)

#查看需要更新的软件包
sudo apt update


#如果需要,可使用以下命令,升级软件包
sudo apt upgrade


#如果需要,可使用以下命令,卸载过时的软件包
sudo apt autoremove


#安装Apache服务器
sudo apt install apache2


#安装完成后,查看Apache服务器的版本
apache2 -version
#显示:Server version: Apache/2.4.29 (Ubuntu)等字样

sudo a2enmod rewrite 命令启用rewrite模块

sudo a2enmod ssl  启用https
sudo a2enmod headers 启用header


配置https服务

启用ssl模块

# a2enmod ssl

systemctl restart apache2


填写ssl证书路径

# cd /etc/apache2/sites-available

# nano  default-ssl.conf

用自己的证书的文件路径替换默认的pubkey和privkey的文件路径


启用ssl网站

# a2ensite default-ssl.conf

# systemctl reload apache2


2、安装与运行PHP

查看可用的PHP安装包

sudo apt list | grep php


列出已经安装的包
sudo apt list --installed | grep php

如果没有php7.4,添加php7.4的源

首先,打开终端。

添加 Ondřej Surý 的 PHP PPA:

sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt list | grep php
sudo apt install php7.4 php7.4-dev php7.4-fpm php7.4-mysql php7.4-mbstring php7.4-xml php7.4-gd php7.4-curl php7.4-zip
sudo apt install libapache2-mod-php7.4

根据你的需求,你可以添加更多的模块,比如php7.4-zip,php7.4-bcmath等。

验证 PHP 版本:

php -v


3.安装mariadb

1 – 更新您的系统

我们总是从系统更新开始安装。

sudo apt update && sudo apt -y upgrade

升级完成后,您可以转到下一步。


2 – 添加 MariaDB APT 存储库

接下来,我们添加包含在 Ubuntu 系统上安装 MariaDB 11.x 所需的软件包的存储库。

需要去官网查看最新版本:https://mariadb.com/downloads/?utm_source=header&utm_medium=website

curl -LsS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | sudo bash -s -- --mariadb-server-version=11.8.2


命令执行的预期输出。

# [info] Checking for script prerequisites.

# [info] MariaDB Server version 11.2 is valid

# [info] Repository file successfully written to /etc/apt/sources.list.d/mariadb.list

# [info] Adding trusted package signing keys...

# [info] Running apt-get update...

# [info] Done adding trusted package signing keys

或者手动添加源

https://mariadb.org/download/?t=repo-config&d=22.04+"jammy"&v=12.0&r_m=blendbyte

3 – 安装 MariaDB 服务器/客户端


接下来,我们从配置的存储库安装MariaDB服务器和客户端包。


sudo apt update

sudo apt -y install mariadb-server mariadb-client

所有依赖项都会自动安装。若要确认已安装软件包的版本,请使用以下命令:

$ mariadb -V

mariadb from 11.2.1-MariaDB, client 15.2 for debian-linux-gnu (x86_64) using  EditLine wrapper


4 – 安全的 MariaDB 安装

mariadb-secure-installation是一个命令行工具,用于通过执行各种与安全相关的任务来保护 MariaDB 安装。您需要以 root 用户身份或使用 sudo 权限运行该命令。

sudo mariadb-secure-installation


为了登录MariaDB以保护它,我们需要root用户的当前密码。如果您刚刚安装了MariaDB,并且尚未设置root密码,则应在此处按回车键。

Enter current password for root (enter for none):

OK, successfully used password, moving on...


设置 root 密码或使用unix_socket可确保未经适当授权,任何人都无法登录 MariaDB root 用户。


Switch to unix_socket authentication [Y/n]

Enabled successfully!

Reloading privilege tables..

 ... Success!


同意设置新密码。

Change the root password? [Y/n] y

New password:

Re-enter new password:

Password updated successfully!

Reloading privilege tables..

 ... Success!


默认情况下,MariaDB安装具有匿名用户,允许任何人登录MariaDB,而无需为他们创建用户帐户。这仅用于测试,并使安装更顺利一些。在进入生产环境之前,应将其删除。


Remove anonymous users? [Y/n] y

 ... Success!


通常,应该只允许root从'localhost'进行连接。这确保了有人无法从网络中猜测 root 密码。


Disallow root login remotely? [Y/n] y

 ... Success!


默认情况下,MariaDB带有一个名为“test”的数据库,任何人都可以访问。这也仅用于测试,应在进入生产环境之前将其删除。


Remove test database and access to it? [Y/n] y

 - Dropping test database...

 ... Success!

 - Removing privileges on test database...

 ... Success!


重新加载权限表将确保到目前为止所做的所有更改将立即生效。


Reload privilege tables now? [Y/n] y

 ... Success!


如果您已完成上述所有步骤,则您的MariaDB安装现在应该是安全的。

5 – 测试 MariaDB 安装


使用命令行连接到数据库服务器。mariadb


mariadb -u root -p


确认连接成功。


Welcome to the MariaDB monitor.  Commands end with ; or g.

Your MariaDB connection id is 42

Server version: 11.2.1-MariaDB-1:11.2.1+maria~ubu2204 mariadb.org binary distribution


Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.


Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.


MariaDB [(none)]>


要检查版本运行,请执行以下操作:


MariaDB [(none)]> SELECT VERSION();

+---------------------------------------+

| VERSION()                             |

+---------------------------------------+

| 11.2.1-MariaDB-1:11.2.1+maria~ubu2204 |

+---------------------------------------+

1 row in set (0.002 sec)

-----------------------------------


类别 :  默认(760)  |  浏览(18)  |  评论(0)
发表评论(评论将通过邮件发给作者):

Email: