Install and configure Linux, Apache, MySQL, and PHP on an ECS instance to host web applications.
Use Terraform Explorer to automatically build a LAMP environment on an 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 security group rules are added to open ports 22 and 80 on the ECS instance.
-
The ECS instance has at least 4 GiB of memory.
Deploy a LAMP stack
Alibaba Cloud Linux 3 or CentOS 8
-
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.
NoteOn Alibaba Cloud Linux 3, install
compat-openssl10for compatibility with earlier 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 initial root password.
-
On Alibaba Cloud Linux 3:
echo $(PASSWORD=$(sudo grep 'temporary password' /var/log/mysqld.log); PASSWORD=${PASSWORD##* }; echo $PASSWORD) -
On CentOS 8, the root user has no initial password.
-
-
Set a new MySQL root password. Replace
<oldpwd>with the initial password and<newpwd>with the new password. On CentOS 8, leave<oldpwd>empty and press Enter when prompted.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.
NoteThis example uses PHP 8.4. To install a different version, change the module name accordingly. For example, use
php:remi-8.1for PHP 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 sed -i "s/\$releasever/8/g" /etc/yum.repos.d/remi-modular.repo /etc/yum.repos.d/remi-safe.repo /etc/yum.repos.d/remi.repo 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
PHP-FPMlistening address.sudo grep 'listen =' /etc/php-fpm.d/www.conf -
Create
/etc/httpd/conf.d/php-fpm.confand configurephp-fpmforwarding rules.ImportantIf
PHP-FPMlistens on127.0.0.1:9000, set SetHandler 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.
sudo systemctl restart httpd -
Create
test.phpin/var/www/html/. Replace<username>and<password>with your MySQL credentials.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 -
Open http://<Public-IP-Address-of-ECS-instance>/test.php in a browser. If
successis returned, the LAMP stack works correctly.
-
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 initial root password.
echo $(PASSWORD=$(sudo grep 'temporary password' /var/log/mysqld.log); PASSWORD=${PASSWORD##* }; echo $PASSWORD) -
Set a new MySQL root password. 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 sed -i "s/\$releasever/7/g" /etc/yum.repos.d/remi-modular.repo /etc/yum.repos.d/remi-safe.repo /etc/yum.repos.d/remi.repo 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
PHP-FPMlistening address.sudo grep 'listen =' /etc/php-fpm.d/www.conf-
If a socket file path is returned, PHP-FPM listens on the socket file.
-
If
127.0.0.1:9000is returned, PHP-FPM listens on local port 9000.
-
-
Create
/etc/httpd/conf.d/php-fpm.confand configurephp-fpmforwarding rules.ImportantIf
PHP-FPMlistens on a socket file, changeproxy:fcgi://127.0.0.1:9000toproxy:unix:<path>;. Replace <path> with the socket file path.sudo tee /etc/httpd/conf.d/php-fpm.conf <<-'EOF' <FilesMatch \.php$> SetHandler "proxy:fcgi://127.0.0.1:9000" </FilesMatch> EOF -
Restart httpd.
sudo systemctl restart httpd -
Create
test.phpin/var/www/html/. Replace<username>and<password>with your MySQL credentials.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 -
Open http://<Public-IP-Address-of-ECS-instance>/test.php in a browser. If
successis returned, the LAMP stack works correctly.
-
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 -
Install the MySQL server.
sudo apt update -y && sudo apt install -y mysql-server -
Change the
rootpassword and authentication plugin. Replace<newpwd>with the actual password.ImportantBy default, root uses the
auth_socketplugin and has no password. Press Enter when prompted.sudo mysql -uroot -p -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY '<newpwd>';" -e "FLUSH PRIVILEGES;" -
Install PHP.
NoteUse
sudo apt search phpto list available PHP versions. To install a different version, replace the version number accordingly. For example, for PHP 8.1:sudo apt install -y php8.1 php8.1-fpm php8.1-mysql.# 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
PHP-FPMlistening address. Replace<version>with your PHP version, such as8.4.sudo grep '^listen =' /etc/php/<version>/fpm/pool.d/www.conf-
If a socket file path is returned, PHP-FPM listens on the socket file.
-
If
127.0.0.1:9000is returned, PHP-FPM listens on local port 9000.
-
-
Create
/etc/apache2/conf-available/php-fpm.confand configure PHP forwarding rules. Replace <listen> with the PHP-FPM listening address.-
If PHP-FPM listens on a socket file, replace
<listen>withunix:<path>;and<path>with the socket file path.ImportantListening on socket files requires read and write permissions. Run
sudo chmod 666 <path>to grant them. Replace <path> with the socket file path. -
If PHP-FPM listens on
127.0.0.1:9000, 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 for Apache to forward PHP requests to PHP-FPM, then enable the PHP-FPM configuration.
sudo a2enmod proxy_fcgi setenvif sudo a2enconf php8.4-fpm -
Restart Apache.
sudo systemctl restart apache2 -
Create
test.phpin/var/www/html/. Replace<username>and<password>with your MySQL credentials.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 -
Open http://<Public-IP-Address-of-ECS-instance>/test.php in a browser. If
successis returned, the LAMP stack works correctly.
-
FAQ
Why can't I access the test.php page?
Possible causes and solutions:
Port 80 is not open in the security group, the system firewall is enabled, or port 80 is used by another service.
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 for remote access. See Deploy MySQL on a Linux instance.