LAMP is an acronym of the names of its original four components: the Linux operating system, Apache HTTP Server, MySQL relational database management system, and PHP programming language. LAMP stacks are commonly used to build websites. This topic describes how to build a LAMP stack on an Elastic Compute Service (ECS) instance that runs Alibaba Cloud Linux 2, Alibaba Cloud Linux 3, or CentOS 7.x.
Prerequisites
An ECS instance that is used to build a LAMP stack is created. For more information, see Create an instance on the Custom Launch tab.
The instance meets the following requirements:
The instance is associated with an auto-assigned public IP address or an elastic IP address (EIP). For information about how to associate an EIP with an instance, see Associate or disassociate an EIP.
The instance runs the Alibaba Cloud Linux 2, Alibaba Cloud Linux 3, or CentOS 7.x operating system.
An inbound rule is added to a security group of the instance to allow traffic on ports 22, 80, and 443. For more information, see Add a security group rule.
Step 1: Install Apache
Connect to the ECS instance on which you want to build a LAMP stack.
For more information, see Connect to a Linux instance by using a password or key.
Run the following command to install Apache and its extension package:
sudo yum -y install httpd httpd-manual mod_ssl mod_perl
Run the following command to check the version of Apache:
httpd -v
A command output similar to the following one indicates that the version of Apache is 2.4.6.
Run the following commands to start Apache and configure Apache to start on system startup:
sudo systemctl start httpd sudo systemctl enable httpd
Run the following command to check the status of Apache:
sudo systemctl status httpd
A command output similar to the following one indicates that Apache is started.
Step 2: Install and configure MySQL
Run the following commands to update the YUM repository:
wget http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm sudo yum install -y mysql57-community-release-el7-10.noarch.rpm sudo yum install -y mysql-community-server --nogpgcheck
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.
Run the following commands to start MySQL and configure MySQL to start on system startup:
sudo systemctl start mysqld sudo systemctl enable mysqld sudo systemctl daemon-reload
Run the following command to obtain the initial password of the root account used to log on to MySQL:
sudo grep "password" /var/log/mysqld.log
A command output similar to the following one is returned. In this example, the initial password is
pw-eWw+ar5c+
.Run the following command to configure the security settings of MySQL:
mysql_secure_installation
Perform the following operations:
Reset the password of the root account.
NoteKeep the password of the root account secure.
Enter password for user root: # Enter the initial password of the root account that you obtained in the previous step. The existing password for the user account root has expired. Please set a new password. New password: # Enter a new password. The password must be 8 to 30 characters in length and must contain uppercase letters, lowercase letters, digits, and special characters. The following special characters are supported: ( ) ` ~ ! @ # $ % ^ & * - + = | { } [ ] : ; ' < > , . ? / Re-enter new password: # Enter the new password again. The 'validate_password' plugin is installed on the server. The subsequent steps will run with the existing configuration of the plugin. 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) :Y # Enter Y and re-enter the new password. New password: # Enter the new password. Re-enter new password: # Enter the new password again. Estimated strength of the password: 100 Do you wish to continue with the password provided?(Press y | Y for Yes, any other key for No) :Y # Enter Y to use the new password.
Enter
Y
to delete the anonymous user account.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 delete the anonymous user account. Success.
Enter
Y
to deny remote access by the root account.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 deny remote access by the root account. Success.
Enter
Y
to delete the test database and the access permissions on the database.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 # Enter Y to delete the test database and the access permissions on the database. - Dropping test database... Success. - Removing privileges on test database... Success.
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 # Enter Y to reload privilege tables. Success. All done!
Step 3: Install PHP
Install PHP.
Alibaba Cloud Linux 3
Run the following command to add and update the Remi repository:
sudo rpm -ivh https://rpms.remirepo.net/enterprise/remi-release-8.rpm --nodeps
Run the following commands to install PHP:
sudo dnf update -y dnf libdnf sudo sed -i 's/PLATFORM_ID="platform:al8"/PLATFORM_ID="platform:el8"/g' /etc/os-release sudo yum -y module install php:remi-7.4 sudo sed -i 's/PLATFORM_ID="platform:el8"/PLATFORM_ID="platform:al8"/g' /etc/os-release
Alibaba Cloud Linux 2/CentOS 7.x
Run the following commands to add the Extra Packages for Enterprise Linux (EPEL) repository:
sudo yum install -y \ https://repo.ius.io/ius-release-el7.rpm \ https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
Run the following command to add the Webtatic repository:
sudo rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
Run the following command to install PHP:
sudo yum -y install php70w-devel php70w.x86_64 php70w-cli.x86_64 php70w-common.x86_64 php70w-gd.x86_64 php70w-ldap.x86_64 php70w-mbstring.x86_64 php70w-mcrypt.x86_64 php70w-pdo.x86_64 php70w-mysqlnd php70w-fpm php70w-opcache php70w-pecl-redis php70w-pecl-mongodb
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.
Run the following command to create a test file in the root directory of the Apache website:
sudo sh -c 'echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php'
Run the following command to restart Apache:
sudo systemctl restart httpd
Enter
http://<Public IP address of the ECS instance>/phpinfo.php
in the address bar of a browser on your computer and press the Enter key.A page similar to the following one indicates that PHP is installed.
(Optional) Step 4: Install phpMyAdmin
phpMyAdmin is a MySQL database management tool that allows you to manage databases by using web interfaces.
Run the following command to create a directory to store phpMyAdmin data:
sudo mkdir -p /var/www/html/phpmyadmin
Run the following commands to download and decompress the phpMyAdmin package.
Run the following commands in sequence to switch to the home directory and download the phpMyAdmin package:
cd wget --no-check-certificate https://files.phpmyadmin.net/phpMyAdmin/4.9.11/phpMyAdmin-4.9.11-all-languages.zip
Run the following commands in sequence to install the unzip tool and decompress the phpMyAdmin package:
sudo yum install -y unzip sudo unzip phpMyAdmin-4.9.11-all-languages
Run the following command to copy the phpMyAdmin files to the prepared directory:
sudo mv phpMyAdmin-4.9.11-all-languages/* /var/www/html/phpmyadmin
(Optional) Run the following command to install php-mysqli.
You must run this command if the operating system of the ECS instance is Alibaba Cloud Linux 3.
sudo yum install -y php-mysqli
Enter
http://Public IP address of the ECS instance/phpmyadmin
in the address bar of a browser on your computer and press the Enter key to go to the logon page of phpMyAdmin.Enter the username and password of your MySQL account and click Go.
If the following page is displayed, the connection to MySQL is established.