Setting up a LAMP stack (Linux, Apache, MySQL/MariaDB, PHP) on a Virtual Private Server (VPS) is the classic foundation for hosting dynamic websites and applications like WordPress, Drupal, or custom PHP code.
Debian/Ubuntu
-
Prerequisites:
-
A VPS running Debian/Ubuntu (such as Ubuntu 24.04 LTS)
-
A non-root user with
sudoprivileges -
SSH access to your server (https://help.servers.guru/hc/help-center/articles/1787555500-how-to-ssh-to-your-vps)
-
-
Update Package Lists:
Before installing any software, update your server's package index to ensure you get the latest stable versions:
sudo apt update && sudo apt upgrade -y -
Install the Apache Web Server:
-
Apache is one of the most reliable and widely used web servers in the world. Install it using the
aptpackage manager:sudo apt install apache2 -y -
Once installed, adjust your firewall settings to allow HTTP (port 80) and HTTPS (port 443) traffic through UFW:
sudo ufw allow 'Apache Full' -
Verify that Apache is running by checking its service status:
sudo systemctl status apache2 -
Test it:
Open your web browser and navigate to your server's public IP address (http://YOUR_SERVER_IP). You should see the default Ubuntu Apache welcome page.
-
-
Install the MySQL Database Server:
-
Install MySQL (or MariaDB as a drop-in alternative) to handle your application's data:
sudo apt install mysql-server -y -
After installation, it is critical to run the built-in security script to remove unsafe default settings:
sudo mysql_secure_installationThis script will prompt you to configure the
VALIDATE PASSWORD PLUGIN(recommended for strong security), set a root password, remove anonymous users, disallow root login remotely, and remove the test databases.
-
-
Install PHP:
-
PHP is the scripting language that processes code to build dynamic web content. Alongside PHP, you will need several extensions so it can communicate with Apache and MySQL.
-
Install PHP and the most common modules with this command:
sudo apt install php libapache2-mod-php php-mysql -y -
Check your installed PHP version to ensure success:
php -v
-
-
Configure Apache to Handle PHP Files (Optional Check):
-
By default, Apache serves
index.htmlfiles beforeindex.php. If you want to change this behavior so Apache prioritizes PHP files first, open thedir.confconfiguration file:sudo nano /etc/apache2/mods-enabled/dir.conf -
It will look like this:
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm -
Save and close the file (
Ctrl+O,Enter, thenCtrl+X), then restart Apache:sudo systemctl restart apache2
-
-
Test PHP Processing:
-
To verify that your server is properly processing PHP scripts, create a test file inside your web root directory (
/var/www/html/):sudo nano /var/www/html/info.php -
Add the following standard PHP code inside the file:
<?phpphpinfo();?> -
Save and close the file. Now, open your browser and navigate to:
http://YOUR_SERVER_IP/info.php -
You should see a detailed system page outlining your PHP configuration, version, and active modules.
-
🔒 Security Note: Once you have verified everything works correctly, delete this file for security reasons, as it exposes sensitive server data:
sudo rm /var/www/html/info.php
-
-
Conclusion:
Congratulations! You now have a fully functional LAMP stack running on your VPS, ready for you to host applications, configure virtual hosts, or install an SSL certificate via Let's Encrypt.
RHEL/CentOS
-
Prerequisites:
-
A VPS running RHEL/CentOS (such as CentOS Stream 10)
-
A non-root user with
sudoprivileges -
SSH access to your server (https://help.servers.guru/hc/help-center/articles/1787555500-how-to-ssh-to-your-vps)
-
-
Update System Packages:
-
Before installing any components, ensure your system repositories and packages are fully up to date:
sudo dnf upgrade -y
-
-
Install and Configure the Apache Web Server:
Apache (known in the Red Hat ecosystem as
httpd) is available directly from the standard DNF repositories.-
Install Apache:
sudo dnf install httpd -y -
Start and Enable Apache:
Ensure Apache starts automatically on system boot and is running right now:
sudo systemctl enable --now httpd -
Configure the Firewall:
CentOS Stream comes with firewalld enabled by default. Allow HTTP and HTTPS traffic through the firewall:
sudo firewall-cmd --permanent --add-service=httpsudo firewall-cmd --permanent --add-service=httpssudo firewall-cmd --reload -
Test it:
Open your web browser and navigate to your server's public IP address (http://YOUR_SERVER_IP). You will see the default CentOS Stream Apache test page.
-
-
Install and Secure MariaDB (Database Server):
CentOS Stream uses MariaDB as the default drop-in replacement for MySQL.
-
Install MariaDB Server:
sudo dnf install mariadb-server -y -
Start and Enable MariaDB:
sudo systemctl enable --now mariadb -
Run the Security Script:
Secure your database installation by setting a root password, removing anonymous users, and disabling remote root logins:
sudo mysql_secure_installationFollow the interactive prompts (pressing
Yfor most recommendations is standard).
-
-
Install PHP:
CentOS Stream provides modern PHP streams. You can check available modules or install the core package alongside common extensions needed for frameworks like WordPress or Drupal:
-
Install PHP and Common Extensions:
sudo dnf install php php-mysqlnd php-gd php-xml php-mbstring -y -
Verify PHP Version:
Check that PHP installed correctly:
php -v -
Restart Apache to Load PHP:
sudo systemctl restart httpd
-
-
Test PHP Processing:
To verify that Apache and PHP are communicating correctly, create a test script in the web root directory (/var/www/html/):
-
Create the test file:
sudo nano /var/www/html/info.php -
Add the PHP code:
PHP<?phpphpinfo();?>(Save and exit by pressing Ctrl+O, Enter, then Ctrl+X).
-
Test in your browser:
Navigate to http://YOUR_SERVER_IP/info.php. You should see the official PHP configuration page detailing your build, modules, and environment settings.
-
🔒 Security Note: Once verification is complete, remove this file to prevent exposing sensitive system diagnostics:
sudo rm /var/www/html/info.php
-
-
Adjust SELinux Permissions (Important for CentOS):
CentOS uses SELinux for enhanced security. If you plan to host web files outside the default /var/www/html/ directory or allow PHP to write to files (like uploads), ensure your SELinux contexts are properly configured:
-
To allow Apache to connect out to databases or networks:
sudo setsebool -P httpd_can_network_connect_db on
-
-
Conclusion:
Your LAMP stack on CentOS Stream 10 is now completely operational, secure, and ready to host your dynamic web applications!