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 (Ubuntu 18.04)
Copy page
Download PDF
Setting up an LNMP environment (Ubuntu 18.04)
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 an Ubuntu 18.04 instance.​
Software versions​
  • Operating system: In the example provided in this topic, Ubuntu 18.04 is used.​
  • Nginx: A high-performance, lightweight web server that supports HTTP and reverse proxy. In the example provided in this topic, Nginx 1.14.0 is used.​
  • MySQL/MariaDB: A database management system. In the example provided in this topic, MariaDB 15.1 is used.​
  • PHP: A server-side scripting language embedded in HTML documents. In the example provided in this topic, PHP 7.2.24 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 update the package repository.​
  • ​
    apt update && apt upgrade -y​
    ​
  1. Execute the following command to install Nginx.​
  • ​
    apt install -y nginx​
    ​
  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.14.0 (Ubuntu)​
    ​
  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.​
  • If the following output is displayed, Nginx is set up successfully.​
  • ​image​​
Step 2: Install and configure MariaDB​
  1. Execute the following command to install MariaDB.​
  • ​
    apt install -y 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 10.1.48-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2​
    ​
  1. Execute the following command to specify that the MariaDB service automatically starts upon instance startup.​
  • ​
    systemctl enable 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.​
Step 3: Install and configure PHP​
  1. Execute the following command to install PHP.​
  • ​
    apt install -y php php-fpm php-mysql​
    ​
  1. Execute the following command to view the PHP version.​
  • ​
    php -v​
    ​
  • If the following output is displayed, PHP is installed.​
  • ​
    PHP 7.2.24-0ubuntu0.18.04.13 (cli) (built: Jul 6 2022 12:23:22) ( NTS )​
    Copyright (c) 1997-2018 The PHP Group​
    Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies​
    with Zend OPcache v7.2.24-0ubuntu0.18.04.13, Copyright (c) 1999-2018, by Zend Technologies​
    ​
By default, PHP starts automatically upon instance startup. You do not need to perform any additional configuration.​
  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.​
  • ​alt​​
  1. Execute the following command to back up the configuration file in the configuration file directory.​
tip​
Nginx loads configuration files with any names in the /sites-enabled directory, causing conflicts. Do not back up the configuration file in the /etc/nginx/sites-enabled/ directory.​
  • ​
    cd /etc/nginx/sites-enabled/ ​
    cp default /tmp/default.conf​
    ​
  1. Execute the following command to modify the original configuration file.​
  • ​
    vim default​
    ​
  1. Press the i key to enter edit mode and modify the following content in index.​
  • ​
    root /var/www/html;#Set this path as the default website root directory for Nginx, which you can change as needed.​
    # Add index.php to the list if you are using PHP​
    index index.php index.html index.htm index.nginx-debian.html;​
    }​
    ​
  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$ {​
    include snippets/fastcgi-php.conf;​
    fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;#Modify the PHP version to the installed PHP version, in this example, it is 7.2.​
    }​
    ​
  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 restart the Nginx service.​
  • ​
    systemctl restart nginx​
    ​
Step 4: Access LNMP​
  1. Execute the following command to create a test file.​
  • ​
    vim <website root directory>/phpinfo.php #Replace <website root directory> with the website root directory you configured in the Nginx configuration file default.conf.​
    ​
  • In the example provided in this topic, the vim /var/www/html/phpinfo.php command is executed.​
  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 Enter to save the modifications and close the file.​
  1. In your local browser, enter http://<ECS instance EIP>/phpinfo.php to access LNMP.​
  • If the following output is displayed, LNMP is set up successfully.​
  • ​alt​​
​
Last updated: 2026.06.18 17:48:25