LightYear
/Docs
DocsComputeInstall a LAMP Stack on Ubuntu 22.04

Install a LAMP Stack on Ubuntu 22.04

Install Linux, Apache, MySQL, and PHP (LAMP) on Ubuntu 22.04 to host dynamic web applications.

intermediate
14 min read
LightYear Docs Team
Updated April 24, 2026
lampapachemysqlphpubuntuweb-server
Ready to get started?

The LAMP stack (Linux, Apache, MySQL, PHP) is a classic web application platform. This guide installs and configures all four components on Ubuntu 22.04.

Step 1 — Update System Packages

>_BASH
$apt update && apt upgrade -y

Step 2 — Install Apache

>_BASH
$apt install -y apache2
$systemctl enable apache2
$systemctl start apache2

Allow Apache through UFW:

>_BASH
$ufw allow 'Apache Full'

Verify Apache is running by visiting http://YOUR_SERVER_IP in a browser. You should see the Apache2 Ubuntu Default Page.

Step 3 — Install MySQL

>_BASH
$apt install -y mysql-server
$systemctl enable mysql
$systemctl start mysql

Run the security script:

>_BASH
$mysql_secure_installation

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

Create a database and user for your application:

>_BASH
$mysql -u root -p
SQL
CREATE DATABASE myapp CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'myapp_user'@'localhost' IDENTIFIED BY 'StrongPassword123!';
GRANT ALL PRIVILEGES ON myapp.* TO 'myapp_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Step 4 — Install PHP

>_BASH
$apt install -y php libapache2-mod-php php-mysql php-cli php-curl php-gd php-mbstring php-xml php-zip

Verify the PHP version:

>_BASH
$php --version
OUTPUT
PHP 8.1.2-1ubuntu2.18 (cli) (built: Jan 12 2026 00:00:00) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies

Step 5 — Test PHP Processing

Create a PHP info file:

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

Visit http://YOUR_SERVER_IP/info.php to see the PHP configuration page.

[!WARNING] Remove the info.php file after testing — it exposes sensitive server configuration information.

>_BASH
$rm /var/www/html/info.php

Step 6 — Configure a Virtual Host

>_BASH
$mkdir -p /var/www/myapp/public
$chown -R www-data:www-data /var/www/myapp
$nano /etc/apache2/sites-available/myapp.conf
APACHE
<VirtualHost *:80>
    ServerName myapp.example.com
    DocumentRoot /var/www/myapp/public

    <Directory /var/www/myapp/public>
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog  ${APACHE_LOG_DIR}/myapp_error.log
    CustomLog ${APACHE_LOG_DIR}/myapp_access.log combined
</VirtualHost>
>_BASH
$a2ensite myapp.conf
$a2enmod rewrite
$systemctl reload apache2

Step 7 — Test the Full Stack

Create a test PHP file that connects to MySQL:

>_BASH
$nano /var/www/myapp/public/index.php
PHP
<?php
$conn = new mysqli('localhost', 'myapp_user', 'StrongPassword123!', 'myapp');
if ($conn->connect_error) {
    die('Connection failed: ' . $conn->connect_error);
}
echo '<h1>LAMP Stack is working!</h1>';
echo '<p>MySQL connection: <strong>OK</strong></p>';
echo '<p>PHP version: ' . phpversion() . '</p>';
$conn->close();

[!TIP] For production applications, use environment variables or a configuration file outside the web root to store database credentials — never hardcode them in PHP files.

Was this article helpful?

Your cookie choices for this website

This site uses cookies and related technologies, as described in our privacy policy, for purposes that may include site operation, analytics, and enhanced user experience. You may choose to consent to our use of these technologies, or manage your own preferences. Cookie policy