Installing Laravel on Shared Hosting: Complete Guide
Laravel is the most popular PHP framework and works excellently on shared hosting. In this guide, you will learn how to install, configure, and prepare Laravel for production on Theory7 hosting.
Requirements for Laravel
Before you begin, check if your hosting meets the requirements.
PHP Requirements
Laravel 11 requires:
- PHP 8.2 or higher
- BCMath PHP Extension
- Ctype PHP Extension
- Fileinfo PHP Extension
- JSON PHP Extension
- Mbstring PHP Extension
- OpenSSL PHP Extension
- PDO PHP Extension
- Tokenizer PHP Extension
- XML PHP Extension
At Theory7 hosting, all required extensions are available by default.
Other Requirements
- SSH access (for Composer)
- MySQL or MariaDB database
- Sufficient disk space (at least 100MB)
- Composer available via command line
Activating SSH Access
Installing Laravel requires SSH access.
Activating SSH in DirectAdmin
- Log in to DirectAdmin
- Go to Account Manager
- Click on SSH Keys
- Generate or upload your SSH key
- Activate SSH access
See also: Activate SSH Access
Connecting
ssh username@yourdomain.nl
# Or with port:
ssh -p 7777 username@server.theory7.net
Installing Laravel via Composer
Now you can install Laravel.
Navigating to the Correct Folder
# Go to your domain folder
cd ~/domains/yourdomain.nl
Creating a Laravel Project
Install Laravel in a subdirectory:
# Create new Laravel project
composer create-project laravel/laravel project
# This creates a folder 'project' with Laravel
Or directly in the current folder:
# In empty folder
composer create-project laravel/laravel .
Verifying Installation
# Check Laravel version
cd project
php artisan --version
# Output: Laravel Framework 11.x.x
Configuring Document Root
Shared hosting requires special configuration for Laravel.
Creating public_html Symlink
Laravel's public folder must be the document root:
# Remove or rename old public_html
cd ~/domains/yourdomain.nl
mv public_html public_html_backup
# Create symlink to Laravel public
ln -s project/public public_html
Alternative: .htaccess in Root
If symlinks do not work:
# In the root .htaccess
RewriteEngine On
RewriteRule ^(.*)$ project/public/$1 [L]
Configuring Database
Laravel needs a database.
Creating Database in DirectAdmin
- Go to Account Manager and then MySQL Management
- Click on Create new Database
- Choose a name and password
- Note the details
Configuring .env File
Edit the .env file:
cd ~/domains/yourdomain.nl/project
nano .env
Adjust the database settings:
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_database_user
DB_PASSWORD=your_database_password
Configuring Application
Set the basic configuration.
Generating App Key
php artisan key:generate
This generates a unique encryption key in .env.
Production Settings
Adjust .env for production:
APP_NAME="My Laravel App"
APP_ENV=production
APP_DEBUG=false
APP_URL=https://yourdomain.nl
LOG_CHANNEL=daily
LOG_LEVEL=error
Cache Settings
Optimize for production:
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_CONNECTION=database
Configuring Storage
Laravel needs write access for storage.
Creating Storage Link
php artisan storage:link
This creates a symlink from public/storage to storage/app/public.
Setting Permissions
# Ensure storage is writable
chmod -R 775 storage
chmod -R 775 bootstrap/cache
Running Migrations
Create the database tables:
# Run migrations
php artisan migrate
# With seeding (if available)
php artisan migrate --seed
Production Optimization
Optimize Laravel for speed.
Config Cache
php artisan config:cache
This caches all configuration in a single file.
Route Cache
php artisan route:cache
Significantly speeds up route resolution.
View Cache
php artisan view:cache
Pre-compiles all Blade templates.
Autoloader Optimization
composer install --optimize-autoloader --no-dev
Everything at Once
php artisan optimize
Setting Up Cronjob
Laravel's scheduler requires a cronjob:
- Go to DirectAdmin
- Go to Advanced Features and then Cron Jobs
- Add a cronjob:
* * * * * cd ~/domains/yourdomain.nl/project && php artisan schedule:run >> /dev/null 2>&1
Common Issues
500 Error After Installation
- Check if .env exists
- Generate app key: `php artisan key:generate`
- Check storage permissions
Storage Link Not Working
# Remove old link
rm -f public/storage
# Create again
php artisan storage:link
Composer Memory Error
# Increase memory limit
php -d memory_limit=512M /usr/local/bin/composer install
Blank Page Without Error Message
- Temporarily enable debug: `APP_DEBUG=true`
- Check the error
- Disable debug again
Performing Updates
Keep Laravel up to date:
# Update dependencies
composer update
# Run new migrations
php artisan migrate
# Clear and rebuild cache
php artisan optimize:clear
php artisan optimize
Related Articles
- Artisan and Console Commands via SSH
- Laravel .env and Storage Configuration
- PHP Frameworks and DirectAdmin
- More Information About Laravel Hosting at Theory7
Need Help?
We are here for you! Are you facing any issues or have questions? Our support team is happy to assist you personally. Send us a message via the ticket system - we usually respond within a few hours and are happy to help you out.
0 van 0 vonden dit nuttig