Skip to content

Install LAMP on Ubuntu 22.04

LAMP Overview

In this tutorial, I will explain how to install a LAMP server on Ubuntu 22.04.

LAMP stands for Linux, Apache, MariaDB, and PHP. It is a set of software commonly used to run a web server.

With LAMP, you can operate:

  • The most famous CMS like WordPress
  • GLPI
  • Nextcloud
  • Applications using Symfony, CakePHP

For the Linux part, I will use the Ubuntu 22.04 distribution.

In addition to installing the components, I will provide some explanations and configuration elements in this tutorial.

I will not cover installing Ubuntu in this tutorial, so we will go directly to the AMP software part.

Apache2 – Web Server

Apache2 will be the web service in charge of processing HTTP and HTTPS requests.

Installing Apache2 on Ubuntu 22.04

To install Apache2, enter the following command:

sudo apt install apache2

Confirm the installation of packages. You can add -y to the installation command to skip confirmation.

The Apache2 server is now installed.

Test Apache2

The easiest way to test its operation is to access the server.

Launch a browser and enter the server’s IP address as the URL: http://w.x.y.z/

You should see the following page:

The index page is located in the following folder: /var/www/html

This folder contains the default site content.

Apache2 Configuration Files

Apache configuration files on Ubuntu 22.04 are located at: /etc/apache2

The main Apache2 configuration file is apache2.conf.

On Ubuntu and Debian, we have several folders named:

  • xxxxx-available
  • xxxxx-enabled

The xxxxx-available folders contain configuration elements that are not yet applied to Apache2, while the xxxxx-enabled folders contain the configurations that are currently loaded.

To apply a configuration, we generally create a symbolic link from the file located in xxxxx-available to the xxxxx-enabled folder.

Useful Commands for Apache2

Here is a list of useful commands for managing Apache2:

View service status:

sudo systemctl status apache2

Reload Apache2 configuration:

sudo systemctl reload apache2

Stop the Apache2 service:

sudo systemctl stop apache2

Start the Apache2 service:

sudo systemctl start apache2

Restart the Apache2 service:

sudo systemctl restart apache2

Enable Apache2 to start on boot:

sudo systemctl enable apache2

Test the Apache2 file configuration:

sudo apachectl configtest

Apache2: Create a Virtual Host Based on Name

Adding a virtual host allows you to run multiple websites on the same Apache2 web server.

Since this is name (URL) based, multiple virtual hosts can use the same IP address and ports 80 and 443. Apache2 will use the URL (domain name) to process requests and deliver the correct content.

To illustrate this tutorial, I will create a virtual host based on the name which will have the URL http://w.x.y.z/

Start by creating a folder to store the files:

sudo mkdir /var/www/lamp

In the /etc/apache2/sites-available folder, create a file that will contain the website configuration:

sudo nano /etc/apache2/sites-available/lamp.test.conf

Here is the content of the file:

lamp.test.conf

Items to edit:

  • Line 2: ServerName is the domain name of the website
  • Line 5: DocumentRoot indicates the path where the site files are stored on the server (corresponds to the folder created previously).
  • Line 7 and 8: Name of Apache2 log files

Now, create a symbolic link of the file in the /etc/apache2/sites-enabled folder to activate the virtual host configuration.

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

Test Apache 2 configuration:

sudo apachectl configtest

If no blocking error is returned, restart the Apache2 service:

sudo systemctl restart apache2

You now have a site that can respond to the URL http://w.x.y.z/

For the virtual host to work, you must point the domain name to the IP of the Apache2 server and create at least an index.html file.

Add UFW Rules for Apache2

If you’ve enabled the firewall on Ubuntu 22.04 with UFW, you’ll need to add rules to allow traffic to Apache ports 80 and 443.

Here are a couple of ways to do this:

Using the predefined apps:

sudo ufw allow Apache Full

Or, by specifying the ports to allow traffic on ports 80 and 443:

sudo ufw allow 80
sudo ufw allow 443

MariaDB – Database Server

We will now move on to the database server, which will store the data. In this tutorial, I will use MariaDB. For MySQL, it is almost the same process.

Install MariaDB on Ubuntu 22.04

To install the MariaDB database server, enter the following command:

sudo apt install mariadb-server -y

In the capture, I did not include the -y option, so I had to confirm the choice.

Once the installation is complete, check the operation of the MariaDB server:

sudo systemctl status mariadb

The MariaDB server is now installed and functional.

Creating a Database and a User

Now, we will create a database (lamp) and a user (lamp) on the MariaDB server.

To begin, connect to the MariaDB server with the following command:

sudo mysql

We will use SQL to create the database, the user, and assign rights.

To create the database:

CREATE DATABASE lamp;

To verify that the database has been created, enter the following query:

SHOW DATABASES;

Next, create the lamp user with the password lamp-password, who can connect from any IP address:

CREATE USER 'lamp'@'%' IDENTIFIED BY 'lamp-password';

The % allows connection from any IP address. To limit the connection to local, replace % with localhost.

Now, grant the user rights to the database with the following requests:

GRANT ALL PRIVILEGES ON lamp.* TO 'lamp'@'%';
FLUSH PRIVILEGES;

The first request gives all rights to tables (.*) in the lamp database to the user ‘lamp’@’%’ The second request allows you to reload the rights. On MariaDB, a user is identified by his login and where he can connect, this is why the user is xxx@yyy and not just xxx, which allows different rights to be assigned depending on where the user logs in.

To view a user’s permissions:

SHOW GRANTS FOR 'lamp'@'%';

MariaDB add-ons

Here is some additional information about MariaDB.

The configuration files are located in the following folder: /etc/mysql

For compatibility reasons, the folder name is mysql

In the commands, you can use either mysql or mariadb.

The database files are located in: /var/lib/mysql.

The MariaDB logs are located in the folder: /var/log/mysql.

PHP

The last component of the LAMP stack is PHP, a widely used programming/scripting language in development.

With Apache, there are two ways to make PHP work:

  • PHP module: Integrates PHP through an Apache module requiring no configuration.
  • PHP-FPM: Operates as an independent service, allowing more flexibility and configuration.

In this tutorial, I’ll show you both methods.

Personally, I prefer to use PHP-FPM, which runs as a separate service, allowing you to dissociate Apache from PHP. Additionally, PHP-FPM enables you to run several different versions of PHP simultaneously.

At the time of writing this tutorial, the version provided in the Ubuntu 22.04 repositories is PHP 8.1.

If you want another version of PHP, you must use the repository ppa:ondrej/php. Install PHP as Apache module

To install PHP enter the following command:

sudo apt install php libapache2-mod-php php-mysqli -y

Once the installation is complete, enter the following command to test PHP:

php -v

This command displays the PHP version

We will now test PHP through Apache with a php file that we will put in the folder /var/www/html.

Create the phpinfo.php file

sudo nano /var/www/html/phpinfo.php

Copy the following content to the file:

phpinfo.php

From a browser go to the following address: http://IP/phpinfo.php

The following page is displayed:

Server API tells us Apache 2.0 Handler, which indicates that the Apache PHP module was used

PHP-FPM with Apache2

Now, we will see how to use PHP-FPM with Apache2.

If you have installed the Apache PHP Module we will first deactivate it:

sudo a2dismod php8.1

Restart Apache :

sudo systemctl restart apache2

To validate that the PHP module is disabled, display the phpinfo.php page. You should see the code on the page because the PHP code is no longer being interpreted.

Install PHP-FPM:

sudo apt install php php-fpm php-mysql

Check that the php-fpm service is started:

sudo systemctl status php8.1-fpm

To work, you must activate two modules on Apache:

sudo a2enmod proxy_fcgi proxy

Restart Apache to take into account the new modules:

sudo systemclt restart apache2

We will be able to move on to the configuration of the virtualhost, before we will display the PHP processes:

ll /run/php

Here I only have one process for PHP 8.1 in FPM (php8.1-fpm.sock)

Now, you must tell Apache that the .php files must be processed by php-fpm.

This directive is done in the virtualhost files.

Here, we will modify the default virtualhost:

sudo nano /etc/apache2/sites-available/000-default.conf

In the VirtualHost part add:

apache-php-fpm.conf

Test Apache configuration:

sudo apachectl configtest

Restart Apache:

sudo systemctl restart apache2

Show the phpinfo.php page again.

Here we can see at the Server API level that we have FPM/FastCGI

Source: https://rdr-it.io/en/ubuntu-22-04-install-a-lamp-server-linux-apache-mariadb-php/

Feedback