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. This topic describes how to manually deploy a LAMP stack on an Elastic Compute Service (ECS) instance.
Prerequisites
A public IP address is automatically assigned to the ECS instance. Alternatively, an elastic IP address (EIP) is associated with the ECS instance. For instructions on how to enable public bandwidth, see Enable public bandwidth.
Inbound rules are added to a security group of the ECS instance to open ports 22 and 80. For information about how to add a security group rule, see Add a security group rule.
The ECS instance has at least 4 GiB of memory.
Deploy a LAMP stack
Alibaba Cloud Linux 3 or CentOS 8
All content is removed from the default CentOS 8 repository at the following address: http://mirror.centos.org/centos/8/
. If you continue using the default CentOS 8 repository on Alibaba Cloud, an error is reported. You must change CentOS 8 repository addresses. For more information, see Change CentOS 8 repository addresses.
Install Apache.
# Install the Apache server. sudo dnf install -y httpd # Automatically start the Apache server on instance startup. sudo systemctl enable httpd # Start the Apache server. sudo systemctl start httpd
Install MySQL.
NoteIf your instance runs Alibaba Cloud Linux 3, you must install
compat-openssl10
, which is compatible with earlier versions of OpenSSL libraries.sudo yum install -y compat-openssl10
# Add an official MySQL repository. sudo rpm -Uvh https://repo.mysql.com/mysql84-community-release-el8-1.noarch.rpm # Install MySQL. sudo dnf install -y mysql-server # Start MySQL and configure MySQL to automatically start on system startup. sudo systemctl start mysqld sudo systemctl enable mysqld
Query the default initial password of the root user.
If your instance runs Alibaba Cloud Linux 3, run the following command:
echo $(PASSWORD=$(sudo grep 'temporary password' /var/log/mysqld.log); PASSWORD=${PASSWORD##* }; echo $PASSWORD)
If your instance runs CentOS 8, the root user does not have an initial password.
Specify a new password for the root user of MySQL. In the following command, replace
<oldpwd>
with the initial password and<newpwd>
with the new password. If your instance runs CentOS 8, replace<oldpwd>
with an empty string and press the Enter key to skip to the next line when you are prompted to enter a password.ImportantThe password must be at least eight characters in length, and contain at least one uppercase letter, one lowercase letter, one digit, and one special character.
sudo mysqladmin -uroot -p'<oldpwd>' password '<newpwd>'
Install PHP.
NoteIn this example, PHP 8.4 is used. If you require another PHP version, change the module name based on the PHP version that you want to install. For example, if you want to install PHP 8.1, change the module name to
php:remi-8.1
.# Specify the remi repository and enable the php:remi-8.4 module. sudo rpm -Uvh http://mirrors.cloud.aliyuncs.com/remi/enterprise/remi-release-8.rpm --nodeps sudo dnf install -y yum-utils && sudo dnf module enable -y php:remi-8.4 # Install PHP, PHP FastCGI Process Manager (PHP-FPM), and the MySQL extension. sudo dnf install -y php php-fpm php-mysqlnd # Start PHP-FPM and configure PHP-FPM to automatically start on system startup. sudo systemctl start php-fpm sudo systemctl enable php-fpm
Verify the LAMP stack.
Query the default listening address of
PHP-FPM
in the configuration file.sudo grep 'listen =' /etc/php-fpm.d/www.conf
Create the
/etc/httpd/conf.d/php-fpm.conf
configuration file and configurephp-fpm
rules in the file.ImportantIf the listening address of
PHP-FPM
is127.0.0.1:9000
, set the SetHandler parameter toproxy:fcgi://127.0.0.1:9000
.sudo tee /etc/httpd/conf.d/php-fpm.conf <<-'EOF' <FilesMatch \.php$> SetHandler "proxy:unix:/run/php-fpm/www.sock;" </FilesMatch> EOF
Restart httpd for the configuration to take effect.
sudo systemctl restart httpd
Create the
test.php
file in the/var/www/html/
directory and add the following content to the file. Replace<username>
with the MySQL username and<password>
with the corresponding password.sudo tee /var/www/html/test.php <<-'EOF' <?php $servername = "localhost"; $username = "<username>"; $password = "<password>"; $conn = new mysqli($servername, $username, $password); if ($conn->connect_error) { die("fail: " . $conn->connect_error); } echo "success\n"; ?> EOF
Enter
http://<Public IP address of the ECS instance>/test.php
in the address bar of a web browser on your on-premises machine. Ifsuccess
is returned, you are connected to MySQL by using the PHP proxy.
Alibaba Cloud Linux 2 or CentOS 7
Install Apache.
# Install the Apache server. sudo yum install -y httpd # Automatically start the Apache server on instance startup. sudo systemctl enable httpd # Start the Apache server. sudo systemctl start httpd
Install MySQL.
# Add an official MySQL repository. sudo rpm -Uvh https://repo.mysql.com/mysql84-community-release-el7-1.noarch.rpm # Install MySQL. sudo yum install -y mysql-server # Start MySQL and configure MySQL to automatically start on system startup. sudo systemctl start mysqld sudo systemctl enable mysqld
Query the default initial password of the root user.
echo $(PASSWORD=$(sudo grep 'temporary password' /var/log/mysqld.log); PASSWORD=${PASSWORD##* }; echo $PASSWORD)
Specify a new password for the root user of MySQL. In the following command, replace
<oldpwd>
with the initial password and<newpwd>
with the new password.ImportantThe password must be at least eight characters in length, and contain at least one uppercase letter, one lowercase letter, one digit, and one special character.
sudo mysqladmin -uroot -p'<oldpwd>' password '<newpwd>'
Install PHP.
# Specify the remi repository and enable the remi-php83 module. sudo rpm -Uvh http://mirrors.cloud.aliyuncs.com/remi/enterprise/remi-release-7.rpm --nodeps sudo yum install -y yum-utils && sudo yum-config-manager --enable remi-php83 # Install PHP, PHP-FPM, and the MySQL extension. sudo yum install -y php php-fpm php-mysqlnd # Start PHP-FPM and configure PHP-FPM to automatically start on system startup. sudo systemctl start php-fpm sudo systemctl enable php-fpm
Verify the LAMP stack.
Query the default listening address of
PHP-FPM
in the configuration file.sudo grep 'listen =' /etc/php-fpm.d/www.conf
If the address of a socket file is returned, PHP-FPM listens to the socket file.
If
127.0.0.1:9000
is returned, PHP-FPM listens on local port 9000.
Create the
/etc/httpd/conf.d/php-fpm.conf
configuration file and configurephp-fpm
rules in the file.ImportantIf the listening address of
PHP-FPM
is the address of a socket file, changeproxy:fcgi://127.0.0.1:9000
toproxy:unix:<path>;
. Replace <path> with the address of your socket file.sudo tee /etc/httpd/conf.d/php-fpm.conf <<-'EOF' <FilesMatch \.php$> SetHandler "proxy:fcgi://127.0.0.1:9000" </FilesMatch> EOF
Restart httpd for the configuration to take effect.
sudo systemctl restart httpd
Create the
test.php
file in the/var/www/html/
directory and add the following content to the file. Replace<username>
with the MySQL username and<password>
with the corresponding password.sudo tee /var/www/html/test.php <<-'EOF' <?php $servername = "localhost"; $username = "<username>"; $password = "<password>"; $conn = new mysqli($servername, $username, $password); if ($conn->connect_error) { die("fail: " . $conn->connect_error); } echo "success\n"; ?> EOF
Enter
http://<Public IP address of the ECS instance>/test.php
in the address bar of a web browser on your on-premises machine. Ifsuccess
is returned, you are connected to MySQL by using the PHP proxy.
Ubuntu 20.04 or later
Install Apache.
# Install the Apache server. sudo apt update -y && sudo apt install -y apache2 # Automatically start the Apache server on instance startup. sudo systemctl enable apache2 # Start the Apache server. sudo systemctl start apache2
Update the software package list and install the MySQL server.
sudo apt update -y && sudo apt install -y mysql-server
Change the password and identity authentication plug-in used by the
root
user of the MySQL server. Replace<newpwd>
in the following command with the actual password.ImportantThe default identity authentication plug-in used by the root user is
auth_socket
. After the command is run, you are prompted to enter a password. Press the Enter key to skip to the next line.sudo mysql -uroot -p -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY '<newpwd>';" -e "FLUSH PRIVILEGES;"
Install PHP.
NoteRun the
sudo apt search php
command to query all PHP versions that you can install. If you want to install a different PHP version, replace the version number in the following command with the actual version number. For example, if you want to install PHP 8.1, run thesudo apt install -y php8.1 php8.1-fpm php8.1-mysql
command.# Install the software-properties-common package and add the ppa:ondrej/php Personal Package Archive (PPA) repository. sudo apt update && sudo apt install -y software-properties-common && sudo add-apt-repository -y ppa:ondrej/php # Install PHP 8.4 and the related components, including PHP-FPM and the MySQL extension. sudo apt install -y php8.4 php8.4-fpm php8.4-mysql
Verify the LAMP stack.
Query the default listening address of
PHP-FPM
in the configuration file. Replace<version>
with your actual PHP version. For example, if you use PHP 8.4, replace<version>
with 8.4.sudo grep '^listen =' /etc/php/<version>/fpm/pool.d/www.conf
If the address of a socket file is returned, PHP-FPM listens to the socket file.
If
127.0.0.1:9000
is returned, PHP-FPM listens on local port 9000.
Create the
/etc/apache2/conf-available/php-fpm.conf
configuration file and configure PHP forwarding rules in the file. Replace <listen> with the actual listening address of PHP-FPM.If the address of a socket file is used as the listening address of PHP-FPM, replace
<listen>
withunix:<path>;
and<path>
with the address of the socket file.ImportantTo listen to socket files, your account must have the read and write permissions on the socket files. You can run the
sudo chmod 666 <path>
command to grant the preceding permissions. Replace <path> with the actual address of a socket file.If
127.0.0.1:9000
is used as the listening address of PHP-FPM, replace<listen>
withfcgi://127.0.0.1:9000
.
sudo tee /etc/apache2/conf-available/php-fpm.conf <<-'EOF' <FilesMatch \.php$> SetHandler "proxy:<listen>" </FilesMatch> EOF
Create a symbolic link for the configuration file.
sudo ln -s /etc/apache2/conf-available/php-fpm.conf /etc/apache2/conf-enabled/
Enable the proxy_fcgi and setenvif modules to allow Apache to forward PHP requests to PHP-FPM. Then, enable the configuration file of PHP-FPM.
sudo a2enmod proxy_fcgi setenvif sudo a2enconf php8.4-fpm
Restart httpd for the configuration to take effect.
sudo systemctl restart apache2
Create the
test.php
file in the/var/www/html/
directory and add the following content to the file. Replace<username>
with the MySQL username and<password>
with the corresponding password.sudo tee /var/www/html/test.php <<-'EOF' <?php $servername = "localhost"; $username = "<username>"; $password = "<password>"; $conn = new mysqli($servername, $username, $password); if ($conn->connect_error) { die("fail: " . $conn->connect_error); } echo "success\n"; ?> EOF
Enter
http://<Public IP address of the ECS instance>/test.php
in the address bar of a web browser on your on-premises machine. Ifsuccess
is returned, you are connected to MySQL by using the PHP proxy.
FAQ
Why am I unable to access the test.php page by using the public IP address of the ECS instance on which the page is hosted?
Possible causes and solutions:
Port 80 is not open in the security groups of the ECS instance, the system firewall is enabled on the ECS instance, or port 80 is used by a different service.
For information about how to troubleshoot the issue based on the preceding causes, see What do I do if I cannot access a service deployed on an instance?
How do I allow remote access to MySQL?
Create a non-root account and allow remote access to MySQL by using the account. For more information, see Deploy MySQL on a Linux instance.