安装 Apache, MariaDB, PHP (LAMP Stack) 在 CentOS 8

[ad_1]

堆栈是 大号inux, 一个修补网络服务器, ySQL/咏叹调数据库, 生命值。 本分步指南说明了如何安装 Apache, MariaDB, PHP (LAMP Stack) 在 CentOS 8 最小服务器。 LAMP 堆栈用于部署基于 Web 的应用程序和托管动态网站。 我在 CentOS 8 上测试了本指南,但是下面给出的步骤也适用于 RHEL 8 服务器。 让我们开始吧。

内容

  1. 安装 Apache, MariaDB, PHP (LAMP Stack) 在 CentOS 8
    1. 1.安装 Apache CentOS 8 上的网络服务器
    2. 1.1。 测试 Apache 网络服务器
    3. 2. 在 CentOS 8 上安装 MariaDB 数据库服务器
    4. 2.1。 设置数据库root用户(管理员)密码
    5. 2.2. 登录 MariaDB 外壳
    6. 3. 在 CentOS 8 上安装 PHP
    7. 3.1。 配置 Apache 使用 PHP-FPM
    8. 3.2. 测试 PHP
    9. 帮助我们帮助您:

安装 Apache, MariaDB, PHP (LAMP Stack) 在 CentOS 8

这是我的测试机详细信息:

  • 操作系统 – CentOS 8 最小服务器 (8.1.1911 (Core))
  • IP地址 – 192.168.225.52/24
  • 主机名 – centos8.ostechnix.lan

下面给出的所有命令都应该运行为 “根” 用户。 如果您以普通用户身份登录,请使用 ‘sudo’ 在每个命令的前面。 这 # 符号代表我是 root 用户的命令。

笔记:

CentOS 8 的主要更新之一是默认包管理器已从 YUM 迁移到 DNF。 但是,如果您习惯了,您仍然可以在 CentOS 8 上使用 YUM。 DNF 的用法与 YUM 完全相同。 我为初学者编制了一份 DNF 命令示例列表。 如果您有兴趣了解如何使用 DNF,请查看以下链接。

  • 初学者的 DNF 命令示例

1.安装 Apache CentOS 8 上的网络服务器

安装 Apache 在 CentOS 8 上,以 root 用户身份运行以下命令:

# dnf install httpd

一次 Apache 已安装,启用并使用命令启动 apache 服务:

# systemctl enable httpd
# systemctl start httpd

或者,您可以使用以下 单行命令 一次性启用和启动apache服务。

# systemctl enable --now httpd

检查是否 Apache 服务是否正在运行,使用命令:

# systemctl status httpd

样本输出:

● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2020-02-07 12:25:08 IST; 39min ago
     Docs: man:httpd.service(8)
 Main PID: 1736 (httpd)
   Status: "Total requests: 6; Idle/Busy workers 100/0;Requests/sec: 0.00253; Bytes served/sec: 13>
    Tasks: 213 (limit: 11501)
   Memory: 25.7M
   CGroup: /system.slice/httpd.service
           ├─1736 /usr/sbin/httpd -DFOREGROUND
           ├─1873 /usr/sbin/httpd -DFOREGROUND
           ├─1874 /usr/sbin/httpd -DFOREGROUND
           ├─1875 /usr/sbin/httpd -DFOREGROUND
           └─1876 /usr/sbin/httpd -DFOREGROUND

Feb 07 12:25:08 centos8.ostechnix.lan systemd[1]: Starting The Apache HTTP Server...
Feb 07 12:25:08 centos8.ostechnix.lan systemd[1]: Started The Apache HTTP Server.
Feb 07 12:25:08 centos8.ostechnix.lan httpd[1736]: Server configured, listening on: port 80

如你看到的, Apache 服务已加载并正在运行。 它正在监听端口 80. 要查找正在侦听特定端口的服务,请参阅 本指南.

检查 Apache 版本:

# httpd -v
Server version: Apache/2.4.37 (centos)
Server built: Dec 23 2019 20:45:34

接下来,所有 Apache 服务器默认端口 80443 通过您的防火墙和路由器。

要允许 http 端口 80 通过防火墙,请运行:

# firewall-cmd --permanent --add-service=http

要允许 https 端口 443:

# firewall-cmd --permanent --add-service=https

使用命令重新启动防火墙以应用更改:

$ systemctl restart firewalld

1.1。 测试 Apache 网络服务器

我们刚刚完成了部署 Apache 网络服务器。 让我们继续检查它是否真的有效。 为此,请打开您的 Web 浏览器(在网络中的任何远程系统上,因为它是没有 GUI 的最小服务器)并导航到 – https://ip地址. 如果您在 CentOS 系统上安装了 GUI,您可以导航到 https://localhost 或者 https://ip地址/ 或者 https://域名.

如果一切正常,您将在浏览器上看到以下测试页面。

好, Apache 网络服务器正在工作!

LAMP 设置的下一步是安装数据库服务器。

2. 在 CentOS 8 上安装 MariaDB 数据库服务器

从终端运行以下命令在 CentOS 8 上安装 MariaDB:

# dnf install mariadb-server mariadb

安装 MariaDB 后,使用以下命令启用并启动 mariadb 服务:

# systemctl enable mariadb
# systemctl start mariadb

或者,使用以下单行命令立即启用和启动 mariadb 服务:

# systemctl enable --now mariadb

要验证 mariadb 服务是否已启动,请运行:

# systemctl status mariadb

样本输出:

● mariadb.service - MariaDB 10.3 database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2020-02-07 14:16:59 IST; 16s ago
     Docs: man:mysqld(8)
           https://mariadb.com/kb/en/library/systemd/
  Process: 5447 ExecStartPost=/usr/libexec/mysql-check-upgrade (code=exited, status=0/SUCCESS)
  Process: 5312 ExecStartPre=/usr/libexec/mysql-prepare-db-dir mariadb.service (code=exited, statu>
  Process: 5288 ExecStartPre=/usr/libexec/mysql-check-socket (code=exited, status=0/SUCCESS)
 Main PID: 5415 (mysqld)
   Status: "Taking your SQL requests now..."
    Tasks: 30 (limit: 11501)
   Memory: 84.6M
   CGroup: /system.slice/mariadb.service
           └─5415 /usr/libexec/mysqld --basedir=/usr

Feb 07 14:16:59 centos8.ostechnix.lan mysql-prepare-db-dir[5312]: Please report any problems at ht>
Feb 07 14:16:59 centos8.ostechnix.lan mysql-prepare-db-dir[5312]: The latest information about Mar>
Feb 07 14:16:59 centos8.ostechnix.lan mysql-prepare-db-dir[5312]: You can find additional informat>
Feb 07 14:16:59 centos8.ostechnix.lan mysql-prepare-db-dir[5312]: https://dev.mysql.com
Feb 07 14:16:59 centos8.ostechnix.lan mysql-prepare-db-dir[5312]: Consider joining MariaDB's stron>
Feb 07 14:16:59 centos8.ostechnix.lan mysql-prepare-db-dir[5312]: https://mariadb.org/get-involved/
Feb 07 14:16:59 centos8.ostechnix.lan mysqld[5415]: 2020-02-07 14:16:59 0 [Note] /usr/libexec/mysq>
Feb 07 14:16:59 centos8.ostechnix.lan mysqld[5415]: 2020-02-07 14:16:59 0 [Warning] Could not incr>
Feb 07 14:16:59 centos8.ostechnix.lan mysqld[5415]: 2020-02-07 14:16:59 0 [Warning] Changed limits>
Feb 07 14:16:59 centos8.ostechnix.lan systemd[1]: Started MariaDB 10.3 database server

Mariadb 服务现已加载并处于活动状态!

检查安装 MariaDB 版本:

# mysql -V
mysql Ver 15.1 Distrib 10.3.17-MariaDB, for Linux (x86_64) using readline 5.1

2.1。 设置数据库root用户(管理员)密码

默认情况下,数据库 root 用户密码为空。 为了保护数据库服务器,我们需要为 root 用户设置密码。 为此,请运行 mysql_secure_installation 脚本:

# mysql_secure_installation

您将被问到几个问题。 阅读并相应地回答它们。 首先,系统会提示您输入 root 用户的当前密码。 由于密码默认为空,只需按 ENTER 键即可。 接下来,系统将提示您回答以下问题:

  1. 设置root密码? – 类型 设置密码并输入两次密码。
  2. 移除匿名用户? – 类型 删除匿名用户。 默认情况下,匿名用户用于测试目的。 您应该在生产中删除匿名用户。
  3. 远程禁止root登录? – 类型 防止 root 用户从任何远程系统访问数据库服务器。
  4. 删除测试数据库并访问它? – 类型 删除测试数据库。
  5. 现在重新加载权限表? – 类型 使所做的所有更改生效。

样本输出:

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): #Press ENTER
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y #Type y and press ENTER
New password:  #Enter password
Re-enter new password: #Re-enter password
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y #Type y and press ENTER
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y #Type y and ress ENTER
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y #Type y and press ENTER
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y #Type y and press ENTER
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

我们现在已经保护了 MariaDB 的安装。

2.2. 登录 MariaDB 外壳

运行以下命令以 root 用户身份登录 mariadb shell:

# mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or g.
Your MariaDB connection id is 16
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 shell,只需运行:

exit;

3. 在 CentOS 8 上安装 PHP

PHP 是 LAMP 堆栈中的最后一个组件。 要将 php 与所有其他必需的 php 模块一起安装,请以 root 用户身份运行以下命令:

# dnf install php php-fpm php-mysqlnd php-opcache php-gd php-xml php-mbstring

3.1。 配置 Apache 使用 PHP-FPM

运行 PHP 的最常见的已知方法之一 Apache 是个 mod_php 模块。 然而,PHP-FPM(FastCGI 进程管理器)比 mod_php 有一些优势。 对于那些想知道的人,PHP-FPM 是 PHP 的 FastCGI 实现。 PHP-FPM 作为独立的 FastCGI 服务器运行并显着提高 PHP 环境的性能。

我们已经安装了 php-fpm,所以让我们使用命令启用和启动 php-fpm 服务:

# systemctl enable php-fpm
# systemctl start php-fpm

或者,使用以下单行命令一次性启用和启动 php-fpm 服务:

# systemctl enable --now php-fpm

要验证 php-fpm 服务是否已启动,请运行:

# systemctl status php-fpm

样本输出:

● php-fpm.service - The PHP FastCGI Process Manager
   Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2020-02-07 17:24:44 IST; 8s ago
 Main PID: 6053 (php-fpm)
   Status: "Ready to handle connections"
    Tasks: 6 (limit: 11501)
   Memory: 22.6M
   CGroup: /system.slice/php-fpm.service
           ├─6053 php-fpm: master process (/etc/php-fpm.conf)
           ├─6054 php-fpm: pool www
           ├─6055 php-fpm: pool www
           ├─6056 php-fpm: pool www
           ├─6057 php-fpm: pool www
           └─6058 php-fpm: pool www

Feb 07 17:24:44 centos8.ostechnix.lan systemd[1]: Starting The PHP FastCGI Process Manager...
Feb 07 17:24:44 centos8.ostechnix.lan systemd[1]: Started The PHP FastCGI Process Manager.

好了,php-fpm 服务已经加载并激活了。

3.2. 测试 PHP

要验证 PHP 是否正常工作,请创建一个名为 “信息.php” 在网络根文件夹中:

# vi /var/www/html/info.php

添加以下行:

<?php 
phpinfo(); 
?>

重新开始 Apache 服务使更改生效:

# systemctl restart httpd

现在,打开您的网络浏览器并导航到 https://IP_Address/info.php.

您将看到以下 php 页面,其中列出了所有已安装的模块和其他详细信息,例如 php 版本、构建日期和命令等。

恭喜! 在这个阶段,您应该在 CentOS 8 服务器上拥有一个功能齐全的 LAMP 堆栈。

我们已经发布了在不同的 Linux 平台上安装 LAMP 堆栈。 检查以下链接以了解如何在各种 Linux 发行版上设置 LAMP。

  • LAMP 堆栈指南

感谢您的光临!

帮助我们帮助您:

  • 订阅我们的电子邮件通讯: 立即注册
  • 支持 OSTechNix: 通过贝宝捐款
  • 下载免费的电子书和视频: TradePub 上的 OSTechNix
  • 联系我们: 红迪网 | Facebook | 推特 | 领英 | RSS订阅

祝你有美好的一天!!

ApacheCentOS 8DatabaseLAMP StackLinuxMariaDBPHPWeb 服务器

[ad_2]

Related Posts