登录弹性云服务器。
安装Apache。
执行以下命令,更新Ubuntu系统内的软件包。
sudo apt update
执行以下命令,安装Apache。
sudo apt-get -y install apache2
执行以下命令,查看Apache版本。
apache2 -v
回显如下类似信息:
依次执行以下命令,启动Apache服务并设置服务开机自启动。
sudo systemctl start apache2
sudo systemctl enable apache2
执行以下命令,查看Apache服务状态是否已启动。
sudo systemctl status apache2
出现如下图所示active状态,则表示Apache服务已启动。
安装MySQL并配置。
执行以下命令,安装MySQL。
sudo apt -y install mysql-server
执行以下命令,查看MySQL版本。
mysql -V
回显如下类似信息:
执行以下命令,启动MySQL服务。
sudo systemctl enable mysql
sudo systemctl daemon-reload
配置MySQL
执行以下命令,进入MySQL。
sudo mysql
设置root用户密码。
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by 'newpassword';
退出MySQL。
exit;
执行以下命令,对MySQL进行安全性配置。
sudo mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root: #输入上一步骤中设置的root用户密码
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: Y
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
Using existing password for root.
Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : N #是否更改root用户密码,输入N
... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y #是否删除匿名用户,输入Y
Success.
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y #禁止root远程登录,输入Y
Success.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y #是否删除test库和对它的访问权限,输入Y
- Dropping test database...
Success.
- Removing privileges on test database...
Success.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y #是否重新加载授权表,输入Y
Success.
All done!
安装PHP。
执行以下命令,安装software-properties-common软件包。
sudo apt-get install -y software-properties-common
执行以下命令,安装PHP第三方软件包Ondrej PPA。
sudo add-apt-repository ppa:ondrej/php
出现如下回显信息时,请按下Enter键。
执行以下命令,安装PHP。
sudo apt-get install -y php8.3 php8.3-fpm libapache2-mod-php8.3
执行以下命令,查看PHP版本。
php -v
回显信息如下类似信息,表示PHP安装成功。
浏览器访问测试。
执行以下命令,进入Apache配置文件目录,将apache2.conf文件复制到apache2.conf.bak文件中,并将原先的示例配置文件保留作为备份。
sudo cp /etc/apache2/apache2.conf /etc/apache2/apache2.conf.bak
执行以下命令,修改Apache配置文件,添加Apache对PHP的支持。
执行以下命令,打开Apache配置文件。
sudo vim /etc/apache2/apache2.conf
按i键进入编辑模式。
修改打开的“apache2.conf”文件,将如下内容写入文件。
DirectoryIndex index.html index.php
SetHandler application/x-httpd-php
按Esc键退出编辑模式,并输入:wq保存后退出。
在Apache网站根目录中,创建测试网页。
执行以下命令,获取Apache网站根目录的路径信息。
sudo cat /etc/apache2/sites-available/000-default.conf
执行以下命令,在网站根目录创建测试网页,使用phpinfo()函数添加至网页内容中。phpinfo()函数会展示PHP的所有配置信息。
sudo sh -c 'echo "" > /var/www/html/phpinfo.php'
执行以下命令,重启Apache服务
sudo systemctl restart apache2
使用浏览器访问“http://服务器IP地址/phpinfo.php”,显示如下页面,说明环境搭建成功。
在搭建LAMP环境成功后,建议执行以下命令,删除phpinfo.php测试文件,以消除数据泄露风险。
sudo rm -rf <网站根目录>/phpinfo.php
本示例中网站的根目录为/var/www/html,则需要运行以下命令删除测试文件。
sudo rm -rf /var/www/html/phpinfo.php