How to Deploy a LAMP Stack in Linux
The LAMP stack (Linux, Apache, MySQL, PHP) is a popular open-source web development platform used to host dynamic websites and applications. This guide will walk you through setting up a LAMP stack on a Linux server.
Prerequisites
- A Linux-based server (Ubuntu, Debian, CentOS, or any other Linux distribution)
- A user account with sudo privileges
- Basic knowledge of the Linux command line
Step 1: Update Your System
Before installing any software, update the package repository to ensure you get the latest versions:
sudo apt update && sudo apt upgrade -y # For Uduntu/Debian-based systems
Step 2: Install Apache Web Server
Apache is the most widely used web server for hosting websites.
Install Apache
sudo apt install apache2 -y # Ubuntu/Debian
Start and Enable Apache
sudo systemctl start apache2 # Ubuntu/Debian
sudo systemctl enable apache2 # Enable on boot
Verify Apache Installation
Open a web browser and navigate to your server's IP address:
http://your-server-ip
-Use ip addr show
to check the IP address of your server.
If Apache is running, you should see the default Apache welcome page.
Step 3: Install MySQL Database Server
MySQL is the database component of the LAMP stack.
Install MySQL
sudo apt install mysql-server -y # Ubuntu/Debian
Secure MySQL Installation
sudo mysql_secure_installation
Follow the prompts to set a root password and remove unnecessary settings for security.
Step 4: Install PHP
PHP is the scripting language that processes dynamic content.
Install PHP and Required Modules
sudo apt install php libapache2-mod-php php-mysql php-cli php-curl php-json -y # Ubuntu/Debian
Restart Apache to Apply Changes
sudo systemctl restart apache2 # Ubuntu/Debian
Test PHP Installation
Create a test PHP file:
echo "" | sudo tee /var/www/html/info.php
Then access it in a browser:
http://your-server-ip/info.php
If PHP is installed correctly, you will see the PHP info page.
Step 5: Configure Firewall (If Necessary)
To allow web traffic, open the necessary ports:
sudo ufw allow 80/tcp # Ubuntu/Debian
sudo ufw allow 443/tcp
Conclusion
You have successfully deployed a LAMP stack on your Linux server! You can now start developing and hosting your websites using Apache, MySQL, and PHP.