Introduction
LAMP (Linux, Apache, MySQL, PHP) is a powerful open-source stack for web development. This guide walks you through installing PHP and configuring a full LAMP environment on Linux (Ubuntu/Debian). Let’s get started!
Prerequisites
- A Linux server (Ubuntu 22.04/Debian 12 used here)
- Terminal access with
sudo
privileges - Stable internet connection
Step 1: Update Your System
sudo apt update && sudo apt upgrade -y
Step 2: Install Apache Web Server
sudo apt install apache2 -y
Verify Apache:
Access your server’s IP in a browser:
http://your_server_ip
You should see the Apache default page.
Step 3: Install MySQL Database
sudo apt install mysql-server -y
Secure MySQL:
sudo mysql_secure_installation
Follow prompts to:
- Set a root password
- Remove anonymous users
- Disable remote root login
- Remove test databases
Step 4: Install PHP and Extensions
sudo apt install php libapache2-mod-php php-mysql php-cli php-curl php-gd php-zip php-mbstring -y
Key Extensions:
php-mysql
: MySQL database connectivityphp-gd
: Image processingphp-curl
: HTTP requestsphp-mbstring
: Multibyte string support
Step 5: Configure Apache for PHP
-
Restart Apache:
sudo systemctl restart apache2
-
Test PHP Processing:
Create a test file:sudo nano /var/www/html/info.php
Add this content:
Access via browser:
http://your_server_ip/info.php
You’ll see PHP configuration details.
Step 6: Verify LAMP Functionality
- Create a PHP-MySQL Test Script:
connect_error) { die("Connection failed: " . $conn->connect_error); } echo "LAMP stack is working!"; ?>
Save as
/var/www/html/test.php
. A success message confirms connectivity.
Step 7: (Optional) Install phpMyAdmin
For database management:
sudo apt install phpmyadmin -y
During installation:
- Select
apache2
with SPACEBAR → TAB → ENTER - Choose “Yes” to configure with
dbconfig-common
- Set phpMyAdmin password
Link to Apache:
sudo ln -s /usr/share/phpmyadmin /var/www/html/phpmyadmin
Access at:
http://your_server_ip/phpmyadmin
Troubleshooting Tips
- Apache not running?
sudo systemctl status apache2 sudo journalctl -xe
- PHP not executing?
Ensurelibapache2-mod-php
is installed and Apache is restarted. - Connection issues with MySQL?
Verify credentials and check MySQL service status:sudo systemctl status mysql
Conclusion
You’ve successfully set up a LAMP stack! Now you can:
- Host PHP applications (e.g., WordPress)
- Develop dynamic websites
- Manage databases via phpMyAdmin
Next Steps:
- Configure virtual hosts for multiple websites
- Set up SSL/TLS with Let’s Encrypt
- Explore PHP frameworks like Laravel
> Note: For CentOS/RHEL, replace apt
with yum/dnf
and adjust package names (e.g., httpd
instead of apache2
).
Enjoy your fully functional web development environment! 🚀