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. In most cases, LAMP stacks are used to build websites. LAMP has extensive community support and a wealth of resources and is suitable for developing, deploying, and maintaining web applications of various sizes. LAMP stacks are highly flexible and allow you to modify and customize servers. You can configure LAMP stack components based on your business requirements to maximize performance and security. 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 ECS instance meets the following requirements:
The ECS 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 ECS instance runs Alibaba Cloud Linux 2, Alibaba Cloud Linux 3, or CentOS 7.x.
An inbound rule is added to a security group of the ECS instance to allow traffic on ports 22, 80, and 443. For information about how to add an inbound security group rule, 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 view the version of Apache:
httpd -v
The following command output indicates that Apache is installed:
Run the following commands in sequence 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
The following command output indicates that Apache is started.
Step 2: Install and configure MySQL
Install MySQL
If the ECS instance runs the Alibaba Cloud Linux 3 operating system, you must install MySQL dependency packages.
sudo yum install compat-openssl10
Run the following command to update the Yellowdog Updater Modified (YUM) repository:
sudo rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-11.noarch.rpm
Run the following command to install MySQL:
sudo yum -y install mysql-community-server
Run the following command to view the version of MySQL:
mysql -V
The following command output indicates that MySQL is installed:
mysql Ver 8.0.36 for Linux on x86_64 (MySQL Community Server - GPL)
Run the following command to start MySQL:
sudo systemctl start mysqld
Run the following commands in sequence to configure MySQL to automatically start on system startup:
sudo systemctl enable mysqld sudo systemctl daemon-reload
Configure MySQL
Run the following command to view the
/var/log/mysqld.log
file and obtain and record the initial password of the root user:sudo grep 'temporary password' /var/log/mysqld.log
The following command output indicates that
ARQTRy3+****
is the initial password of the root user. You can use this initial password when you reset the password of the root user.2021-11-10T07:01:26.595215Z 1 [Note] A temporary password is generated for root@localhost: ARQTRy3+****
Run the following command to configure the security settings of MySQL:
sudo mysql_secure_installation
Enter the initial password of 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.
Securing the MySQL server deployment. Enter password for user root: # Enter the initial password that you obtained in the previous step.
Configure a new password for MySQL.
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 contain uppercase letters, lowercase letters, digits, and any of the following special characters: ( ) ` ~ ! @ # $ % ^ & * - + = | { } [ ] : ; ' < > , . ? / Re-enter new password: # Re-enter the new password. 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 # The strength of the new password is displayed in the command output. Change the password for root ? (Press y|Y for Yes, any other key for No) :Y # Enter Y to confirm the new password. # After the new password is configured, verify the password. New password: # Enter the new password. Re-enter new password: # Re-enter the new password. 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 users.
Remove anonymous users? (Press y|Y for Yes, any other key for No) :Y Success.
Enter Y to deny remote access by the root user.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) :Y Success.
Enter Y to delete the test database and the access permissions on the database.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) :Y - Dropping test database... Success. - Removing privileges on test database... Success.
Enter Y to reload privilege tables.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) :Y Success. All done!
For more information, see the official MySQL documentation.
Step 3: Install and configure PHP
Install PHP
Install PHP.
Alibaba Cloud Linux 3
Run the following command to update the YUM repository:
sudo rpm -Uvh https://mirrors.aliyun.com/remi/enterprise/remi-release-8.rpm --nodeps
Run the following command to enable the
php:remi-8.0
module stream:sudo yum module enable -y php:remi-8.0
Run the following command to install PHP:
sudo yum install -y php php-cli php-fpm php-common php-mysqlnd php-gd php-mbstring php-xml
Alibaba Cloud Linux 2
Run the following command to update the YUM repository:
sudo rpm -Uvh https://mirrors.aliyun.com/remi/enterprise/remi-release-7.rpm
Run the following command to modify the configuration of the
yum-plugin-releasever-adapter
plugin to supportremi
repository adaptation for Alibaba Cloud Linux 2:sudo echo ", remi-php54.repo, remi-php71.repo, remi-php73.repo, remi-php80.repo, remi-php82.repo, remi.repo, epel.repo, remi-modular.repo, remi-php70.repo, remi-php72.repo, remi-php74.repo, remi-php81.repo, remi-php83.repo, remi-safe.repo" >> /etc/yum/pluginconf.d/releasever-adapter.conf
Run the following command to enable the PHP 8.0 repository:
sudo sed -i '1,10s/enabled=0/enabled=1/' /etc/yum.repos.d/remi-php80.repo
Run the following command to install PHP:
sudo yum install -y php php-cli php-fpm php-common php-mysqlnd php-gd php-mbstring
CentOS 7.x
Update the YUM repositories.
Run the following commands to install the Extra Packages for Enterprise Linux (EPEL) repository and Remi repository:
sudo yum install -y epel-release sudo yum install -y https://rpms.remirepo.net/enterprise/remi-release-7.rpm
Run the following command to enable the PHP 8.0 repository:
sudo yum install -y yum-utils sudo yum-config-manager --enable remi-php80
Run the following command to install PHP:
sudo yum install -y php php-cli php-fpm php-mysqlnd php-zip php-devel php-gd php-mcrypt php-mbstring php-curl php-xml php-pear php-bcmath php-json
Run the following command to view the version of PHP:
php -v
The following command output indicates that PHP is installed:
PHP 8.0.30 (cli) (built: Aug 3 2023 17:13:08) ( NTS gcc x86_64 ) Copyright (c) The PHP Group Zend Engine v4.0.30, Copyright (c) Zend Technologies
Modify the Apache configuration file to add support for PHP
Run the following command to back up the Apache configuration file:
sudo cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.bak
Modify the Apache configuration file to add support for PHP.
ImportantIf you do not add support for PHP, PHP pages cannot be displayed when you access the pages by using a web browser.
Run the following command to open the Apache configuration file:
sudo vim /etc/httpd/conf/httpd.conf
Press the
I
key to enter Insert mode.Add the following configuration information:
<FilesMatch \.php$> SetHandler "proxy:fcgi://127.0.0.1:9000" </FilesMatch>
Press the
Esc
key, enter:wq
, and then press theEnter
key to save and close the configuration file.Run the following command to restart PHP FastCGI Process Manager (PHP-FPM):
sudo systemctl restart php-fpm
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 web browser on your computer and press the Enter key.The page shown in the following figure indicates that PHP is installed.
After you confirm that the LAMP stack is built, we recommend that you run the following command to delete the phpinfo.php 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