How to Install a LAMP Stack on Ubuntu VPS

LAMP is a set of four open-source programs that work together to serve websites: Linux (the operating system), Apache (the web server), MySQL or MariaDB (the database), and PHP (the programming language). It is the foundation most PHP sites — including WordPress — run on. This guide walks through installing LAMP on an Ubuntu VPS step by step, and explains what each layer does.

System Requirements

Step 1: Update the System

SSH into your VPS and run:

apt update && apt upgrade -y

Step 2: Install Apache

apt install apache2 -y

Open the firewall for web traffic:

ufw allow 'Apache Full'

ufw enable

Test by opening http://YOUR_VPS_IP in a browser — you should see the Apache default page.

Step 3: Install MySQL

apt install mysql-server -y

Run the security script:

mysql_secure_installation

Follow the prompts: set a root password, remove anonymous users, disable remote root login.

Step 4: Install PHP

apt install php libapache2-mod-php php-mysql php-cli php-curl php-zip php-gd -y

Verify the version: php -v

Create a test file to confirm PHP is working:

echo "<?php phpinfo(); ?>" > /var/www/html/info.php

Open http://YOUR_VPS_IP/info.php — you should see the PHP Info page.

Step 5: Create a Virtual Host

Set up a dedicated directory and config for your domain:

mkdir -p /var/www/yourdomain.com/public_html

nano /etc/apache2/sites-available/yourdomain.com.conf

Paste the following:

<VirtualHost *:80>

ServerName yourdomain.com

DocumentRoot /var/www/yourdomain.com/public_html

</VirtualHost>

Enable the site: a2ensite yourdomain.com.conf && systemctl reload apache2

Security clean-up: After confirming PHP works, delete the info.php test file immediately — it exposes your PHP configuration to the public: rm /var/www/html/info.php

Detailed Install Commands for Each LAMP Component

This section goes deeper into the commands for every layer of the stack so you can install a LAMP stack on your Ubuntu VPS with confidence, while understanding what each package actually does. Start by updating the system and installing Apache as the web server:

sudo apt update && sudo apt upgrade -y
sudo apt install apache2 -y
sudo systemctl enable apache2
sudo systemctl status apache2

The systemctl enable command sets Apache to start automatically on every reboot, while systemctl status confirms the service is healthy (you should see active (running)). Next, install the database. On recent Ubuntu releases, MariaDB is recommended — it is fully MySQL-compatible and completely open source:

sudo apt install mariadb-server mariadb-client -y
sudo systemctl enable mariadb
sudo mysql_secure_installation

During mysql_secure_installation the script asks several questions. Answer them as follows: set a strong root password, remove anonymous users (Y), disallow remote root login (Y), drop the test database (Y), and reload privilege tables (Y). This step is critical because it closes the insecure defaults that attackers routinely scan for. Then install PHP along with the modules most web apps need:

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

Each module serves a purpose: php-mysql lets PHP talk to the database, php-gd handles image processing, php-mbstring supports Unicode and multibyte text, php-curl is needed for calling external APIs, and php-zip plus php-xml are required by WordPress and many plugins. After installation, reload Apache with sudo systemctl reload apache2 so the PHP module takes effect.

Testing LAMP with info.php and Configuring a Virtual Host

Once all three layers are installed, the next step is to confirm PHP works with Apache. The common approach is to create an info.php file that calls phpinfo() to display the full PHP configuration:

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
# Open http://YOUR_VPS_IP/info.php in a browser

If you see the purple table listing your PHP version, enabled modules, and configuration values, your LAMP stack is fully working. Next, set up a virtual host so a single VPS can host multiple websites or domains, each with its own folder and config file:

sudo mkdir -p /var/www/yourdomain.com/public_html
sudo chown -R $USER:$USER /var/www/yourdomain.com/public_html
sudo nano /etc/apache2/sites-available/yourdomain.com.conf

Paste the following into the file, adjusting ServerName and the paths to match your own domain:

<VirtualHost *:80>
    ServerName yourdomain.com
    ServerAlias www.yourdomain.com
    DocumentRoot /var/www/yourdomain.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/yourdomain_error.log
    CustomLog ${APACHE_LOG_DIR}/yourdomain_access.log combined
</VirtualHost>

Enable the new site, disable the default site, then reload Apache:

sudo a2ensite yourdomain.com.conf
sudo a2dissite 000-default.conf
sudo apache2ctl configtest   # check syntax first; reload only when you see Syntax OK
sudo systemctl reload apache2

After this, once you point your domain's DNS to the VPS IP address, your website will load immediately. And always remember to delete the info.php file once testing is complete.

LAMP vs LEMP (Apache vs Nginx): What's the Difference?

Besides LAMP, another very popular stack is LEMP. The main difference is the web server: LAMP uses Apache, while LEMP uses Nginx (pronounced "engine-x", hence the E). Both host PHP sites equally well, but each has its strengths. See the comparison table below:

Topic LAMP (Apache) LEMP (Nginx)
Configuration Easier; supports per-directory .htaccess Centralized; must reload on every change
High concurrency Uses more RAM per connection Handles many connections more efficiently
Static files Adequate Serves static files fast and lightweight
Best for WordPress, general sites, beginners High-traffic sites, reverse proxy, APIs

For beginners and typical WordPress sites, LAMP is a great fit because it is easy to configure and gives you .htaccess for tuning rewrite rules at the folder level. For sites expecting very high traffic or that need a reverse proxy, LEMP has the performance edge.

Common Problems and Basic Security

After installing LAMP, a few recurring issues tend to come up — all of them easy to fix. Common examples:

Security must not be overlooked. At minimum, after setting up LAMP you should configure a firewall to open only the necessary ports (80, 443, and SSH):

sudo ufw allow OpenSSH
sudo ufw allow 'Apache Full'
sudo ufw enable
sudo ufw status

In addition, run mysql_secure_installation through every prompt (as covered above) to disable remote root login and remove anonymous accounts; install a free SSL certificate with Let's Encrypt via Certbot to enable HTTPS; disable ServerTokens and ServerSignature so Apache doesn't reveal its version; and keep system packages patched regularly with sudo apt update && sudo apt upgrade to receive the latest security fixes. Running LAMP on a VPS with full root access — such as AsiaGB's, starting from just 500 THB/month — gives you complete control over all of these security settings.

Frequently Asked Questions

What's the difference between LAMP and LEMP?

LAMP uses Apache as its web server; LEMP uses Nginx (pronounced "engine-x"). Nginx often handles high concurrent traffic better, while Apache is simpler to configure and supports .htaccess.

Do I need a VPS, or is shared hosting enough?

Installing LAMP yourself requires root access, so it needs a VPS. On shared hosting the provider has already installed LAMP for you.

Can I install WordPress right after setting up LAMP?

Yes. Once LAMP is ready, create a MySQL database and install WordPress into the web directory straight away.

Should I choose MySQL or MariaDB?

On recent Ubuntu releases, MariaDB is recommended. It is fully open source and nearly 100% MySQL-compatible, using the same commands and SQL syntax. Web apps like WordPress run on MariaDB with no changes needed.

How much VPS spec do I need for LAMP?

For small to medium sites, 1 GB of RAM is enough to start, but if you run WordPress with several plugins or expect many concurrent visitors, 2 GB or more is recommended. AsiaGB's Ubuntu VPS starts from 500 THB/month with full root access so you can install LAMP yourself.

Need an Affordable Ubuntu VPS?

AsiaGB Linux VPS starts from 500 THB/month — full root access, dedicated IP, available in Thailand and Singapore.

View VPS Plans