安装和使用动态编程语言


Red Hat Enterprise Linux 10

在 Red Hat Enterprise Linux 10 中安装和使用 Python 和 PHP

摘要

安装和使用 Python 3。安装 PHP 脚本语言,将 PHP 与 Apache HTTP 服务器或 ngninx Web 服务器一起使用,并使用命令行界面运行 PHP 脚本。

对红帽文档提供反馈

我们致力于为您提供高质量的文档,您的反馈将对我们有很大帮助。为帮助我们改进,您可以通过 Red Hat Jira 跟踪系统提交建议或报告错误。

流程

  1. 登录到 Jira 网站。

    如果您还没有帐户,请创建一个。

  2. 单击顶部导航栏中的 Create
  3. Summary 字段中输入描述性标题。
  4. Description 字段中输入您对改进的建议。包括文档相关部分的链接。
  5. 点窗口底部的 Create

第 1 章 安装和使用 Python

Python 是一种高级编程语言,支持多种编程模式,如面向对象、所需功能以及程序式范式。Python 具有动态语义,可用于通用编程。

在 Red Hat Enterprise Linux 中安装的许多安装的软件包都是使用 Python 编写的。示例包括系统工具、数据分析和 Web 应用程序的软件包。要使用这些软件包,您必须安装 python* 软件包。

1.1. Python 版本

要为开发和运行应用程序选择适当的环境,请查看 Red Hat Enterprise Linux (RHEL) 10 中的 Python 版本。

Python 3.12 是 RHEL 10 中的默认 Python 实现。Python 3.12 在 BaseOS 存储库中的非模块化 python3 RPM 软件包中分发,通常被默认安装。Python 3.12 将支持 RHEL 10 的整个生命周期。

Python 3 的额外版本将作为非模块化 RPM 软件包,通过 RHEL 10 次要发行本中的 AppStream 存储库发布,且具有较短的生命周期。您将能够与 Python 3.12 并行安装这些额外的 Python 3 版本。

未版本化的 python 命令指向默认的 Python 3.12 版本。

1.2. 安装 Python 3

要开发和运行 Python 应用程序,请在 RHEL 10 系统上安装 Python 3。

默认 Python 实现通常会默认安装。但是,您也可以手动安装它。

流程

  • 要安装 Python 3.12,请输入:

    # dnf install python3

验证

  • 验证系统上安装的 Python 版本:

    $ python3 --version

1.3. 安装其他 Python 3 软件包

要扩展应用程序和脚本的功能,请在 RHEL 10 系统上安装其他 Python 3 软件包。

带有 python3- 前缀的软件包包含默认 Python 3.12 版本的附加组件模块。

流程

  • 要安装,例如 Python 3.12 的 Requests 模块,请输入:

    # dnf install python3-requests
  • 要从 Python 3.12 安装 pip 软件包安装程序,请输入:

    # dnf install python3-pip

1.4. 为开发人员安装其他 Python 3 工具

要获得额外的 Python 工具,请使用 CodeReady Linux Builder (CRB)存储库。安装提供所需工具的 python3 软件包。

重要

红帽不支持 CodeReady Linux Builder 存储库中的内容。

例如,CRB 存储库包含以下软件包:

  • python3-pytest
  • python3-idle
  • python3-debug
  • python3-cython
注意

并非所有与上游 Python 相关的软件包都包括在 RHEL 中。

流程

  1. 启用 CodeReady Linux Builder 存储库:

    # subscription-manager repos --enable codeready-builder-for-rhel-10-x86_64-rpms
  2. 安装,例如 python3-cython 软件包:

    # dnf install python3-cython

1.5. 使用 Python

要安装或与提供系统工具、数据分析工具或使用 Python 编写的 Web 应用程序的软件包交互,请运行 Python 解释器或 Python 相关的命令。

先决条件

  • Python 已安装。
  • 如果要下载并安装第三方应用程序,请安装 python3-pip 软件包。

    警告

    以 root 用户身份使用 pip 安装 Python 软件包会将文件放置在系统位置。这会覆盖 RHEL 库,并可能导致系统不稳定或与支持的软件包冲突。红帽不支持在系统级别上使用 pip 安装软件。要避免这些问题,请在虚拟环境中使用 pip,或使用 --user 选项以非 root 用户身份安装软件包。

流程

  • 要运行 Python 3.12 解释器或相关命令,请使用以下命令:

    $ python3
    $ python3 -m venv --help
    $ python3 -m pip install <package>
    $ pip3 install <package>

第 2 章 打包 Python 3 RPM

您可以使用 DNF 软件包管理器在系统中安装 Python 软件包。DNF 使用 RPM 软件包格式,它提供了更好的软件下游控制。

与原生 Python 软件包相比,将 Python 项目打包到 RPM 软件包中具有以下优势:

  • 可以依赖于 Python 和非 Python 软件包,并严格由 DNF 软件包管理器强制执行。
  • 您可以用加密的方式为软件包签名。使用加密签名,您可以将操作系统的其余部分与 RPM 软件包的内容进行验证、集成和测试。
  • 您可以在构建过程中运行测试。

原生 Python 软件包的打包格式遵循 Python 打包授权(PyPA)发布的规格。历史上,大多数 Python 项目使用 distutilssetuptools 工具打包,并在 setup.py 文件中定义软件包信息。但是,创建原生 Python 软件包的选项随着时间而演变:

  • 要打包使用 setup.py 文件的 Python 软件,请按照本文档操作。
  • 要使用 pyproject.toml 文件打包更现代的软件包,请阅读 pyproject-rpm-macros 源中的 README 文件。pyproject-rpm-macros 软件包位于 CodeReady Linux Builder (CRB)存储库中。该仓库包含不受支持的软件包。根据需要,pyproject-rpm-macros 内容更改,以支持较新的 Python 打包标准。有关 PyPA 规格和 pyproject-rpm-macros 存储库的链接,请参阅附加资源。

2.1. 示例 Python 软件包的 spec 文件描述

参阅以下 python3-pello 软件包示例中有关 Python RPM spec 文件的备注。

与非 Python RPM spec 文件相比,Python 项目的 RPM spec 文件有一些具体信息。例如,建议任何 Python 库的 RPM 软件包名称包含 python3- 前缀。

例 2.1. 使用 Python 编写的程序的 SPEC 文件示例

%global python3_pkgversion 3

Name:           python-pello
Version:        1.0.2
Release:        1%{?dist}
Summary:        Example Python library

License:        MIT
URL:            https://github.com/fedora-python/Pello
Source:         %{url}/archive/v%{version}/Pello-%{version}.tar.gz

BuildArch:      noarch
BuildRequires:  python%{python3_pkgversion}-devel

# Build dependencies need to be specified manually
BuildRequires:  python%{python3_pkgversion}-setuptools

# Test dependencies need to be specified manually
# Runtime dependencies need to be BuildRequired manually to run tests during build
BuildRequires:  python%{python3_pkgversion}-pytest >= 3


%global _description %{expand:
Pello is an example package with an executable that prints Hello World! on the command line.}

%description %_description

%package -n python%{python3_pkgversion}-pello
Summary:        %{summary}

%description -n python%{python3_pkgversion}-pello %_description


%prep
%autosetup -p1 -n Pello-%{version}


%build
# The macro only supports projects with setup.py
%py3_build


%install
# The macro only supports projects with setup.py
%py3_install


%check
%pytest


# Note that there is no %%files section for python-pello
%files -n python%{python3_pkgversion}-pello
%doc README.md
%license LICENSE.txt
%{_bindir}/pello_greeting

# The library files needed to be listed manually
%{python3_sitelib}/pello/

# The metadata files needed to be listed manually
%{python3_sitelib}/Pello-*.egg-info/
  • 通过定义 python3_pkgversion 宏,您可以设置将为哪个 Python 版本构建此软件包。要为默认的 Python 版本 3.12 进行构建,请删除该行。
  • 将 Python 项目打包到 RPM 中时,需要将 python- 前缀添加到项目的原始名称中。这里的项目名称是 Pello,因此源 RPM (SRPM)的名称是 python-pello
  • BuildRequires 指定了构建和测试此软件包所需的软件包。在 BuildRequires 中,始终包含 python3-devel 和其他构建 Python 软件包的工具。另外,列出软件所需的项目,如 python3-setuptools。在 %check 部分中添加需要运行测试的任何运行时和测试依赖项。
  • 当为二进制 RPM 选择名称(用户必须安装的软件包)时,请添加版本化的 Python 前缀。对默认的 Python 3.12 使用 python3- 前缀。您可以使用 %{python3_pkgversion} 宏,对于默认的 Python 版本 3.12 它评估为 3,除非您将其设置为一个显式版本,例如,当有较新版本的 Python 可用时(请参阅脚注 1)。
  • %py3_build%py3_install 宏会分别运行 setup.py buildsetup.py install 命令,使用附加参数来指定安装位置、要使用的解释器以及其他详情。

    注意

    使用 setuptools 软件包中的 setup.py buildsetup.py install 命令已弃用。在后续的 Red Hat Enterprise Linux 版本中不支持这些命令。您可以改为使用 pyproject-rpm-macros

  • %check 部分运行打包项目的测试。确切的命令取决于项目本身,但您可以使用 %pytest 宏以一种 RPM 友好的方式运行 pytest 命令。

2.2. Python 3 RPM 的常见宏

在 Python RPM spec 文件中,总是对 Python 3 RPM 使用宏,而不是硬编码其值。

您可以重新定义在这些宏中使用哪个 Python 3 版本。为此,请在 spec 文件的顶部定义 python3_pkgversion 宏。如需更多信息,请参阅 Python 软件包示例的 spec 文件描述。如果您定义了 python3_pkgversion 宏,下表中的宏值与 Python 3 版本匹配。

Expand
表 2.1. Python 3 RPM 宏
Macro常规定义描述

%{python3_pkgversion}

3

所有其他宏使用的 Python 版本。当您为不同于默认的 Python 版本构建时,重新定义这个宏。

%{python3}

/usr/bin/python3

Python 3 解释器。

%{python3_version}

3.12

Python 3 解释器的 major.minor 版本。

%{python3_sitelib}

/usr/lib/python3.12/site-packages

安装纯 Python 模块的位置。

%{python3_sitearch}

/usr/lib64/python3.12/site-packages

包含特定于架构扩展模块的模块的位置已安装。

%py3_build

 

使用适合 RPM 软件包的参数扩展 setup.py build 命令。

%py3_install

 

使用适合 RPM 软件包的参数扩展 setup.py install 命令。

%{py3_shebang_flags}

sP

Python 解释器指令宏的默认标记集合,%py3_shebang_fix

%py3_shebang_fix

 

将 Python 解释器指令改为 #! %{python3},保留任何现有标志(如果找到的话),并添加 %{py3_shebang_flags} 宏中定义的标记。

2.3. 为 Python RPM 使用自动生成的依赖项

使用上游提供的元数据为 Python RPM 启用自动生成依赖项。

先决条件

流程

  1. 生成的 RPM 中包含以下目录之一:

    • .dist-info
    • .egg-info

      RPM 构建过程会自动从这些目录中生成虚拟 pythonX.Ydist,例如:

      python3.12dist(pello)

      然后,Python 依赖项生成器读取上游元数据,并使用生成的 pythonX.Ydist 虚拟提供为每个 RPM 软件包生成运行时要求。生成的需求标签的示例:

      Requires: python3.12dist(requests)
  2. 检查生成的 Requires
  3. 要删除某些生成的 Requires,请在 spec 文件的 %prep 部分中修改上游提供的元数据。
  4. 要禁用自动要求生成器,请在主软件包的 %description 声明前立即包含 %{?python_disable_dependency_generator} 宏。

第 3 章 安装 Tcl/Tk

Tcl 是一个动态编程语言,Tk 是一个图形用户界面(GUI)工具包。它们为开发带有图形界面的跨平台应用程序提供了强大而易用的平台。

作为动态编程语言,"Tcl"为编写脚本提供了简单而灵活的语法。tcl 软件包为此语言和 C 库提供了解释器。您可以使用 Tk 作为 GUI 工具包,其为创建图形界面提供一组工具和小部件。您可以使用各种用户界面元素,如按钮、菜单、对话框、文本框及画布来画图。Tk 是许多动态编程语言的 GUI。

有关 Tcl/Tk 的更多信息,请参阅 Tcl/Tk manualTcl/Tk 文档网页

3.1. 安装 Tcl

要开发和运行跨平台软件应用程序和脚本,请在 RHEL 10 系统上安装 Tcl 动态编程语言。

通常默认安装了默认的 Tcl 实现。但是,您也可以手动安装它。

流程

  • 要安装 Tcl,请使用:

    # dnf install tcl

验证

  • 要验证安装在您的系统中的 Tcl 版本,请运行解释器 tclsh

    $ tclsh
  • 在解释器中运行这个命令:

    % info patchlevel
    8.6
  • 您可以通过按 Ctrl+C退出解释器界面

3.2. 安装 Tk

要获得用于创建图形用户界面(GUI)的一组工具和小部件,请安装 Tk 工具包。您可以将 Tk 用于不同的动态应用程序。

默认 Tk 工具包实现通常会默认安装。但是,您也可以手动安装它。

流程

  • 安装 Tk

    # dnf install tk

验证

  • 要验证系统上是否安装了 Tk 版本,请运行 window shell wish。您需要运行图形显示:

    $ wish
  • 在 shell 中运行这个命令:

    % puts $tk_version
    8.6
  • 您可以通过按 Ctrl+C退出解释器界面

第 4 章 使用 PHP 脚本语言

超文本预处理器(PHP)是一种通用脚本语言,主要用于服务器端脚本。您可以使用 PHP ,使用 Web 服务器来运行 PHP 代码。

4.1. 安装 PHP 脚本语言

要开发和部署服务器端 Web 应用,请在系统上安装 PHP。

在 RHEL 10 中,PHP 脚本语言作为以下版本的 php RPM 软件包提供:

  • PHP 8.3
  • 从 RHEL 10.2 开始可用的 PHP 8.4

先决条件

  • 您有 root 权限。

流程

  • 安装 PHP 软件包:

    • 对于 PHP 8.3,请输入:

      # dnf install php
    • 对于 PHP 8.4,请输入:

      # dnf install php8.4

4.2. 从 RHEL 10 版本的 PHP 8.3 升级到 PHP 8.4

您可以将 PHP 脚本语言从 8.3 升级到 8.4,以访问新功能和改进。

有关显著变化和增强,请参阅 RHEL 10.2 发行注记文档中的发行注记。https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/10/html/10.2_release_notes/new-features-and-enhancements#Jira-RHEL-105827

注意

使用 HEKETI--allowerasing 选项可能会意外删除 PHP 扩展。要避免删除所需的扩展,请列出已安装的 PHP 扩展,删除现有的 HEKETIphp 软件包,然后将 PHP 8.4 与您需要的扩展一起安装。

先决条件

  • 您有 root 权限。

流程

  1. 列出当前安装的 PHP 软件包和扩展:

    # dnf list --installed php-\*
  2. 注意上一命令输出中的已安装的扩展。
  3. 删除现有的 PHP 软件包:

    # dnf remove php\*
  4. 安装 PHP 8.4 以及您之前记录的扩展:

    # dnf install php8.4-common php8.4-cli <extension_name> <extension_name>

4.3. 通过 Web 服务器使用 PHP 脚本语言

要托管并提供动态 Web 应用程序,您可以配置 Apache HTTP 服务器或 nginx web 服务器,以使用 FastCGI Process Manager (FPM)处理 PHP 脚本。

4.3.1. 在 Apache HTTP 服务器中使用 PHP

在 Red Hat Enterprise Linux 10 中,您可以使用 Apache HTTP 服务器将 PHP 作为 FastCGI 进程服务器运行。作为替代 PHP FastCGI 守护进程,您可以使用 FastCGI Process Manager (FPM)来管理高网站负载。PHP 默认在 RHEL 10 中使用 FastCGI 进程管理器

先决条件

  • 在您的系统上安装 PHP 脚本语言。

流程

  • 配置 Apache HTTP 服务器和 php-fpm,以运行 PHP 脚本。

    1. 安装 httpd 软件包:

      # dnf install httpd
    2. 启动 Apache HTTP 服务器

      # systemctl start httpd

      或者,如果 Apache HTTP 服务器已在您的系统中运行,请在安装 PHP 后重启 httpd 服务:

      # systemctl restart httpd
    3. 启动 php-fpm 服务:

      # systemctl start php-fpm
    4. 可选: 在引导时启用这两个服务:

      # systemctl enable php-fpm httpd
    5. 要获取有关 PHP 设置的信息,请在 /var/www/html/ 目录中创建带有以下内容的 index.php 文件:

      # echo '<?php phpinfo(); ?>' > /var/www/html/index.php
    6. 要运行 index.php 文件,请将浏览器指向:

      http://<hostname>/
    7. 可选:如果您有具体要求,请调整配置:

      • /etc/httpd/conf/httpd.conf - 一般的 httpd 配置
      • /etc/httpd/conf.d/php.conf - httpd特定 PHP 配置
      • /usr/lib/systemd/system/httpd.service.d/php-fpm.conf - 默认情况下,php-fpm 服务与 httpd 一起启动
      • /etc/php-fpm.conf - FPM 主配置
      • /etc/php-fpm.d/www.conf - 默认 www 池配置
  • 运行 "Hello, World!"使用 Apache HTTP 服务器的 PHP 脚本.

    1. /var/www/html/ 目录中为您的项目创建一个 hello 目录:

      # mkdir hello
    2. /var/www/html/hello/ 目录中创建 hello.php 文件,其内容如下:

      # <!DOCTYPE html>
      <html>
      <head>
      <title>Hello, World! Page</title>
      </head>
      <body>
      <?php
          echo 'Hello, World!';
      ?>
      </body>
      </html>
    3. 启动 Apache HTTP 服务器

      # systemctl start httpd
    4. 要运行 hello.php 文件,请将浏览器指向:

      http://<hostname>/hello/hello.php

      因此,会显示带有 "Hello, World!" 文本的网页。

      如需更多信息,请参阅 httpd (8)httpd.conf (5)php-fpm (8) 手册页。

4.3.2. 使用带有 nginx web 服务器的 PHP

配置 nginx web 服务器,以处理 RHEL 10 系统上的 PHP 脚本。

先决条件

  • 在您的系统上安装 PHP 脚本语言。

流程

  • 配置 nginx web 服务器和 php-fpm,以运行 PHP 脚本。

    1. 安装 nginx 软件包:

      # dnf install nginx
    2. 启动 nginx 服务器:

      # systemctl start nginx

      或者,如果 nginx 服务器已在您的系统中运行,请在安装 PHP 后重启 nginx 服务:

      # systemctl restart nginx
    3. 启动 php-fpm 服务:

      # systemctl start php-fpm
    4. 可选: 在引导时启用这两个服务:

      # systemctl enable php-fpm nginx
    5. 要获取 PHP 设置的信息,请在 /usr/share/nginx/html/ 目录中使用以下内容创建 index.php 文件:

      # echo '<?php phpinfo(); ?>' > /usr/share/nginx/html/index.php
    6. 要运行 index.php 文件,请将浏览器指向:

      http://<hostname>/
    7. 可选:如果您有具体要求,请调整配置:

      • /etc/nginx/nginx.conf - nginx 主配置
      • /etc/nginx/conf.d/php-fpm.conf - FPM 配置 nginx
      • /etc/php-fpm.conf - FPM 主配置
      • /etc/php-fpm.d/www.conf - 默认 www 池配置
  • 运行 "Hello, World!"使用 nginx 服务器的 PHP 脚本。

    1. /usr/share/nginx/html/ 目录中为您的项目创建一个 hello 目录:

      # mkdir hello
    2. /usr/share/nginx/html/hello/ 目录中创建一个包含以下内容的 hello.php 文件:

      # <!DOCTYPE html>
      <html>
      <head>
      <title>Hello, World! Page</title>
      </head>
      <body>
      <?php
          echo 'Hello, World!';
      ?>
      </body>
      </html>
    3. 启动 nginx 服务器:

      # systemctl start nginx
    4. 要运行 hello.php 文件,请将浏览器指向:

      http://<hostname>/hello/hello.php

      因此,会显示带有 "Hello, World!" 文本的网页。

      如需更多信息,请参阅 nginx (8)php-fpm (8) man page。

4.4. 使用命令行运行 PHP 脚本

PHP 脚本通常使用 Web 服务器来运行。但是,您也可以使用命令行来运行 脚本。

先决条件

  • 在您的系统上安装 PHP 脚本语言。

流程

  1. 打开文本编辑器,并使用以下内容创建名为 hello.php 的 PHP 文件:

    <?php
        echo 'Hello, World!';
    ?>
  2. 在命令行中,更改到包含 hello.php 的目录。
  3. 从命令行运行 PHP 脚本:

    $ php hello.php
    Hello, World!

    如需更多信息,请参阅 php (1) 手册页。

Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

关于红帽文档

Legal Notice

Theme

© 2026 Red Hat
返回顶部