网站搜索

在 Fedora 24 服务器和工作站上使用 MariaDB 和 PHP/PHP-FPM 设置 Nginx


您的计算机上可能已经安装了 Fedora 24 服务器版本,并且您渴望并期待设置一个 Web 服务器来运行网站和 Web 应用程序。别再犹豫了,因为我们将在这里完成所有这些,通过简单易行的步骤,您最终会欣赏到的。

在本操作指南中,我们将介绍如何在 Fedora 24 Web 服务器上安装 LEMP 堆栈的不同步骤。与LAMP类似,但在LEMP下,我们使用Nginx网络服务器。

不要错过:在 Fedora 24 服务器上安装 LAMP(Linux、Apache、MariaDB 和 PHP)

第1步:更新系统包

您可以通过更新系统软件包开始,如下所示:

dnf update

完成后,继续安装复合 LEMP 软件包。

第 2 步:安装 Nginx Web 服务器

Nginx是Apache Web服务器的替代品,它重量轻,消耗的系统资源少,因此在企业生产环境中具有高性能、稳定性和灵活性。

要在 Fedora 24 上安装 Nginx,请发出以下命令:

dnf install nginx  

安装完成后,您需要管理系统上的 Nginx 服务。首先,您需要通过运行以下命令将其设置为在启动时自动启动:

systemctl enable nginx.service

然后启动服务如下:
# systemctl 启动 nginx.service

接下来,检查 Nginx 服务器是否正在运行,您可以发出以下命令来执行此操作:

systemctl status nginx.service

为了通过 HTTP/HTTPS 协议查看您的 Nginx Web 服务器,您需要允许通过系统防火墙访问它。为此,请运行以下命令:

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

然后重新加载系统防火墙配置以使上述更改生效,如下所示:

systemctl reload firewalld

现在继续设置 Nginx server_name 指令,使用您最喜欢的编辑器,打开文件 /etc/nginx/nginx.conf 并找到配置指令,如下所示:

server_name server-ip-address;

注意:Nginx文档目录根目录是/usr/share/nginx/html,这是你可以放置所有Web文件的地方。

Nginx 安装过程中更重要的一件事是检查 Nginx 安装索引页面是否可以在您的网络浏览器中加载,因此打开您的网络浏览器并输入 URL:

http://server-ip-address

您应该能够查看以下页面:

第3步:安装MariaDB服务器

MariaDB 是最著名的 MySQL 关系数据库服务器的一个分支,要在 Fedora 24 服务器上安装 MariaDB,请发出以下命令:

dnf install mariadb-server

完成 MariaDB 安装后,您需要通过运行以下一系列命令来启用、启动和验证该服务。

systemctl enable mariadb-service  
systemctl start mariadb-service 
systemctl status mariadb-service  

现在是时候使用以下命令来保护您的 MariaDB 安装了:

mysql_secure_installation

执行上述命令后,系统会询问您以下几个问题:

Enter current password for root(enter for none): Here, Simply press [Enter]
Next you will be asked to set a root user password for your MariaDB server.
Set root password? [Y/n]: y and hit [Enter]
New password: Enter a new password for root user
Re-enter new password: Re-enter the above password 
Remove anonymous users? [Y/n]: y to remove anonymous users
It is not always good to keep your system open to remote access by root user, in case an attacker lands on your root user password, he/she can cause damage to your system. 
Disallow root login remotely? [Y/n]: y to prevent remote access for root user. 
Remove test database and access to it? [Y/n]: y to remove the test database
Finally, you need to reload privileges tables on your database server for the above changes to take effect.
Reload privileges tables now? [Y/n]: y to reload privileges tables 

第 4 步:安装 PHP 和模块

要在 Fedora 24 上安装 PHP 及其模块,请使用以下命令:

dnf install php php-commom php-fpm php-mysql php-gd

现在PHP和一些PHP模块已经完成安装,您需要配置PHP以便可以运行PHP文件。

默认情况下,PHP-FPM 配置为与 Apache Web 服务器一起使用,但对于我们这里的示例,我们使用 Nginx Web 服务器。因此,我们需要按照以下步骤更改该设置:

使用您最喜欢的编辑器,打开文件 /etc/php-fpm.d/www.conf,如下所示:

vi /etc/php-fpm.d/www.conf

然后在以下行中将用户和组的值从 apache 更改为 nginx:

; RPM: apache Choosed to be able to access some dir as httpd 
user = nginx 
; RPM: Keep a group allowed to write in log dir. 
group = nginx

然后重新启动 PHP-FPMNginx Web 服务器以使上述更改生效:

systemctl restart php-fpm.services
systemctl restart nginx.services

之后,通过发出以下命令确认它们正在运行:

systemctl status php-fpm.services
systemctl status nginx.services

现在您可以使用您最喜欢的编辑器来测试这一切,在您的 Nginx 根目录中创建一个名为 info.php 的文件,如下所示:

vi /usr/share/nginx/html/info.php

在文件中添加以下行,保存并退出。

<?php
phpinfo()
?>

然后打开 Web 浏览器并输入以下 URL 以验证 PHP 信息:

http://server-ip-address/info.php

此时,您必须已在 Fedora 24 服务器上成功安装并配置 LEMP 堆栈。在某些情况下,你们中的一些人一定遇到了错误,或者想要对所关心的问题进行更多解释,您可以在下面的评论部分发表评论,我们将一起寻找解决方案。