如何在Fedora 32/31/30/29上安装LAMP Stack

时间:2020-02-23 14:33:16  来源:igfitidea点击:

在这篇文章中,我们将研究如何在Fedora 32/31/30/29上安装和配置LAMP Stack。 LAMP Stack是用于托管Web应用程序的开源工具的集合。 LAMP堆栈的首字母缩略词包括:L Linux主机系统可以是VM或者物理系统,也可以是容器A Apache HTTP Server M MySQL/MariaDB数据库P PHP编程语言

要使LAMP堆栈正常运行,我们需要一个一个地安装和配置每个组件。在Fedora 31/30/29上安装和配置LAMP Stack的步骤应该很简单。

安装Fedora 32/31/30/29

在撰写本文时,Fedora的最新版本是Fedora30。我们有一个安装教程,该教程应适用于物理服务器,虚拟机和Vagrant Box部署:

如何在物理服务器/虚拟环境上安装Fedora

安装基本软件包/将SELinux设置为Permissive

安装诸如vim,curl,wget,telnet之类的基本软件包。

sudo dnf -y update
sudo dnf -y install vim bash-completion curl wget telnet

如果这是我们第一次了解SELinux,建议我们将其置于"允许"模式或者完全禁用它。通过运行以下命令检查SELinux状态:

# sestatus 
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   enforcing
Mode from config file:          enforcing
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Memory protection checking:     actual (secure)
Max kernel policy version:      31

默认模式是"强制"。要将其置于许可模式,请执行:

sudo setenforce 0
sudo sed -i 's/^SELINUX=.*/SELINUX=permissive/g' /etc/selinux/config

要完全禁用它:

sudo sed -i 's/^SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config

确认配置的持久状态

$cat /etc/selinux/config | grep SELINUX=
# SELINUX= can take one of these three values:
SELINUX=permissive

安装Apache httpd服务器

安装并更新Fedora OS之后,继续安装Apache httpd服务器。

sudo dnf -y install httpd

配置httpd基本设置:

编辑配置文件/etc/httpd/conf/httpd.conf并设置:

ServerAdmin theitroad@localhost
ServerName  example.com
ServerTokens Prod

我们可以选择设置Keepalive

KeepAlive On

启用并启动" httpd"服务

sudo systemctl start httpd
sudo systemctl enable httpd

如果我们正在运行防火墙,则允许http和https服务。

sudo firewall-cmd --add-service={http,https} --permanent
sudo firewall-cmd --reload

安装PHP和扩展

安装PHP并配置httpd以支持PHP脚本的执行。查看我们的文章:

如何在Fedora上安装PHP

不要忘记安装通用扩展:

sudo dnf -y install php php-cli php-php-gettext php-mbstring php-mcrypt php-mysqlnd php-pear php-curl php-gd php-xml php-bcmath php-zip

确认我们安装的PHP版本:

$php -v
PHP 7.2.11 (cli) (built: Oct 9 2016 15:09:36) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2016 Zend Technologies

可以通过将-m选项传递给php命令来查看已加载的扩展名。

$php -m

在文件/etc/php.ini中设置PHP时区

date.timezone = Africa/Nairobi

查看受支持的时区的完整列表。

安装MySQL/MariaDB数据库

要在Fedora 31/30/29上安装MySQL,请使用我们的教程:

如何在Fedora上安装MySQL 8.0

Fedora上游存储库具有MariaDB 10.3,可以使用dnf命令安装。

sudo dnf install mariadb-server

安装后,设置字符集

sudo vim /etc/my.cnf.d/mariadb-server.cnf

mysqld部分设置字符集

[mysqld]
character-set-server=utf8

然后启动" mariadb"服务,并使其在启动时启动

sudo systemctl start mariadb
sudo systemctl enable mariadb

执行MariaDB初始设置,例如设置root密码,禁用远程root登录等:

$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): 
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
New password: 
Re-enter new 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
 ... 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
 ... 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
 - 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
 ... 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!

回答如下问题:输入当前的root密码(不输入任何密码):<Enter>设置root密码? [是/否] y要删除匿名用户吗? [是/否] y要删除测试数据库并访问它吗? [是/否] y现在重新加载特权表? [Y/n] y

测试登录

$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.10-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)]> select version();
+-----------------+
| version()       |
+-----------------+
| 10.3.10-MariaDB |
+-----------------+
1 row in set (0.001 sec)

创建和删除测试数据库:

MariaDB [(none)]> CREATE DATABASE test_db;
Query OK, 1 row affected (0.001 sec)

MariaDB [(none)]> GRANT ALL PRIVILEGES ON test_db.* TO 'test_user'@'localhost' IDENTIFIED BY "StrongPassword";
Query OK, 0 rows affected (0.002 sec)

MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.001 sec)

要删除数据库和用户,请使用

MariaDB [(none)]> DROP DATABASE test_db;
Query OK, 0 rows affected (0.003 sec)

MariaDB [(none)]> DROP USER 'test_user'@'localhost';
Query OK, 0 rows affected (0.001 sec)

我们已经确认我们的数据库服务器可以正常工作。要允许远程连接,请在防火墙上允许端口3306

sudo firewall-cmd --add-service=mysql --permanent
sudo firewall-cmd --reload

我们还可以限制来自受信任网络的访问

sudo firewall-cmd --permanent --add-rich-rule 'rule family="ipv4" \
service name="mysql" source address="10.1.1.0/24" accept'

测试灯泡组安装

作为对我们在Fedora 31/30/29上安装灯泡堆栈的测试,请创建一个PHP测试页

sudo vim /var/www/html/phpinfo.php

加:

<?php
//Show all information, defaults to INFO_ALL
phpinfo();
?>

重新加载httpd服务并在浏览器上打开页面。

sudo systemctl reload httpd

打开添加的PHP信息页http://[serverIP]/phpinfo.php

我们已在Fedora 32/31/30/29服务器或者台式机工作站上成功安装了LAMP堆栈。