LAMP Stack Installation and Setup Guide

Empower your server: Install and configure Apache, MySQL, and PHP on Ubuntu.

Estimated Time: Approximately 45 - 60 minutes

Overview

This comprehensive guide will walk you through the installation and setup of a robust LAMP stack (Linux, Apache, MySQL, PHP) on your Ubuntu server. The LAMP stack is a popular open-source solution renowned for its reliability, security, and widespread use in hosting dynamic websites and web applications.

Here's a quick breakdown of each component's role:

  • Linux: The operating system foundation for your server.
  • Apache: The web server software responsible for serving web pages.
  • MySQL: The relational database management system for storing your application's data.
  • PHP: The server-side scripting language used to process dynamic content.

This guide is perfect for beginners looking to set up a foundational web server environment, ideal for hosting various PHP-based web applications like WordPress, Drupal, or custom-built frameworks.

Estimated Time

45 - 60 minutes

Experience Level

Beginner

Basic command-line knowledge is helpful. No prior server administration experience is strictly required, but familiarity with a terminal is beneficial.

System Requirements

  • Operating System: Ubuntu 22.04 LTS (Jammy Jellyfish) or 20.04 LTS (Focal Fossa). A fresh installation is recommended.
  • Hardware: Minimum 1 GB RAM, 1 CPU core, and 10 GB free disk space. For production, more resources are advised.
  • Server Access: Access to a terminal via SSH (Secure Shell) as a non-root user with sudo privileges. If you're on a cloud provider (e.g., AWS, DigitalOcean, Linode), ensure your security groups or firewalls allow SSH (port 22) and HTTP/HTTPS (ports 80/443) traffic.
  • Connectivity: A stable internet connection for downloading packages.
  • Public IP Address: A static public IP address for your server if you plan to host publicly accessible websites.

Step-by-Step Instructions

Step 1: Update Package Index and Upgrade System

It's crucial to always update your local package index before installing any new software. This ensures your system's package list is current and that you install the latest stable versions and security patches.

sudo apt update && sudo apt upgrade -y

Step 2: Configure UFW (Uncomplicated Firewall)

Before installing any services, it's a best practice to set up a firewall to secure your server. UFW is a user-friendly frontend for `iptables` that makes firewall management easy.

Install UFW (if not already present):

sudo apt install ufw -y

Allow SSH, HTTP, and HTTPS traffic:

sudo ufw allow OpenSSH
sudo ufw allow 'Apache'
sudo ufw allow 'Apache Full' (allows both HTTP and HTTPS)

Enable UFW:

sudo ufw enable

Check UFW status:

sudo ufw status

Step 3: Install Apache Web Server

Apache HTTP Server is the workhorse that serves your website content to visitors. Install it with a single command:

sudo apt install apache2 -y

Once installed, enable and start the Apache service to ensure it runs automatically on boot and is active now:

sudo systemctl enable apache2
sudo systemctl start apache2

Verify Apache Installation:

First, find your server's public IP address:

curl -4 icanhazip.com

Then, open your web browser and navigate to http://your_server_ip (replace your_server_ip with the IP address you found). You should see the default Apache "Ubuntu Default Page".

Step 4: Install MySQL Database Server

MySQL is a powerful relational database management system used to store and manage your application's data. Install it:

sudo apt install mysql-server -y

After installation, it's highly recommended to secure your MySQL instance. This script guides you through setting a root password, removing anonymous users, disallowing remote root logins, and more:

sudo mysql_secure_installation

Create a Dedicated MySQL Database and User (Best Practice):

It's unsafe to use the MySQL root user for your web applications. Instead, create a dedicated database and a user with specific privileges for your application.

sudo mysql -u root -p

Once in the MySQL prompt (`mysql>`), execute the following commands, replacing `your_database_name`, `your_username`, and `your_password` with secure choices:

CREATE DATABASE your_database_name CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'your_username'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON your_database_name.* TO 'your_username'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Step 5: Install PHP and Modules

PHP is the scripting language that processes dynamic content, allowing your web applications to interact with the database and generate HTML. We'll install the core PHP package along with modules for Apache integration and MySQL connectivity:

sudo apt install php libapache2-mod-php php-mysql -y

Install Common PHP Modules (Optional but Recommended):

Many PHP applications require additional modules. It's a good idea to install some commonly used ones:

sudo apt install php-cli php-gd php-curl php-mbstring php-xml php-zip php-intl -y

Restart Apache after PHP installations:

sudo systemctl restart apache2

Confirm the PHP installation by checking its version:

php -v

Step 6: Configure Apache for PHP (Optional but good practice)

Apache processes files in a specific order. Ensure `.php` files are prioritized:

Edit Apache's `dir.conf` file:

sudo nano /etc/apache2/mods-enabled/dir.conf

Move `index.php` to be the first entry in the `DirectoryIndex` line:

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

Change to:

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

Save and exit (`Ctrl+O`, `Enter`, `Ctrl+X`). Then restart Apache:

sudo systemctl restart apache2

Step 7: Test PHP with Apache

To confirm that PHP is correctly integrated with Apache, we'll create a simple PHP info file in your web server's document root.

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

Now, open your browser and navigate to http://your_server_ip/info.php (using your server's public IP). You should see a detailed PHP information page, confirming PHP is working correctly with Apache.

Remove the test file:

sudo rm /var/www/html/info.php

Step 8: Set Proper File Permissions

Proper file permissions are essential for security and for your web applications to function correctly. The Apache user (`www-data`) needs read access to files and often write access to specific directories (e.g., for uploads, caches).

Change ownership of your web root:

sudo chown -R $USER:www-data /var/www/html

Set directory permissions (read, write, execute for owner, read & execute for group):

sudo find /var/www/html -type d -exec chmod 755 {} \;

Set file permissions (read & write for owner, read for group):

sudo find /var/www/html -type f -exec chmod 644 {} \;

Verification

Confirm your LAMP stack is fully operational and secure:

  • Firewall is active: Run sudo ufw status. It should show Status: active with rules for OpenSSH, Apache, and Apache Full.
  • Apache is running: Run systemctl status apache2. Look for active (running) in the output.
  • MySQL is secure and accessible: Try logging in with your application user mysql -u your_username -p your_database_name. It should work.
  • PHP is processing: The PHP info page successfully loaded (and you promptly removed the info.php file).
  • File permissions are correct: Your web files are owned by your user and the `www-data` group, with appropriate read/write access.

Conclusion & Next Steps

Congratulations! You have successfully installed and configured a robust and secure LAMP stack on your Ubuntu server. This strong foundation is now ready for your web development projects and deployment of dynamic web applications.

From here, you can dive into exciting possibilities and further harden your server:

  • Deploy PHP Applications: Upload your application files (e.g., WordPress, Drupal, Laravel) to `/var/www/html` or set up Apache virtual hosts for multiple websites.
  • Apache Virtual Hosts: For hosting multiple websites or domains on a single server, learn to configure Apache virtual hosts. This isolates each website's configuration.
  • HTTPS/SSL with Certbot: Secure your website with free SSL certificates from Let's Encrypt using `certbot` to enable HTTPS, encrypting all traffic between your server and users.
  • PHP Configuration (`php.ini`): Adjust PHP settings in `/etc/php/X.X/apache2/php.ini` (where `X.X` is your PHP version) for optimal performance and to suit your application's needs (e.g., `upload_max_filesize`, `memory_limit`, `max_execution_time`). Remember to restart Apache after changes.
  • Database Management Tools: Explore graphical tools like phpMyAdmin (for web-based management) or DBeaver (desktop client) for easier database administration.
  • Backup Strategy: Implement a robust backup plan for your database and application files.
  • Monitoring: Consider setting up server monitoring tools to keep an eye on performance and resource usage.

Need Further Assistance or Custom Solutions? Contact Us!