A LAMP stack (Linux, Apache, MySQL, PHP) is a popular setup for hosting dynamic websites and web applications. This guide provides a concise, step-by-step process to install a LAMP stack on a VPS, with examples tailored for a provider like VPS.DO, which offers Ubuntu-based VPS hosting with full root access.
Prerequisites
- A VPS with Ubuntu (e.g., VPS.DO’s 1H2G plan with Ubuntu 20.04 or 22.04).
- Root or sudo access to the VPS.
- Basic familiarity with SSH and terminal commands.
Step-by-Step Installation
1. Connect to Your VPS
Access your VPS via SSH using a terminal or tool like PuTTY. For VPS.DO users, log in with the credentials provided in your SolusVM control panel:
ssh root@your_vps_ip
2. Update the System
Ensure your system is up to date to avoid compatibility issues:
sudo apt update && sudo apt upgrade -y
3. Install Apache
Apache is the web server. Install it and verify it’s running:
sudo apt install apache2 -y sudo systemctl start apache2 sudo systemctl enable apache2
Check Apache by visiting http://your_vps_ip in a browser. You should see the default Apache page.
4. Install MySQL
MySQL is the database server. Install and secure it:
sudo apt install mysql-server -y sudo systemctl start mysql sudo systemctl enable mysql sudo mysql_secure_installation
Follow the prompts to set a root password and secure the installation (e.g., remove anonymous users, disable remote root login).
5. Install PHP
PHP processes dynamic content. Install PHP and common modules:
sudo apt install php libapache2-mod-php php-mysql -y
Test PHP by creating a file:
sudo nano /var/www/html/info.php
Add:
<?php phpinfo(); ?>
Save and visit http://your_vps_ip/info.php to confirm PHP is working. Delete this file afterward for security:
sudo rm /var/www/html/info.php
6. Configure Apache for PHP
Ensure Apache prioritizes PHP files. Edit the configuration:
sudo nano /etc/apache2/mods-enabled/dir.conf
Modify the DirectoryIndex line to prioritize index.php:
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
Restart Apache:
sudo systemctl restart apache2
7. Test the LAMP Stack
Create a test PHP file to verify database connectivity:
sudo nano /var/www/html/testdb.php
Add:
<?php
$conn = new mysqli("localhost", "root", "your_mysql_password", "mysql");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected to MySQL successfully!";
$conn->close();
?>
Save and visit http://your_vps_ip/testdb.php. If successful, it displays “Connected to MySQL successfully!”
8. Secure Your VPS
- Update your firewall (e.g., UFW) to allow HTTP/HTTPS:
sudo ufw allow 80 sudo ufw allow 443 sudo ufw enable
- Regularly back up your VPS using VPS.DO’s SolusVM panel or manual snapshots.
- Keep software updated: sudo apt update && sudo apt upgrade -y.
Conclusion
Your LAMP stack is now ready to host websites or applications. With VPS.DO’s fast KVM-based VPS and full root access, you can easily scale or customize your setup. For further assistance, contact your provider’s support (e.g., VPS.DO’s 24/7 ticket-based support).