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
$apt update && apt upgrade -yStep 2 — Install Apache
$apt install -y apache2$systemctl enable apache2$systemctl start apache2Allow Apache through UFW:
$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
$apt install -y mysql-server$systemctl enable mysql$systemctl start mysqlRun the security script:
$mysql_secure_installationFollow the prompts to set a root password, remove anonymous users, and disable remote root login.
Create a database and user for your application:
$mysql -u root -pCREATE 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
$apt install -y php libapache2-mod-php php-mysql php-cli php-curl php-gd php-mbstring php-xml php-zipVerify the PHP version:
$php --versionPHP 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 TechnologiesStep 5 — Test PHP Processing
Create a PHP info file:
$echo "<?php phpinfo(); ?>" > /var/www/html/info.phpVisit http://YOUR_SERVER_IP/info.php to see the PHP configuration page.
[!WARNING] Remove the
info.phpfile after testing — it exposes sensitive server configuration information.
$rm /var/www/html/info.phpStep 6 — Configure a Virtual Host
$mkdir -p /var/www/myapp/public$chown -R www-data:www-data /var/www/myapp$nano /etc/apache2/sites-available/myapp.conf<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>$a2ensite myapp.conf$a2enmod rewrite$systemctl reload apache2Step 7 — Test the Full Stack
Create a test PHP file that connects to MySQL:
$nano /var/www/myapp/public/index.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.
