In this tutorial you will learn how to install LAMP on Ubuntu 24.04.
LAMP stands for Linux, Apache, Mysql and PHP.
First you’ll install all the required packages and then you’ll configure each one separately.
Table of Contents
1. Install LAMP on Ubuntu
First you’ll install all the required packages and then later on you’ll configure them.
Update and upgrade everything
sudo apt update && sudo apt upgrade
Install Apache2
sudo apt install apache2
Install Mysql server
sudo apt install mysql-server
Install PHP and some required packages
sudo apt install php php-mysql
sudo apt install php-curl php-cgi
sudo apt install php-gd php-mbstring php-xml php-xmlrpc
2. Configure Apache on Ubuntu
Check that apache is working
sudo systemctl status apache2
Check the Apache version you have installed
apache2 -v
Open a browser and put your IP address
http://your_IP_address/
If you get an Apache page, then you’re good.
2.1 Configure a virtual host for your domain on Apache
I will show you how to configure a virtual host for domain.com as an example.
You should change that domain by yours.
Create a new file with these contents:
sudo vi /etc/apache2/sites-available/mydomain.com.conf
<Directory /var/www/mydomain.com/public_html>
Require all granted
</Directory>
<VirtualHost *:80>
ServerName mydomain.com
ServerAlias www.mydomain.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/mydomain.com/public_html
ErrorLog /var/log/apache2/mydomain.com.error.log
CustomLog /var/log/apache2/mydomain.com.access.log combined
</VirtualHost>
Create the required directory and give the proper permissions
sudo mkdir -p /var/www/mydomain.com/public_html
sudo chown -R www-data:www-data /var/www/mydomain.com/public_html
sudo chmod -R 755 /var/www/mydomain.com/public_html
Enable your domain and disable the default configuration
sudo a2ensite mydomain.com
sudo a2dissite 000-default.conf
Reload apache for the changes to take effect.
sudo systemctl reload apache2
3. Configure the MySQL database
Connect to Mysql with root user and create a database and a user with the permissions.
sudo mysql -u root
CREATE DATABASE webdata;
CREATE USER 'webuser' IDENTIFIED BY 'password';
GRANT ALL ON webdata.* TO 'webuser';
exit
Secure your Mysql installation
sudo mysql_secure_installation
Type Y for all these questions
Remove anonymous users?
Disallow root login remotely?
Remove test database and access to it?
Reload privilege tables now?
4. Configure PHP
To configure PHP first check the version
php -v
Edit the php.ini file and change these values
sudo vi /etc/php/8.3/apache2/php.ini
error_log = /var/log/php/error.log
upload_max_filesize = 200M
Create the directory for the logs
sudo mkdir /var/log/php
sudo chown www-data /var/log/php
And now restart apache for the changes to take effect.
sudo systemctl restart apache2
5. Test the LAMP installation on Ubuntu
Create a small file called test.php and put these contents:
sudo vi /var/www/mydomain.com/public_html/test.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Database Connection</title>
</head>
<body>
<h1>Database Connection Status</h1>
<p>
<?php
// Database connection variables
$servername = "localhost";
$username = "webuser";
$password = "password";
$dbname = "webdata";
// Create connection using procedural style
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
echo "Not Connected: " . mysqli_connect_error();
} else {
echo "Connected Successfully to database: " . $dbname;
}
// Close the connection
mysqli_close($conn);
?>
</p>
</body>
</html>
Change the owner and group for this file
sudo chown www-data:www-data /var/www/mydomain.com/public_html/test.php
Open your browser and go to
http://mydomain.com/test.php
If everything is ok you should see this on your browser:
Database Connection Status
Connected Successfully to database: webdata