Table of Contents
- What Is Composer and Why Use It on Hosting
- Check PHP Version on DirectAdmin
- Install Composer on Hosting via SSH
- Create and Use composer.json for Dependencies
- Running composer install and composer update
- Project Structure for Shared Hosting
- Using Autoload in PHP Scripts
- Fixing Memory Limit and Timeout Issues
- Performance Tips for Production
- Frequently Asked Questions
- Summary
Composer is an indispensable dependency manager for modern PHP development. Whether you're working with Laravel, Symfony, Slim Framework, or any PHP project that requires packages from Packagist, setting up Composer correctly on shared hosting will help you manage dependencies professionally. This guide walks through every step, from installation to production optimization.
1. What Is Composer and Why Use It on Hosting
Composer is a Dependency Manager for PHP that automatically handles the libraries and packages your project requires. Instead of manually downloading and including files one by one, Composer reads your composer.json file and installs everything needed.
Key benefits of using Composer on hosting:
- Automated dependency management — Specify required packages in composer.json and Composer handles the rest
- Version control — Pin exact versions of each package, preventing breaking changes
- Autoloading — Composer automatically generates an autoloader, eliminating manual require statements
- Security updates — Update all packages at once, including security patches
- Consistent workflow — Every team member uses the same environment, eliminating "works on my machine" issues
2. Check PHP Version on DirectAdmin
Composer requires PHP 7.2.5 or higher (PHP 8.0+ recommended). Before installing, verify your hosting supports the required version.
How to check PHP version via DirectAdmin:
- Log in to your DirectAdmin Control Panel
- Navigate to Advanced Features → PHP Version Selector
- Select the desired PHP version (PHP 8.1 or 8.2 recommended)
- Click Save to apply the settings
Verify via SSH:
php -v
The output should show PHP 8.x.x or at least 7.4.x. You'll also need SSH access enabled on your hosting account. Contact AsiaGB Support to enable SSH access if needed.
Extensions required by Composer:
openssl— For HTTPS connections to Packagistphar— For running composer.pharfilter,json— For package processing
3. Install Composer on Hosting via SSH
Connect to your hosting via SSH, then follow these steps:
Step 1: Download the Composer installer script
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
Step 2: Verify the installer hash for security
php -r "if (hash_file('sha384', 'composer-setup.php') === file_get_contents('https://composer.github.io/installer.sig')) { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); }"
Step 3: Run the installer
php composer-setup.php
php -r "unlink('composer-setup.php');"
This creates a composer.phar file in the current directory. Run Composer commands with:
php composer.phar [command]
Install globally (recommended): If your hosting allows creating files in ~/bin:
mkdir -p ~/bin
mv composer.phar ~/bin/composer
chmod +x ~/bin/composer
export PATH="$HOME/bin:$PATH"
Add the export to ~/.bashrc to make it permanent:
echo 'export PATH="$HOME/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
4. Create and Use composer.json for Dependencies
The composer.json file is the heart of Composer dependency management. It specifies the packages your project requires along with version constraints.
Example composer.json for a Slim Framework application:
{
"name": "myproject/slim-app",
"description": "My PHP application",
"require": {
"php": "^8.1",
"slim/slim": "^4.11",
"slim/psr7": "^1.6",
"monolog/monolog": "^3.0",
"vlucas/phpdotenv": "^5.4"
},
"require-dev": {
"phpunit/phpunit": "^10.0"
},
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
"minimum-stability": "stable",
"prefer-stable": true
}
Understanding version constraints:
^4.11— Accepts 4.11.x up to (but not including) 5.0~4.11— Accepts only 4.11.x patch releases>=4.0— Accepts 4.0 and above*— Accepts any version (not recommended)
Create composer.json interactively with:
composer init
5. Running composer install and composer update
Two primary commands you need to master:
| Command | When to Use | Result | composer.lock |
|---|---|---|---|
composer install | First deploy / fresh clone | Installs from lock file | Unchanged |
composer update | Need to update packages | Updates per constraints | Updated |
composer require guzzle/guzzle | Add new package | Adds and installs | Updated |
composer remove package/name | Remove package | Removes and uninstalls | Updated |
composer dump-autoload | After adding new classes | Regenerates autoloader | Unchanged |
composer show | View installed packages | Displays list | Unchanged |
For production servers, use:
composer install --no-dev --optimize-autoloader --no-interaction
--no-dev— Skip dev dependencies (like PHPUnit)--optimize-autoloader— Generate class map for better performance--no-interaction— Use default values without prompting
6. Project Structure for Shared Hosting
Structuring your project correctly on shared hosting is critical for security. Files that don't need to be publicly accessible should never be placed in public_html.
Recommended structure:
~/
├── projects/
│ └── myapp/
│ ├── composer.json
│ ├── composer.lock
│ ├── src/ ← source code (not public)
│ ├── vendor/ ← dependencies (not public)
│ └── public/ ← symlink or copy to public_html
└── public_html/
└── myapp/
├── index.php ← entry point
└── .htaccess
vendor/, .env, and composer.json outside public_html to prevent external access.
Your index.php in public_html should contain only:
<?php
require __DIR__ . '/../../projects/myapp/vendor/autoload.php';
// Application bootstrap
7. Using Autoload in PHP Scripts
After running composer install, Composer generates vendor/autoload.php automatically. This file is the entry point for loading all installed classes.
Basic autoload usage:
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use GuzzleHttp\Client;
$logger = new Logger('app');
$logger->pushHandler(new StreamHandler('logs/app.log', Logger::DEBUG));
$logger->info('Application started');
$client = new Client(['base_uri' => 'https://api.example.com']);
$response = $client->get('/users');
For custom project classes, define namespaces in composer.json:
"autoload": {
"psr-4": {
"App\\Controllers\\": "src/Controllers/",
"App\\Models\\": "src/Models/",
"App\\Services\\": "src/Services/"
}
}
Then run composer dump-autoload to regenerate the autoloader after adding new namespaces.
8. Fixing Memory Limit and Timeout Issues
The most common issues when using Composer on shared hosting are memory limits and execution timeouts. Here's how to resolve each:
Issue: PHP memory limit exceeded
# Increase memory limit for Composer session only
php -d memory_limit=1G composer.phar install
# Or with global Composer
COMPOSER_MEMORY_LIMIT=-1 composer install
Issue: Execution timeout
php -d max_execution_time=300 composer.phar install
Issue: SSL certificate error
composer config --global cafile /etc/ssl/certs/ca-certificates.crt
Issue: Connection timeout with Packagist
composer config repo.packagist composer https://packagist.phpcomposer.com
vendor/ folder via FTP or rsync instead.
9. Performance Tips for Production
When deploying to production, several techniques can significantly improve Composer autoloader performance:
1. Optimize the autoloader
composer dump-autoload --optimize --classmap-authoritative
This generates a static class map, so PHP doesn't need to scan directories on every class load — improving performance by 20-40%.
2. Skip dev packages
composer install --no-dev
3. Enable PHP OPcache
Enable OPcache in DirectAdmin → PHP Settings to cache compiled PHP files, including those in vendor/.
4. Check for security vulnerabilities
composer audit
Run before every deployment to identify packages with known security issues.
5. Commit the lock file
Always commit composer.lock to git, but add vendor/ to .gitignore to avoid committing thousands of dependency files.
10. Frequently Asked Questions
Does Composer work on shared hosting?
Yes, as long as your hosting supports SSH access and PHP 7.2 or higher, Composer works fine on shared hosting with DirectAdmin. AsiaGB enables SSH access on all hosting packages — contact Support to activate it.
Do I need SSH access to use Composer?
Yes, Composer requires SSH to run install and update commands. Without SSH, you can run Composer locally and upload the vendor/ folder via FTP, though this is less convenient for projects requiring frequent updates.
What memory limit does Composer need?
Composer recommends 512MB to 1024MB for medium to large projects. Use php -d memory_limit=1G composer.phar install or set COMPOSER_MEMORY_LIMIT=-1 for unlimited memory in that session.
Can I use Composer with WordPress on shared hosting?
Yes, Composer can manage WordPress plugins and themes via WPackagist, or handle custom plugin and theme dependencies. The Bedrock WordPress setup uses Composer as its foundation.
Can I upload the vendor folder via FTP instead of SSH?
Yes, run composer install locally first, then upload the entire vendor/ folder via FTP. However, vendor folders often contain thousands of files, making uploads slow. SSH is strongly recommended when available.
Why is Composer so slow? How can I speed it up?
Use --prefer-dist to download zip archives instead of git clones, increase process-timeout with composer config --global process-timeout 600, or switch to an Asian mirror like packagist.phpcomposer.com for servers in Asia.
11. Summary
Composer is an essential tool for modern PHP developers. Setting it up on shared hosting with DirectAdmin is straightforward as long as you have SSH access and PHP 7.2 or higher.
Key takeaways:
- Use
composer installfor deployments,composer updatewhen updating packages - Always keep
vendor/and.envoutside public_html - Optimize the autoloader with
--optimize-autoloaderfor production - Commit
composer.lockto git for consistent environments across your team - Run
composer auditregularly to check for security vulnerabilities
Need Hosting That Supports Composer and SSH?
AsiaGB Hosting offers shared hosting with DirectAdmin, SSH access, PHP 8.x, SSD storage for fast performance, and 99% uptime to keep your PHP projects running smoothly.
View Hosting Plans