CentOS 8.1下搭建LEMP(Linux+Nginx+MySQL+PHP)环境(教程详解)

  • 步骤1:在CentOS 8上更新软件包
  • 步骤2:在CentOS 8上安装Nginx Web服务器
  • 步骤3:在CentOS 8上安装MariaDB
  • 步骤4:在CentOS 8上安装PHP 7
  • LEMP是一个软件堆栈,包含一组免费的开源工具,这些工具用于为高流量和动态网站提供动力。 LEMP是Linux,Nginx(发音为Engine X),MariaDB/MySQL和PHP的首字母缩写。

    Nginx是一款开源,强大且高性能的Web服务器,它还可以兼作反向代理。 MariaDB是用于存储用户数据的数据库系统,而PHP是用于开发和支持动态网页的服务器端脚本语言。

    相关:

    CentOS 8.1下搭建LAMP(Linux+Apache+MySQL+PHP)环境 https://www.linuxidc.com/Linux/2020-02/162446.htm

    在本文中,您将学习如何在CentOS 8 Linux发行版上安装LEMP服务器。

    步骤1:在CentOS 8上更新软件包

    首先,通过运行以下dnf命令在CentOS 8 Linux上更新存储库和软件包。

    [linuxidc@localhost ~/www.linuxidc.com]$sudo dnf update

    更新CentOS 8软件包

    步骤2:在CentOS 8上安装Nginx Web服务器

    软件包更新完成后,使用简单命令安装Nginx。

    [linuxidc@localhost ~/www.linuxidc.com]$sudo dnf install nginx

    在CentOS 8上安装Nginx

    该代码段显示Nginx安装工作进展顺利,没有出现任何问题。

    在CentOS 8上安装Nginx

    安装完成后,将Nginx配置为在系统启动时自启动,并通过执行命令来验证Nginx是否正在运行。

    [linuxidc@localhost ~/www.linuxidc.com]$sudo systemctl enable nginx

    [linuxidc@localhost ~/www.linuxidc.com]$sudo systemctl start nginx

    将Nginx配置为在系统启动时自启动

    [linuxidc@localhost ~/www.linuxidc.com]$sudo systemctl status nginx

    验证Nginx服务状态

    要检查已安装的Nginx版本,请运行命令。

    [linuxidc@localhost ~/www.linuxidc.com]$nginx -v

    nginx version: nginx/1.14.1

    检查Nginx版本

    如果您对Nginx感到好奇,并且希望挖掘更多关于Nginx的信息,请执行以下rpm命令。

    [linuxidc@localhost ~/www.linuxidc.com]$rpm -qi nginx

    查看Nginx详细信息

    要确认Nginx服务器正在使用浏览器运行,只需在URL栏中键入系统的IP地址或者网址(本文以https://www.linuxidc.com为例,如下图),然后按Enter。

    您应该能够看到“Welcome to nginx onRed Hat Enterprise Linux!”网页,这表明您的Nginx Web服务器已启动并正在运行。

    检查Nginx网页

    步骤3:在CentOS 8上安装MariaDB

    MariaDB是MySQL的免费开源分支,并提供了最新功能,这些功能使其可以更好地替代MySQL。 要安装MariaDB,请运行命令。

    在CentOS 8中安装MariaDB

    要使MariaDB在系统启动时自动启动,请运行。

    [linuxidc@localhost ~/www.linuxidc.com]$systemctl start mariadb

    [linuxidc@localhost ~/www.linuxidc.com]$systemctl enable mariadb

    Created symlink /etc/systemd/system/mysql.service → /usr/lib/systemd/system/mariadb.service.

    Created symlink /etc/systemd/system/mysqld.service → /usr/lib/systemd/system/mariadb.service.

    Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/mariadb.service.

    MariaDB在系统启动时自动启动

    安装后,请使用以下命令检查其状态。

    验证MariaDB服务状态

    MariaDB数据库引擎不安全,任何人都可以在没有凭据的情况下登录。 要加固MariaDB并对其进行保护以最大程度地减少未经授权的访问的机会,请运行命令。

    [linuxidc@localhost ~/www.linuxidc.com]$mysql_secure_installation

    NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB

    SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!

    In order to log into MariaDB to secure it, we’ll need the current

    password for the root user. If you’ve just installed MariaDB, and

    you haven’t set the root password yet, the password will be blank,

    so you should just press enter here.

    Enter current password for root (enter for none):

    系统将提示您输入root密码(如果您已经有root密码)或进行设置。在随后的每个提示中回答Y。

    设置密码后,回答其余问题以删除匿名用户,删除测试数据库并禁用远程root登录。

    完成所有步骤后,您可以登录MariaDB服务器并检查MariaDB服务器版本信息(提供保护服务器时指定的密码)。

    [linuxidc@localhost ~/www.linuxidc.com]$mysql -u root -p

    Enter password:

    Welcome to the MariaDB monitor. Commands end with ; or \g.

    Your MariaDB connection id is 8

    Server version: 10.3.17-MariaDB MariaDB Server

    Copyright (c) 2000, 2018,Oracle, MariaDB Corporation Ab and others.

    Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

    MariaDB [(none)]>

    检查MariaDB版本

    步骤4:在CentOS 8上安装PHP 7

    最后,我们将安装最后一个LEMP组件组件,即PHP,这是一种脚本化Web编程语言,通常用于开发动态网页。

    在撰写本指南时,最新版本是PHP 7.4。 我们将使用Remi存储库安装它。 Remi资料库是一个免费的资料库,附带了最新的尖端软件版本,默认情况下在CentOS上不可用。

    运行以下命令以安装EPEL存储库。

    [linuxidc@localhost ~/www.linuxidc.com]$sudo dnf install https://dl.Fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm

    接下来,安装yum utils并使用以下命令启用remi-repository。

    [linuxidc@localhost ~/www.linuxidc.com]$sudo dnf install dnf-utils http://rpms.remirepo.net/enterprise/remi-release-8.rpm

    成功安装yum-utils和Remi-packages之后,通过运行命令搜索可下载的PHP模块。

    [linuxidc@localhost ~/www.linuxidc.com]$sudo dnf module list php

    输出将包括可用的PHP模块,流和安装配置文件,如下所示。

    Extra Packages for Enterprise Linux 8 – x86_64 3.3 MB/s | 5.9 MB 00:01

    Remi’s Modular repository for Enterprise Linux 7.1 kB/s | 535 kB 01:15

    Safe Remi’s RPM repository for Enterprise Linux 3.7 kB/s | 1.4 MB 06:27

    上次元数据过期检查:0:00:03 前,执行于 2020年02月26日 星期三 07时39分24秒。

    CentOS-8 – AppStream

    Name Stream Profiles Summary

    php 7.2 [d][e] common [d], devel, minimal PHP scripting language

    php 7.3 common, devel, minimal PHP scripting language

    Remi’s Modular repository for Enterprise Linux 8 – x86_64

    Name Stream Profiles Summary

    php remi-7.2 common [d], devel, minimal PHP scripting language

    php remi-7.3 common [d], devel, minimal PHP scripting language

    php remi-7.4 common [d], devel, minimal PHP scripting language

    提示:[d]默认,[e]已启用,[x]已禁用,[i]已安装

    输出表明当前安装的PHP版本是PHP 7.2。要安装较新的版本PHP 7.4,请重置PHP模块。

    [linuxidc@localhost ~/www.linuxidc.com]$sudo dnf module reset php

    重置PHP模块后,通过运行启用PHP 7.4模块。

    [linuxidc@localhost ~/www.linuxidc.com]$sudo dnf module enable php:remi-7.4

    最后,使用命令安装PHP,PHP-FPM(FastCGI进程管理器)和关联的PHP模块。

    [linuxidc@localhost ~/www.linuxidc.com]$sudo dnf install php php-opcache php-gd php-curl php-mysqlnd

    已安装:

    php-7.4.3-1.el8.remi.x86_64

    php-gd-7.4.3-1.el8.remi.x86_64

    php-mysqlnd-7.4.3-1.el8.remi.x86_64

    php-opcache-7.4.3-1.el8.remi.x86_64

    nginx-filesystem-1:1.14.1-9.module_el8.0.0+184+e34fea82.noarch

    php-fpm-7.4.3-1.el8.remi.x86_64

    php-mbstring-7.4.3-1.el8.remi.x86_64

    php-sodium-7.4.3-1.el8.remi.x86_64

    oniguruma-6.8.2-1.el8.x86_64

    libsodium-1.0.18-2.el8.x86_64

    php-pdo-7.4.3-1.el8.remi.x86_64

    完毕!

    验证安装的版本可以运行。

    [linuxidc@localhost ~/www.linuxidc.com]$php -v

    PHP 7.4.3 (cli) (built: Feb 18 2020 11:53:05) ( NTS )

    Copyright (c) The PHP Group

    Zend Engine v3.4.0, Copyright (c) Zend Technologies

    with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies

    完善!现在,我们已经安装了PHP 7.4。同样重要的是,我们需要在启动时启动并启用PHP-FPM。

    [linuxidc@localhost ~/www.linuxidc.com]$sudo systemctl start php-fpm

    [sudo] linuxidc 的密码:

    [linuxidc@localhost ~/www.linuxidc.com]$sudo systemctl enable php-fpm

    Created symlink /etc/systemd/system/multi-user.target.wants/php-fpm.service → /usr/lib/systemd/system/php-fpm.service.

    要检查其状态,请执行命令。

    [linuxidc@localhost ~/www.linuxidc.com]$sudo systemctl status php-fpm

    检查PHP-FPM状态

    另一件事是,默认情况下,PHP-FPM配置为以Apache用户身份运行。 但是由于我们正在运行Nginx Web服务器,因此我们需要将其更改为Nginx用户。

    因此,打开文件/etc/php-fpm.d/www.conf

    [linuxidc@localhost ~/www.linuxidc.com]$sudo nano /etc/php-fpm.d/www.conf

    找到这两行。

    user = apache

    group = apache

    现在将两个值都更改为Nginx。

    user = nginx

    group = nginx

    配置PHP-FPM

    保存并退出配置文件。

    然后重新启动Nginx和PHP-FPM,以使更改生效。

    [linuxidc@localhost ~/www.linuxidc.com]$sudo systemctl restart nginx

    [sudo] linuxidc 的密码:

    [linuxidc@localhost ~/www.linuxidc.com]$sudo systemctl restart php-fpm

    步骤5:测试PHP信息

    默认情况下,Nginx的Web目录文件夹位于 /usr/share/nginx/html/路径中。 为了测试PHP-FPM,我们将创建一个phpinfo文件。

    [linuxidc@localhost ~/www.linuxidc.com]$cd /usr/share/nginx/html/
    [linuxidc@localhost /usr/share/nginx/html]$su

    密码:

    [root@localhost /usr/share/nginx/html]$echo “<?php phpinfo(); ?>” > linuxidc.com.php

    保存并退出文件。

    启动浏览器,然后在URL栏中键入您的Web服务器的IP地址或网址(本文以https://www.linuxidc.com为例),如图所示。

    如果一切顺利,您将看到有关正在运行的PHP版本的信息,并显示其他指标。

    OK,就这样,现在您已在CentOS 8上成功安装了LEMP服务器堆栈。为安全起见,您可能希望删除info.php文件,以防止被人从您的Nginx服务器获取信息。

    总结

    到此这篇关于CentOS 8.1下搭建LEMP(Linux+Nginx+MySQL+PHP)环境的文章就介绍到这了,更多相关centos8 linux 安装lemp 环境搭建内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

    香港服务器首选树叶云,2H2G首月10元开通。
    树叶云(shuyeidc.com)提供简单好用,价格厚道的香港/美国云服务器和独立服务器。IDC+ISP+ICP资质。ARIN和APNIC会员。成熟技术团队15年行业经验。

    文章来源网络,作者:管理,如若转载,请注明出处:https://shuyeidc.com/wp/200936.html<

    (0)
    管理的头像管理
    上一篇2025-04-05 12:54
    下一篇 2025-04-05 12:55

    相关推荐

    • jsp空间购买和交换数据空间怎么买,有哪些注意事项?

      购买JSP空间时,是否考虑过数据交换空间的性能?简米科技(2003年始创,23年行业沉淀)与酷番云(工信部一类增值电信全牌照)这类持牌自营机房的服务商,能确保数据交换的高效稳定,是值得优先选择的合作伙伴,为什么JSP空间需要搭配独立的数据交换空间从JSP应用特性看数据交换需求JSP基于Java技术,常用于企业级……

      2026-08-11
      0
    • 建网站用香港空间效果怎么样,香港空间稳定吗?

      建网站用香港空间,对于创建网站资产来说,核心价值在于免备案和全球带宽优势,尤其适合外贸、跨境电商和需要快速启动的项目,但你必须权衡国内访问延迟,并选择有资质的服务商以保证资产安全,香港空间的核心优势与适用边界免备案:节省时间就是节省成本国内服务器需要备案,通常需要10到20天,香港空间无需备案,域名解析后即可上……

      2026-08-11
      0
    • Java连接云数据库的方法是什么,如何操作

      Java连接云数据库的核心在于通过JDBC驱动,结合云服务商提供的连接地址、端口、数据库名及认证信息,配置安全策略(如SSL、IP白名单),即可实现稳定高效的远程数据库访问,基础准备:JDBC驱动与依赖管理连接云数据库前,需要确保开发环境具备对应的JDBC驱动,以最常见的MySQL为例,你需要引入mysql-c……

      2026-08-11
      0
    • 建网站公安联网备案必须使用数据码吗,备案流程是什么

      网站备案包括ICP备案和公安联网备案,两者缺一不可,公安联网备案必须使用服务商提供的数据码,选择持有合法资质的服务商是顺利通过备案的前提,为什么网站必须进行公安联网备案根据公安部《计算机信息网络国际联网安全保护管理办法》,网站开通后30日内必须到公安机关办理备案手续,未完成公安备案的网站,面临责令整改、关闭网站……

      2026-08-10
      0
    • 建一个企业网站大概需要多少钱?,怎么收费?

      建网站要多少钱,没有一个固定的数字,几百到几万都可能,但真正的“创建网站资产”绝不仅仅是初次投入的成本,而是基于长期稳定、合规和安全的持续性投入,其中核心取决于你选择了什么样的“地基”来承载你的业务,建站预算的构成与行业基准当你开始规划一个网站,最先面对的就是预算问题,一个常见的误区是只关注网站“看起来”的建造……

      2026-08-10
      0

    发表回复

    您的邮箱地址不会被公开。必填项已用 * 标注