Install Linux, Apache, MySQL and PHP (LAMP) on Ubuntu 22.04

Install Linux, Apache, MySQL and PHP (LAMP) on Ubuntu 22.04

Install and configure a LAMP local development environment on Ubuntu 22.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.

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

The 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. To 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, you 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.

MySQL database

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

Installing PHP

There are different versions of PHP. In this example we will install version 7.4.

sudo apt-get update
sudo apt -y install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt -y install php7.4

Once installed, we can check the version number:

php -v
PHP 7.4.33 (cli) (built: Sep  2 2023 08:03:46) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.33, Copyright (c), by Zend Technologies

You can check which php modules are installed:

php -m

Extra modules/packages can be installed using:

sudo apt-get install php7.4-PACKAGE_NAME

There are several packages that you may fined useful to get started with:

sudo apt-get install -y php7.4-cli php7.4-json php7.4-common php7.4-mysql php7.4-zip php7.4-gd php7.4-mbstring php7.4-curl php7.4-xml php7.4-bcmath

Rewrite incoming requests

When hosting websites, it is useful to be able to rewrite incoming requests. For example, instead of a user making a request like /products.php?id=3271 they can just use /products/3271 which is much cleaner. To achieve this, install the Apache rewrite module:

sudo su -c "a2enmod rewrite"

You can then add the following directives to your site config files:

Make sure you replace MY_SITE with your own website directory name!

<Directory /var/www/html/MY_SITE>
  Options Indexes FollowSymLinks
  AllowOverride All
</Directory>

After checking the syntax, we need to restart apache for any changes to take effect.

sudo apachectl configtest
sudo systemctl reload apache2

Manage Apache permissions

We need to make sure you can create and update files and folders within your web directory. Apache also needs to traverse the directory structure and read the files. And finally, we want to restrict access to anyone else. We do this by setting permissions on all the files and folders within /var/www/html.

Set the Apache user and group

Before setting permissions, we need to 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.

To prevent issues with your php sessions, set the same username on /var/lib/php/sessions/

Make sure you replace USER with your own username!

sudo chown -R USER /var/lib/php/sessions/

After checking the syntax, we need to restart apache again for these changes to take effect.

sudo apachectl configtest
sudo systemctl reload apache2

Grant Apache user group access

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.

Make sure you replace www-data with your own user group!

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

Allow yourself access to the files and folders within /var/www/html by giving owner user permissions to yourself.

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 others:

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 {} +