zl程序教程

您现在的位置是:首页 >  后端

当前栏目

如何在Fedora 31上使用PHP和MariaDB(LEMP Stack)安装Nginx

PHP安装Nginx 使用 如何 Fedora 31 Stack
2023-06-13 09:13:15 时间

LEMP软件堆栈是一组开源软件,这些软件安装在一起以使服务器能够托管网站和应用程序。它是L inux,E Nginx服务器,M ySQL(此处使用MariaDB)和P HP 的首字母缩写。

在本指南中,您将在基于Fedora 31的服务器上安装LEMP堆栈。我们还将安装PHPMyAdmin,Redis和Opcache。

运行Fedora 31的服务器。 非root用户sudo用户。 确保所有内容都已更新。
$ sudo dnf upgrade

系统需要的软件包很少。
$ sudo dnf install wget curl nano -y

其中一些软件包可能已经安装在您的系统上。禁用SELinux。

$ sudo setenforce 0
配置防火墙

第一步是配置防火墙。Fedora服务器预装有Firewalld防火墙。

检查防火墙是否正在运行。

$ sudo firewall-cmd --state

您应该获得以下输出。

running

将防火墙的默认区域设置为public。

$ sudo firewall-cmd --set-default-zone=public

检查当前允许的服务/端口。

$ sudo firewall-cmd --zone=public --permanent --list-services

它应该显示以下输出。

dhcpv6-client mdns ssh

允许HTTP和HTTPS端口。
$ sudo firewall-cmd --zone=public --permanent --add-service=http
$ sudo firewall-cmd --zone=public --permanent --add-service=https

再次检查防火墙的状态。

$ sudo firewall-cmd --zone=public --permanent --list-services

您应该看到类似的输出。

dhcpv6-client http https mdns ssh

重新加载防火墙。

$ sudo systemctl reload firewalld

安装PHP

Fedora 31默认附带PHP 7.3。但是我们要安装PHP 7.4,为此需要添加REMI存储库。

安装REMI存储库,这是用于安装PHP软件包的正式Fedora存储库。

$ sudo dnf -y install https://rpms.remirepo.net/fedora/remi-release-31.rpm

启用remi和remi-php74存储库,并禁用remi-modular存储库。这将启用安装PHP 7.4软件包所需的存储库。
$ sudo dnf config-manager --set-enabled remi
$ sudo dnf config-manager --set-enabled remi-php74
$ sudo dnf config-manager --set-disabled remi-modular

安装PHP 7.4以及一些其他软件包。

$ sudo dnf install -y php-cli php-fpm php-mysqlnd

检查PHP是否正常工作。

$ php --version

您应该看到类似的输出。

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

安装MariaDB

MariaDB是MySQL的直接替代品,这意味着运行和操作MariaDB的命令与MySQL的命令相同。

默认情况下,Fedora 31随MariaDB 10.3一起提供,但我们也可以安装最新的10.4(在编写本教程时可用)。为此,我们需要添加官方的MariaDB存储库。

MariaDB.repo在/etc/yum.repos.d/目录中创建。

$ sudo nano /etc/yum.repos.d/MariaDB.repo

在其中添加以下代码。

# MariaDB 10.4 Fedora repository list

# http://downloads.mariadb.org/mariadb/repositories/

[mariadb]

name = MariaDB

baseurl = http://yum.mariadb.org/10.4/fedora31-amd64

gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB

gpgcheck=1

通过按Ctrl + X并在出现提示时输入Y来保存文件。

要安装MariaDB,请发出以下命令

$ sudo dnf install MariaDB-server -y

注意命令。如果要在Fedora存储库中安装copy(10.3),则应使用,sudo dnf install mariadb-server但对于10.4版本,我们使用sudo dnf install MariaDB-server。

检查MariaDB是否正确安装。

$ mysql --version

您应该看到以下输出。

mysql Ver 15.1 Distrib 10.4.12-MariaDB, for Linux (x86_64) using EditLine wrapper

启用并启动MariaDB服务。

$ sudo systemctl enable mariadb

$ sudo systemctl start mariadb

运行以下命令以执行默认配置,例如提供root密码,删除匿名用户,禁止远程root登录以及删除测试表。

$ sudo mysql_secure_installation

使用MariaDB 10.4,现在将询问您使用root密码还是unix_socket插件。使用unix_socket插件,您可以使用Linux用户凭据登录到MariaDB。尽管您将需要传统的用户名/密码来使用第三方应用程序(例如PhpMyAdmin),但仍被认为更安全。在本教程中,我们将坚持使用unix_socket插件。您仍然可以通过为数据库创建的特定用户来使用PhpMyAdmin。

按Enter键选择默认选项(大写字母,在这种情况下为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, well need the current

password for the root user. If youve just installed MariaDB, and

havent set the root password yet, 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 or using the unix_socket ensures that nobody

can log into the MariaDB root user without the proper authorisation.

You already have your root account protected, so you can safely answer n.

Switch to unix_socket authentication [Y/n] [PRESS ENTER]

Enabled successfully!

Reloading privilege tables..

 ... Success!

You already have your root account protected, so you can safely answer n.

Change the root password? [Y/n] [ANSWER n]

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] [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] [PRESS 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] [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] [PRESS ENTER]

 ... Success!

Cleaning up...

All done! If youve completed all of the above steps, your MariaDB

installation should now be secure.

Thanks for using MariaDB!

而已。下次您要登录MySQL时,请使用以下命令

$ sudo mysql

出现提示时输入您的root密码。

安装Nginx

Fedora 31默认带有Nginx的稳定版本(1.16.1)。如果要安装Nginx的主线版本,请遵循我们的从源代码构建Nginx的指南。确保获取最新版本的Nginx以及本教程中提到的依赖项。其余说明将保持不变(在./configure命令期间相应地更改路径名)。对于本教程,我们将坚持使用稳定版的Nginx。

安装Nginx服务器。

$ sudo dnf install nginx -y

检查它是否正常工作。

$ nginx -v

您应该看到以下输出。

nginx version: nginx/1.16.1

启动并启用Nginx。

$ sudo systemctl start nginx

$ sudo systemctl enable nginx

在浏览器中打开服务器的IP地址以查看下一页。这意味着Nginx正常工作。

Fedora Nginx测试页

配置Nginx

设置服务器块所在的目录。

$ sudo mkdir /etc/nginx/sites-available

$ sudo mkdir /etc/nginx/sites-enabled

创建您的网站所在的目录。

$ sudo mkdir /var/www/example.com/html -p

使用-p指令可创建以前不存在的父目录。

运行以下命令为您的站点添加配置文件。

$ sudo nano /etc/nginx/sites-available/example.com.conf

将以下代码粘贴到编辑器中。

server {

 listen *:80;

 server_name example.com;

 root /var/www/example.com/html;

 index index.php index.html;

 location / {

 try_files $uri $uri/ =404;

 access_log /var/log/nginx/example.com.access.log;

 error_log /var/log/nginx/example.com.error.log;

 location ~ /.php$ {

 try_files $uri =404;

 fastcgi_pass unix:/run/php-fpm/www.sock;

 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

 fastcgi_index index.php;

 include fastcgi_params;

该文件假定我们将example.com在目录中托管/var/www/html。如果您不打算使用任何域并将服务器配置为仅可通过IP地址/本地主机访问,则需要从nginx.conf文件中删除相应的服务器块设置,否则它将与您创建的服务器块混淆。

通过将其链接到sites-enabled目录来激活该配置文件。

$ sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/

打开/etc/nginx/nginx.conf文件进行编辑。

$ sudo nano /etc/nginx/nginx.conf 

在行之后粘贴以下行 include /etc/nginx/conf.d/*.conf

include /etc/nginx/sites-enabled/*.conf;

server_names_hash_bucket_size 64;

将值types_hash_max_size从2048 更改为4096。

types_hash_max_size 4096;

按Ctrl + X关闭编辑器,并在提示保存文件时按Y。测试Nginx配置。

$ sudo nginx -t

您应该看到以下输出,表明您的配置正确。

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

重新加载Nginx服务。

$ sudo systemctl reload nginx

配置PHP-FPM

打开文件/etc/php-fpm.d/www.conf。

$ sudo nano /etc/php-fpm.d/www.conf

我们需要将Unix用户/ PHP进程组设置为nginx。在文件中找到user=apache和group=apache行,并将其更改为nginx。

...

; Unix user/group of processes

; Note: The user is mandatory. If the group is not set, the default users group

; will be used.

; RPM: apache user chosen to provide access to the same directories as httpd

user = nginx

; RPM: Keep a group allowed to write in log dir.

group = nginx

另外,将unix套接字的所有权权限设置为nginx并删除;它们前面的。

listen.owner = nginx

listen.group = nginx

通过按Ctrl + X并在出现提示时输入Y来保存文件。

重新启动PHP-fpm进程。

$ sudo systemctl restart php-fpm

要测试您的PHP设置,请test.php在html文件夹中创建一个文件。

$ sudo nano /var/www/example.com/html/test.php

向其中添加以下内容并通过按Ctrl + X并在出现提示时输入Y来保存文件。

 ?php phpinfo();

http:// yourserverip /test.php在您的Web浏览器中启动,您应该看到以下内容。

PHP信息页面

这就是本教程的全部内容。您的LEMP设置已完成,您可以开始制作和托管您的网站和应用程序。

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/32310.html

apachelinuxmysqlnginxphpRedisshell开源