Hello, friends. In this opportunity, you will learn how to install Apache and PHP on Ubuntu 22.04. This process is simple, so we will approach it quickly.
What is Apache and PHP?
In a nutshell, Apache HTTPD is a software that allows us to deploy a web server to serve web pages and websites to the internet or a local network. It is widely known in the market and together with Nginx are the dominators of the market.
As it could not be otherwise, it is open source, which allows us to know the source code of the same, so we can use it in various projects at various levels. As it is oriented to servers all over the world, we can install it on Windows, macOS, BSD, and Linux.
Thus, the study of Apache is considered elementary for dealing with web servers and everything related to web programming.
However, Apache by itself cannot serve web applications created with a programming language. Apart from Apache, we have to install an interpreter of the programming language in which the new application has been created. Of the web-oriented programming languages, the most popular is PHP.
PHP is a programming language for the web that is also open source, free and multiplatform. It allows us to add functionality to the website by processing data.
Both form the basis of a functional web server capable of processing data.
Install Apache on Ubuntu 22.04
Before you begin, make sure your server is fully up-to-date.
sudo apt update
sudo apt upgrade
Since Apache is present in the official repositories, we can install it by running
sudo apt install apache2
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
apache2-bin apache2-data apache2-utils bzip2 file libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap liblua5.3-0 libmagic-mgc libmagic1 mailcap
mime-support ssl-cert
Suggested packages:
apache2-doc apache2-suexec-pristine | apache2-suexec-custom www-browser bzip2-doc
The following NEW packages will be installed:
apache2 apache2-bin apache2-data apache2-utils bzip2 file libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap liblua5.3-0 libmagic-mgc libmagic1 mailcap
mime-support ssl-cert
0 upgraded, 16 newly installed, 0 to remove and 12 not upgraded.
Need to get 2,501 kB of archives.
After this operation, 16.1 MB of additional disk space will be used.
Do you want to continue? [Y/n]
The next step is to enable port 80
and 443
in the system firewall.
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
This is because port 80
is for HTTP and 443
is for HTTPS.
The next step is to start and enable Apache to start along with the system.
sudo systemctl enable --now apache2
Synchronizing state of apache2.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable apache2
Execute the above command, start it and give it permissions to start with the system at once.
Next, check the status of the Apache service
sudo systemctl status apache2

The ultimate test is to open a web browser and access http://localhost
if you installed Apache on your local computer or http://your-server-ip-or-domain
if you installed it on a server.

If you see the following screen, then the process was successful.
Install PHP on Ubuntu 22.04
As we said before, Apache by itself cannot serve websites created with PHP, so we have to install PHP.
Moreover, PHP is present in the official Ubuntu 22.04 repositories.
So, we can install it by running
sudo apt install php php-cli
With this, PHP will be installed, but we also need the library that links it to Apache.
sudo apt install libapache2-mod-php
In addition to this, you need to know that every application requires specific PHP libraries. Some of these are present in the official repositories, such as php-zip
or php-curl
but it will always depend on the application.
To complete the process, you can restart the server
sudo systemctl restart apache2
The final test will be to create a PHP file and see if the server processes it.
To achieve this, create the file in the Apache root document
sudo nano /var/www/html/test.php
And now add some PHP code
<?php
phpinfo();
?>
Save your changes and close the editor.
Open your web browser and try opening http://localhost/test.php
or http://your-server-ip-or-domain/test.php
.
And you should see something like this

So everything went well.
Conclusion
Apache and PHP are two main components of any server. In this post, we have guided you on how to install them smoothly on an Ubuntu 22.04 server.