升级Ubuntu服务器导致网页显示源代码

升级Ubuntu服务器导致wordpress网页的源代码,本文给出该问题的解决方法。

1. 问题描述

很久没打理个人博客了,决定升级wordpress,但因php版本太低不能升级成功。刚开始想的是仅升级php,搞了很久还是没成功,于是决定升级整个Ubuntu服务器,从14.04 LTS一路升级到18.04 LTS(需要升级两次,先升级到16.04 LTS)。

使用命令sudo do-release-upgrade升级Ubuntu Server,其他按提示操作即可。升级成功后,可以使用命令lsb_release -d查看Ubuntu的版本,使用uname -r查看内核版本。

$ lsb_release -d
Description:    Ubuntu 18.04.3 LTS

$ uname -r
4.15.0-74-generic

然而,升级完,访问本服务器上的任何一个网站,都只是显示PhP代码,举例如下:

<?php
/**
 * Front to the WordPress application. This file doesn't do anything, but loads
 * wp-blog-header.php which does and tells WordPress to load the theme.
 *
 * @package WordPress
 */

/**
 * Tells WordPress to load the WordPress theme and output it.
 *
 * @var bool
 */
define('WP_USE_THEMES', true);

/** Loads the WordPress Environment and Template */
require( dirname( __FILE__ ) . '/wp-blog-header.php' );

该页面实际上是wordpress/index.php的内容。

2. 问题解决

出现上述问题,是因为php页面没有正确解析。首先检查下php和apache是否安装正常,如下:

$ php -version 
PHP 7.2.24-0ubuntu0.18.04.1 (cli) (built: Oct 28 2019 12:07:07) ( NTS )

$ sudo apt-get install apache2
apache2 is already the newest version (2.4.29-1ubuntu4.11).

接下来,检查apache的配置文件。Ubuntu的apache配置文件不是httpd.conf,而是apache2.conf,位于/etc/apache2/目录下。

(1)加载模块

apache2.conf查看是否包含下面这一行,若无,添加。

LoadModule php7_module /usr/lib/apache2/modules/libphp7.2.so

注:使用命令apachectl -l可以查看apache启动时加载的模块。再者,用户可以在apache配置文件apache2.conf指定要加载的模块。这两部分加起来,就是apache加载的所有模块。使用命令apachectl -t -D DUMP_MODULES可以查看所有加载的模块,以我机器为例:

$ apachectl -t -D DUMP_MODULES
Loaded Modules:
 core_module (static)
 so_module (static)
 watchdog_module (static)
 http_module (static)
 log_config_module (static)
 logio_module (static)
 version_module (static)
 unixd_module (static)
 access_compat_module (shared)
 alias_module (shared)
 auth_basic_module (shared)
 authn_core_module (shared)
 authn_file_module (shared)
 authz_core_module (shared)
 authz_host_module (shared)
 authz_user_module (shared)
 autoindex_module (shared)
 deflate_module (shared)
 dir_module (shared)
 env_module (shared)
 filter_module (shared)
 headers_module (shared)
 mime_module (shared)
 mpm_prefork_module (shared)
 negotiation_module (shared)
 reqtimeout_module (shared)
 rewrite_module (shared)
 setenvif_module (shared)
 status_module (shared)
 wsgi_module (shared)
 php7_module (shared)

(2)添加文件类型

apache2.conf查看是否包含下面这两行,若无,添加。

AddType application/x-httpd-php-source .phps
AddType application/x-httpd-php .php

(3)重启apache

使用命令sudo service apache2 restart重启apache2。不出意外的话,现在应该一切正常了:-)

参考资料:

[1] apache - PHP code is not being executed, but the code shows in the browser source code - Stack Overflow

[2] Can't find httpd.conf

本文系Spark & Shine原创,转载需注明出处本文最近一次修改时间 2022-04-13 22:12

results matching ""

    No results matching ""