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

时间:2020-03-21 11:45:15  来源:igfitidea点击:

LAMP 堆栈是Linux,Apache Web Server,MySQL/MariaDB,PHP的组合。
此步骤教程介绍了如何在CentOS 8最小服务器中安装Apache,MariaDB,PHP( LAMP 堆栈)。
LAMP 堆栈用于部署基于Web的应用程序和主机动态。
我在CentOS 8上测试了本教程,但下面给出的步骤应该在RHEL 8服务器上工作。
让我们开始。

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

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

  • 操作系统 - CentOS 8 Minimal Server(8.1.1911(核心))
  • IP地址 - 192.168.225.52/24
  • hostname - centos8.theitroad.lan.

下面给出的所有命令都应该作为"根"用户运行。
如果我们以普通用户身份登录,请在每个命令的前面使用"sudo"。
符号表示我作为root用户的命令。

说明:

CentOS 8中的一个主要更新之一是默认包管理器已从yum迁移到dnf。
但是,如果我们习惯了,我们仍然可以在CentOS 8上yum。
DNF的用法与Yum完全相同。

1.在CentOS 8上安装Apache Web服务器

要在CentOS 8上安装Apache,请将以下命令用作root用户:

# dnf install httpd

安装Apache后,使用命令启用和启动Apache服务:

# systemctl enable httpd
# systemctl start httpd

或者,我们可以使用以下One-Liner命令在一个Go中启用和启动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 2017-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
Nov 07 12:25:08 centos8.theitroad.lan systemd[1]: Starting The Apache HTTP Server...
Nov 07 12:25:08 centos8.theitroad.lan systemd[1]: Started The Apache HTTP Server.
Nov 07 12:25:08 centos8.theitroad.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 Server默认端口80和443通过防火墙和路由器。

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

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

允许HTTPS端口443:

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

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

$systemctl restart firewalld

1.1. 测试Apache Web服务器

我们刚刚完成了所有必要的步骤来部署Apache Web服务器。
让我们继续检查它是否真的工作。
为此,请打开Web浏览器(在网络中的任何远程系统上,因为它是没有GUI的最小服务器,并导航到HTTP://IP-Address。
如果CentOS系统上安装了GUI,则可以导航到http://localhost或者http://IP地址/或者http://域名。

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

好吧,Apache Web服务器正在工作!

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

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

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

# dnf install mariadb-server mariadb

安装MariaDB后,使用命令启用和启动MariaDB服务:

# systemctl enable mariadb
# systemctl start mariadb

或者,使用以下One-Liner命令一次启用并启动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 2017-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
Nov 07 14:16:59 centos8.theitroad.lan mysql-prepare-db-dir[5312]: Please report any problems at ht>
Nov 07 14:16:59 centos8.theitroad.lan mysql-prepare-db-dir[5312]: The latest information about Mar>
Nov 07 14:16:59 centos8.theitroad.lan mysql-prepare-db-dir[5312]: You can find additional informat>
Nov 07 14:16:59 centos8.theitroad.lan mysql-prepare-db-dir[5312]: http://dev.mysql.com
Nov 07 14:16:59 centos8.theitroad.lan mysql-prepare-db-dir[5312]: Consider joining MariaDB's stron>
Nov 07 14:16:59 centos8.theitroad.lan mysql-prepare-db-dir[5312]: https://mariadb.org/get-involved/
Nov 07 14:16:59 centos8.theitroad.lan mysqld[5415]: 2017-02-07 14:16:59 0 [Note] /usr/libexec/mysq>
Nov 07 14:16:59 centos8.theitroad.lan mysqld[5415]: 2017-02-07 14:16:59 0 [Warning] Could not incr>
Nov 07 14:16:59 centos8.theitroad.lan mysqld[5415]: 2017-02-07 14:16:59 0 [Warning] Changed limits>
Nov 07 14:16:59 centos8.theitroad.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键即可继续。
接下来,系统将提示我们回答以下问题:

  • 设置root密码? - 键入y以设置密码并两次输入密码。
  • 删除匿名用户? - 键入y以删除匿名用户。默认情况下给出匿名用户进行测试目的。我们应该删除生产中的匿名用户。
  • 远程禁止root登录? - 键入y以防止root用户从任何远程系统访问数据库服务器。
  • 删除测试数据库并访问它? - 键入y以删除测试数据库。
  • 现在重新加载权限表? - 键入y以生效所有更改。

示例输出:

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 shell

运行以下命令以作为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, 2016, 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. 使用PHP-FPM配置Apache

使用apache运行php的最常见的方法之一是mod_php模块。
但是,PHP-FPM(FastCGI Process Manager)在Mod_php上具有一些优点。
对于那些想知道的,PHP-FPM是PHP的FastCGI实现。
PHP-FPM作为独立FastCGI服务器运行,并显着提高了PHP环境的性能。

我们已经安装了PHP-FPM,因此让我们使用命令启用并启动PHP-FPM服务:

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

或者,使用以下One-Liner命令一次启用并启动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 2017-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
Nov 07 17:24:44 centos8.theitroad.lan systemd[1]: Starting The PHP FastCGI Process Manager...
Nov 07 17:24:44 centos8.theitroad.lan systemd[1]: Started The PHP FastCGI Process Manager.

嗯,PHP-FPM服务现在已加载和活动。

3.2. 测试PHP.

要验证PHP是否正常工作,请在Web根文件夹中创建名为"info.php"的示例文件:

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

添加以下行:

<?php 
phpinfo(); 
?>

重新启动Apache服务以生效更改:

# systemctl restart httpd

现在,打开Web浏览器并导航到http://ip_address/info.php。

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