Updated on 2025-08-07 GMT+08:00

Manually Deploying LAMP (Ubuntu)

Scenarios

LAMP is an acronym for Linux, Apache, MySQL, and PHP. It is often used to build websites and web applications. LAMP is maintained by a community of global experts and provides abundant resources. This helps you create, deploy, and maintain applications efficiently.

This section describes how to deploy LAMP on an ECS running Ubuntu.

Advantages

  • You can unleash ECS features like elastic scaling, strong security, high availability, and easy management.
  • The LAMP architecture provides extensive web resources for lightweight and fast development, and delivers high performance at a low cost.

Resource Planning

Table 1 lists the resource configuration and software versions used in this practice. The commands and parameters may vary according to the hardware specifications or software versions you would use.

Table 1 Resources and costs

Resource

Description

Cost

ECS

  • Billing mode: Yearly/Monthly
  • AZ: AZ1
  • Specifications: s7.large.4
  • Image: Ubuntu 22.04
  • EIP: Auto assign
  • EIP type: Dynamic BGP
  • Billed by: Traffic
  • Bandwidth: 5 Mbit/s

The following resources generate costs:

  • ECSs
  • EVS disks
  • EIPs

For billing details, see Billing Modes.

Apache

An open-source web server software

Free

MySQL

An open-source relational database software

Free

PHP

An open-source software used for web development

Free

Procedure

To deploy an LAMP environment on an ECS running Ubuntu, do as follows:

  1. Install Apache.
  2. Install and configure MySQL.
  3. Install PHP.
  4. Test the deployment.

Procedure

Preparations

  • An EIP has been bound to the ECS. For details, see Binding an EIP.
  • The rules listed in Table 2 have been added to the security group that the target ECS belongs to. For details, see Adding a Security Group Rule.
    Table 2 Security group rules

    Direction

    Priority

    Action

    Type

    Protocol & Port

    Source

    Inbound

    1

    Allow

    IPv4

    TCP: 80

    0.0.0.0/0

    Inbound

    1

    Allow

    IPv4

    TCP: 22

    0.0.0.0/0

    Inbound

    1

    Allow

    IPv4

    TCP: 443

    0.0.0.0/0

Procedure

  1. Log in to the ECS.
  2. Install Apache.

    1. Run the following command to update the software packages in Ubuntu:
      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

      Information similar to the following is displayed.

    4. Run the following commands in sequence to start Apache and enable it to start automatically upon ECS startup:
      sudo systemctl start apache2
      sudo systemctl enable apache2
    5. Run the following command to check whether the Apache service is started:
      sudo systemctl status apache2

      If the active state is displayed, as shown in the following figure, the Apache service has been started.

  3. Install and configure 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:
      mysql -V

      Information similar to the following is displayed.

    3. Run the following commands to start MySQL:
      sudo systemctl enable mysql
      sudo systemctl daemon-reload
    4. Configure MySQL.
      1. Run the following command to access MySQL:
        sudo mysql
      2. Set the password of user root.
        ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by 'newpassword';
      3. Exit the MySQL database.
        exit;
      4. Run the following commands to perform security configuration for MySQL:
        sudo mysql_secure_installation
        Securing the MySQL server deployment.
        
        Enter password for user root:    #Enter the password of user root you have set.
        
        VALIDATE PASSWORD COMPONENT can be used to test passwords
        and improve security. It checks the strength of password
        and allows the users to set only those passwords which are
        secure enough. Would you like to setup VALIDATE PASSWORD component?
        
        Press y|Y for Yes, any other key for No: Y
        
        There are three levels of password validation policy:
        
        LOW Length >= 8
        MEDIUM Length >= 8, numeric, mixed case, and special characters
        STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
        
        Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2
        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) : N   #Press N to keep the password unchanged.
        
         ... skipping.
        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   #Press Y to remove anonymous users.
        Success.
        
        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   #Press Y to disallow remote logins of user root.
        Success.
        
        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   #Press Y to delete the test database and remove access to it.
         - Dropping test database...
        Success.
        
         - Removing privileges on test database...
        Success.
        
        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   #Press Y to reload privilege tables.
        Success.
        
        All done!

  4. Install PHP.

    1. Run the following command to install the software-properties-common software package:
      sudo apt-get install -y software-properties-common
    2. Run the following command to install the third-party software package Ondrej PPA of PHP:
      sudo add-apt-repository ppa:ondrej/php

      If the following information is displayed, press Enter.

    3. Run the following command to install PHP:
      sudo apt-get install -y php8.3 php8.3-fpm libapache2-mod-php8.3
    4. Run the following command to check the PHP version:
      php -v

      If information similar to the following is displayed, PHP has been installed.

  5. Test the deployment.

    1. Run the following command to go to the Apache configuration file directory, copy the apache2.conf file to the apache2.conf.bak file, and retain the original example configuration file as a backup:
      sudo cp /etc/apache2/apache2.conf /etc/apache2/apache2.conf.bak
    2. Perform the following operations to modify the Apache configuration file to support PHP:
      1. Run the following command to open the Apache configuration file:
        sudo vim /etc/apache2/apache2.conf
      2. Press i to enter insert mode.
      3. Modify the apache2.conf file and add the following to the file:
        DirectoryIndex index.html index.php
        <FilesMatch \.php$>
        SetHandler application/x-httpd-php
        </FilesMatch>
      4. Press Esc to exit insert mode. Then, enter :wq to save the settings and exit.
    3. Create a test web page in the Apache website root directory.
      1. Run the following command to obtain the path of the Apache website root directory:
        sudo cat /etc/apache2/sites-available/000-default.conf

      2. Run the following command to create a test web page in the website root directory and add the phpinfo() function to the web page content: The phpinfo() function displays all PHP configuration information.
        sudo sh -c 'echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php'
    4. Run the following command to restart Apache:
      sudo systemctl restart apache2
    5. Enter http://<server-IP-address>/phpinfo.php in the address bar. If the following page is displayed, the environment has been deployed.

    6. After the LAMP environment is set up, run the following command to delete the phpinfo.php test file to prevent potential data breaches:
      sudo rm -rf <website-root-directory>/phpinfo.php

      In this example, the root directory of the website is /var/www/html. Run the following command to delete the test file:

      sudo rm -rf /var/www/html/phpinfo.php

Follow-up Operations

After the LAMP environment is deployed, refer to the following to set up a website or application: