Lightning LAMP Setup: Quick and Easy Web Development

WebCoder

What is LAMP?
The LAMP stack consists of four open-source components—Linux, Apache, MySQL, and PHP that work together to build and run dynamic websites and web applications. In this setup:
- Linux provides the operating system.
- Apache functions as the web server.
- MySQL manages the database.
- PHP serves as the scripting language that delivers server-side functionality.
Node.js
Node.js allows JavaScript to run outside of a web browser. It enables the creation of server-side programs and even full-stack applications using only JavaScript.
npm (Node Package Manager)
npm comes bundled with Node.js. It is used to download, install, and manage tools or libraries (called packages) for your projects. It also allows you to publish your own packages for others to use.
nvm (Node Version Manager)
nvm is a command-line tool that lets you install and switch between multiple versions of Node.js on the same machine. This is especially useful when different projects require different Node.js versions.
LAMP + Node.js Installation Script – Explanation
- Stop on errors
- Script will exit if any command fails (set -e).
- Update system packages
- apt update → refresh package list
- apt upgrade → install latest updates
- Install Apache (Web Server)
- apt install apache2 → install Apache
- systemctl enable/start apache2 → start now & auto-start on boot
- Install MySQL (Database)
- apt install mysql-server → install MySQL
- systemctl enable/start mysql → start now & auto-start on boot
- Install PHP (Server-side scripting language)
- apt install php libapache2-mod-php php-mysql → PHP + MySQL support
- systemctl restart apache2 → reload Apache with PHP
- Create test PHP page
- Creates
info.php
to check PHP:http://example.com/info.php
- Creates
- Install Node.js using NVM (Node Version Manager)
- curl → download tool if missing
- Install NVM → easy Node.js version management
- Load NVM
- Add NVM to .bashrc → available in future terminals
- Load NVM now → available immediately
- Install latest Node.js LTS
- nvm install --lts → install latest stable Node.js
- nvm use --lts → use it now
- nvm alias default 'lts/*' → set as default
- Update PATH
- Make node and npm commands work immediately
- Show installed versions
- Prints NVM, Node.js, and NPM versions
How to Run the Installation Script
- Copy the script content into a file.
- Save the file with a .sh extension (for example: lamp.sh).
- Open your terminal and navigate to the file’s directory.
- Make the script executable (only needed once):
Script:
#!/bin/bash
# LAMP + Node.js Installation Script (Ubuntu/Debian)
# Exit on any error
set -e
echo "============================"
echo " Updating system packages..."
echo "============================"
sudo apt update -y
sudo apt upgrade -y
# ----------------------------
# Install LAMP
# ----------------------------
echo "============================"
echo " Installing Apache..."
echo "============================"
sudo apt install apache2 -y
sudo systemctl enable apache2
sudo systemctl start apache2
echo "============================"
echo " Installing MySQL..."
echo "============================"
sudo apt install mysql-server -y
sudo systemctl enable mysql
sudo systemctl start mysql
echo "============================"
echo " Installing PHP..."
echo "============================"
sudo apt install php libapache2-mod-php php-mysql -y
echo " Restarting Apache to load PHP..."
sudo systemctl restart apache2
echo "============================"
echo " LAMP installation completed!"
echo "============================"
# Create PHP test file
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php > /dev/null
echo "Test PHP by visiting: `http://example.com/info.php`"
# ----------------------------
# Install Node.js, NVM, and NPM
# ----------------------------
echo "============================"
echo " Installing Node.js via NVM..."
echo "============================"
# Install curl if not installed
sudo apt install curl -y
# Install NVM (Node Version Manager)
export NVM_DIR="$HOME/.nvm"
if [ ! -d "$NVM_DIR" ]; then
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash
fi
# Add NVM to bashrc if not already present
if ! grep -q 'NVM_DIR' ~/.bashrc; then
echo 'export NVM_DIR="$HOME/.nvm"' >> ~/.bashrc
echo '[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"' >> ~/.bashrc
echo '[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"' >> ~/.bashrc
fi
# Load NVM into current session
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
# ----------------------------
# Install Node.js LTS + set default
# ----------------------------
echo "Installing latest LTS version of Node.js..."
nvm install --lts
nvm use --lts
nvm alias default 'lts/*'
# Ensure Node & NPM are in PATH immediately
export PATH="$NVM_DIR/versions/node/$(nvm version)/bin:$PATH"
echo "============================"
echo " Node.js, NPM, and NVM installed successfully!"
echo "============================"
echo "Versions installed:"
echo "NVM: $(nvm -v)"
echo "Node: $(node -v)"
echo "NPM: $(npm -v)"