Servers & VPS

How to Install LAMP Stack on a VPS with Ubuntu

Install Apache, MySQL (MariaDB), and PHP on your Ubuntu VPS in under 20 minutes with this straightforward step-by-step tutorial.

Numerous wires and cables mounted into server patch panel in modern data center

To install LAMP on an Ubuntu VPS, run four commands in sequence: install Apache, then MariaDB, then PHP with its modules, and finally enable and test each component. In under 20 minutes you'll have a complete environment ready to host PHP-based websites.

This tutorial assumes you already have SSH access to your server and an updated OS. If you've just provisioned a VPS and haven't done that yet, check out our VPS initial setup guide first.

What Is the LAMP Stack?

LAMP is an acronym for four open-source technologies that together form the most widely used web hosting environment:

  • Linux — the operating system (Ubuntu in this case)
  • Apache — the web server that handles HTTP/HTTPS requests
  • MySQL / MariaDB — the relational database
  • PHP — the server-side programming language

WordPress, Joomla, Drupal, PrestaShop, and thousands of PHP applications run on LAMP. It's the de facto standard stack for PHP projects worldwide.

Step 1 — Install Apache

Update the package index and install Apache:

sudo apt update
sudo apt install apache2 -y

Enable Apache to start automatically on boot and verify it's running:

sudo systemctl enable apache2
sudo systemctl status apache2

You should see Active: active (running). Open a browser and visit http://YOUR_SERVER_IP — you'll see Apache's welcome page with the "It works!" message.

Open firewall ports

If UFW is active (recommended), allow web traffic:

sudo ufw allow 'Apache Full'

This opens both port 80 (HTTP) and port 443 (HTTPS) in a single command.

Step 2 — Install MariaDB

MariaDB is the community fork of MySQL, fully compatible and with better performance in most use cases. It's the recommended choice on modern Ubuntu:

sudo apt install mariadb-server -y
sudo systemctl enable mariadb
sudo systemctl status mariadb

Secure the installation

Run the security script bundled with MariaDB. It will ask a series of interactive questions:

sudo mysql_secure_installation

Recommended answers:

  • Enter current password for root: press Enter (no password initially)
  • Switch to unix_socket authentication: Y
  • Change the root password: N (socket auth is more secure)
  • Remove anonymous users: Y
  • Disallow root login remotely: Y
  • Remove test database: Y
  • Reload privilege tables: Y

Create a dedicated database and user

Never use the MariaDB root user in your application. Create dedicated credentials:

sudo mariadb

CREATE DATABASE my_site CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'my_user'@'localhost' IDENTIFIED BY 'strong_password';
GRANT ALL PRIVILEGES ON my_site.* TO 'my_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Step 3 — Install PHP and Essential Modules

Install PHP along with the modules most commonly needed by web applications:

sudo apt install php libapache2-mod-php php-mysql php-cli php-curl php-gd php-mbstring php-xml php-zip php-bcmath -y

Verify the installed version:

php -v

Ubuntu 22.04 ships with PHP 8.1; Ubuntu 24.04 ships with PHP 8.3. Both are stable and actively supported.

Restart Apache to load PHP

sudo systemctl restart apache2

Step 4 — Verify the LAMP Stack is Working

The definitive test is to create a phpinfo() file and view it in a browser:

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

Enter this content:

<?php
phpinfo();
?>

Save the file (Ctrl+O, Enter, Ctrl+X) and visit http://YOUR_SERVER_IP/info.php.

You'll see a table with all PHP information: version, loaded modules, configuration. Look for the mysql section to confirm the extension is active.

Delete the file when you're done — it exposes sensitive server information:

sudo rm /var/www/html/info.php

The PHP configuration file is at /etc/php/X.X/apache2/php.ini. These are the values worth adjusting for most web projects:

Directive Recommended value Purpose
upload_max_filesize 32M Maximum uploaded file size
post_max_size 64M Maximum POST body size
memory_limit 256M Maximum RAM per PHP process
max_execution_time 60 Maximum seconds per script
expose_php Off Hides PHP version in HTTP headers

After any change to php.ini, restart Apache:

sudo systemctl restart apache2

If you'd rather skip manual server configuration, elenlace.com offers managed VPS plans with PHP pre-optimized for production workloads.

Key Takeaways

  • Install in order: Apache → MariaDB → PHP. Reversing the order can leave dependencies incomplete.
  • Always run mysql_secure_installation — an unsecured MariaDB instance is an open door.
  • Create one database user per application; never use root credentials in your app code.
  • The phpinfo() file is useful for verification but must be deleted immediately after.
  • Tune php.ini for your application's needs before going live.

Need help installing WordPress, configuring SSL certificates, or tuning Apache performance? Visit elenlace.com to learn how our team can configure your VPS to your exact requirements.

FAQ

Can I use MySQL instead of MariaDB?

Yes. MySQL is no longer in Ubuntu's default repositories, but you can install it by adding the official MySQL repository from dev.mysql.com. MariaDB is MySQL-compatible and is the preferred choice on Ubuntu due to native integration and better performance for typical hosting workloads.

How do I install a specific PHP version on Ubuntu?

Add Ondřej Surý's PPA: sudo add-apt-repository ppa:ondrej/php, then install the version you need, for example sudo apt install php8.2. You can have multiple versions coexist and switch between them using update-alternatives.

Is it safe to expose MariaDB port 3306 to the internet?

No, unless strictly necessary and IP-restricted. MariaDB should listen only on localhost (127.0.0.1) by default. For remote database access, use an SSH tunnel instead: ssh -L 3306:localhost:3306 user@server.

How much RAM does a VPS need to run LAMP?

For development or low-traffic sites, 1 GB of RAM is sufficient. For WordPress with moderate traffic, 2 GB is recommended. High-traffic sites or WooCommerce stores should consider 4 GB or more, combined with caching (Redis, Memcached) and PHP-FPM instead of mod_php.

Further reading

Other providers and guides worth comparing:

← All