LAMP (Linux, Apache, MySQL/MariaDB, PHP) is a proven stack for hosting dynamic web applications. This guide installs all components and verifies they work together.
Prerequisites
- Ubuntu/Debian server with sudo access
- Internet connection
- Basic familiarity with command line
Installation Steps
1. Update package lists
sudo apt update
2. Install Apache2
sudo apt install -y apache2
3. Install MySQL/MariaDB
For MySQL 8.0:
sudo apt install -y mysql-server
For MariaDB (drop-in MySQL replacement):
sudo apt install -y mariadb-server mariadb-client
4. Install PHP and required modules
sudo apt install -y php php-mysql php-cli php-cgi php-common php-mbstring php-xml
5. Enable PHP module in Apache
sudo a2enmod php
sudo a2enmod rewrite
6. Enable services to start on boot
sudo systemctl enable apache2
sudo systemctl enable mysql
Or for MariaDB:
sudo systemctl enable mariadb
7. Start all services
sudo systemctl start apache2
sudo systemctl start mysql
Or for MariaDB:
sudo systemctl start mariadb
Post-Installation Configuration
1. Secure your MySQL/MariaDB installation
sudo mysql_secure_installation
This script will prompt you to:
- Set a root password
- Remove anonymous users
- Disable remote root login
- Remove test databases
Follow the prompts carefully.
2. Create a test database and user
sudo mysql -u root -p
Enter your root password, then run:
CREATE DATABASE testdb;
CREATE USER 'testuser'@'localhost' IDENTIFIED BY 'testpassword';
GRANT ALL PRIVILEGES ON testdb.* TO 'testuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Replace testpassword with a strong password.
3. Create a PHP test file
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/phpinfo.php
Verification Steps
1. Check Apache status
sudo systemctl status apache2
Should show active (running).
2. Check MySQL/MariaDB status
sudo systemctl status mysql
Or:
sudo systemctl status mariadb
3. Test PHP in browser
Open http://your-server-ip/phpinfo.php in your browser. You should see the PHP information page.
4. Test database connection from command line
mysql -u testuser -p testdb
Enter your test user password. If successful, you'll see the mysql> prompt. Type EXIT; to quit.
Firewall Configuration
If using UFW, allow HTTP and HTTPS:
sudo ufw allow 'Apache Full'
Or for HTTP only:
sudo ufw allow 'Apache'
If using a cloud provider's security group, add inbound rules for port 80 (HTTP) and port 443 (HTTPS).
Tips
- Store database credentials in a secure PHP configuration file, never hardcode them
- Use strong passwords for database users
- Regularly back up your databases and website files
- MySQL/MariaDB listens on port 3306 by default; restrict external connections via firewall
- Remove the
phpinfo.phpfile after testing for security:sudo rm /var/www/html/phpinfo.php
For RHEL/CentOS/Rocky
Replace apt with dnf:
sudo dnf install -y httpd mysql-server php php-mysql php-cli php-common php-mbstring php-xml
sudo systemctl enable httpd mariadb
sudo systemctl start httpd mariadb
sudo mysql_secure_installation