6
September
2022
Reinstalling the Apache2 web server in Linux Mint 21 and installation of PHP 8.1
16:34

Reinstalling the Apache2 web server in Linux Mint 21 and installation of PHP 8.1

6 September 2022 16:34

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

Problems with the launch of the web server Apache2

The problem was as follows - after reinstalling Linux Mint, the service was not launched "apache2":
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.
Logs show that the service does not find a configuration file /etc/apache2/apache2.conf.

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

Also, after a complete removal Apache2 the traces of the old installation remained: "which apache2"

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

Solving the problem

1) Delete the old version

sudo apt-get purge apache2

2) Delete traces of the past installation

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

3) Add the PPA-repository "ondrej/apache2" and update the cache of packages:

Comment. If the OS is Debian, then there is no command in it "apt-add-repository ".

Only in Debian (in other OSes the following not need to be done):

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

In Ubuntu and Linux Mint:

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

4) We 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 directive 'ServerName' in the configuration file:

sudo nano /etc/apache2/apache2.conf

Closer to the beginging of the file, add the following line:

ServerName linux-pc

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

Save the configuration Ctrl+O, Ctrl+X

6) Launch the Apache2 service:

sudo service apache2 start

7) We check the work of the web server, the opening of the browser and the transition to the address, be sure to add the prefix http: //

For example, http://linux-pc

8) set up virtual hosts in the folder "/etc/apache2/sites-available"

An example of a test file (Content without comment 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 catalog:

cd /etc/apache2/sites-enabled
sudo ln -s ../sites-available/test.conf test.conf

Check:

ls -l

10) In the Hosts file, add a pseudonym

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 work of the website by the transition to http://test

14) If, instead of the result of the PHP operation, the contents of the file are displayed (because the dynamic site in the PHP language),
you need to reinstall the PHP module for apache2

First, we 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 which PHP is needed for the project. For example, if the TWIG Twig engine has an old version, select 7.4, otherwise 8.1

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

I first install the PHP of the corresponding version and PHP for the command line.

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

I assign php 8.1 as "main" if there are several parallel configurations and versions of php:

sudo update-alternatives --config php

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

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

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, the command:

sudo a2enmod php8.1

In the console, messages about the successful registration of the module are displayed

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 site at http://test. If something doesn't work, we check the log files.

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

16) It is necessary to debug the program. Often errors are associated with the incompatibility of the old versions of Frameworks (in the Vendor folder) with the new version of PHP 8.1.
The remaining problems are solved in place in the code of the PHP program, based on the analysis of logs in the catalog /var/log/apache2.


Addition of 11/22/22: inclusion of PHP 8.1 in the Apache2 web server

If the apt upgrade command says that PHP 8.1 in Apache2 is not turned on 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.

Turn on the PHP 8.1 in Apache2:

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

Checking the PHP version:

php --version

Service status "The PHP 8.1 FastCGI Process Manager":

systemctl status php8.1-fpm

The result of the Phpinfo () team;
PHP Version 8.1.2-1ubuntu2.8



Related publications