Install LAMP Stack on Ubuntu 22.04 | Easy Setup – OrcaCore
This article provides a comprehensive guide on How To Install LAMP Stack on Ubuntu 22.04. The LAMP stack is a widely adopted open-source solution stack that serves as the backbone for countless web applications. Its popularity stems from its robust nature, flexibility, and the fact that it’s entirely free to use.
LAMP is an acronym representing the four essential components needed to build a fully functional web development environment. These components work together seamlessly to deliver dynamic, database-driven websites. Let’s break down what each letter stands for:
- L stands for Linux, the operating system. Ubuntu 22.04 is the specific Linux distribution used in this guide.
- A stands for Apache, the web server. It’s responsible for serving web content to users.
- M stands for MariaDB or MySQL, the database management system. It stores and manages the website’s data.
- P stands for PHP, the scripting language. It’s used to create dynamic web pages and interact with the database.
You can now proceed to the steps below to complete the LAMP Stack Setup on Ubuntu 22.04.
Before diving into the LAMP Stack Setup, ensure you’re logged into your server as a non-root user with sudo privileges. It’s also a good practice to set up a basic firewall for security. If you haven’t already done so, you can refer to our article on the Initial Server Setup with Ubuntu 22.04.
Now, let’s get started with the LAMP Stack Setup process.
1. Ubuntu 22 LAMP Stack Setup: Install Apache Web Server
The first step in setting up the LAMP stack is installing the Apache web server.
First, update your package lists:
sudo apt update
Next, install Apache using the following command:
sudo apt install apache2
After installation, configure the UFW firewall to allow HTTP traffic. To see the available UFW application profiles, use:
sudo ufw app list
**Output**
Available applications:
Apache
Apache Full
Apache Secure
OpenSSH
To allow traffic on port 80 (HTTP), run:
sudo ufw allow in "Apache"
You can verify your UFW firewall settings with:
sudo ufw status
**Output**
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Apache ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Apache (v6) ALLOW Anywhere (v6)
Now, access your default Ubuntu 22.04 Apache web page by entering your server’s IP address in your web browser:
http://your_server_ip
You should see the default Apache landing page.
Note: You can find your server’s IP address using one of these commands:
ip addr show ens3 | grep inet | awk '{ print $2; }' | sed 's//.*$//'
curl http://icanhazip.com
2. Ubuntu 22 LAMP Stack Setup: Install MariaDB
The next step in setting up the LAMP stack involves installing MariaDB or MySQL. MariaDB is a popular open-source relational database management system forked from MySQL.
To install MariaDB on Ubuntu 22.04, execute the following command:
sudo apt install mariadb-server
After installation, it’s highly recommended to run the mysql_secure_installation
script to enhance the security of your MariaDB installation:
sudo mysql_secure_installation
The script will prompt you with several questions:
- Enter current password for root (enter for none):
- Switch to unix_socket authentication [Y/n]
- Change the root password? [Y/n]
- Remove anonymous users? [Y/n]
- Disallow root login remotely? [Y/n]
- Remove test database and access to it? [Y/n]
- Reload privilege tables now? [Y/n]
Answer these prompts based on your security preferences.
Once the script completes, test your access to the MariaDB console:
sudo mysql -u root -p
Enter the root password you set during the security script. To exit the MariaDB shell, use:
MariaDB [(none)]> exit
3. Ubuntu 22 LAMP Stack Setup: Install PHP
The final step to complete the LAMP stack setup is installing PHP, the scripting language.
In addition to the php
package, you’ll need php-mysql
, a PHP module enabling PHP to communicate with MySQL-based databases. Also, you need libapache2-mod-php
to allow Apache to process PHP files. Core PHP packages are installed as dependencies.
Install PHP and the necessary packages with:
sudo apt install php libapache2-mod-php php-mysql
Verify the PHP installation by checking its version:
php -v
**Output**
PHP 8.1.2 (cli) (built: Mar 4 2022 18:13:46) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
with Zend OPcache v8.1.2, Copyright (c), by Zend Technologies
Now, create a PHP test script to ensure Apache can handle PHP files:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
Access the script through your web browser:
http://server-ip-address/info.php
This will display a page containing information about your PHP configuration.
After confirming the PHP server’s information, it’s crucial to remove the info.php
file due to its sensitive data.
sudo rm /var/www/html/info.php
You can always recreate this file if needed.
Conclusion
You have successfully completed the LAMP Stack Setup on Ubuntu 22.04. The LAMP stack is a powerful platform for hosting and developing dynamic websites and web applications, providing a complete web server environment.
Alternative Solutions for Setting Up a Web Server Environment
While the traditional LAMP Stack Setup is a reliable approach, other methods exist for setting up a web server environment on Ubuntu 22.04. Here are two alternatives:
1. Using Docker Compose:
Docker Compose allows you to define and manage multi-container Docker applications. This provides a highly portable and reproducible environment.
-
Explanation: Instead of installing Apache, MariaDB, and PHP directly on the Ubuntu server, you define each component as a separate Docker container. Docker Compose then orchestrates the creation and linking of these containers. This ensures consistency across different environments and simplifies deployment.
-
Code Example (docker-compose.yml):
version: "3.8"
services:
web:
image: php:8.1-apache
ports:
- "80:80"
volumes:
- ./html:/var/www/html
depends_on:
- db
db:
image: mariadb:10.6
environment:
MYSQL_ROOT_PASSWORD: your_root_password
MYSQL_DATABASE: your_database
MYSQL_USER: your_user
MYSQL_PASSWORD: your_password
volumes:
- db_data:/var/lib/mysql
volumes:
db_data:
In this example:
- The
web
service uses thephp:8.1-apache
Docker image, which includes PHP and Apache. - The
db
service uses themariadb:10.6
Docker image. - Environment variables are used to configure the MariaDB database.
- A volume is created to persist the database data.
- Create a directory named "html" in the same directory as docker-compose.yml, put your php file there.
- To start the application, navigate to the directory containing
docker-compose.yml
and rundocker-compose up -d
.
2. Using a Pre-built Virtual Machine Image:
Several providers offer pre-built virtual machine images with the LAMP stack already installed and configured.
-
Explanation: This method involves downloading and running a virtual machine image that contains a fully functional LAMP stack. This approach is ideal for quickly setting up a development environment or testing purposes. Providers like Bitnami and TurnKey Linux offer pre-built LAMP stack virtual machines.
-
Process:
- Download a LAMP stack virtual machine image from a provider like Bitnami.
- Import the image into a virtualization software such as VirtualBox or VMware.
- Start the virtual machine.
- Access the web server through the virtual machine’s IP address.
These alternative methods provide different approaches to setting up a web server environment compared to the traditional manual installation. The choice depends on your specific needs and preferences. Docker Compose offers portability and consistency, while pre-built virtual machines provide a quick and easy setup.