Install an Apache, MySQL, and PHP environment on Ubuntu

更新时间:
复制 MD 格式

This topic describes how to install an Apache, MySQL, and PHP environment on an ECS instance that runs Ubuntu.

Details

Important

Note the following items:

  • If you perform risky operations, such as modifying instances or data, ensure that you have a disaster recovery and fault tolerance plan in place to protect your data.

  • Before you modify the configuration or data of an instance, such as an ECS or RDS instance, create a snapshot or enable a feature such as RDS log backup.

  • If you have submitted security information, such as logon credentials, on the Alibaba Cloud platform, change them promptly.

Connect to the Linux instance using the management terminal. For more information, see ECS Remote Connection Guide.

Install a Nginx+MySQL+PHP environment

This example uses the following software versions:

  • Operating system: Ubuntu 22.04 64-bit

  • Apache version:

  • MySQL version:

  • PHP version:

Note
  • This example uses Ubuntu 22.04 64-bit. The installation steps are similar for other Ubuntu versions, but the software versions installed by the apt package manager, some configuration files, and start commands may differ. Notes for different operating system versions are included in the steps.

  • When you use yum to install Apache, MySQL, and PHP, the installed version may vary because it depends on the YUM source that is available at the time of download.

Step 1: Install Apache

  1. Run the following command to update the package index on your Ubuntu system.

    sudo apt update
  2. Run the following command to install Apache.

    sudo apt-get -y install apache2
  3. Run the following command to check the Apache version.

    apache2 -v

    The following output indicates that Apache is installed and the version is 2.4.41.

    Server version: Apache/2.4.41 (Ubuntu)
    Server built:   2022-01-05T14:49:56
  4. Run the following command to start the Apache service and set it to start automatically on boot.

    sudo systemctl start apache2
  5. In a browser on your local computer or another host with public network access, open http://<Public IP address of your ECS instance>.

    If the Apache default index page appears, the Apache service is running.

    Note

    Apache listens on port 80 by default. If you cannot access the Apache service, make sure that you add an inbound security group rule to allow traffic on port 80 for the ECS instance. For more information, see Add a security group rule.

    Apache default index page

Step 2: Install MySQL

  1. Install MySQL.

    1. Run the following command to install MySQL.

      sudo apt -y install mysql-server
    2. Run the following command to check the MySQL version.

      sudo mysql -V

      The following output indicates that MySQL is installed and the version is 8.0.29.

      mysql  Ver 8.0.29-0ubuntu0.20.04.3 for Linux on x86_64 ((Ubuntu))
  2. Run the following command to start the MySQL service.

    sudo systemctl start mysql

Step 3: Install PHP

  1. Run the following command to install PHP.

    sudo apt -y install php-fpm
    sudo apt -y install libapache2-mod-php
  2. Run the following command to check the PHP version.

    sudo php -v

    The following output indicates that PHP is installed and the version is 7.4.3.

    PHP 7.4.3 (cli) (built: Nov 25 2021 23:16:22) ( NTS )
    Copyright (c) The PHP Group
    Zend Engine v3.4.0, Copyright (c) Zend Technologies
        with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies