How to install and set up PHP and Apache(LAMP stack) on Ubuntu 22.04

In this guide we are going to Install and set up Apache virtual host to serve PHP content on a Ubuntu 22.04 system.

The Apache HTTP Server(Apache), is one of the most popular free and open-source cross-platform web server software, released under the terms of Apache License 2.0. Apache is popular as part of the LAMP setup, being the A in the Acronym. The apache server functionality can be extended with the many available modules.

PHP is a general-purpose scripting language geared towards web development. It is one of the popular programming languages for the web. Popular tools such as WordPress are coded using php. Big companies like Facebook also uses php heavily.

Related Content:

Prerequisites

To follow along this guide, please ensure you have the following:

  • An uodated Ubuntu 22.04 server
  • Root access to the server or User with sudo access
  • Internet access

Ensuring that the server is up to date

Before proceeding, it is always a good practice to ensure that the server is up to date. Use these commands to achieve this

1
2
sudo apt update
sudo apt upgrade -y

Installing PHP on Ubuntu 22.04

PHP is available in the default Ubuntu 22.04 repos. The latest version of PHP as of writing this blog is PHP 8.1.

Install PHP and some common packages using this command:

1
2
3
4
sudo apt install -y \
    php \
    php-common \
    php-cli

Check the installed version of PHP

1
2
3
4
5
6
7
8
$ apt-cache policy php
php:
  Installed: 2:8.1+92ubuntu1
  Candidate: 2:8.1+92ubuntu1
  Version table:
 *** 2:8.1+92ubuntu1 500
        500 http://europe-west1.gce.archive.ubuntu.com/ubuntu jammy/main amd64 Packages
        100 /var/lib/dpkg/status

Check the installed php version using this command:

1
2
3
4
5
$ php -v
PHP 8.1.2 (cli) (built: Aug  8 2022 07:28:23) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.2, Copyright (c), by Zend Technologies

Installing apache on Ubuntu 22.04

Apache packages are available in the default Ubuntu 20.04 repositories as apache2. Install it using this command:

1
sudo apt install -y apache2

Confirm the installed packages using this command:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
$ apt-cache policy apache2
apache2:
  Installed: 2.4.52-1ubuntu4.1
  Candidate: 2.4.52-1ubuntu4.1
  Version table:
 *** 2.4.52-1ubuntu4.1 500
        500 http://europe-west1.gce.archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages
        500 http://security.ubuntu.com/ubuntu jammy-security/main amd64 Packages
        100 /var/lib/dpkg/status
     2.4.52-1ubuntu4 500
        500 http://europe-west1.gce.archive.ubuntu.com/ubuntu jammy/main amd64 Packages

The service will be started by default. Check the status using this command:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
$ sudo systemctl status apache2
● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2022-10-20 18:50:48 UTC; 3min 36s ago
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 113972 (apache2)
      Tasks: 34 (limit: 4392)
     Memory: 34.9M
        CPU: 308ms
     CGroup: /system.slice/apache2.service
             ├─113972 /usr/sbin/apache2 -k start
             ├─114005 /usr/sbin/apache2 -k start
             ├─114006 /usr/sbin/apache2 -k start
             ├─114007 /usr/sbin/apache2 -k start
             ├─114008 /usr/sbin/apache2 -k start
             ├─114009 /usr/sbin/apache2 -k start
             └─114010 /usr/sbin/apache2 -k start

Oct 20 18:50:47 unstable-ubuntusrv systemd[1]: Starting The Apache HTTP Server...
Oct 20 18:50:48 unstable-ubuntusrv systemd[1]: Started The Apache HTTP Server.

To enable the service on boot, use this command

1
sudo systemctl enable apache2

To confirm that Apache is installed and working fine, please visit the server’s IP address in the browser (http://server_ip). If you don’t know the server IP, get it using this command in your terminal:

1
curl -4 icanhazip.com

If all is well, you should see the Apache2 Ubuntu Default Page.

Configuring virtual hosts

With Apache, it is possible to host multiple sites on the same server where the Apache is running. This can always be achieved using multiple domains each representing a site that the server is hosting.

Apache virtual hosts are similar to Nginx server blocks. Each virtual host will have configuration details for a single host. You can have as many virtual hosts as you want.

In this example, we will use the domain site1.citizix.com for the virtual host but be free to use the domain of your choice. Please note that the DNS for the domain to be used should already be configured to point to the IP address of the server. Confirm that with:

1
dig -t A site1.citizix.com

Creating directory and sample content

The default page of apache is configured as a default virtual host serving content from /var/www/html. It’s a best practice to create the directory of your site in the /var/www directory since its a best practice to serve from there.

Lets create a directory for our site with the following command:

1
sudo mkdir /var/www/site1.citizix.com

Lets create a simple index.php page to serve from our site. You will require vim installed for this to work, if not use this command:

1
sudo apt install -y vim

Edit the file:

1
sudo vim /var/www/site1.citizix.com/index.php

Add content to the file:

1
<?php phpinfo(); ?>

Finally, lets make sure that the$USER user owns the site directory:

1
sudo chown -R $USER:$GROUP /var/www/site1.citizix.com

Creating virtual host

Apache Virtual host files specifies the configuration of each sites and tell the Apache web server how to respond to various domain requests.

The Apache configurations will be stored in the directory /etc/httpd/conf.d/. Lets create a config for our site:

1
sudo vim /etc/apache2/sites-available/site1.citizix.com.conf

Add the following content:

1
2
3
4
5
6
7
8
<VirtualHost *:80>
    ServerName site1.citizix.com
    ServerAlias site1.citizix.com
    DocumentRoot /var/www/site1.citizix.com
    DirectoryIndex index.php
    ErrorLog /var/log/apache2/site1.citizix.com.error.log
    CustomLog /var/log/apache2/site1.citizix.com.requests.log combined
</VirtualHost>

This will tell Apache the following:

  • ServerName and ServerAlias - name of the host to serve
  • DocumentRoot - where to find the root directly that holds the publicly accessible web documents
  • DirectoryIndex - The file that apache will serve when the site is accessed
  • ErrorLog - where to store error logs for this site
  • CustomLog - where to store request logs for this particular site

Save and close the file when you are finished

Enable the site

1
2
3
4
$ sudo a2ensite site1.citizix.com
Enabling site site1.citizix.com.
To activate the new configuration, you need to run:
  systemctl reload apache2

Then reload apache2 config

1
sudo systemctl reload apache2

Testing the Virtual Host

Now that the configuration is in place, we need to test that everything is working as expected. Since we created the index.php file above, let us access it by going to http://server_url/. In my case this is http://site1.citizix.com/. You should see the php info page.

Conclusion

In this guide, we managed to set up Apache to serve PHP site on a Ubuntu 22.04 server.

comments powered by Disqus
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy