Tuesday, September 15, 2026

Learn how to install Apache, MySQL and PHP on Ubuntu. Complete LAMP stack installation guide with one-shot commands, PHP testing and php-opcache troubleshooting

How to Install LAMP Stack on Ubuntu | Complete Guide
UBUNTU • LINUX • SERVER • PHP

How to Install LAMP Stack on Ubuntu

A complete step-by-step guide to installing Apache, MySQL and PHP on Ubuntu — including a one-shot installation command and a fix for the common php-opcache package error.

The LAMP stack is one of the most popular environments for hosting PHP applications on Linux. It combines Linux, Apache, MySQL and PHP into a complete web application platform.

Whether you're building a WordPress website, Laravel application, PHP API or simply creating a local development environment, installing LAMP gives you everything you need to get started.

What Does LAMP Stand For?

🐧

Linux

The operating system powering the server.

🌐

Apache

Handles HTTP requests and serves web pages.

🗄️

MySQL

Stores and manages application data.

🐘

PHP

Processes dynamic server-side application code.

How the LAMP Stack Works

A typical request flows through the stack like this:

Browser HTTP Request
Apache Web Server
PHP Application
MySQL Database

Install LAMP on Ubuntu

First update your Ubuntu package index:

Terminal
sudo apt update

Install Apache, MySQL and PHP

You can install the complete LAMP environment with the following command:

One-shot installation
sudo apt install -y apache2 mysql-server php libapache2-mod-php php-mysql php-cli php-curl php-gd php-mbstring php-xml php-zip php-bcmath php-intl php-soap

Start Apache and MySQL

Enable both services so they start automatically with Ubuntu:

Terminal
sudo systemctl enable --now apache2 mysql

Enable Apache URL Rewriting

The Apache rewrite module is commonly required by modern PHP applications and frameworks such as Laravel.

Terminal
sudo a2enmod rewrite
sudo systemctl restart apache2

Test Apache

Open your browser and visit:

Apache URL
http://localhost

If Apache is working correctly, you should see the Apache default welcome page.

Test PHP

Create a temporary PHP information page:

Terminal
echo '<?php phpinfo(); ?>' | sudo tee /var/www/html/info.php >/dev/null

Now open:

PHP Test
http://localhost/info.php

Remove the PHP Info Page

Security Note

The phpinfo() page can expose configuration and environment information. Do not leave it publicly accessible on a production server.

Remove test file
sudo rm /var/www/html/info.php

Configure Website Permissions

For a local development environment, you can configure the Apache web directory as follows:

Permissions
sudo chown -R "$USER":www-data /var/www/html
sudo chmod -R 775 /var/www/html
Production warning:

Avoid applying broad recursive permissions blindly on production systems. Production web servers should use carefully designed ownership and permission policies.

Common Error: php-opcache

Error:
Package php-opcache is not available,
but is referred to by another package.

Error: Package 'php-opcache' has no installation candidate

If you encounter this error, don't worry. It doesn't necessarily mean that the LAMP installation has failed.

On some Ubuntu releases, php-opcache isn't available as a standalone package under that exact name in the configured repositories.

Solution

Remove php-opcache from the installation command and install the remaining packages normally.

Verify the Installation

Run the following command to verify Apache, MySQL and PHP:

Verification
systemctl is-active apache2 && systemctl is-active mysql && php -v && apache2 -v

You should see:

active
active
PHP version information
Apache version information

Check Individual Versions

Version commands
apache2 -v
php -v
mysql --version

Secure MySQL

After installing MySQL, run the security configuration utility:

MySQL security
sudo mysql_secure_installation
Production tip: Always review MySQL authentication, users, passwords and remote-access configuration before exposing a database server to the internet.

Where Is the Website Directory?

Apache's default document root is:

/var/www/html

You can create a simple PHP website here:

Create PHP page
sudo nano /var/www/html/index.php

Add:

index.php
<?php

echo "Hello from Ubuntu LAMP!";

?>

Then open:

http://localhost

LAMP vs XAMPP

Native Ubuntu LAMP

  • Uses Ubuntu packages
  • Managed with systemd
  • Apache configuration under /etc/apache2
  • Website directory: /var/www/html
  • Good fit for Ubuntu servers

XAMPP / LAMPP

  • Bundled development environment
  • Common installation path: /opt/lampp
  • Uses its own service management commands
  • Convenient for beginners and local development
  • Different from Ubuntu's native LAMP packages

One-Shot Complete Installation

If you want to install and configure the basic LAMP stack in one go, use this command:

Complete LAMP installation
sudo apt update && \
sudo apt install -y apache2 mysql-server php libapache2-mod-php php-mysql php-cli php-curl php-gd php-mbstring php-xml php-zip php-bcmath php-intl php-soap && \
sudo systemctl enable --now apache2 mysql && \
sudo a2enmod rewrite && \
sudo chown -R "$USER":www-data /var/www/html && \
sudo chmod -R 775 /var/www/html && \
sudo systemctl restart apache2 && \
echo "====================================" && \
echo "       LAMP INSTALLATION DONE        " && \
echo "====================================" && \
echo "Apache : http://localhost" && \
echo "PHP    : $(php -v | head -n 1)" && \
echo "MySQL  : $(mysql --version)" && \
echo "===================================="

Final Checklist

Apache installed
MySQL installed
PHP installed
Apache service running
MySQL service running
PHP integrated with Apache
mod_rewrite enabled
Website directory configured
PHP tested successfully
Temporary phpinfo file removed

Conclusion

Installing LAMP on Ubuntu is straightforward when you use the distribution's native packages. Apache provides the web server, PHP handles application execution, and MySQL provides persistent data storage.

If you encounter the php-opcache installation error, simply avoid specifying that unavailable standalone package and continue with the supported PHP packages from your Ubuntu repositories.

Once the stack is running, your Ubuntu machine is ready for PHP applications, WordPress, Laravel, APIs and other MySQL-backed web applications.

No comments:

Post a Comment

Thank you for Commenting Will reply soon ......

Featured Posts

Learn how to install Apache, MySQL and PHP on Ubuntu. Complete LAMP stack installation guide with one-shot commands, PHP testing and php-opcache troubleshooting

How to Install LAMP Stack on Ubuntu | Complete Guide UBUNTU • LINUX • SERVER • PHP ...