Scenarios
The LNMP environment is a website server architecture consisting of Nginx, MySQL or MariaDB, and PHP running on Linux. This document describes how to manually build the LNMP environment on a Cloud Virtual Machine (CVM) instance of Tencent Cloud.
To manually build the LNMP environment, you should be familiar with common Linux commands and understand the usage and version compatibility of the software to be installed.
Example Software Versions
The software versions and descriptions of the LNMP environment to be built in this document are as follows:
Linux: Linux operating system. Ubuntu 22.04 is used as an example in this document.
Nginx: a web server program. It is used to parse web programs. Nginx 1.18.0 is used as an example in this document.
MySQL: a database management system. MySQL 8.0.41 is used as an example in this document.
PHP: a program for generating web pages on the web server. PHP 8.1.2 is used as an example in this document.
Prerequisites
Operation Steps
Step 1: Logging In to a Linux Instance
Step 2: Installing and Configuring Nginx
1. Run the following command to update the software packages in the Ubuntu system.
2. Run the following command to install Nginx.
sudo apt -y install nginx
3. Run the following command to view the Nginx version.
If the returned result is similar to the following content, it indicates that Nginx has been successfully installed.
nginx version: nginx/1.18.0 (Ubuntu)
4. Run the following command to modify the default configuration file of Nginx.
sudo vim /etc/nginx/sites-enabled/default
5. Press i to switch to the edit mode.
6. Locate server{...} and replace it with the following content.
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location ~ \\.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
}
}
7. After the content is entered, press Esc, enter :wq, save the file, and return to the command line.
8. Run the following command to restart the Nginx service.
sudo systemctl restart nginx
9. Run the following command to create the homepage file index.html.
sudo vi /var/www/html/index.html
10. Press i to switch to the edit mode and enter the following content:
11. After the content is entered, press Esc, enter :wq, save the file, and return to the command line.
12. In your browser, access the public IP address of the Ubuntu CVM instance to check if the Nginx service is running properly.
13. If the result is as shown in the following figure, it indicates that Nginx has been successfully installed and configured.
Step 3: Installing and Configuring MySQL
1. Run the following command to install MySQL.
sudo apt -y install mysql-server
2. Run the following command to view the MySQL version.
If the returned result is similar to the following content, it indicates that MySQL has been successfully installed.
mysql Ver 8.0.41-0ubuntu0.22.04.1 for Linux on x86_64 ((Ubuntu))
3. Run the following command to log in to MySQL for the first time.
Note:
When you log in to MySQL for the first time, the system prompts you to enter the password. If you do not want to enter the password, you can directly press Enter to access MySQL.
If the result is as shown in the following figure, access to MySQL is successful.
4. Run the following command to change the root password.
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
5. Run the following command to apply the configuration.
6. Run the following command to exit MySQL.
Step 4: Installing and Configuring PHP
1. Run the following command to install PHP.
sudo apt -y install php-fpm
2. Run the following command to view the PHP version.
If the returned result is similar to the following content, it indicates that PHP has been successfully installed.
PHP 8.1.2-1ubuntu2.19 (cli) (built: Sep 30 2024 16:25:25) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
with Zend OPcache v8.1.2-1ubuntu2.19, Copyright (c), by Zend Technologies
3. Run the following command to create the phpinfo.php file in the Nginx website root directory.
sudo vim <website root directory>/phpinfo.php
<website root directory> is a variable that can be viewed in the Nginx configuration file, which is the default file /etc/nginx/sites-enabled/default used in this document. You can run the cat /etc/nginx/sites-enabled/default command to view the file content, where the /var/www/html section is the website root directory, as shown in the following figure.
Therefore, the corresponding command to be run is as follows:
sudo vim /var/www/html/phpinfo.php
4. Press i to enter the edit mode and add the following configuration information.
The phpinfo() function displays all configuration information on PHP.
After the content is entered, press Esc, enter :wq, save the file, and return to the command line.
5. Run the following command to start PHP.
sudo systemctl start php8.1-fpm
Verifying the Environment Configuration
In the browser, enter http://<Ubuntu public IP address of the instance >/phpinfo.php to access the environment.
As shown in the following figure, if you can view the PHP configuration information page, it indicates that the LNMP environment has been successfully built.
Related Operations
FAQs
If you encounter problems when using CVM, see the following documents to analyze and solve them based on your actual situation.