HostingB2B » How to » VPS Hosting » How to Install a LAMP Stack on Ubuntu (VPS)

How to Install a LAMP Stack on Ubuntu (VPS)

Summarize with:
Summarize with AI
Share:

Setting up a reliable web server starts with the right foundation. This guide explains how to install LAMP stack on Ubuntu using a VPS, covering every step from Apache installation to final testing, so you get a stable, self-managed hosting environment.

What Is a LAMP Stack?

LAMP stands for Linux, Apache, MySQL, and PHP. Together, these components let a server store data, run scripts, and serve dynamic pages. Because each part is open source, LAMP remains popular among developers who need flexibility. Therefore, learning how to install LAMP stack on Ubuntu is a valuable skill for anyone running a self-managed VPS.

Prerequisites

Before starting, you need an Ubuntu VPS with root or sudo access, a static IP address, and a domain name pointed to your server. Additionally, a HostingB2B Ubuntu VPS hosting plan provides the resources and root access this setup requires.

Related ReadHow to Install Hermes Agent on a VPS

Step 1: Install Apache

First, update your package list, then install Apache with one command. Afterward, enable the service so it starts automatically on reboot.

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

Verifying Apache

Once installed, open your server’s IP address in a browser. If the default Apache page appears, the installation succeeded and the web server is ready for the next step.

Step 2: Install MySQL and Secure It

Next, install the MySQL server package. As a result, your stack gains a full relational database engine for storing site data.

sudo apt install mysql-server -y

Running mysql_secure_installation

Afterward, run the built-in security script to remove test databases, disable remote root login, and set a strong password. Consequently, this step protects your database from common, automated attacks.

sudo mysql_secure_installation

Step 3: Install PHP

Then, install PHP along with the Apache module and a few common extensions. As a result, Apache can process dynamic PHP files and communicate with MySQL.

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

By default, this installs the latest stable PHP version provided by Ubuntu’s repository (PHP 8.x).

Step 4: Configure a Virtual Host

Afterward, create the document root folder that matches your domain, then create a virtual host file. This file tells Apache where to find your site’s files and which domain name to respond to.

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

Add the following configuration, then save and exit:

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

Once configured, enable the site and reload Apache.

sudo a2ensite yourdomain.com.conf
sudo systemctl reload apache2

Step 5: Test the Stack (info.php)

Finally, create a simple info.php file inside the same document root you configured in Step 4, not the default /var/www/html folder. Otherwise, the file won’t load when you visit your domain.

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

Add this single line, then save and exit:

<?php phpinfo(); ?>

When you visit http://yourdomain.com/info.php in a browser, PHP’s configuration details appear, confirming that every part of the stack works correctly together.

Important: delete this file once testing is complete. Leaving info.php on a live server exposes configuration details that attackers can use, so remove it immediately after you confirm PHP is working.

sudo rm /var/www/yourdomain.com/info.php

Securing Your Stack

Firewall Rules

Meanwhile, configure the firewall to allow only necessary ports, such as HTTP, HTTPS, and SSH. This limits exposure while keeping the server fully functional.

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

SSL with Let’s Encrypt

Furthermore, install a free SSL certificate using Certbot, the official Let’s Encrypt client, to encrypt traffic between your server and visitors. For advanced settings, the Apache documentation covers every module in detail.

sudo apt install certbot python3-certbot-apache -y
sudo certbot --apache -d yourdomain.com -d www.yourdomain.com

LAMP vs LEMP: Which to Choose

Since Nginx has grown in popularity, many administrators compare LAMP with LEMP before choosing a stack. The table below summarizes the main differences.

FeatureLAMPLEMP
Web ServerApacheNginx
Configuration.htaccess supportServer block config
PerformanceStrong for dynamic sitesFaster under high concurrency
Best ForWordPress, small-to-medium sitesHigh-traffic, static-heavy sites

Common Use Cases by Industry

  • E-commerce: hosts product catalogs and checkout systems reliably.
  • Education: powers learning management systems and student portals.
  • Agencies: manages multiple client sites on a single VPS.
  • SaaS: supports custom PHP applications and internal APIs.

Enterprise Deployment Considerations

Larger deployments often need more than a single server can offer. Therefore, businesses running high-traffic applications should plan for redundancy, automated backups, and proactive monitoring. For teams without in-house server administrators, a managed VPS hosting solution from HostingB2B removes this burden, since our support team handles setup, security, and tuning for you.

FAQ Section

What is the fastest way to install LAMP stack on Ubuntu?

Using Ubuntu’s built-in package manager is fastest, since it installs Apache, MySQL, and PHP with minimal setup.

Is LAMP still relevant in 2026?

Yes. LAMP remains widely used because Apache and MySQL continue to receive active development and broad community support.

Can I install LAMP stack on a fresh VPS?

Yes, a fresh Ubuntu VPS is actually the recommended starting point, since it avoids conflicting software.

Do I need root access to install LAMP?

Yes, root or sudo privileges are required to install and configure each component correctly.

What is the difference between LAMP and LEMP?

LAMP uses Apache as the web server, while LEMP uses Nginx; otherwise, both stacks share MySQL and PHP.

From Guide to Live Server

Skip the Manual Setup — Launch Your Ubuntu VPS Now

You’ve seen how Apache, MySQL, and PHP come together on a fresh Ubuntu server. Get a VPS from HostingB2B with full root access, SSD storage, and 24/7 support to run this exact stack — or let our team handle installation and security for you with Managed VPS Hosting.

UK flag UK
UAE flag UAE
Cyprus flag Cyprus
Malta flag Malta
Netherlands flag Amsterdam
Explore VPS Hosting Plans →

Ready to Get Started?

Ready to put this guide into practice? Deploy a fast, reliable Ubuntu VPS with HostingB2B and get full root access, SSD storage, and 24/7 support.

Conclusion

You now know how to install LAMP stack on Ubuntu from start to finish, including securing and testing your setup. However, if you would rather skip the manual work, HostingB2B also provides Managed VPS hosting, where our support team handles installation, security, and ongoing maintenance for you.

© 2026 All Rights Reserved. HostingB2B

Hosting B2B LTD is a Company registered in Cyprus with Company number HE410139 and VAT CY10410139C

Contact Info

© 2026 All Rights Reserved. HostingB2B