在 Ubuntu 上让 LAMP(Apache + MySQL/MariaDB + PHP) 开机自启,核心就是让 Apache 和 数据库 服务开机启动。下面按常见情况说明。
sudo systemctl enable apache2
# MySQL
sudo systemctl enable mysql
# 或 MariaDB
sudo systemctl enable mariadb
systemctl status apache2
systemctl status mysql
sudo systemctl enable --now apache2
sudo systemctl enable --now mysql
sudo apt update
sudo apt install apache2 mariadb-server php libapache2-mod-php -y
安装后默认一般就是开机启动的。
sudo update-rc.d apache2 defaults
sudo update-rc.d mysql defaults
重启系统:
sudo reboot
重启后检查:
systemctl is-enabled apache2
systemctl is-enabled mysql
输出应为:
enabled
apache2mysqlmariadb开机启动成功,但外部访问不了,通常是:
如果你用的是 Ubuntu 版本不同 或 LAMP 是手动编译的,可以告诉我具体版本,我可以给你更精确的配置。