homepage >> Installing LAMP on Ubuntu computer fo    
 

Yossef Benharosh is an apt web developer and the author of the eBook The essentials of object oriented PHP.

Yossef Benharosh web developer profile linkedin twitter github

Installing LAMP on Ubuntu computer for development purposes

This guide is for those who would like to install LAMP stack on their Ubuntu PC at home. The website is not secured and so it shouldn't be used for an in-comming traffic from the interent. It is neither the recommanded way to install websites locally (Docker container can be better alternative if that's your thing) instead the procedure explained here is merely a quick and dirty way to do the job.

Here's a step-by-step guide on how to set up a LAMP stack with the root directory in /srv/[sitename]/public_html on Linux computer:

 

1. Install LAMP Components

First, ensure you have Apache, PHP, and MySQL installed:

sudo apt update
sudo apt install apache2 php mysql-server libapache2-mod-php

 

2. Set Up the Custom Root Directory

  1. Create the Directory:

    mkdir -p /srv/[sitename]/public_html
    chmod -R 755 /srv/[sitename]/public_html
    
  2. Give Apache Access to Your [sitename]'s Directory:

    Add www-data (Apache's user) to your user group:

    sudo usermod -a -G $USER www-data
    chmod -R g+rx /srv/[sitename]

 

3.Configure Apache

  1. Create a New Virtual Host Configuration:

    sudo nano /etc/apache2/sites-available/[sitename].conf
    

    Add the following:

    <VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /srv/[sitename]/public_html
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    
        <Directory /srv/sitename/public_html>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
        </Directory>
    </VirtualHost>
    
  2. Enable the New Site:

    sudo a2dissite 000-default.conf
    sudo a2ensite [sitename].conf
    sudo systemctl reload apache2
    
  3. Enable .htaccess (if needed):

    To use .htaccess files for custom rules, ensure AllowOverride All is set in your <Directory>block (as shown above). Then enable the rewrite module:

    sudo a2enmod rewrite
    sudo systemctl restart apache2

 

4. Adjust Permissions for Development

If Apache still has permission issues, you can loosen them for development:

chmod -R 775 /srv/[sitename]/public_html
chown -R $USER:www-data /srv/[hostname]/public_html

 

5. Test Your Setup

  1. Create a PHP Info File:

    echo "<?php phpinfo(); ?>" > /srv/[sitename]/public_html/info.php
    
  2. Access it in Your Browser: Open http://localhost/info.php to verify PHP is working.

 

6. Create a New MySQL Database and User

Run the following commands to set up the database and user based on your provided credentials:

sudo mysql -u root -p

Enter your MySQL root password, then execute these commands inside the MySQL shell:

-- Create the database
CREATE DATABASE [db_name];
-- Create the user and assign a password
CREATE USER '[db_user]'@'localhost' IDENTIFIED BY '[db_user_pass]';
-- Grant the user all privileges on the database
GRANT ALL PRIVILEGES ON [db_name].* TO '[db_user]'@'localhost';
-- Apply changes
FLUSH PRIVILEGES;
EXIT;

 

7. Optional: Import the Database

To import a database (if needed), follow these steps:

  1. Place the SQL dump File in an Accessible Location: Assume your dump file is named my_dump.sql and located in /srv/[sitename].

  2. Use mysql Command for Import:

    Run the following command from your terminal:

    mysql -u [db_user] -p [db_name] < /srv/[sitename]/my_dump.sql

    It will prompt for the password ([db_user_pass]).

  3. Monitor Progress:

    Depending on the size of your database, the import process might take some time.

  4. After the import completes, you can log in to MySQL to verify the database:

    mysql -u [db_user] -p

    Run the following SQL commands to ensure the data exists:

    USE [db_name];
    SHOW TABLES;

 

8. Optional: Install Required PHP packages

Often, you may need to install PHP packages that are missing. In order to install them you need to run commands in your terminal (here I use mysql package as an example to a pacage):

sudo apt install php-mysql

This will install the mysqli extension along with other MySQL-related extensions for PHP.

After installing the extension, restart Apache to load the new module:

sudo systemctl restart apache2

To verify the Installation:

echo "<?php phpinfo(); ?>" > /srv/[sitename]/public_html/info.php
  • Access it in Your Browser: Open http://localhost/info.php.

  • Search for mysqli: Use your browser's search function (Ctrl+F or Cmd+F) to look for "mysqli". If it is installed, you'll see a section displaying mysqli details.

 

9. Troubleshooting

  1. Ensure PHP Is Installed and Active:

    Check the installed PHP version:

    php -v
  2. Verify the Extension Directory:

    Check if mysqli (or any other extension) is available in the extension directory:

    php -m | grep mysqli
  3. Enable the Extension (if required):

    If the extension isn't enabled, you can enable it manually:

    sudo phpenmod mysqli
    sudo systemctl restart apache2
  4. Check for Errors:

    Run the following command to ensure PHP recognizes the extension:

    php -i | grep mysqli

 

With these steps, you’ll have a fully functional LAMP stack using /srv/[sitename]/public_html as your website root.

 

Recommended for you:

Linux terminal cheat sheet

Upload files to Laravel app

Using jQuery and AJAX to dynamically update a page