// SSD VPS

How to Install and Configure a LAMP Stack on a Linux VPS: Complete Guide — CLIQHOST

November 14, 2026 · by Alex M.

How to Install and Configure a LAMP Stack on a Linux VPS: Complete Guide — CLIQHOST

The LAMP stack (Linux, Apache, MySQL, PHP) remains one of the most popular technology combinations for hosting web applications. From WordPress and Joomla to custom PHP applications, LAMP provides a stable, well-documented and highly flexible foundation.

In this guide, you'll learn how to fully install and configure a LAMP stack on a SSD VPS running Ubuntu 22.04, step by step, with real commands and optimization tips.


What is the LAMP Stack and Why Use It?

LAMP is an acronym for:

  • Linux — the operating system (Ubuntu, Debian, CentOS, etc.)
  • Apache — the web server that handles HTTP requests
  • MySQL / MariaDB — the relational database
  • PHP — the server-side scripting language

This combination powers millions of websites and web applications around the world. Key advantages:

  • Open-source — all components are free
  • Vast ecosystem — documentation, tutorials and an enormous community
  • Compatibility — supports WordPress, Joomla, Drupal, Laravel and much more
  • Flexibility — each component can be replaced or extended

If you want to run a PHP application in production without the overhead of a hosting panel, an NVMe VPS with a manually configured LAMP stack is the ideal solution.


Prerequisites

Before starting, make sure you have:

  • A VPS with Ubuntu 22.04 LTS (recommended)
  • SSH access with root or sudo user privileges
  • A domain name or public IP address
  • Basic knowledge of the Linux command line

If you don't have a server yet, you can quickly order a VPS SSD from CLIQHOST and have it active within minutes.


Step 1: Update the System

Before any installation, update the package list and existing system:

sudo apt update && sudo apt upgrade -y

Reboot the server if there are kernel updates:

sudo reboot

Step 2: Install Apache

Apache is the web server that will process HTTP requests and serve your application's files.

Installation

sudo apt install apache2 -y

Start and Enable on Boot

sudo systemctl start apache2
sudo systemctl enable apache2

Check Status

sudo systemctl status apache2

If everything is working correctly, you'll see active (running) in the output.

Firewall Configuration

Allow HTTP and HTTPS traffic through UFW:

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

Now, if you access your server's IP from a browser, you'll see the default Apache page: "Apache2 Ubuntu Default Page".

💡 Tip: You can disable the default Apache page with sudo a2dissite 000-default.conf after configuring your own VirtualHost.


Step 3: Install MySQL

MySQL will manage the databases for your web applications.

Installation

sudo apt install mysql-server -y

Securing the Installation

Run the MySQL security script:

sudo mysql_secure_installation

You'll be guided through a set of questions:

  • Validate Password Plugin — recommended Y (enables password validation)
  • Remove anonymous users — Y
  • Disallow root login remotely — Y (important for security)
  • Remove test database — Y
  • Reload privilege tables — Y

Creating a User and Database

Connect to MySQL:

sudo mysql

Create a database and a dedicated user (don't use root for applications):

CREATE DATABASE myapp_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'myapp_user'@'localhost' IDENTIFIED BY 'VerySecurePassword123!';
GRANT ALL PRIVILEGES ON myapp_db.* TO 'myapp_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;

⚠️ Warning: Always use complex passwords and separate users per application. Never use the MySQL root account in application connections.


Step 4: Install PHP

PHP is the scripting language that bridges Apache and MySQL, executing your web application's logic.

Install PHP and Essential Modules

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

Verify PHP Version

php -v

Expected output:

PHP 8.1.x (cli) (built: ...)

Configure PHP for Production

Edit the php.ini file:

sudo nano /etc/php/8.1/apache2/php.ini

Modify or verify these settings:

; Memory limit per script
memory_limit = 256M

; Maximum uploaded file size
upload_max_filesize = 64M
post_max_size = 64M

; Maximum execution time
max_execution_time = 300

; Hide PHP version from HTTP headers (security)
expose_php = Off

; Enable error logging (don't display errors in browser on production)
display_errors = Off
log_errors = On
error_log = /var/log/php_errors.log

Restart Apache to apply the changes:

sudo systemctl restart apache2

PHP Test

Create a test file:

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

Content:

<?php
phpinfo();

Access http://SERVER_IP/info.php in your browser. You'll see the PHP Info page with all installation details.

⚠️ Important: Delete this file after verification — it exposes sensitive server information!

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

Step 5: Configure Virtual Hosts in Apache

Virtual Hosts allow you to host multiple websites on the same server.

Create Directory Structure

sudo mkdir -p /var/www/example.com/public_html
sudo chown -R $USER:$USER /var/www/example.com
sudo chmod -R 755 /var/www/example.com

Create a VirtualHost Configuration File

sudo nano /etc/apache2/sites-available/example.com.conf

Content:

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public_html

    <Directory /var/www/example.com/public_html>
        Options -Indexes +FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/example.com_error.log
    CustomLog ${APACHE_LOG_DIR}/example.com_access.log combined
</VirtualHost>

Enable the Site and Rewrite Module

sudo a2ensite example.com.conf
sudo a2enmod rewrite
sudo systemctl reload apache2

Step 6: Install SSL with Let's Encrypt (Certbot)

Every modern website must run on HTTPS. Certbot automates obtaining and renewing free SSL certificates from Let's Encrypt.

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

Certbot will automatically modify the Apache configuration and redirect HTTP → HTTPS.

Verify automatic renewal:

sudo certbot renew --dry-run

If you need a commercial SSL certificate for business projects, CLIQHOST offers certificates from globally recognized certificate authorities.


Step 7: Optimize Apache Performance

Enable Compression and Caching Modules

sudo a2enmod deflate
sudo a2enmod expires
sudo a2enmod headers
sudo systemctl restart apache2

Configure GZIP Compression

Add to your .htaccess file or directly in the VirtualHost:

<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css
    AddOutputFilterByType DEFLATE application/javascript application/json
    AddOutputFilterByType DEFLATE application/x-font-ttf application/vnd.ms-fontobject
    BrowserMatch ^Mozilla/4 gzip-only-text/html
    BrowserMatch ^Mozilla/4\.0[678] no-gzip
    BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
</IfModule>

Configure Cache for Static Resources

<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "access plus 1 year"
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType image/png "access plus 1 year"
    ExpiresByType image/webp "access plus 1 year"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
    ExpiresByType text/html "access plus 1 day"
</IfModule>

Step 8: Configure Apache MPM

Apache has multiple processing modules (MPM). The default is mpm_prefork, but for better performance with PHP-FPM, mpm_event is recommended.

sudo a2dismod mpm_prefork
sudo a2enmod mpm_event
sudo apt install php8.1-fpm -y
sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php8.1-fpm
sudo systemctl restart apache2 php8.1-fpm

💡 PHP-FPM manages PHP processes separately from Apache, significantly improving performance under heavy load. Ideal for medium-to-high traffic sites on an NVMe VPS.


Step 9: Monitoring and Logs

View Apache Logs in Real Time

# Error logs
sudo tail -f /var/log/apache2/error.log

# Access logs
sudo tail -f /var/log/apache2/access.log

Check Resource Usage

# CPU and RAM usage
htop

# Disk space
df -h

# Apache processes
ps aux | grep apache2

For advanced monitoring (Prometheus, Grafana, Zabbix), CLIQHOST offers server management services where our team configures and monitors your infrastructure.


Step 10: Complete LAMP Stack Testing

Create a simple PHP file that tests the database connection:

sudo nano /var/www/example.com/public_html/test_db.php
<?php
$host = 'localhost';
$dbname = 'myapp_db';
$username = 'myapp_user';
$password = 'VerySecurePassword123!';

try {
    $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8mb4", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "✅ MySQL connection successful!";
} catch (PDOException $e) {
    echo "❌ Connection error: " . $e->getMessage();
}

Access https://example.com/test_db.php. If you see the success message, the LAMP stack is fully functional.

⚠️ Delete the test file immediately after verification!

sudo rm /var/www/example.com/public_html/test_db.php

Additional Security for the LAMP Stack

Protecting the Admin Directory

If you have an /admin directory, add password protection:

sudo htpasswd -c /etc/apache2/.htpasswd admin_user

In VirtualHost or .htaccess:

<Directory /var/www/example.com/public_html/admin>
    AuthType Basic
    AuthName "Restricted Area"
    AuthUserFile /etc/apache2/.htpasswd
    Require valid-user
</Directory>

Disabling Unnecessary HTTP Methods

<LimitExcept GET POST HEAD>
    deny from all
</LimitExcept>

Hiding Apache Version

Edit /etc/apache2/conf-available/security.conf:

ServerTokens Prod
ServerSignature Off

Conclusion

You've installed and configured a complete LAMP stack on a Linux VPS, including:

  • ✅ Apache with Virtual Hosts and optimization modules
  • ✅ MySQL with dedicated users and databases
  • ✅ PHP with production settings and PHP-FPM
  • ✅ Automatic SSL with Certbot
  • ✅ Basic security measures

A properly configured LAMP stack gives you a solid foundation for any PHP application, from a simple WordPress blog to a complex enterprise application.

If you want to skip the manual configuration stage or prefer a professionally managed environment, the CLIQHOST team is here to help. Explore our SSD VPS plans, NVMe VPS or server management services — or contact us for a custom solution.

SHARE
// what clients say

What Our Clients Say

Real reviews from customers who trust CLIQHOST for performance, reliability and expert technical support.

★★★★★

"We moved our online shop from a foreign host and the difference is night and day — pages load instantly and support replies in minutes, in Romanian."

AM
Andrei M.
eCommerce owner · Chișinău
★★★★★

"Migrated 12 client sites to CLIQHOST. Free migration, zero downtime, and the cPanel setup is exactly what my team needed. Highly recommend."

EV
Elena V.
Web agency · Bălți
★★★★★

"Our NVMe VPS handles traffic spikes without a sweat. Full root, local datacenter, and billing in MDL — everything we wanted from a provider."

DC
Dmitri C.
SaaS founder · Chișinău