手动搭建 LNMP 环境(CentOS 7)教程
操作场景
示例软件版本
前提条件
操作步骤
步骤1:登录 Linux 实例
步骤2:安装 Nginx
/etc/yum.repos.d/
下创建 nginx.repo
文件。vi /etc/yum.repos.d/nginx.repo
2.按 i 切换至编辑模式,写入以下内容。
[nginx]
name = nginx repo
baseurl = https://nginx.org/packages/mainline/centos/7/$basearch/
gpgcheck = 0
enabled = 1
3.按 Esc,输入 :wq,保存文件并返回。
yum install -y nginx
5.执行以下命令,打开 default.conf
文件。
vim /etc/nginx/conf.d/default.conf
default.conf
文件。server{...}
,并将 server
大括号中相应的配置信息替换为如下内容。用于取消对 IPv6 地址的监听,同时配置 Nginx,实现与 PHP 的联动。server {
listen 80;
root /usr/share/nginx/html;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
#
location / {
index index.php index.html index.htm;
}
#error_page 404 /404.html;
#redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
#pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
systemctl start nginx
10.执行以下命令,设置 Nginx 为开机自启动。
systemctl enable nginx
11.在本地浏览器中访问以下地址,查看 Nginx 服务是否正常运行。
http://云服务器实例的公网 IP
显示如下,则说明 Nginx 安装配置成功。
步骤3:安装数据库
1.执行以下命令,查看系统中是否已安装 MariaDB。
rpm -qa | grep -i mariadb
返回结果类似如下内容,则表示已存在 MariaDB。
为避免安装版本不同造成冲突,请执行以下命令移除已安装的 MariaDB。
yum -y remove 包名
若返回结果为空,则说明未预先安装,则执行下一步。
2.执行以下命令,在 /etc/yum.repos.d/
下创建 MariaDB.repo
文件。
vi /etc/yum.repos.d/MariaDB.repo
3.按 i 切换至编辑模式,写入以下内容,添加 MariaDB 软件库。
# MariaDB 10.11 RedHatEnterpriseLinux repository list - created 2025-04-08 16:06 UTC
# https://mariadb.org/download/
[mariadb]
name = MariaDB
# rpm.mariadb.org is a dynamic mirror if your preferred mirror goes offline. See https://mariadb.org/mirrorbits/ for details.
# baseurl = https://rpm.mariadb.org/10.11/rhel/$releasever/$basearch
baseurl = https://mirrors.tencentyun.com/mariadb/yum/10.11/rhel/$releasever/$basearch
module_hotfixes = 1
# gpgkey = https://rpm.mariadb.org/RPM-GPG-KEY-MariaDB
gpgkey = https://mirrors.tencentyun.com/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgcheck = 1
yum -y install MariaDB-client MariaDB-server
6.执行以下命令,启动 MariaDB 服务。
systemctl start mariadb
7.执行以下命令,设置 MariaDB 为开机自启动。
systemctl enable mariadb
8.执行以下命令,验证 MariaDB 是否安装成功。
mysql
显示结果如下,则成功安装。
步骤4:安装配置 PHP
yum install -y epel-release
rpm -Uvh https://mirrors.tencent.com/remi/enterprise/remi-release-7.rpm
2.运行以下命令,启用 PHP 8.3仓库。
yum-config-manager --enable remi-php83
3.执行以下命令,安装 PHP 8.3 所需要的包。
yum install -y php php-cli php-fpm php-mysqlnd php-zip php-devel php-gd php-mcrypt php-mbstring php-curl php-xml php-pear php-bcmath php-json
4.执行以下命令,启动 php-fpm 服务。
systemctl start php-fpm
5.执行以下命令,设置 php-fpm 服务为开机自启动。
systemctl enable php-fpm
验证环境配置
echo "<?php phpinfo(); ?>" >> /usr/share/nginx/html/index.php
2.执行以下命令,重启 Nginx 服务。
systemctl restart nginx
3.在本地浏览器中访问如下地址,查看环境配置是否成功。
http://云服务器实例的公网 IP/index.php
显示结果如下,则说明环境配置成功。