我的操作系统是 Ubuntu 22.04
我开始安装 php 8.1
然后我安装了 Apache/2.4.52 (Ubuntu) 服务器
然后我添加了以下内容:
sudo apt-get install -y php php-cli php-common php-mysql php-zip php-gd php-mbstring php-curl php-xml php-bcmath libapache2-mod-php
命令:php -v,产生:
PHP 8.1.2-1ubuntu2.8 (cli) (built: Nov 2 2022 13:35:25) (NTS)
然后我修改了:
/etc/apache2/mods-enabled/dir.conf: DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
我重新启动了 apache2 服务器。
我添加了一个 index.php 文件,但是当我尝试访问网页时,我只看到 index.php 的列表
<?php
echo 'This is a test';
我其中还有一个 index.html 文件:
<h1>This is a test</h1>
当我删除 index.php 并返回网页时,我看到
This is a test
正如预期,采用大而粗的字体。
PHP 手册(以及我在此网站上找到的答案)引用了 httpd.conf 文件,并希望我添加:
LoadModule php_module modules/libphp.so
但是我在 Ubuntu 22.04 上没有 httpd.conf,我在 /etc/apaache2 文件夹中有一个 apache2.conf,它是一个从各个子文件夹加载所有内容的骨架文件。
当我尝试时:
sudo a2enmod php
我有:
ERROR: Module php does not exist!
有任何想法吗?
编辑:
我做到了:
cd /etc/apache2
grep -R php
这就是我得到的:
sites-available/default-ssl.conf: <FilesMatch "\.(cgi|shtml|phtml|php)$">
sites-available/default-ssl.conf.original: <FilesMatch "\.(cgi|shtml|phtml|php)$">
mods-enabled/dir.conf: DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
mods-available/dir.conf: DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
并且这样做:
sudo a2enmod php8.1
产生同样的错误:
ERROR: Module php8.1 does not exist!
然后通过 grep 搜索已安装的软件包,我得到:
libapache2-mod-php8.1/jammy-updates,jammy-security,now 8.1.2-1ubuntu2.8 amd64 [installed,automatic]
libapache2-mod-php/jammy,now 2:8.1+92ubuntu1 all [installed]
php-bcmath/jammy,now 2:8.1+92ubuntu1 all [installed]
php-cli/jammy,now 2:8.1+92ubuntu1 all [installed]
php-common/jammy,now 2:92ubuntu1 all [installed]
php-curl/jammy,now 2:8.1+92ubuntu1 all [installed]
php-gd/jammy,now 2:8.1+92ubuntu1 all [installed]
php-mbstring/jammy,now 2:8.1+92ubuntu1 all [installed]
php-mysql/jammy,now 2:8.1+92ubuntu1 all [installed]
php-xml/jammy,now 2:8.1+92ubuntu1 all [installed]
php-zip/jammy,now 2:8.1+92ubuntu1 all [installed]
php8.1-bcmath/jammy-updates,jammy-security,now 8.1.2-1ubuntu2.8 amd64 [installed]
php8.1-cli/jammy-updates,jammy-security,now 8.1.2-1ubuntu2.8 amd64 [installed]
php8.1-common/jammy-updates,jammy-security,now 8.1.2-1ubuntu2.8 amd64 [installed]
php8.1-curl/jammy-updates,jammy-security,now 8.1.2-1ubuntu2.8 amd64 [installed]
php8.1-gd/jammy-updates,jammy-security,now 8.1.2-1ubuntu2.8 amd64 [installed]
php8.1-mbstring/jammy-updates,jammy-security,now 8.1.2-1ubuntu2.8 amd64 [installed]
php8.1-mysql/jammy-updates,jammy-security,now 8.1.2-1ubuntu2.8 amd64 [installed]
php8.1-opcache/jammy-updates,jammy-security,now 8.1.2-1ubuntu2.8 amd64 [installed,automatic]
php8.1-readline/jammy-updates,jammy-security,now 8.1.2-1ubuntu2.8 amd64 [installed,automatic]
php8.1-xml/jammy-updates,jammy-security,now 8.1.2-1ubuntu2.8 amd64 [installed]
php8.1-zip/jammy-updates,jammy-security,now 8.1.2-1ubuntu2.8 amd64 [installed]
php8.1/jammy-updates,jammy-security,now 8.1.2-1ubuntu2.8 all [installed,automatic]
php/jammy,now 2:8.1+92ubuntu1 all [installed]