The LAMP stack is a popular set of open-source software used to host web applications. It stands for Linux, Apache, MySQL, and PHP. In this tutorial, you will learn how to install the LAMP stack on Ubuntu 22.04.

What You Can Do with a LAMP Server:

  • Host Websites: Serve dynamic content using Apache and PHP.
  • Manage Databases: Store and retrieve information using MySQL.
  • Run Web Applications: Deploy and manage web applications built with PHP.

Components of the LAMP Stack:

  • Apache: Web server to handle HTTP requests.
  • MySQL: Database server to manage data.
  • PHP: Server-side scripting language for dynamic content.

Step 1: Create a Cloud Instance

Before installing the LAMP stack, you need to create a cloud instance to host it. Here’s how to set up your instance:

Step 1.1: Click on “Create”

  • From your hosting provider’s dashboard, click on the Create button and select Instances.
Shape.host Resources dashboard with Create > Instance highlighted.

Step 1.2: Choose a Location

  • Select the data center closest to your target audience (e.g., New York, London, Tokyo).
Shape.host instance form with the data center location selector highlighted.

Step 1.3: Choose an Instance Plan and OS

  • Choose an instance plan based on your needs (e.g., Standard, CPU-Optimized).
  • Select Ubuntu 22.04 as the operating system.
Shape.host instance form with the operating system image selector highlighted.

Step 1.4: Create the Instance

  • Fill in the necessary details, such as authentication (SSH keys or password), and choose a hostname. Click Create Instance.
Shape.host instance form with the Create Instance button highlighted.

Step 1.5: Find Your IP Address

  • Once the instance is created, go to the Resources section to find the public IP address of your instance. You’ll need this to connect to the instance remotely.
Shape.host Resources dashboard with the server IP address highlighted.

Step 2: Install Apache

Install Apache, the web server software, and configure it to start at boot:

apt-get install apache2 -y
Terminal showing APT package management commands and output.

Start and enable Apache:

systemctl start apache2
systemctl enable apache2
Terminal showing systemd service management commands or output.

Check Apache’s status to ensure it’s running properly:

systemctl status apache2
Terminal showing systemd service management commands or output.

Verify the Apache version:

apache2ctl -version
Terminal displaying the Apache server version.

Step 3: Check Apache Installation via Web Browser

After Apache is installed, you can verify that it’s working correctly by accessing the default Apache web page.

  1. Open your web browser.
  2. In the address bar, type http://<your-server-ip>, replacing <your-server-ip> with the actual public IP address of your server.
  3. If Apache is working correctly, you should see the default Apache welcome page that says “Apache2 Ubuntu Default Page”.

If you see this page, Apache has been successfully installed and is running properly.

Ubuntu Apache2 default welcome page.

Step 4: Install PHP 8.1

To enable dynamic content, you need PHP. Install PHP 8.1 and the necessary modules for Apache:

apt-get install php8.1 libapache2-mod-php8.1 php8.1-mysql php8.1-gd php8.1-curl php8.1-xml -y
Terminal showing mysql commands or output during Install PHP 8.1.

Check the PHP version to ensure the installation was successful:

php --version
Terminal showing php commands or output during Install PHP 8.1.

Step 5: Install MySQL

Install MySQL, the database management system:

apt-get install mysql-server -y
Terminal showing APT package management commands and output.

Start and enable MySQL:

systemctl start mysql
systemctl enable mysql
Terminal showing systemd service management commands or output.

Check the status of MySQL to ensure it’s running:

systemctl status mysql
Terminal showing systemd service management commands or output.

Verify the MySQL version:

mysql --version
Terminal showing mysql commands or output during Install MySQL.

Step 6: Secure MySQL

To secure your MySQL installation and set the root password, run the following command:

mysql_secure_installation
Terminal showing mysql commands or output during Secure MySQL.

Follow the on-screen prompts to complete the MySQL secure installation process.

Step 7: Test PHP with Apache

To test if PHP is working correctly with Apache, create a simple info.php file in the /var/www/html/ directory:

nano /var/www/html/info.php
Terminal showing nano commands or output during Test PHP with Apache.

Add the following code to the file:

<?php
   phpinfo();
?>

Save the file and exit the editor.

Step 8: Verify LAMP Installation

Open a web browser and navigate to http://<your-server-ip>/info.php. You should see a page displaying PHP information, confirming that Apache, PHP, and MySQL are working together correctly.

PHP information page displaying runtime and module configuration.

You have successfully installed the LAMP stack on Ubuntu 22.04. Your server is now ready to host PHP applications, connect to a MySQL database, and serve web pages using Apache.

To host websites and applications efficiently, Shape.Host offers Linux SSD VPS services. Shape.Host’s SSD-powered VPS ensures fast and reliable performance, perfect for hosting LAMP-based applications.

By utilizing Shape.Host’s Linux SSD VPS, you can take full advantage of the LAMP stack’s potential with enhanced performance and scalability. Explore Shape.Host’s VPS offerings to find the perfect solution for your project.