Technology Encyclopedia Home >How to install and configure LAMP stack on Linux?

How to install and configure LAMP stack on Linux?

To install and configure a LAMP (Linux, Apache, MySQL, PHP) stack on Linux, follow these steps:

Step 1: Update Your System

First, ensure your system is up-to-date:

sudo apt update
sudo apt upgrade

Step 2: Install Apache

Install the Apache web server:

sudo apt install apache2

After installation, you can start and enable Apache to run on boot:

sudo systemctl start apache2
sudo systemctl enable apache2

Verify Apache is running by navigating to http://your_server_ip in your web browser.

Step 3: Install MySQL

Install MySQL server:

sudo apt install mysql-server

During installation, you'll be prompted to set a root password. Follow the on-screen instructions.

Secure MySQL installation by running:

sudo mysql_secure_installation

Follow the prompts to remove unnecessary services, disable remote root login, and remove the test database.

Step 4: Install PHP

Install PHP along with some common modules:

sudo apt install php libapache2-mod-php php-mysql

Restart Apache to load the PHP module:

sudo systemctl restart apache2

Step 5: Test PHP

Create a PHP test file in Apache's web root directory:

sudo nano /var/www/html/info.php

Add the following content to the file:

<?php
phpinfo();
?>

Save and close the file. Navigate to http://your_server_ip/info.php in your web browser to see the PHP information page, confirming PHP is working correctly.

Step 6: Configure Firewall

Ensure your firewall allows HTTP and HTTPS traffic:

sudo ufw allow 'Apache Full'

Example Use Case

You can now deploy a simple PHP application on your server. For instance, you can set up a WordPress site by downloading WordPress, extracting it to /var/www/html/, and configuring the wp-config.php file with your MySQL database details.

Recommendation for Cloud Services

For a more managed and scalable solution, consider using cloud services like Tencent Cloud. Tencent Cloud offers a variety of services that can simplify the deployment and management of your LAMP stack, such as Tencent Cloud Virtual Machine (CVM) for the server infrastructure and Tencent Cloud Database MySQL for your database needs. Additionally, services like Tencent Cloud Load Balancer can help distribute traffic across multiple instances, enhancing reliability and performance.