Introduction

This documentation will guide you through how to set up a LAMP stack (Linux, Apache, MySQL, PHP) on your Ubuntu machine. A LAMP stack is a popular and powerful web development environment that allows you to create dynamic websites and applications.

We will guide you on how to install and configure each component of the LAMP stack step by step. You will also learn how to test your LAMP stack and troubleshoot common issues. By the end, you will have a fully functional LAMP stack ready for your web projects. Let’s get started!

Installing Apache

Apache is a widely used web server software that allows your Ubuntu machine to serve web content to users accessing it through their web browsers. To install Apache on your Linux Ubuntu machine, follow these steps:

  1. Open a terminal window.
  2. Run the following command to update the packages and make sure you have the latest:
  3. sudo apt update
  4. Then, install the Apache package by running:
  5. sudo apt install apache2
  6. Once the installation is complete, Apache should be up and running automatically. You can verify this by entering your server's IP address or hostname into a web browser. If Apache is installed correctly, you should see the default Apache2 Ubuntu Default Page.
Screenshot showing the apache 'it works' page

Installing PHP

PHP is a server-side scripting language commonly used for web development. To install PHP on your Ubuntu machine, follow these steps:

  1. Open a terminal window.
  2. Install PHP along with the necessary modules by running:
  3. sudo apt install php8.2 libapache2-mod-php8.2 php8.2-mysql
  4. After the installation is complete, restart the Apache web server to apply the changes:
  5. sudo systemctl restart apache2

Installing MySQL

MySQL is a popular open-source relational database management system used to store and manage data. To install MySQL on your Ubuntu machine, follow these steps:

  1. Open a terminal window.
  2. Install the MySQL server package by running:
  3. sudo apt install mysql-server
  4. Once the installation is complete, MySQL should start automatically. You can verify this by checking the MySQL service status:
  5. sudo systemctl status mysql

Adding a new user

To add a new user to MySQL, follow these steps:

  1. Log in to the MySQL root account using the following command:
  2. sudo mysql -u root -p
  3. Enter your MySQL root password when prompted.
  4. Once logged in, create a new MySQL user with the desired username and password:
  5. CREATE USER 'newuser'@'%' IDENTIFIED WITH mysql_native_password BY 'password';
  6. Replace 'newuser' with the desired username and 'password' with the desired password.
  7. Grant appropriate privileges to the new user. For example, to grant all privileges on all databases:
  8. GRANT ALL PRIVILEGES ON *.* TO 'newuser'@'localhost' WITH GRANT OPTION;
  9. Flush the privileges to apply the changes and exit the MySQL shell:
  10. FLUSH PRIVILEGES;
    exit;

Adding a new database

To add a new database to MySQL, follow these steps:

  1. Log in to the MySQL root account if necessary:
  2. sudo mysql -u root -p
  3. Enter your MySQL root password when prompted.
  4. Once logged in, create a new database.
  5. CREATE DATABASE dbname;
  6. Replace 'dbname' with the desired database name.

Assigning permissions

To assign your user permissions to our newly created database, follow these steps:

  1. Log in to the MySQL root account if necessary:
  2. sudo mysql -u root -p
  3. Enter your MySQL root password when prompted.
  4. Grant the user permission to access and manipulate the new database:
  5. GRANT ALL ON dbname.* TO 'username'@'%';
  6. Replace 'dbname' with the database name and 'username' with the username you created earlier.
  7. Flush the privileges to apply the changes and exit the MySQL shell:
  8. FLUSH PRIVILEGES;
    exit;

Configuring a vhost

A virtual host (vhost) allows Apache to serve multiple websites from the same IP address. To configure a vhost in Apache on your Ubuntu machine, follow these steps:

  1. Navigate to the Apache sites-available directory:
  2. cd /etc/apache2/sites-available;
  3. Create a new configuration file for your virtual host. You can copy the default configuration file as a template:
  4. sudo cp 000-default.conf yourdomain.conf
  5. Edit the newly created configuration file with a text editor of your choice:
  6. sudo nano yourdomain.conf
  7. Configure the virtual host by specifying the server name, document root, and any other necessary settings. Here's an example configuration:
  8. <VirtualHost *:80>
        ServerName yourdomain.com
        DocumentRoot /var/www/yourdomain.com/public_html
        ErrorLog ${APACHE_LOG_DIR}/yourdomain_error.log
        CustomLog ${APACHE_LOG_DIR}/yourdomain_access.log combined
    </VirtualHost>
  9. Save the changes and exit the text editor.
  10. Enable the virtual host by creating a symbolic link to the sites-enabled directory:
  11. sudo a2ensite yourdomain.conf

  12. Reload Apache to apply the changes:
  13. sudo systemctl reload apache2

Testing the LAMP stack

To test that your LAMP (Linux, Apache, MySQL, PHP) stack is working correctly, follow these steps:

  1. Create a PHP info file in the Apache document root directory:
  2. sudo nano /var/www/html/info.php

  3. Add the following PHP code to the file:
  4. <?php

    phpinfo();

    ?>

  5. Save the changes and exit the text editor.
  6. Open a web browser and navigate to http://your_server_ip/info.php (replace 'your_server_ip' with your server's IP address).
  7. If PHP is installed and configured correctly, you should see a page displaying detailed information about your PHP configuration.

This concludes the installation and configuration of Apache, PHP, MySQL, and setting up a virtual host on your Linux Ubuntu machine. You now have a fully functional LAMP stack ready for web development and hosting.