Install and configure a LAMP local development environment on Ubuntu 24.04
Last updated:
php linux ubuntu apache web server mysql
Overview
Setting-up a local web server can make website development much easier. This article will show you how to create and configure such a server.
LAMP refers to a Linux computer environment that includes an Apache web server, MySQL database and PHP programming language. It can be set-up on any Linux computer. A popular Linux distribution is Ubuntu.
Install the Apache webserver
Apache allows your computer to run as a server. You can install it using the following command.
sudo apt update
sudo apt install apache2
We will configure Apache once we've set-up our firewall.
Install a firewall
A firewall is a good idea for any web server, even if you only intend to use it for local development. UFW, or Uncomplicated FireWall, is is very straight forward. Let's install the firewall.
sudo apt install ufw
We can check the status of the firewall.
sudo ufw status
Status: active
If the firewall is not active, we can enable it.
sudo ufw enable
The firewall can allow specific types of connections, which are grouped into profiles. We can list the available profiles.
sudo ufw app list
Apache
Apache Full
Apache Secure
The profiles indicate which traffic is allowed.
- Apache: Allows unencrypted web traffic using port 80.
- Apache Full: Allows encrypted and unencrypted web traffic using port 80 and 443.
- Apache Secure: Only allows encrypted web traffic using port 443.
We will use the Apache profile for now, as we have not configured an SSL certificate yet.
sudo ufw allow in "Apache"
To check that everything is working put your server IP address into the your web browser's address bar. If you are setting this up on your local machine you can also use localhost instead.
http://localhost
This should show the default Apache2 landing page.
Configure Apache permissions
We should configure Apache permissions if your webserver is on your local machine.
Set the Apache user and group
Let's set the default user that will be assigned as the owner user for any new files or directories.
Edit /etc/apache2/envvars.
sudo nano /etc/apache2/envvars
......
export APACHE_RUN_USER=www-data
export APACHE_RUN_GROUP=www-data
......
Set APACHE_RUN_USER to your username. Also, make a note of APACHE_RUN_GROUP as we will need this later.
ctrl + o to save, then ctrl + x to exit nano.
Grant Apache user group access
Your website files and folders are usually stored in /var/www/html.
We will allow Apache access to the files and folders within /var/www/html by giving user group permissions to the APACHE_RUN_GROUP specified above, in this case www-data.
Set www-data as the user group on /var/www/html and all its subdirectories and files.
sudo chgrp -R www-data /var/www/html
Grant www-data read and write permissions on all directories within /var/www/html.
sudo find /var/www/html -type d -exec chmod g+rx {} +
Grant www-data read permissions on all files within /var/www/html.
sudo find /var/www/html -type f -exec chmod g+r {} +
Grant yourself owner user access
Make sure you replace USER with your own username!
Set yourself as the owner user on /var/www/html and all its subdirectories and files.
sudo chown -R USER /var/www/html/
Grant yourself read, write and execute permissions on all directories within /var/www/html.
sudo find /var/www/html -type d -exec chmod u+rwx {} +
Grant yourself read and write permissions on all files within /var/www/html.
sudo find /var/www/html -type f -exec chmod u+rw {} +
Restrict access to anyone else
We don't want anyone else having access to the directories and files within /var/www/html.
Remove read, write and execute permissions for all other users.
sudo chmod -R o-rwx /var/www/html/
Make these changes permanent
We want any files created or updated within /var/www/html to be assigned the same owner user, user group and others permissions that we set on /var/www/html.
sudo find /var/www/html -type d -exec chmod +s {} +
Let's restart apache for our changes to take effect.
sudo apachectl configtest
sudo systemctl reload apache2
Install MySQL
A common way to handle website data is by using a MySQL database. We will install this next.
sudo apt install mysql-server
Once installed we need to configure MySQL root users authentication method.
sudo mysql
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
mysql> exit
There is a wizard to help configure your MySQL installation. We can run this with.
sudo mysql_secure_installation
Install PHP
There are different versions of PHP. In this example we will install version 8.4.
sudo apt update
sudo apt install -y software-properties-common
sudo LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php -y
sudo apt update
sudo apt install -y php8.4
Once installed, we can check the version number.
php -v
PHP 8.4.16 (cli) (built: Dec 18 2025 23:38:28) (NTS)
Copyright (c) The PHP Group
Built by Debian
Zend Engine v4.4.16, Copyright (c) Zend Technologies
with Zend OPcache v8.4.16, Copyright (c), by Zend Technologies
You can check which php modules are installed.
php -m
Extra modules/packages can be installed using.
sudo apt install php8.4-PACKAGE_NAME
There are several packages that you may fined useful to get started with.
sudo apt install -y php8.4-cli php8.4-json php8.4-common php8.4-mysql php8.4-zip php8.4-gd php8.4-mbstring php8.4-curl php8.4-xml php8.4-bcmath
Manage PHP permissions
To prevent issues with your php sessions, use the same username that you used when granting yourself owner access when you set-up Apache.
Make sure you replace USER with your own username!
sudo chown -R USER /var/lib/php/sessions/
Our webserver is now ready to start serving a website. All we have to do now is create one!
Download a CMS or framework
There are 2 main options when creating websites...
1. Using a Content Management System (CMS)
A CMS gives you a good solid foundation to create your own website. It also provides a dashboard, making it simple to add and update pages.
Some popular Content Management Systems include Grav and ModX.
We can show you how to install Grav onto your web server.
2. Using a framework
A framework gives you a clear structure to follow but without any management dashboard.