How to install Apache and PHP on Ubuntu 22.04

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

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
1.- Apache service status on Ubuntu 22.04
1.- Apache service status on Ubuntu 22.04

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.

2.- Apache default page
2.- Apache default page

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

3.- Apache and PHP on Ubuntu 22.04
3.- Apache and PHP on Ubuntu 22.04

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.

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"
Angelo
Angelo
I am Angelo. A systems engineer passionate about Linux and all open-source software. Although here I'm just another member of the family.

7 COMMENTS

  1. Your description worked perfectly for me with Ubuntu 22.04 on a Dedicated VPS. Thank you! I am not familiar with LINUX… learning. Now I need to install MariaDB. What is a nice procedure [steps] and what command will ensure updates?

    • Hi. This post can be useful for you

      https://unixcop.com/how-to-install-mariadb-on-ubuntu-22-04/

      You can run from time to time

      sudo apt update
      sudo apt upgrade

  2. Fantastic, thank you. Unfortunately now I keep getting:

    Job for apache2.service failed.
    See “systemctl status apache2.service” and “journalctl -xeu apache2.service” for details.
    root@localhost:/etc/apache2/sites-available#

    I looked up the log and it keeps mentioning some file is not closed… but it seems closed to me.

    I don’t know what I did… and I can’t seem to undo. Know I can’t access http or https. I placed my ssl certificates into /etc/apache2/[mysitedomain]/. Then updated the file /etc/apache2/sites-available/default-ssl.con with the location of the “ssl-certs”.

    I’m clearly now enabling something. I’ve reloaded several times, and every time I get to “sudo a2enmod ssl” I get stuck. If I gave you access may I pay you to help?

  3. Hey, I was thinking to install a Control Panel like Ajenti… but even when I open port 8000 it simple never opens. Any ideas? Or can u help with that as well? The provider I’m using IONOS has a plex service but it doesn’t do well with upgrading php beyond php7.4 , I need php8+

  4. Thank you everything went exactly as you said.

    However all tests at every stage gave up: “No such file or directory”.

    Having installed Ubuntu and been met with failure after failure e.g. “waiting for Network configuration” (took ages to resolve) followed by nextcloud IP not being the same as or included in the trusted domains and the trusted domain not being anything to do with the setup, Apache and php failing, I’m uninstalling Ubuntu and reformatting the spare PC for Windows.

    Sorry but the convoulute getting nowhere with Ubuntu just isn’t worth it.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest articles

Join us on Facebook