6
September
2022
Reinstalling Apache2 web server on Linux Mint 21 and installing PHP 8.1
16:34

Reinstalling Apache2 web server on Linux Mint 21 and installing PHP 8.1

6 September 2022 16:34

After switching to Linux Mint 21 "Vanessa", which is based on the Ubuntu jammy package base, Apache2 stopped running.
How I restored the web server is described below.

Problems starting the Apache2 web server

The problem was the following - after reinstalling Linux Mint, the "apache2" service did not start:
sudo service apache2 start
Job for apache2.service failed because the control process exited with error code.
See "systemctl status apache2.service" and "journalctl -xeu apache2.service" for details.
The logs show that the service does not find the /etc/apache2/apache2.conf configuration file.

An attempt to fix it by reinstalling did not lead to anything:
sudo apt-get update && sudo apt-get purge apache2 && sudo apt-get install apache2

Also, after completely uninstalling Apache2, traces of the old installation of "which apache2" remained

  • should output an empty line, but the output was different, pointing to the /usr/sbin folder.

Solving the problem

1) Remove the old version

sudo apt-get purge apache2

2) Remove traces of the previous installation

sudo apt-get autoremove
sudo rm /usr/sbin/apache2

3) Add a PPA repository "ondrej/apache2" and update the package cache:

Comment. If the OS is Debian, then it does not have the "apt-add-repository" command by default.

only on Debian, others don't need to run first:

sudo apt-get install software-properties-common
sudo apt-get update

On Ubuntu and Linux Mint:

sudo apt-add-repository ppa:ondrej/apache2
sudo apt-get update

The ppa:ondrej/apache2 repository is required to install PHP versions 8.1, 8.2, 8.3, 8.4, or 8.5 on older OS versions (e.g., Linux Mint 21).
It is not necessary to install it on the new Linux Mint 22.3 "Zena" or Debian 13 "trixie" (PHP 8.4 is included in the default repositories).

4) Install Apache2

sudo apt-get install apache2

5) Check the configuration

sudo apache2ctl configtest

Error message:
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message

You need to specify the name or IP address of the web server in the 'ServerName' directive in the configuration file

sudo nano /etc/apache2/apache2.conf

Closer to the beginning of the file add the line

ServerName linux-pc

where linux-pc is the name that is displayed in the terminal after the @ symbol

Save the configuration Ctrl+O, Ctrl+X

6) Start the Apache2 service:

sudo service apache2 start

7) Check the operation of the web server by opening the browser and going to the address, always adding the http:// prefix

For example,http://linux-pc

8) Configure virtual hosts in the “/etc/apache2/sites-available” folder

Sample test file (contents without comments grep -v "^[[:blank:]]#" test.conf):

<VirtualHost *:80>
ServerName test
ServerAdmin webmaster@localhost
DocumentRoot /home/user/test/
<Directory "/home/user/test">
Options Indexes MultiViews
AllowOverride None
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/errortest.log
CustomLog ${APACHE_LOG_DIR}/accesstest.log combined
</VirtualHost>

9) create soft links from the sites-enabled directory:

cd /etc/apache2/sites-enabled

sudo ln -s ../sites-available/test.conf test.conf

We check:

ls -l

10) Add an alias in the hosts file

sudo nano /etc/hosts

127.0.0.1 test

Ctrl+O, Ctel+X

12) Re-read the configuration or restart the Apache2 service as follows:

sudo service apache2 reload

13) We check the operation of the website by going to http://test

14) If the contents of the file are displayed instead of the result of PHP work (since the site is in PHP language),

you need to reinstall the PHP module for Apache2

First, let's find out which PHP modules for Apache2 are available for installation:

apt-cache search libapache2-mod-php*

For example, there are modules for Apache2: php 7.4 and php8.1

Next, we decide what php is needed for the project. For example, if the Twig template engine has an old version, select 7.4, otherwise 8.1

libapache2-mod-php8.1 - scripting language embedded in HTML, executed on the server side (Apache 2 module)
libapache2-mod-php7.4 - transition package

I first install php of the appropriate version and php for the command line.

sudo apt-get install php8.1 php8.1-cli

I assign php 8.1 as “master” if there are several parallel configurations and php versions

sudo update-alternatives --config php

I remove the libapache2-mod-php8.1 module along with libapache2-mod-php(this trick solves many problems):

sudo apt purge libapache2-mod-php8.1 libapache2-mod-php

I immediately install only one module libapache2-mod-php8.1:

sudo apt install libapache2-mod-php8.1

We allow the launch of the php8.1 module for Apache with the command:

sudo a2enmod php8.1

The console displays messages about successful module registration

Considering dependency mpm_prefork for php8.1:

Considering conflict mpm_event for mpm_prefork:

Considering conflict mpm_worker for mpm_prefork:

Module mpm_prefork already enabled

Considering conflict php5 for php8.1:

Module php8.1 already enabled

15) Open the website at http://test. If something doesn't work, check the logs

su -
cd /var/log/apache2
tail errortest.log

16)The program needs to be debugged. often errors are associated with the incompatibility of old versions of frameworks (vendor) with the new version of php 8.1.
Other problems are solved locally, in the PHP program code, based on analysis of logs in the directory /var/log/apache2.


Update from 11/22/22: Enabling PHP 8.1 in the Apache2 web server

If the apt upgrade command says that PHP 8.1 in Apache2 is not enabled by default

NOTICE: Not enabling PHP 8.1 FPM by default.

NOTICE: To enable PHP 8.1 FPM in Apache2 do:

NOTICE: a2enmod proxy_fcgi setenvif

NOTICE: a2enconf php8.1-fpm

NOTICE: You are seeing this message because you have apache2 package installed.

If such a message was displayed in the terminal, enable the proxy_fcgi and setenvif modules in Apache2 and the php8.1-fpm configuration file:

sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php8.1-fpm
sudo systemctl reload apache2

Checking PHP version:

php --version

"The PHP 8.1 FastCGI Process Manager" service status:

systemctl status php8.1-fpm

The result of the phpinfo() command;
PHP Version 8.1.2-1ubuntu2.8

The Apache 2 web server reinstallation is complete, and the PHP 8.1 scripting language interpreter is working!

Updated June 30, 2026. Installing Additional PHP Modules

Additional modules required to run website content management systems (CMS):

sudo apt install php8.1-common php8.1-opcache php8.1-readline \
php8.1-curl php8.1-dom php8.1-mbstring php8.1-simplexml php8.1-xml \
php8.1-zip php8.1-intl php8.1-yaml php8.1-apcu

Disabling the PPA Repository

After installing the PHP 8.1 language interpreter and additional PHP modules, the ppa:ondrej/apache2 repository can be disabled:

sudo apt-add-repository --remove ppa:ondrej/apache2
sudo apt update

Last modified: 30.06.2026



Related publications