Skip to main content

How to Install LAMP in Ubuntu 24.04 Noble Numbat

In this guide we will see how to install LAMP in Ubuntu 24.04 Noble Numbat using apt, the default package manager.

Introduction

In our Linux Support interventions, LAMP (Linux, Apache, MySQL, PHP), LEMP (NginX) stacks and the like are critical for Web Apps, CMS and e-commerce such as WordPress, Magento, Drupal and many others.

To install LAMP in Ubuntu 24.04 Noble Numbat we start by installing the Apache web server with HTTPS, the first component of the stack, which will be responsible for serving content.

To optimize web server performance, we recommend using PHP-FPM, mpm_event and HTTP2. Most websites use secure connections. We will also see how to generate a valid and free HTTPS certificate with Let's Encrypt.

Installing LAMP in Ubuntu 24.04

1. Install Apache2

By default, the mpm_prefork multi-process module is provided, which allows you to use mod_php or php8.3, the in-house interpreter. This approach is not recommended for heavily visited sites, since having reached the maximum number of threads, the web server stops responding until a thread is freed.

  1. Let's install Apache2

    sudo apt install apache2
  2. Let's make sure that the mpm_prefork module is disabled, as it is a performance bottleneck.

    sudo a2dismod mpm_prefork.
  3. Let's enable mpm_event and http2.

    sudo a2enmod mpm_event http2.
  4. Let's restart Apache2.

    sudo service apache2 restart

2. Install PHP-FPM

PHP-FPM is an alternative implementation of PHP FastCGI, known for its speed, three times faster than the classic module, with additional functionality. FPM is a real daemon that stays listening for PHP requests via a UNIX port or socket. It represents the P of our LAMP.

  1. Let's install FPM and FastCGI.

    sudo apt install php-fpm
  2. Let's enable the modules.

    sudo a2enmod actions proxy_fcgi alias headers.
  3. Let's restart the PHP FPM process.

    sudo service php8.3-fpm restart.
  4. Let's restart Apache2.

    sudo service apache2 restart

PHP-FPM Virtual Host Configuration.

You can now configure all Apache2 virtual hosts to use FPM by adding this handler in their definition:

<FilesMatch ".+\.ph(p[345]?|t|tml)$">
    SetHandler "proxy:fcgi://127.0.0.1:9000"
</FilesMatch>

Testing PHP with info.php:

<?php
  phpinfo();
  phpinfo(INFO_MODULES);
?>

Let's install the necessary modules and PHP-MySQL.

  1. We can choose to install some of the most common modules.

    sudo apt-get install php-pear \
                    php8.3-{mysql,curl,gd,intl,imap,mcrypt,ps,pspell,snmp,sqlite,tidy,xmlrpc,xsl}
  2. Alternatively, we can use the modules found in Plesk.

    sudo apt-get install php-pear \
                    php8.3-{bcmath,enchant,imap,ldap,mysql,curl,dba,enchant,gd,intl,ldap,mbstring,mcrypt,odbc,opcache,pgsql,sqlite3,pspell,soap,tidy,xml,xmlrpc,xsl,zip}

3. Install Let's Encrypt for Free SSL Certificates.

Using Let's Encrypt and the certbot daemon, you can generate universally recognized, secure and free SSL certificates. You need to configure the DNS name of the website to reach the IP of our server.

Example of DNS configuration.

To configure DNS, we access our provider's panel and create a record of type "A".

mydomain.ext A 1.1.1.1

We install Let's Encrypt and generate the SSL and TLS certificates.

sudo apt-get install certbot python3-certbot-apache

Let's create the Apache2 configuration.

sudo certbot --apache

Let's test the renewal of certificates.

sudo certbot renew --dry-run.

We check that Cron's scheduled process for renewal is present.

sudo cat /etc/cron.d/certbot.

If the resolution of our domain is correct, our virtual hosts will respond in HTTPS, returning the famous "green lock" to us on the browser.

Apache2 Virtual Hosts

HTTP Virtual Hosts

To configure an HTTP virtual host, we can use a simple definition like the one listed below. It is available in the /etc/apache2/sites-available directory as the default vhost, from which we can start.

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
HTTPS Virtual Hosts.

HTTPS virtual hosts include the necessary paths for using SSL and enabling PHP.

<VirtualHost *:443>
    ServerAdmin [email protected]
    DocumentRoot "/var/www/data/online"
    ServerName mydomain.ext
    ServerAlias www.mydomain.ext
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    Protocols h2 h2c http/1.1
    SSLEngine on
    Include /etc/letsencrypt/options-ssl-apache.conf
    SSLCertificateFile /etc/letsencrypt/live/mydomain.ext/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/mydomain.ext/privkey.pem
    <FilesMatch ".+\.ph(p[345]?|t|tml)$">
        SetHandler "proxy:fcgi://127.0.0.1:9000"
    </FilesMatch>
</VirtualHost>

4. Install MySQL or MariaDB

To install MySQL or MariaDB, we use the corresponding packages mysql-server and mariadb-server.

  1. To install MySQL server:

    sudo apt install mysql-server
  2. To install MariaDB (I personally prefer it):

    sudo apt install mariadb-server

For detailed configuration, you can refer to our MySQL installation and configuration guide for Ubuntu 24.04.

Looking for professional support?

Rely on one of our Linux Consultants, 30' analysis of your system for Free!

Conclusion

We managed to install LAMP in Ubuntu 24.04, now our server is ready to host a CMS or Web App

If you found this guide useful, please share it on social, others may need it.

Add new comment

Comment

  • Allowed HTML tags: <br> <p> <code class="language-*"> <pre>
  • Lines and paragraphs break automatically.
  • Only images hosted on this site may be used in <img> tags.