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:
Install LAMP on Ubuntu
First update your Ubuntu package index:
sudo apt update
Install Apache, MySQL and PHP
You can install the complete LAMP environment with the following command:
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:
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.
sudo a2enmod rewrite
sudo systemctl restart apache2
Test Apache
Open your browser and visit:
http://localhost
If Apache is working correctly, you should see the Apache default welcome page.
Test PHP
Create a temporary PHP information page:
echo '<?php phpinfo(); ?>' | sudo tee /var/www/html/info.php >/dev/null
Now open:
http://localhost/info.php
Remove the PHP Info Page
The phpinfo() page can expose configuration and
environment information. Do not leave it publicly accessible
on a production server.
sudo rm /var/www/html/info.php
Configure Website Permissions
For a local development environment, you can configure the Apache web directory as follows:
sudo chown -R "$USER":www-data /var/www/html
sudo chmod -R 775 /var/www/html
Avoid applying broad recursive permissions blindly on production systems. Production web servers should use carefully designed ownership and permission policies.
Common Error: php-opcache
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.
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:
systemctl is-active apache2 && systemctl is-active mysql && php -v && apache2 -v
You should see:
active
PHP version information
Apache version information
Check Individual Versions
apache2 -v
php -v
mysql --version
Secure MySQL
After installing MySQL, run the security configuration utility:
sudo mysql_secure_installation
Where Is the Website Directory?
Apache's default document root is:
/var/www/html
You can create a simple PHP website here:
sudo nano /var/www/html/index.php
Add:
<?php
echo "Hello from Ubuntu LAMP!";
?>
Then open:
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:
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
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 ......