Hosting VPS and Dedicated Spanish version

Support article

How to Install LAMP or LEMP on a VPS (Apache or Nginx)

Install a LAMP or LEMP stack on your VPS: Apache or Nginx, MySQL or MariaDB and PHP. Step by step for Ubuntu and Debian.

Published: 11/07/2026 Updated: 11/07/2026

Introduction

For a VPS to serve dynamic web pages (like WordPress, PrestaShop or a PHP-built website), you need to install a software stack that includes a web server, database and PHP. The two most used combinations are:

  • LAMP: Linux + Apache + MySQL + PHP.
  • LEMP: Linux + Enginx (the E is for Engine x) + MySQL + PHP.

In this article you’ll see what the difference is between them and how to install them step by step on a VPS with Ubuntu or Debian.

LAMP or LEMP: which to choose?

AspectLAMP (Apache)LEMP (Nginx)
Web serverApacheNginx
EaseEasier to configureA bit more technical
PerformanceGoodBetter with high traffic
Ideal forBeginners, WordPressSites with many visitors, caching
CompatibilityMaximum (with almost everything)Very broad

Summary: if you’re starting out, choose LAMP with Apache, because it’s simpler and compatible with everything. If you’re looking for maximum performance or have many visitors, choose LEMP with Nginx.

At miHosting, professional hosting plans use LiteSpeed (an even faster web server than Apache and Nginx). For a self-managed VPS, Apache or Nginx are the usual options.

Prerequisites

Before starting you need:

  • A VPS with Ubuntu 22.04/24.04 or Debian 12 freshly installed.
  • SSH access as root or a user with sudo.
  • The system updated (apt update && apt upgrade -y).

How to install LAMP (Apache)

Step 1: Install Apache

apt install apache2 -y

Check it works by opening your IP in the browser:

http://your-server-ip

You should see the Apache welcome page.

[Imagen sugerida: screenshot of the Apache welcome page]

Step 2: Install MySQL or MariaDB

MariaDB is a free and faster replacement for MySQL. Recommended.

apt install mariadb-server -y

Secure the installation by running:

mysql_secure_installation

Answer the questions: set a database root password and say yes (Y) to the rest.

Step 3: Install PHP

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

Also install common extensions:

apt install php-curl php-gd php-mbstring php-xml php-zip php-intl -y

Step 4: Check that PHP works

Create a test file:

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

Open in the browser:

http://your-server-ip/info.php

You should see the PHP information page.

⚠️ Delete the info.php file after checking it. It exposes server information that shouldn’t be left public.

rm /var/www/html/info.php

How to install LEMP (Nginx)

Step 1: Install Nginx

apt install nginx -y

Check it works by opening your IP in the browser:

http://your-server-ip

You should see the Nginx welcome page.

Step 2: Install MySQL or MariaDB

apt install mariadb-server -y
mysql_secure_installation

Step 3: Install PHP (FPM version)

Nginx doesn’t integrate PHP directly like Apache, it needs PHP-FPM:

apt install php-fpm php-mysql -y

Common extensions:

apt install php-curl php-gd php-mbstring php-xml php-zip php-intl -y

Step 4: Configure Nginx to use PHP

Edit the default site configuration:

nano /etc/nginx/sites-available/default

Find the section that handles PHP files and uncomment it (remove the #). It should look like this:

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php8.x-fpm.sock;
}

Change php8.x to the version you have installed (check with ls /var/run/php/).

Save and restart Nginx:

systemctl restart nginx

Step 5: Check that PHP works

Create the test file:

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

Open http://your-server-ip/info.php in the browser. If you see the PHP information, everything works.

Delete the file afterwards: rm /var/www/html/info.php.

Open ports in the firewall

For your VPS to serve websites, the firewall must allow web traffic (HTTP and HTTPS).

With ufw (Ubuntu/Debian)

ufw allow 80/tcp
ufw allow 443/tcp
ufw reload

With firewalld (AlmaLinux/Rocky)

firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload

Useful tips

  • Choose MariaDB over MySQL. It’s faster, free and 100% compatible.
  • Install the PHP extensions you need. WordPress, PrestaShop and other apps require specific extensions.
  • Always delete the info.php file. It exposes sensitive server data.
  • Change the owner of /var/www. Apache and Nginx need permissions to read and write web files:
    chown -R www-data:www-data /var/www
  • Take a snapshot before starting. If something fails, you go back in one click.

Common problems

The Apache or Nginx page doesn’t load

Check that the web server is active:

systemctl status apache2   # for Apache
systemctl status nginx     # for Nginx

If it’s not active, start it:

systemctl start apache2

Also check that the firewall allows port 80.

The info.php file downloads instead of executing

PHP isn’t properly configured. In Apache, make sure you have libapache2-mod-php installed. In Nginx, check that PHP-FPM is active:

systemctl status php8.x-fpm

I can’t connect to the database

Verify that MariaDB or MySQL is active:

systemctl status mariadb

Check that the username and password are correct and that the user has permissions on the database.

Nginx gives 502 Bad Gateway error

PHP-FPM isn’t running or the socket is incorrect. Restart PHP-FPM and check the version:

systemctl restart php8.x-fpm
ls /var/run/php/

After installing, the website is slow

Check the server’s resources with htop. A VPS with 1 GB of RAM may be tight if you install web server, database and PHP at the same time.

Frequently asked questions

What’s the difference between LAMP and LEMP?

LAMP uses Apache as the web server; LEMP uses Nginx. Apache is easier; Nginx is faster with high traffic.

Can I switch from Apache to Nginx later?

Yes, but it requires uninstalling Apache, installing Nginx and reconfiguring sites. It’s not complicated, but it’s worth backing up first.

Are MariaDB and MySQL the same?

MariaDB is a MySQL derivative, 100% compatible. Most applications (WordPress, PrestaShop) work the same with either. MariaDB is faster and free.

Which PHP version should I install?

The latest stable version. On Ubuntu 24.04 it’s usually PHP 8.3. WordPress and most modern apps work with PHP 8.x.

Do I need a panel after installing LAMP or LEMP?

It’s not mandatory. You can manage everything by command line. A panel (like CyberPanel or aaPanel) makes management easier if you don’t want to use commands.

Your VPS, ready to serve websites

Installing LAMP or LEMP turns your VPS into a web server capable of serving any dynamic page: WordPress, PrestaShop, Moodle or your own PHP application. If you’re starting out, LAMP with Apache is the simplest; if you’re looking for performance, LEMP with Nginx is the option.

If you prefer not to handle all this technical configuration, at miHosting we offer managed VPS where we install and configure the web server, database and PHP for you. You can also install a control panel if you prefer to manage everything visually. Open a ticket from your client panel and we’ll advise you.