All Products
Search
Document Center

Simple Application Server:Manually build a LAMP development environment (Alibaba Cloud Linux/CentOS 7.x)

Last Updated:Aug 12, 2024

LAMP is a popular web development environment. The name is an acronym coined from four components: Linux operating system, Apache HTTP Server, MySQL relational database management system, and PHP programming language. This topic describes how to manually build a LAMP stack on a simple application server that runs Alibaba Cloud Linux 2, Alibaba Cloud Linux 3, or CentOS 7.x.

Note

If you do not have any existing simple application server, we recommend that you use a LAMP application image to quickly build a LAMP environment. For more information, see Build a LAMP environment by using an application image.

Preparations

  • You must create a simple application server to deploy the LAMP environment. For more information, see Create a simple application server.

  • The simple application server must meet the following requirements:

    • The operating system is Alibaba Cloud Linux 2, Alibaba Cloud Linux 3, or CentOS 7.x.

    • The security group of the simple application server allows inbound traffic over ports 22, 80, and 443. For more information, see Manage the firewall of a simple application server.

Step 1: Install Apache

  1. Remotely connect to the simple application server on which you want to deploy the LAMP environment. For more information, see Connect to a Linux server.

  2. Run the following command to install Apache and its extension package:

    sudo yum -y install httpd httpd-manual mod_ssl mod_perl
  3. Run the following command to view the version of Apache:

    httpd -v

    The following command output indicates that Apache is installed:

    httpd -v

  4. 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
  5. Run the following command to check the status of Apache:

    sudo systemctl status httpd

    The following command output indicates that Apache is started.

    image.png

Step 2: Install and configure MySQL

Install MySQL

  1. (Optional) If the ECS instance runs the Alibaba Cloud Linux 3 operating system, you must install MySQL dependency packages.

    sudo yum install -y compat-openssl10
  2. 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
  3. Run the following command to download software package index information:

    yum makecache
  4. Run the following command to install MySQL:

    sudo yum -y install mysql-community-server
  5. Run the following command to view the version of MySQL:

    mysql -V

    The following command output indicates that MySQL is installed.

    image

  6. Run the following command to start MySQL:

    sudo systemctl start mysqld
  7. 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

  1. 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+****
  2. Run the following command to configure the security settings of MySQL:

    sudo mysql_secure_installation
    1. Enter the initial password of MySQL.

      Note

      For 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.
    2. 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.
    3. Enter Y to delete the anonymous users.

      Remove anonymous users? (Press y|Y for Yes, any other key for No) :Y
      Success.
    4. 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.
    5. 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.
    6. 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 PHP

  1. Run the following command to back up the Apache configuration file:

    sudo cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.bak
  2. Modify the Apache configuration file to add support for PHP.

    Important

    If you do not add support for PHP, PHP pages cannot be displayed when you access the pages by using a web browser.

    1. Run the following command to open the Apache configuration file:

      sudo vim /etc/httpd/conf/httpd.conf
    2. Press the I key to enter Insert mode.

    3. Add the following configuration information:

      <FilesMatch \.php$>
          SetHandler "proxy:fcgi://127.0.0.1:9000"
      </FilesMatch>
    4. Press the Esc key, enter :wq, and then press the Enter key to save and close the configuration file.

    5. Run the following command to restart PHP FastCGI Process Manager (PHP-FPM):

      sudo systemctl restart php-fpm
  3. 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'
  4. Run the following command to restart Apache:

    sudo systemctl restart httpd
  5. 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.

    image

  6. 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