Setting up a LAMP stack (Linux, Apache, MySQL/MariaDB, and PHP) on Ubuntu

Setting up a LAMP stack (Linux, Apache, MySQL/MariaDB, and PHP) on Ubuntu involves the following steps. Here’s a step-by-step guide:

1. Update Your System

Start by updating your package manager and installed packages:

bashCopy codesudo apt update && sudo apt upgrade -y

2. Install Apache

Apache is the web server component of LAMP.

bashCopy codesudo apt install apache2 -y

Verify Apache Installation:

Check if Apache is running:

bashCopy codesudo systemctl status apache2

You should see a message indicating it’s active and running.

Test Apache:

Open your browser and go to http://your-server-ip. You should see the default Apache welcome page.

3. Install MySQL (or MariaDB)

MySQL (or MariaDB) is the database component.

bashCopy codesudo apt install mysql-server -y

Secure MySQL Installation:

Run the security script to set up a root password and remove unnecessary defaults:

bashCopy codesudo mysql_secure_installation

Follow the prompts:

  • Set a root password.
  • Remove anonymous users.
  • Disallow root login remotely.
  • Remove test databases.
  • Reload privilege tables.

Test MySQL:

Log in to verify it’s working:

bashCopy codesudo mysql -u root -p

4. Install PHP

PHP is the scripting language used for web development.

bashCopy codesudo apt install php libapache2-mod-php php-mysql -y

Test PHP Installation:

Create a phpinfo file to test PHP:

bashCopy codesudo nano /var/www/html/info.php

Add the following code:

phpCopy code<?php
phpinfo();
?>

Save and exit. Open your browser and go to http://your-server-ip/info.php. You should see the PHP information page.

5. Adjust Apache Configuration (Optional)

By default, Apache may prioritize index.html over index.php. Adjust this if needed:

bashCopy codesudo nano /etc/apache2/mods-enabled/dir.conf

Move index.php to the beginning of the DirectoryIndex list:

textCopy code<IfModule mod_dir.c>
    DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>

Restart Apache to apply changes:

bashCopy codesudo systemctl restart apache2

6. Verify LAMP Stack

Ensure all components are working:

  • Apache: Visit http://your-server-ip to check the Apache welcome page.
  • MySQL: Log into the MySQL shell (sudo mysql -u root -p).
  • PHP: Visit http://your-server-ip/info.php for the PHP info page.

7. Secure Your Setup

  • Remove the info.php file once testing is done:bashCopy codesudo rm /var/www/html/info.php
  • Configure a firewall (optional):bashCopy codesudo ufw allow in "Apache Full" sudo ufw enable
  • Regularly update your system and components.

This completes the LAMP stack setup on Ubuntu.

CATEGORIES:

Linux-Servers-Ubuntu

Comments are closed

Latest Comments

No comments to show.