Setting Up a LAMP Stack with Composer Dependency Management

The LAMP stack—Linux, Apache, MySQL, and PHP—provides a solid foundation for developing and deploying web applications. Composer enhances this setup by managing PHP package dependencies efficiently. Here's how to get everything running on a Linux system.

Installing Composer

Before proceeding, ensure Composer is available system-wide:

curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
sudo chmod +x /usr/local/bin/composer

Verify the installation:

composer --version

Setting Up Apache

Update your package index and install the Apache web server:

sudo apt-get update
sudo apt-get install apache2

Apache should start automatically. To check its status:

sudo systemctl status apache2

Installing MySQL Server

Deploy the MySQL database engine along with client utilities:

sudo apt-get install mysql-server mysql-client

During installation, you'll be prompted to set a root password. Store this credential securely—you'll need it for database administration.

Secure the MySQL installation:

sudo mysql_secure_installation

Installing PHP and Required Extensions

Install PHP along with the Apache module and MySQL driver:

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

For additional common extensions:

sudo apt-get install php-cli php-mbstring php-xml php-curl php-zip

Restart Apache to load PHP:

sudo systemctl restart apache2

Create a test script to confirm PHP integration:

<?php
// Save as /var/www/html/status.php
echo '<h1>PHP Configuration</h1>';
echo '<p>Version: ' . phpversion() . '</p>';

Access http://your_server_ip/status.php in a browser to verify the setup.

Managing Dependencies with Composer

Navigate to your project directory and initialize Composer:

cd /var/www/html/myproject
composer init

This creates a composer.json file. Add dependencies as needed:

{
    "name": "myapp/web",
    "description": "Custom web application",
    "require": {
        "php": ">=7.4",
        "guzzlehttp/guzzle": "^7.0",
        "twig/twig": "^3.0"
    }
}

Install all declared packages:

composer install

Composer resolves dependencies, downloads packages to the vendor/ directory, and geneartes an autlooader.

To update packages later:

composer update

Directory Permissions

Ensure the web server can read your project files:

sudo chown -R www-data:www-data /var/www/html/myproject
sudo chmod -R 755 /var/www/html/myproject

Virtual Host Configuration

Create a custom virtual host for your application. Edit /etc/apache2/sites-available/myproject.conf:

<VirtualHost *:80>
    ServerName myproject.local
    DocumentRoot /var/www/html/myproject/public
    
    <Directory /var/www/html/myproject/public>
        Options -Indexes +FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    
    ErrorLog ${APACHE_LOG_DIR}/myproject-error.log
    CustomLog ${APACHE_LOG_DIR}/myproject-access.log combined
</VirtualHost>

Enable the site and reload Apache:

sudo a2ensite myproject.conf
sudo a2enmod rewrite
sudo systemctl reload apache2

Add the hostname to /etc/hosts for local testing:

127.0.0.1    myproject.local

Tags: LAMP composer PHP apache MySQL

Posted on Fri, 09 Oct 2026 16:53:59 +0000 by insanityonline