LAMP is a popular web development environment. The name is an acronym coined from four components: Linux operating system, Apache HTTP Server, MySQL relational database management system, and PHP programming language. This topic describes how to deploy a LAMP environment on an Ubuntu simple application server.
If you do not have any existing simple application server, we recommend that you use a LAMP application image to quickly build a LAMP environment. For more information, see Build a LAMP development environment.
If you want to deploy a LAMP environment on a simple application server that runs Alibaba Cloud Linux or CentOS 7.x, refer to Manually build a LAMP development environment (Alibaba Cloud Linux/CentOS 7.x).
Preparations
You must create a simple application server to deploy the LAMP environment. For more information, see Create a simple application server.
The simple application server must meet the following requirements:
The server runs the Ubuntu 22.04, Ubuntu 20.04, or Ubuntu 18.04 operating system.
The security group of the simple application server allows inbound traffic over ports 22, 80, and 443. For more information, see Manage the firewall of a simple application server.
Step 1: Install Apache
Remotely connect to the simple application server on which you want to deploy the LAMP environment. For more information, see Connect to a Linux server.
Run the following command to update the Ubuntu software packages:
sudo apt update
Run the following command to install Apache:
sudo apt-get -y install apache2
Run the following command to check the version of Apache:
apache2 -v
A command output similar to the following one indicates that Apache is installed and its version is 2.4.52.
Run the following commands to start Apache and configure Apache to automatically start on system startup:
sudo systemctl start apache2 sudo systemctl enable apache2
Run the following command to check the status of Apache:
sudo systemctl status apache2
A command output similar to the following one indicates that Apache is started.
Step 2: Install and configure MySQL
Install MySQL.
Run the following command to install MySQL:
sudo apt -y install mysql-server
Run the following command to check the version of MySQL:
mysql -V
A command output similar to the following one indicates that MySQL is installed and its version is 8.0.35.
Run the following command to start MySQL:
sudo systemctl start mysql
Run the following commands in sequence to configure MySQL to start on system startup:
sudo systemctl enable mysql sudo systemctl daemon-reload
Configure MySQL.
Run the following command to access MySQL:
sudo mysql
Run the following command to set a password for the root user:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by 'mynewpassword';
In this example, the password is
Mysql@1234
. Sample command:ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by 'Mysql@1234';
Run the following command to exit MySQL:
exit;
Run the following command to configure the security settings of MySQL:
sudo mysql_secure_installation
Follow the command line instructions to configure the following settings in sequence.
Enter the password of the root user. In this example,
Mysql@1234
is entered.ecs-user@iZbp19jsi7s0g7m4zgc****:~# sudo mysql_secure_installation Securing the MySQL server deployment. Enter password for user root:
NoteFor data security purposes, no output is returned when you enter a password. You need only to enter the correct password and then press the Enter key.
Enter
Y
to configure a password strength policy.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
Specify a password strength value.
In this example, 2 is used.
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: 0
Enter
Y
to change the password of the root user.Change the password for root ? ((Press y|Y for Yes, any other key for No) : Y
Enter the new password of the root user.
New password: Re-enter new password: Estimated strength of the password: 100
Enter
Y
to use the new password that you set.Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y
Enter
Y
to delete the anonymous user account that comes with MySQL.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
Enter
Y
to deny remote access by the root user to MySQL.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
Enter
Y
to delete the database named test.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) :
Enter
Y
to reload privilege tables.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
When the configuration is complete, All done! is displayed in the command output.
Check whether you can log on to MySQL.
Run the following command to log on to MySQL:
sudo mysql -uroot -p
At the
Enter password:
prompt, enter the password that you set for MySQL.NoteFor data security purposes, no output is returned when you enter a password. You need only to enter the correct password and then press the Enter key.
The following example command output indicates that you are logged on to MySQL.
ecs-user@iZbp19jsi7s0g7m4zgc****:~# sudo mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 15 Server version: 8.0.29-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>
Run the following command to exit MySQL:
exit;
Step 3: Install PHP
Run the following commands in sequence to install PHP:
sudo apt -y install php-fpm sudo apt -y install libapache2-mod-php
Run the following command to check the version of PHP:
php -v
A command output similar to the following one indicates that PHP is installed and its version is 7.4.3.
In the root directory of the Apache website, create a test webpage.
Run the following command to view the path information of the root directory of the Apache website:
sudo cat /etc/apache2/sites-available/000-default.conf
The
DocumentRoot /var/www/html
line in the command output indicates that the website root directory is/var/www/html
.Run the following command to create a test webpage in the website root directory and add the
phpinfo()
function to the webpage.The
phpinfo()
function is used to show all configuration information of PHP.sudo sh -c 'echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php'
Run the following command to restart Apache:
sudo systemctl restart apache2
Enter
http://<Public IP address of the simple application server>/phpinfo.php
in the address bar of the local browser and press theEnter
key.A page similar to the following one indicates that PHP is installed.
After the LAMP stack is built, we recommend that you delete the phpinfo.php test file to prevent data leaks.
sudo rm -rf <Website root directory>/phpinfo.php
In this example, the website root directory
/var/www/html
is used. Run the following command to delete the test file:sudo rm -rf /var/www/html/phpinfo.php