21+ years in hosting · Jaipur, India · INR billing with 18% GST · 24/7 email support
Blog · WebJi® India

How to Install LAMP on Ubuntu 22.04 and 24.04

Quick answer

To install LAMP on Ubuntu 22.04 or 24.04, run sudo apt install apache2, then mysql-server or mariadb-server, then php libapache2-mod-php php-mysql. Ubuntu 22.04 ships PHP 8.1 and 24.04 ships PHP 8.3. Add a vhost in /etc/apache2/sites-available, run a2ensite, and confirm a phpinfo() page renders.

For developers and VPS owners building Apache, MySQL and PHP by hand on Ubuntu, with the checks that catch the usual first-day errors.

✓ In business since 2005
✓ INR + GST billing
✓ 24/7 Email Support — avg reply < 60 min
GuideWebJi®
knowledge
Read time
5 min
Sections
8
Updated
14 Nov 2026
Written by
WebJi® team

What does a LAMP stack on Ubuntu install?

A LAMP stack on Ubuntu 22.04 or 24.04 is four apt packages: apache2, mysql-server or mariadb-server, php, and the glue packages libapache2-mod-php and php-mysql. Install them in that order, because PHP's Apache module registers with a web server that already exists.

  • apache2 is Apache 2.4 and runs as apache2.service.
  • mysql-server gives MySQL 8.0 on both releases; mariadb-server gives MariaDB 10.6 on 22.04 and 10.11 on 24.04.
  • php is PHP 8.1 on 22.04 and PHP 8.3 on 24.04. Upstream security support for PHP 8.1 ended on 31 December 2025, and Ubuntu now patches its 8.1 package itself; PHP 8.3 has upstream security fixes until 31 December 2027.
  • libapache2-mod-php is mod_php, which runs PHP inside Apache with no separate FPM service.
  • php-mysql adds the mysqli and PDO drivers PHP uses to reach the database.

Everything here comes from the Ubuntu main repository, so no PPA is involved. A tutorial that starts by adding ondrej/php is installing a version Ubuntu does not ship, which is a different job.

How to install Apache on Ubuntu and open the firewall

Apache starts on its own after install and answers on port 80 straight away. The only real decision is the firewall order: ufw must allow SSH before you enable it, or the session you are typing in dies.

  1. Run sudo apt update, then sudo apt install apache2.
  2. Confirm with sudo systemctl status apache2: Active: active (running) and the unit enabled at boot.
  3. Allow SSH first with sudo ufw allow OpenSSH.
  4. Add the web profile with sudo ufw allow 'Apache Full', which opens 80 and 443 together, then sudo ufw enable.
  5. Open http://your-server-ip/ and look for the Apache2 Ubuntu Default Page.

No default page means the request never reached Apache. Check sudo ss -tlnp | grep :80 for a listener, then the provider's own firewall, before you edit any configuration file.

How to install and secure MySQL or MariaDB for LAMP

Pick one engine. For WordPress and most PHP applications either is fine, and the commands after the install are the same shape whichever you choose.

Run sudo apt install mysql-server or sudo apt install mariadb-server, then sudo systemctl status mysql (or mariadb). On Ubuntu the root account authenticates through the local socket, so sudo mysql opens a prompt without asking for a password.

  • sudo mysql_secure_installation drops anonymous users, blocks remote root login and removes the test database. On Ubuntu’s MySQL the root account keeps socket authentication, so the script’s password step does not change how sudo mysql logs in.
  • sudo mysql -e "SELECT VERSION();" prints the server version, which is the check worth keeping.
  • Leave the database on 127.0.0.1; nothing in a single-server LAMP install needs port 3306 exposed.

The application database and its dedicated user are a separate five-statement job. The MySQL on Ubuntu guide gives those statements with the exact grants and the error each one throws.

How to install PHP 8.x for Apache on Ubuntu

One apt command installs PHP with the extensions a typical app expects. Which major version you get depends on the release, not on the command.

  1. sudo apt install php libapache2-mod-php php-mysql php-cli php-curl php-mbstring php-xml php-zip php-gd.
  2. php -v prints the CLI version: 8.1.x on 22.04, 8.3.x on 24.04.
  3. apache2ctl -M | grep php should list php_module. If it does not, run sudo a2enmod php8.3 (or php8.1) and restart Apache.
  4. Edit /etc/apache2/mods-enabled/dir.conf so DirectoryIndex starts with index.php instead of index.html.
  5. sudo apache2ctl configtest must print Syntax OK, then sudo systemctl reload apache2.

That DirectoryIndex order explains the blank page or file listing people hit right after copying an app: Ubuntu lists index.html first, and Apache stops at the first name it finds.

How to create an Apache virtual host and set ownership

The default vhost serves /var/www/html. Give a real domain its own directory and config file before you upload anything, so ownership and paths are right from the first request.

  1. sudo mkdir -p /var/www/example.com/public_html.
  2. sudo chown -R www-data:www-data /var/www/example.com, then sudo find /var/www/example.com -type d -exec chmod 755 {} \; and sudo find /var/www/example.com -type f -exec chmod 644 {} \; so folders and files get different modes.
  3. Create /etc/apache2/sites-available/example.com.conf with a <VirtualHost *:80> block holding ServerName, ServerAlias, DocumentRoot, and a <Directory> block with Require all granted, AllowOverride All and Options -Indexes +FollowSymLinks.
  4. sudo a2ensite example.com.conf, then sudo a2dissite 000-default.conf when this is the only site.
  5. sudo apache2ctl configtest, then sudo systemctl reload apache2. apache2ctl -S shows which vhost claims the name.

Permissions are the other half of the job: directories at 755 so Apache can walk into them, files at 644, owner www-data. Setting 777 to make an upload pass is how a small permission problem becomes a security one.

How to test PHP on the LAMP stack with phpinfo

A single file proves that Apache, mod_php and the handler chain all agree. Put it in the site root once a vhost is live, not in /var/www/html.

  1. echo '<?php phpinfo(); ?>' | sudo tee /var/www/example.com/public_html/info.php.
  2. Open http://example.com/info.php. You want a table headed PHP Version 8.3.x with Server API: Apache 2.0 Handler.
  3. Note Loaded Configuration File and memory_limit while the page is open; that is the php.ini your app actually reads.
  4. sudo rm /var/www/example.com/public_html/info.php before you close the terminal.

If the browser offers to download the file instead of rendering it, mod_php is not enabled for that site: run sudo a2enmod php8.3, reload Apache and test the same URL again.

How to fix 403 and 500 errors on a new LAMP server

Both errors sit in /var/log/apache2/error.log with a reason on the same line. Read that line first, because the browser page never carries enough detail to fix anything.

Fix 403 Forbidden and 500 Internal Server Error
SymptomCauseFix
403 on every file in the vhostAH01630 client denied by server configuration, or a parent folder without the execute bitAdd Require all granted to the Directory block, then chmod o+x on each parent path
403 on the site root onlyNo file matches DirectoryIndex and Options -Indexes blocks a listingUpload index.php, put it first in dir.conf, then reload Apache
500 with no PHP error in the pageInvalid command 'php_value' from a .htaccess written for mod_php on a php-fpm hostRemove the php_* lines, or move that vhost to mod_php
500 straight after a2ensiteA typo in the vhost, such as an unclosed </Directory>apache2ctl configtest prints the file and the line number
404 for a site that existsThe default vhost still answers for that namea2dissite 000-default.conf, then apache2ctl -S to confirm the match
Access denied for user from PHPThe app connects as a user never granted that schemaCreate the user at localhost and GRANT on that one database

One habit prevents most of these: change a single thing, run sudo apache2ctl configtest, reload, and look at the site. A reload costs nothing, while a restart on a busy server drops open connections.

Where should a LAMP stack run: VPS or cPanel hosting?

Root access, your own PHP version and a database you can tune are the whole point of LAMP, and shared hosting gives you none of them. A WebJi® Linux VPS gives you root, so the stack above is yours to build and patch; confirm the Ubuntu release you want when you request the quote.

If you would rather not maintain Apache and MySQL by hand, cPanel hosting puts the same stack behind a panel that handles vhosts, updates and mail. VPS and managed plans are quoted by email at support@webji.services before payment, in INR before 18% GST, and the contact page reaches the team in Hindi or English.

Sources: Ubuntu package archive (packages.ubuntu.com) for the apache2, mysql-server, mariadb-server and php versions in jammy and noble; PHP “Supported Versions” page (php.net/supported-versions.php); Apache HTTP Server 2.4 documentation for DirectoryIndex, Require and Options.

FAQ

Frequently asked questions

How do I install a LAMP stack on Ubuntu 22.04 or 24.04?

Run sudo apt update, then install three layers in order: sudo apt install apache2, sudo apt install mysql-server (or mariadb-server), and sudo apt install php libapache2-mod-php php-mysql. Ubuntu 22.04 gives PHP 8.1 and 24.04 gives PHP 8.3. Finish with a vhost, an a2ensite, and a phpinfo page you delete after testing.

In what order should I install Apache, MySQL and PHP?

Apache first, database second, PHP last. libapache2-mod-php registers itself with Apache when it installs, and php-mysql needs a database client library that arrives with either MySQL or MariaDB. The order does not break anything if you reverse the last two, but PHP before Apache means an extra a2enmod and reload.

How do I open the firewall for Apache on Ubuntu?

Run sudo ufw allow OpenSSH before you enable ufw, then sudo ufw allow 'Apache Full', which opens 80 and 443 in one rule, and sudo ufw enable. Confirm with sudo ufw status. If you enable ufw first on a remote server, the SSH session drops and the provider console is the only way back.

How do I test that PHP works with Apache?

Create info.php in the site root with phpinfo() in it, open it in a browser, and look for PHP Version and Server API: Apache 2.0 Handler. Delete the file straight after. If the browser downloads the file instead of rendering it, run sudo a2enmod php8.3 (or php8.1 on 22.04) and reload Apache.

MariaDB or MySQL for an Ubuntu server?

Either works for WordPress and most PHP apps. mysql-server installs MySQL 8.0 on both releases; mariadb-server installs MariaDB 10.6 on 22.04 and 10.11 on 24.04. MariaDB keeps the smaller memory footprint and the MySQL-compatible command line, while MySQL 8.0 has the newer JSON and window-function behaviour some apps now expect.

Why do I get 403 Forbidden after adding a vhost?

The Apache error log names it: AH01630 client denied by server configuration means the Directory block lacks Require all granted, or a parent folder of the DocumentRoot is missing the execute bit. Run sudo chmod o+x on each parent path, reload Apache, and check that DirectoryIndex lists index.php before index.html.

Why do I get 500 Internal Server Error on a new LAMP site?

Read /var/log/apache2/error.log; the line above the 500 says why. The two common causes are a syntax error in the vhost, which apache2ctl configtest reports with a line number, and php_value or php_flag in .htaccess on a host running php-fpm instead of mod_php, which logs Invalid command 'php_value'.

Last editorial update: 14 November 2026. Prices follow the dated sources on this page; checkout confirms current charges.

WebJi® since 2005

Ready to get started with WebJi®?

Join 12,000+ businesses across India and overseas. INR pricing, 18% GST shown before payment and 24/7 email support in Hindi and English — average first reply under 60 minutes.