You need to enable JavaScript to run this app.
Elastic Compute Service

Elastic Compute Service

Copy page
Download PDF
Building environments
Setting up an LNMP environment (CentOS 7)
Copy page
Download PDF
Setting up an LNMP environment (CentOS 7)
An LNMP environment refers to a web application platform built on a Linux system, consisting of Linux, Nginx, MySQL or MariaDB, and PHP. This topic describes how to deploy an LNMP environment on a CentOS 7.6 instance.​
Software versions​
  • Operating system: In the example provided in this topic, CentOS 7.6 is used. You can also use CentOS 7.9, veLinux 1.0 (CentOS compatible version), or other CentOS 7 series images.​
  • Nginx: A high-performance, lightweight web server that supports HTTP and reverse proxy. In the example provided in this topic, Nginx 1.22.0 is used.​
  • MySQL/MariaDB: A database management system. In the example provided in this topic, MariaDB 15.1 and MySQL Community Edition 8.4.3 are used.​
  • PHP: A server-side scripting language embedded in HTML documents. In the example provided in this topic, PHP 7.0.33 is used.​
Prerequisites​
  • An EIP is associated with the Linux instance, enabling the instance to connect to the Internet. For information about how to associate an EIP with an ECS instance, see Associating EIPs.​
Step 1: Install and configure Nginx​
  1. Remotely connect to and log in to the ECS instance. For more information, see Logging in to Linux instances.​
  1. Execute the following command to install Nginx.​
  • ​
    yum install -y https://mirrors.ivolces.com/nginx_all/centos/7/x86_64/RPMS/nginx-1.22.0-1.el7.ngx.x86_64.rpm​
    ​
  1. Execute the following command to view the Nginx version.​
  • ​
    nginx -v​
    ​
  • If the following output is displayed, Nginx is installed.​
  • ​
    nginx version: nginx/1.22.0​
    ​
  1. Execute the following command to view the path of the Nginx configuration file.​
  • ​
    cat /etc/nginx/nginx.conf​
    ​
  • In the following output, the include configuration item indicates the path of the file.​
  • ​image​​
  1. Execute the following command to back up the configuration file in the configuration file directory.​
  • ​
    cd /etc/nginx/conf.d ​
    cp default.conf default.conf.bak​
    ​
  1. Execute the following command to modify the original configuration file.​
  • ​
    vi default.conf​
    ​
  1. Press the i key to enter edit mode and modify the following content in location.​
  • ​
    location / {​
    root /usr/share/nginx/html; #Replace this path with your website root directory. In this document, /usr/share/nginx/html is used as an example, which you may reuse directly; however, in actual business scenarios, it is recommended that you replace it with your own website root directory.​
    index index.html index.htm index.php; #Add index.php as the default homepage information.​
    }​
    ​
  1. Remove the comment markers at the beginning of the lines inside the curly braces of location ~ \.php$, #and modify the following content.​
  • ​
    location ~ \.php$ { ​
    root /usr/share/nginx/html; #Replace this path with your website root directory. In this document, /usr/share/nginx/html is used as an example. You may reuse it directly; however, in actual business scenarios, it is recommended that you replace it with your own website root directory.​
    fastcgi_pass 127.0.0.1:9000; #Nginx forwards PHP requests to PHP-FPM for processing via the 9000 port on the local machine.​
    fastcgi_index index.php; ​
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #Change /scripts$fastcgi_script_name to $document_root$fastcgi_script_name.​
    include fastcgi_params;​
    }​
    ​
  1. Press the Esc key, enter :wq, and then press Enter to save the modifications and close the file.​
  1. Execute the following command to start the Nginx service.​
  • ​
    systemctl start nginx​
    ​
  1. Execute the following command to specify that the Nginx service automatically starts upon instance startup.​
  • ​
    systemctl enable nginx​
    ​
  1. In the local browser, enter http://<ECS instance EIP>/ to access Nginx.​
  • The following output indicates that Nginx is set up successfully.​
  • ​image​​
Step 2: Install and configure MariaDB/MySQL​
Install and configure MariaDB​
  1. Execute the following command to install MariaDB.​
  • ​
    yum -y install mariadb-server mariadb-client​
    ​
  1. Execute the following command to view the MariaDB version.​
  • ​
    mysql -V​
    ​
  • If the following output is displayed, MariaDB is installed.​
  • ​
    mysql Ver 15.1 Distrib 5.5.68-MariaDB, for Linux (x86_64) using readline 5.1​
    ​
  1. Execute the following command to start the MariaDB service and specify that the MariaDB service automatically starts upon instance startup.​
  • ​
    systemctl enable --now mariadb​
    ​
  1. Execute the following command to check whether the MariaDB service is started.​
  • ​
    systemctl status mariadb​
    ​
  • If Active: active (running) is displayed in the output, the MariaDB service is started.​
  1. Execute the following command to perform MariaDB security operations and specify the password.​
  • ​
    mysql_secure_installation​
    ​
  1. Press the Enter key to confirm the password.​
  1. Enter y and press the Enter key to confirm modification of the database password for the root user.​
  1. Specify the MariaDB password and press the Enter key. The password you enter is not displayed by default.​
  1. Enter the new password again and press the Enter key to confirm your new password.​
tip​
Enter a password that meets the strength requirements of the password verification policy.​
  1. Enter y and press the Enter key to remove the anonymous user.​
  1. Enter y and press the Enter key to prohibit remote login by the root account.​
  1. Enter y and press the Enter key to delete the test database and its access permission.​
  1. Enter y and press the Enter key to reload the authorization table.​
Install and configure MySQL​
  1. Execute the following command to download the RPM package for the MySQL Community Edition repository configuration file.​
  • ​
    wget https://dev.mysql.com/get/mysql84-community-release-el7-1.noarch.rpm​
    ​
  1. Execute the following command to install the MySQL 8.4 Community Edition software repository configuration file.​
  • ​
    rpm -Uvh mysql84-community-release-el7-1.noarch.rpm​
    ​
  1. Execute the following command to install MySQL 8.4.​
  • ​
    yum -y install mysql-community-server​
    ​
tip​
If the Failing package is: mysql-community-libs-compat-8.4.3-1.el7.x86_64 error occurs, the GPG key for MySQL is upgraded and needs to be retrieved again. In this case, execute the following commands in sequence: rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022 and yum -y install mysql-server.​
  1. Execute the following command to view the MySQL version.​
  • ​
    mysql -V​
    ​
  • If the following output is displayed, MySQL is installed.​
  • ​
    mysql Ver 8.4.3 for Linux on x86_64 (MySQL Community Server - GPL)​
    ​
  1. Execute the following command to start the MySQL service and specify that the MySQL service automatically starts upon instance startup.​
  • ​
    systemctl enable --now mysqld​
    ​
  1. Execute the following command to check whether the MySQL service is started.​
  • ​
    systemctl status mysqld​
    ​
  • If Active: active (running) is displayed in the output, the MySQL service is started.​
  1. Execute the following command to view the initial password that was automatically specified during MySQL installation.​
  • ​
    grep 'temporary password' /var/log/mysqld.log​
    ​
  • Output similar to the following is displayed. In this example, the initial password is 0zNwvmpr:fOd.​
  • ​
    2021-09-17T09:31:12.142217Z 1 [Note] A temporary password is generated for root@localhost: 0zNwvmpr:fOd​
    ​
  1. Execute the following command to perform MySQL security operations and specify the password.​
  • ​
    mysql_secure_installation​
    ​
  1. Enter the initial password obtained in the previous step and press the Enter key.​
  1. Specify the new MySQL password and press the Enter key. The password you enter is not displayed by default.​
  1. Enter the new password again and press y to confirm your new password.​
tip​
Enter a password that meets the strength requirements of the password verification policy.​
  1. Repeat the preceding steps to specify the new password, and then enter y to confirm the new password.​
  1. Enter y and press the Enter key to remove the anonymous user.​
  1. Enter y and press the Enter key to prohibit remote login by the root account.​
  1. Enter y and press the Enter key to delete the test database and its access permission.​
  1. Enter y and press the Enter key to reload the authorization table.​
Step 3: Install and configure PHP​
  1. Execute the following command to add and update the EPEL repository.​
  • ​
    wget -O /etc/yum.repos.d/epel.repo http://mirrors.ivolces.com/repo/epel-7.repo​
    ​
  1. Execute the following command to add the Remi repository.​
  • ​
    rpm -Uvh https://mirrors.ivolces.com/remi/enterprise/remi-release-7.rpm​
    ​
  1. Execute the following command to install the yum-config-manager repository management tool.​
  • ​
    yum -y install yum-utils​
    ​
  1. Execute the following command to enable the specified Remi PHP 7.0 repository by using yum-config-manager.​
  • ​
    yum-config-manager --enable remi-php70​
    ​
  1. Execute the following command to install PHP 7.0 and common modules.​
  • ​
    yum -y install php php-mysqlnd php-fpm​
    ​
  1. Execute the following command to start the PHP-FPM service.​
  • ​
    systemctl start php-fpm​
    ​
  1. Execute the following command to specify that the PHP-FPM service automatically starts upon instance startup.​
  • ​
    systemctl enable php-fpm​
    ​
  1. Execute the following command to view the PHP version.​
  • ​
    php -v​
    ​
  • If the following output is displayed, PHP is installed.​
  • ​
    PHP 7.0.33 (cli) (built: Jun 7 2022 14:11:00) ( NTS )​
    Copyright (c) 1997-2017 The PHP Group​
    Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies​
    ​
Step 4: Access LNMP​
  1. Execute the following command to create a test file.​
  • ​
    vim <Website root directory>/phpinfo.php​
    ​
  • Replace <website root directory> in the command with the website root directory that you configured in the Nginx configuration file default.conf. In the example provided in this topic, /usr/share/nginx/html is used. The command used in this topic is vim /usr/share/nginx/html/phpinfo.php.​
  1. Press the i key to enter edit mode, and then enter the following content.​
  • ​
    <?php echo phpinfo(); ?>​
    ​
  1. Press Esc, enter :wq, and then press the Enter key to save the file and return to the CLI interface.​
  1. In your local browser, enter http://<ECS instance EIP>/phpinfo.php to access LNMP.​
​
Last updated: 2026.06.18 17:48:25