Root-Server Tutorial

ROOT-SERVER TUTORIAL

Install Apache2 with PHP8.2-fpm

Learn how to install Apache2 with PHP8.2-fpm.

Author:

Martin Hofmann

Last updated:

26/03/2023

Introduction

This tutorial describes how to install Apache2 with php8.2-fpm on an Ubuntu Server. The tutorial has only been tested on Ubuntu 22.04, but should also work on Ubuntu 18.04 and 20.04.

Requirements

  • Server (Root or VPS) with Ubuntu Server 22.04.

Step 1 - Check for system updates

Check your system for updates and install them by running this command:

apt-get update && sudo apt-get -y upgrade

Step 2 - Install Apache2

Install Apache2 by running the following command:

apt-get install apache2

Step 3 - Install PHP8.2-FPM

Because PHP8.2 is not yet included in the Ubuntu repository, we need to add the respective PPA repository by running:

add-apt-repository ppa:ondrej/php

Next, update package sources:

apt-get update

Now, we will install PHP8.2 with some commonly used modules (bz2,curl,mbstring,intl,bcmath,xml,mysql,zip,gd,mbstring,imagick). You can add or remove modules if you already know your specific requirements:

apt-get install php8.2-{fpm,cli,bz2,curl,mbstring,intl,bcmath,xml,mysql,zip,gd,mbstring,imagick}

Next, enable the Apache2 php8.2-fpm confic, as well as the mods proxy_fcgi and setenvif by running:

a2enmod proxy_fcgi setenvif
a2enconf php8.2-fpm 

Restart Apache2:

systemctl reload apache2

Edit the following file by running:

nano /var/www/html/test.php

And add:

<?php
    phpinfo();
?>

Now, use a browser and head to http://ip_of_your_server/test.php
Note that the server API should be FPM or FastCGI.

Check PHP

At this point, you have successfully installed Apache2 with php8.2-fpm. Note that the path of the php.ini is '/etc/php/8.2/fpm/php.ini'.

If you need, we can increase the memory limit and the maximum file size for uploads as well as other values by editing:

nano /etc/php/8.2/fpm/php.ini
- memory_limit = 128M
+ memory_limit = 256M
- upload_max_filesize = 2M
+ upload_max_filesize = 16M
- post_max_size = 8M
+ post_max_size = 16M

To apply the changes, restart php8.2-fpm:

/etc/init.d/php8.2-fpm restart

Now, verify the values via the test.php script.

Step 4 - (Optional) Add a new virtual host

The server is only listening on IP. If you have a domain, we can add it as a new virtual host. To do this, run:

mkdir /var/www/vhostname
nano /etc/apache2/sites-available/vhostname.conf

Insert the following content and replace vhost and yourdomain.de with your data:

<VirtualHost *:80>
        ServerName www.yourdomain.de
        ServerAlias yourdomain.de #Optional Alias the Server is listen too
        ServerAdmin webmaster@yourdomain.de
        DocumentRoot /var/www/vhostname

        ErrorLog ${APACHE_LOG_DIR}/vhostname_error.log
        CustomLog ${APACHE_LOG_DIR}/vhostname_access.log combined

        <Directory "/var/www/vhostname">
            AllowOverride All
        </Directory>
</VirtualHost>

You need to enable the virtual host and restart Apache2:

a2ensite vhostname
systemctl reload apache2

Step 4.1 - (Optional) Install a TLS Certificate from Let's Encrypt

To secure your server via a TLS certificate, simply install Certbot:

apt-get install certbot python3-certbot-apache

Next, generate and install the TLS certificate by running the following command and following the steps provided. Please note that you will have to replace yourDomain.de,www.yourAlais.de with your specific data:

certbot --apache -d yourDomain.de,www.yourAlais.de

Step 5 - (Optional) Enable HTTP/2 Protocol

If needed, you can easily enable the Apache2 HTTP2 Module by simply running:

a2enmod http2
systemctl restart apache2

Conclusion

You have now installed Apache2 with PHP8.2-FPM. Optionally, you have also installed a TLS certificate with HTTP/2 Protocol. If you have multiple domains you can repeat Step #4 to add these domains. Please keep in mind that the server is not yet secure and you should take additional steps to ensure its security. This tutorial is only focused on the installation of Apache2 with php8.2-fpm.

Licence

MIT

Copyright (c) 2021 netcup

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Contributor's Certificate of Origin

By making a contribution to this project, I certify that:

  1. The contribution was created in whole or in part by me and I have the right to submit it under the licence indicated in the file; or

  2. The contribution is based upon previous work that, to the best of my knowledge, is covered under an appropriate licence and I have the right under that licence to submit that work with modifications, whether created in whole or in part by me, under the same licence (unless I am permitted to submit under a different licence), as indicated in the file; or

  3. The contribution was provided directly to me by some other person who certified (a), (b) or (c) and I have not modified it.

  4. I understand and agree that this project and the contribution are public and that a record of the contribution (including all personal information I submit with it, including my sign-off) is maintained indefinitely and may be redistributed consistent with this project or the licence(s) involved.

Published 26/03/2023 by Martin Hofmann

Submit your tutorial

Get 60€ netcup vouchers for every published tutorial.