Setup a LAMP Server on a VPS in Under an Hour: The Ultimate Guide to Web Hosting

Setup a LAMP Server on a VPS in Under an Hour: The Ultimate Guide to Web Hosting

posted 6 min read

In today’s digital age, having a reliable web hosting solution is paramount. A Virtual Private Server (VPS) offers a balance of affordability, performance, and full control over your server environment. This guide will walk you through setting up a robust LAMP (Linux, Apache, MariaDB, PHP) stack on your VPS using an automated Bash script—all in under an hour.

For the complete installation script and further details, please visit our GitHub repository: https://github.com/giftbalogun/vpsLAMPsetup.

Why Choose a VPS for Web Hosting?

A VPS offers dedicated resources and increased flexibility compared to shared hosting. This means better performance, enhanced security, and complete control over the software and configurations installed on your server. Whether you’re hosting a personal blog or a complex web application, a VPS is an excellent choice.

Understanding the LAMP Stack

The LAMP stack is a proven combination of technologies that work together to create a powerful web hosting environment:

  • Linux - The operating system that serves as the foundation.
  • Apache - A popular and reliable web server for handling HTTP requests.
  • MariaDB - A robust database management system (a fork of MySQL) used to store and manage data.
  • PHP - A server-side scripting language that enables dynamic web content.

Step-by-Step Setup Guide

1. Updating the System

Before installing any packages, it’s important to update your system to ensure all repositories and packages are current. This minimizes conflicts and security vulnerabilities.

sudo apt update -y
sudo apt upgrade -y

The setup script automatically performs these steps. In case of an error, the script is designed to cancel the installation gracefully.

2. Installing Apache Web Server

Apache is the core component of our LAMP stack. The script installs Apache, starts the service immediately, and enables it to run on boot, ensuring your web server is always operational.

sudo apt install apache2 -y
sudo systemctl start apache2
sudo systemctl enable apache2

These commands ensure that Apache is installed correctly and persists across reboots.

3. Installing PHP and Essential Extensions

PHP powers dynamic web content. The script installs PHP along with a suite of extensions needed to support various functionalities such as database connectivity, JSON processing, and more.

sudo apt install php libapache2-mod-php php-cli php-common php-redis php-mysql php-cgi php-curl php-json php-mbstring php-xml php-zip php-pdo php-phar php-iconv -y

Each extension is chosen to provide comprehensive support for modern web applications.

4. Installing MariaDB

MariaDB serves as our database system. It’s known for its speed and reliability, making it a great alternative to MySQL. The script installs MariaDB, starts it, and configures it to launch on startup.

sudo apt install mariadb-server -y
sudo systemctl start mariadb
sudo systemctl enable mariadb

5. Securing Your Database

Security is critical when managing databases. The script runs mysql_secure_installation to help you set a root password, remove anonymous users, and disable remote root logins. This hardening process is essential to protect your data.

sudo mysql_secure_installation

6. Installing phpMyAdmin

phpMyAdmin provides a web-based interface for managing your MariaDB databases. The script installs phpMyAdmin and creates a symbolic link in Apache’s root directory, making it easily accessible through your browser.

sudo apt install phpmyadmin -y
sudo ln -s /usr/share/phpmyadmin /var/www/html/phpmyadmin

After installation, you can access phpMyAdmin at http://your-server-ip/phpmyadmin.

7. Restarting Apache

To apply all configuration changes and updates, the script restarts Apache. This ensures that all new installations and settings take effect immediately.

sudo systemctl restart apache2

8. Installing Composer

Composer is a dependency manager for PHP that simplifies the management of libraries and packages. The script downloads Composer and moves it to a system-wide accessible location.

curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
composer --version

This ensures that Composer is installed correctly and can be used for managing your PHP project dependencies.

The Complete Automated Installation Script

The following Bash script encapsulates all the steps mentioned above. It includes error handling to ensure that if any step fails, the installation process is halted and the system remains in a stable state.



#!/bin/bash

# Function to handle cancellation
cancel_installation() {
  echo -e "\nInstallation canceled by the user. Cleaning up..."
  exit 1
}

# Set trap for SIGINT (Ctrl+C)
trap cancel_installation SIGINT

# Update package list
echo "Updating package list..."
sudo apt update -y || cancel_installation
sudo apt upgrade -y || cancel_installation

# Install Apache
echo "Installing Apache..."
if ! sudo apt install apache2 -y; then
  echo "Failed to install Apache. Exiting..."
  exit 1
fi
sudo systemctl start apache2 || cancel_installation
sudo systemctl enable apache2 || cancel_installation

# Install PHP and necessary extensions
echo "Installing PHP and required extensions..."
if ! sudo apt install php libapache2-mod-php php-cli php-common php-redis php-mysql php-cgi php-curl php-json php-mbstring php-xml php-zip php-pdo php-phar php-iconv -y; then
  echo "Failed to install PHP. Exiting..."
  exit 1
fi

# Install MariaDB
echo "Installing MariaDB..."
if ! sudo apt install mariadb-server -y; then
  echo "Failed to install MariaDB. Exiting..."
  exit 1
fi
sudo systemctl start mariadb || cancel_installation
sudo systemctl enable mariadb || cancel_installation

# Secure MariaDB installation
echo "Securing MariaDB..."
sudo mysql_secure_installation || cancel_installation

# Install phpMyAdmin
echo "Installing phpMyAdmin..."
if ! sudo apt install phpmyadmin -y; then
  echo "Failed to install phpMyAdmin. Exiting..."
  exit 1
fi

# Enable phpMyAdmin in Apache
echo "Enabling phpMyAdmin in Apache..."
sudo ln -s /usr/share/phpmyadmin /var/www/html/phpmyadmin || cancel_installation

# Restart Apache
echo "Restarting Apache..."
sudo systemctl restart apache2 || cancel_installation

# Install Composer
echo "Installing Composer..."
if ! curl -sS https://getcomposer.org/installer | php; then
  echo "Failed to download Composer. Exiting..."
  exit 1
fi
sudo mv composer.phar /usr/local/bin/composer
composer --version || cancel_installation

# Final Summary
echo "Installation complete!"
echo "---------------------------------------------------------"
echo "Apache is installed and running."
echo "PHP and necessary extensions are installed."
echo "MariaDB is installed and running."
echo "phpMyAdmin is accessible at http://$(hostname -I | awk '{print $1}')/phpmyadmin"
echo "Composer is installed and ready to use."
echo "---------------------------------------------------------"

This script ensures that each step is executed correctly, and that the system is left in a stable state even if errors occur. By automating the installation process, you can deploy a fully functional LAMP server quickly and with confidence.

Wrapping Up

With your LAMP server now set up on your VPS, you have a robust environment ready for hosting websites, web applications, or even development projects. The flexibility of a VPS combined with the reliability of the LAMP stack makes this an ideal solution for many use cases.

Remember to keep your server updated and secure by routinely applying patches and monitoring its performance. With tools like phpMyAdmin and Composer at your disposal, managing your server and deploying applications becomes a streamlined process.

Conclusion

This guide has walked you through setting up a LAMP server on a VPS in under an hour. The key takeaways include:

  • Using a VPS for greater control, performance, and security.
  • The benefits of the LAMP stack for robust web hosting.
  • Automating server setup with a comprehensive Bash script.
  • Ensuring secure database management and easy PHP dependency management with Composer.

For the full installation script and more details, be sure to visit our GitHub repository: https://github.com/giftbalogun/vpsLAMPsetup.

Happy Hosting!

If you read this far, tweet to the author to show them you care. Tweet a Thanks
Gift, this is a solid guide for setting up a LAMP stack on a VPS, and the automation script is a great touch!  The step-by-step breakdown makes it accessible even for those new to server management. One suggestion: Have you considered adding a section on firewall configuration (like UFW rules) to enhance security right from the start?
Thanks James, for this suggestion, a newbie would not know but personally, I added on my servers, but I will create a new guide solely focus on firewall integration into the script.
Thanks that sounds like its going to be a good guide.
Great initiative to implement LAMP on VPS hosting plan. Can we use this approach on Hostinger as well?
Yes, if you are running a VPS with nothing preconfigured.
Super pro tip that few know about but has a huge impact on performance:
Increase the PHP memory allocation in WordPress by modifying the `wp-config.php` file.
Open the `wp-config.php` file located in the root directory of your WordPress installation and add the following: `define('WP_MEMORY_LIMIT', '256M');`. This sets the maximum memory limit WordPress can use to 256MB or any value you specify. If this doesn't work due to server restrictions, you may also need to adjust the `php.ini` file by setting `memory_limit = 256M`, or update the `.htaccess` file with `php_value memory_limit 256M`, depending on your hosting environment.
Note: The default memory allocation for a Wordpress instance is 40Mb.

More Posts

Laravel 12 & Laravel Cloud: The Next Big Leap in Web Development

Snehal Kadwe - Mar 9

The Ultimate Guide to Mastering Git Commands: Everything You Need to Know

Hanzla Baig Dev - Feb 21

Laravel Under The Hood - How to Extend the Framework

Oussama Mater - Feb 3

A Web App to Showcase all your GitHub Projects

2KAbhishek - May 6, 2024

Connect a Solana wallet in Next.js to start building dApps effortlessly.

adewumi israel - Jan 24
chevron_left