- Documentation
Elastic Compute Service
Elastic Compute Service
Best Practices
Self-service website creation
Building environments
Setting up an LNMP environment (Ubuntu 18.04)
Building environments
Setting up an LNMP environment (Ubuntu 18.04)
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
- Execute the following command to update the package repository.
- Execute the following command to install Nginx.
- Execute the following command to view the Nginx version.
- If the following output is displayed, Nginx is installed.
- Execute the following command to specify that the Nginx service automatically starts upon instance startup.
- In the local browser, enter http://<ECS instance EIP>/ to access Nginx.
- If the following output is displayed, Nginx is set up successfully.
-
Step 2: Install and configure MariaDB
- Execute the following command to install MariaDB.
- Execute the following command to view the MariaDB version.
- If the following output is displayed, MariaDB is installed.
- Execute the following command to specify that the MariaDB service automatically starts upon instance startup.
- Execute the following command to check whether the MariaDB service is started.
- If Active: active (running) is displayed in the output, the MariaDB service is started.
- Execute the following command to perform MariaDB security operations and specify the password.
- Press the Enter key to confirm the password.
- Enter y and press the Enter key to confirm modification of the database password for the root user.
- Specify the MariaDB password and press the Enter key. The password you enter is not displayed by default.
- 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.
- Enter y and press the Enter key to remove the anonymous user.
- Enter y and press the Enter key to prohibit remote login by the root account.
- Enter y and press the Enter key to delete the test database and its access permission.
- Enter y and press the Enter key to reload the authorization table.
Step 3: Install and configure PHP
- Execute the following command to install PHP.
- Execute the following command to view the PHP version.
- If the following output is displayed, PHP is installed.
By default, PHP starts automatically upon instance startup. You do not need to perform any additional configuration.
- Execute the following command to view the path of the Nginx configuration file.
- In the following output, the include configuration item indicates the path of the file.
-
- 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.
- Execute the following command to modify the original configuration file.
- Press the i key to enter edit mode and modify the following content in index.
- Remove the comment markers #at the beginning of the lines inside the curly braces of location ~ \.php$ and modify the following content.
- Press the Esc key, enter :wq, and then press Enter to save the modifications and close the file.
- Execute the following command to restart the Nginx service.
Step 4: Access LNMP
- Execute the following command to create a test file.
- In the example provided in this topic, the vim /var/www/html/phpinfo.php command is executed.
- Press the i key to enter edit mode, and then enter the following content.
- Press Esc, enter :wq, and then press Enter to save the modifications and close the file.
- 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.
-
Last updated: 2026.06.18 17:48:25