Alibaba Cloud International Agent: Build LNMP Environment on Alibaba Cloud CentOS/Ubuntu Server

cloud 2026-06-09 阅读 16
1

In the circle of Web development and server operations, the word "LNMP" is a required course for almost every architect and back-end development.

The so-called

LNMP

, that is

L

inux (operating system)

N

GINX (High Performance Web Server)

M

ySQL (relational database)

P

The golden combination of hp (scripting language). Whether it is to build a WordPress blog, to build a micro-mall independent station, or to run a complex enterprise API interface, LNMP is recognized as the most resource-saving and concurrent base in China.

Many newcomers bought Aliyun's ECS server and looked at the cold Linux black command line (terminal) without knowing where to start. Many old tutorials on the Internet five or six years ago, the software version in them has long been out of date, and errors are always reported according to the code.

In today's article, let's abandon rigid official documents and mysterious technical terms and adopt the vernacular style of real people leading the way,

Hand in hand, take you to build the latest and most stable LNMP environment in 2026 from scratch on Aliyun's most commonly used CentOS or Ubuntu system.

.

Preparation: Open up a secure channel for Alibaba Cloud

Before logging into the server and typing commands, there are two very important but easily overlooked things that must be done first.

Alibaba Cloud International Reseller

Log in to your server: Use the computer's own terminal (Mac/Linux) or third-party tools such as PuTTY and Xshell(Windows) to log in through ssh root @ your Aliyun public network IP.

Release Aliyun security group (big pit warning!): Aliyun's server completely locks external network access by default. If the security group is not configured, you will not be able to open the web page if you blow Nginx up again. Practical operation method: log on to ariyun console-> find your ECS instance-> click [security group] -> [configuration rule] -> allow port 80 (HTTP), port 443 (HTTPS) and 3306 ports convenient for you to connect to the database in the inbound direction.

When ready, officially enter the installation steps.

Step 1: Environment Initialization and Update (System Selection)

As the current mainstream system of Aliyun is divided

CentOS (and Anolis/Alibaba Cloud Linux developed by Alibaba)

and

Ubuntu

The two camps have different package managers (install commands). In the following steps, I will give the commands of the two systems at the same time. You can "check in" according to the system you choose when purchasing the server.

CentOS / Alibaba Cloud Linux systems:

Bash

# Update the system software package to the latest

yum update -y

Ubuntu system:

Bas

h

# Update the local software source index and upgrade

apt update && apt upgrade -y

Step 2: Install Nginx (High Performance Web Server)

Nginx is responsible for meeting users' access requests at the front end.

1. Execute the installation command

CentOS/Alibaba Cloud Linux:Bashyum install nginx -y

Ubuntu:Bashapt install nginx -y

2. Start and set the boot self-start

No matter what the system, enter the following two lines of commands to start Nginx and ensure that it will come back to life after the server restarts:

Bash

systemctl start nginx

systemctl enable nginx

3. Verification results

Open your computer browser and enter directly in the address bar.

Your Alibaba Cloud public IP address

(For example:

http://123.45.67.89

). If the screen appears "Welcome to nginx!" The white web page, congratulations, Nginx successfully employed!

Step 3: Install MySQL (Data Depot)

Because the original MySQL is heavier and is better performing and fully compatible in many Linux software sources

MariaDB

Instead, we directly install the most mainstream MySQL/MariaDB here.

1. Execute the installation command

CentOS/Alibaba Cloud Linux:

yum install mariadb-server mariadb -y

Ubuntu:Bashapt install mysql-server -y

2. Start the service

Bash

# CentOS Boot MariaDB

systemctl start mariadb && systemctl enable mariadb

# Ubuntu starts MySQL

systemctl start mysql && systemctl enable mysql

3. Initialize database security settings (required for beginners)

The database just installed is "streaked" and has no password. You must run the following command to initialize the security configuration:

Bash

mysql_secure_installation

The system will pop up a series of questions and answers, please respond at the following pace:

Enter current password for root: Enter directly (because there is no password initially).

Set root password? [Y/n]: Enter Y and set your

Database super administrator password (take a small notebook to remember, don't forget).

For subsequent Remove anonymous users?(to delete anonymous users), Disallow root login remotely?(to prohibit remote root login), and Remove test database?(to delete test libraries), enter Y to enter the carriage return.

Step 4: Install PHP (the engine that makes web pages move)

Nginx can only handle static HTML pages. If it encounters dynamic requests such as user login and database reading, it must outsource the work to PHP.

In order to ensure performance, we install the current mainstream and healthy life cycle.

PHP 8.x

Version.

1. Install PHP and core components

CentOS/Alibaba Cloud Linux (need to install an enterprise extension source first):Bashyum install epel-release -y and then install PHP and core extensions that communicate with Nginx/MySQL: Bashyum install PHP php-fpm php-mysqlnd php-json php-gd php-xml -y

Ubuntu:Bashapt install php php-fpm php-mysql -y

2. Start PHP-FPM

PHP-FPM is a process manager that silently waits for Nginx to dispatch tasks in the background.

Bash

systemctl start php-fpm

systemctl enable php-fpm

Step 5: Get through the second pulse of Ren Du-Configure Nginx to connect PHP

Now, the four components are installed, but they are still "islands" of their own ". We need to modify Nginx's configuration file to tell it: "Whenever we encounter

.php

At the end of the request, it is thrown to the background PHP-FPM processing ".

1. Find and modify the default profile

Typically, the default configuration file is in

/etc/nginx/nginx.conf

Or

/etc/nginx/conf.d/default.conf

. We open it with the system's own text editor:

Bash

vi /etc/nginx/conf.d/default.conf

(If the new system does not have this file, you can directly create a new one and paste the following standard configuration into it.)

:

server {

listen 80;

server_name

localhost;# Change to your domain name later

root /usr/share/nginx/html;# your website root

index index.php index.html index.htm;

location / {

try_files $uri $uri/ =404;

}

# Core: Get through the key configuration of the PHP-FPM

location ~ \.php$ {

fastcgi_pass 127.0.0.1:9000;# or unix:/run/php/php8.x-fpm.sock

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params;

}

}

Save and exit (in the vi editor, press

Escape

, input

:wq

enter).

2. Restart Nginx to make the configuration take effect

You can knock before restarting.

nginx -t

Check the configuration file just changed for syntax errors. If not, restart decisively:

Bash

Alibaba Cloud International Reseller

systemctl restart nginx

Step 6: Ultimate Test-Run through the whole line

All the hard work will be tested in this step.

Let's go to the root directory of the website (just in the configuration file

/usr/share/nginx/html

), create a new test file:

Bash

vi /usr/share/nginx

/html/info.php

Write these three lines of classic PHP test code in the file:

PHP

<?php

phpinfo();

?>

Save to exit.

Now, open the computer browser again and enter your

Alibaba Cloud public IP address/info.php

(For example:

ht

tp://123.45.67.89/info.php

).

If you can see a very detailed one on the screen with a purple or blue tone

PHP Version 8.x

The configuration of the large table, which proves that:

Your Nginx successfully handed over the request to PHP,PHP was running normally, and the entire LNMP environment was completely declared successful!

Conclusion and Daily Maintenance Pit Avoidance Guide

Congratulations! Here you have completed the transformation from small white to primary operation and maintenance. Your Alibaba Cloud server already has a strong website hosting capacity.

Finally send you three production environment daily maintenance iron law:

Delete the test file after the test: after the test is successful, be sure to run rm -f /usr/share/nginx/html/info.php immediately to delete the test file. Because this page exposes too much sensitive configuration information of your server, leaving it online is equivalent to sending invitations to hackers.

Three lines of commands are familiar with by heart: when you modify the code or encounter a web page that cannot be opened, you can skillfully use the following three lines of restart commands to troubleshoot: systemctl restart nginx (restart Web service) systemctl restart php-fpm (restart PHP process) systemctl restart mysql (or mariadb, restart database)

The foundation has been laid. Next, you can safely plug your source code, WordPress or various open source frameworks into it and start your journey of exploding orders or earning traffic!

Alibaba Cloud International Reseller

3
← 返回新闻中心