How to Install a LAMP Stack

A LAMP stack is a development framework based on four open-source software components: Linux, Apache, MySQL, and PHP. They are often deployed together to allow a server to host dynamic websites and PHP-based apps. Here are the step-by-step instructions for setting up a LAMP stack on Ubuntu.

Note: These instructions assume you have a fresh installation of Ubuntu. Make sure your system is up-to-date by running:

```bash

sudo apt update

sudo apt upgrade

```

 

Step 1: Install Apache

  1. Open a terminal window by pressing `Ctrl + Alt + T`.
  2. Install Apache by running the following command:
   ```bash

sudo apt install apache2    

```
  1. Once the installation is complete, enable Apache to start at boot and start the service:
```bash

   sudo systemctl enable apache2

   sudo systemctl start apache2

   ```
  1. To verify that Apache is working, open a web browser and enter your server’s IP address or “http://localhost” if you’re setting this up on your local machine. You should see the default Apache welcome page.

 

Step 2: Install MySQL

  1. Install MySQL by running the following command:
   ```bash    
sudo apt install mysql-server    
```
  1. During the installation, you will be prompted to set a root password for MySQL. Choose a strong password and remember it.
  2. After installation, secure your MySQL installation by running the following command and following the prompts:
   ```bash    
sudo mysql_secure_installation    
```

Step 3: Install PHP

  1. Install PHP and some common extensions by running:
   ```bash    
sudo apt install php libapache2-mod-php php-mysql    
```
  1. Once PHP is installed, you can test it by creating a PHP info file. Create a new file in the web server’s root directory:
   ```bash    
sudo nano /var/www/html/phpinfo.php    
```

Add the following content and save the file:

   ```php    
<?php    phpinfo();    
```
  1. Restart Apache to apply the changes:
   ```bash    
sudo systemctl restart apache2    
```
  1. In your web browser, navigate to `http://localhost/phpinfo.php` (or use your server’s IP address if it’s remote). You should see the PHP information page.

 

Step 4: Test MySQL and PHP

You can create a simple PHP script to test the connection to MySQL:

  1. Create a test PHP file:
```bash    
sudo nano /var/www/html/test-mysql.php    
```

Add the following PHP code and save the file:

   ```php    <?php    $connection = mysqli_connect("localhost", "root", "your_mysql_password");   

if (!$connection) {        

die("Database connection failed: " . mysqli_connect_error());    

}    

echo "Connected to MySQL successfully!";    

mysqli_close($connection);    ?>    

```

Replace `”your_mysql_password”` with the root password you set during the MySQL installation.

  1. Open your web browser and navigate to `http://localhost/test-mysql.php` (or your server’s IP address). You should see a message indicating a successful MySQL connection.

You’ve now successfully installed a LAMP stack on your Ubuntu system. You can start building and hosting web applications on your server.

Have additional questions? Search below: