Running multiple versions of Laravel on the same server is common when hosting multiple projects. Here’s how to do it:
1. Setup Different Laravel Projects in Separate Directories
Store each Laravel project in its own directory under your web server’s root, for example:
/var/www/html/project-laravel-8
/var/www/html/project-laravel-9
2. Install Compatible PHP Versions
Laravel projects may require different PHP versions. Use a version manager like PHP-FPM (FastCGI Process Manager) or tools like update-alternatives
.
a. Check Installed PHP Versions:
php -v
b. Install Additional PHP Versions:
sudo apt install php7.4 php8.0 php8.1 php8.2
sudo apt install php7.4-fpm php8.0-fpm php8.1-fpm php8.2-fpm
c. Configure PHP-FPM:
Each PHP version runs on a separate socket or port. Example /etc/php/7.4/fpm/pool.d/www.conf
:
listen = /run/php/php7.4-fpm.sock
3. Configure Virtual Hosts for Each Laravel Project
You need separate virtual hosts for each project and specify the PHP version using PHP-FPM or ProxyPassMatch
.
a. Example Apache Virtual Hosts:
Create virtual host files in /etc/apache2/sites-available
.
For Laravel 8 (/etc/apache2/sites-available/laravel8.conf
):
<VirtualHost *:80>
ServerName laravel8.local
DocumentRoot /var/www/html/project-laravel-8/public
<Directory /var/www/html/project-laravel-8/public>
AllowOverride All
Require all granted
</Directory>
# Use PHP 7.4 for Laravel 8
<FilesMatch \.php$>
SetHandler "proxy:unix:/run/php/php7.4-fpm.sock|fcgi://localhost"
</FilesMatch>
ErrorLog ${APACHE_LOG_DIR}/laravel8-error.log
CustomLog ${APACHE_LOG_DIR}/laravel8-access.log combined
</VirtualHost>
For Laravel 9 (/etc/apache2/sites-available/laravel9.conf
):
<VirtualHost *:80>
ServerName laravel9.local
DocumentRoot /var/www/html/project-laravel-9/public
<Directory /var/www/html/project-laravel-9/public>
AllowOverride All
Require all granted
</Directory>
# Use PHP 8.1 for Laravel 9
<FilesMatch \.php$>
SetHandler "proxy:unix:/run/php/php8.1-fpm.sock|fcgi://localhost"
</FilesMatch>
ErrorLog ${APACHE_LOG_DIR}/laravel9-error.log
CustomLog ${APACHE_LOG_DIR}/laravel9-access.log combined
</VirtualHost>
b. Enable Virtual Hosts:
sudo a2ensite laravel8.conf
sudo a2ensite laravel9.conf
sudo systemctl restart apache2
4. Setup Local Hostnames
Edit /etc/hosts
(Linux/Mac) or C:\Windows\System32\drivers\etc\hosts
(Windows) to add entries for your local domains:
127.0.0.1 laravel8.local
127.0.0.1 laravel9.local
Now you can access:
- Laravel 8:
http://laravel8.local
- Laravel 9:
http://laravel9.local
5. Database Configuration
Ensure each project has its own .env
file configured for different databases:
DB_DATABASE=laravel8_db
6. Use Supervisor for Queue Workers
If your projects require workers for queues or jobs, configure Supervisor for each Laravel version:
Example Supervisor config for Laravel 8:
[program:laravel8-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/html/project-laravel-8/artisan queue:work
autostart=true
autorestart=true
numprocs=1
redirect_stderr=true
stdout_logfile=/var/www/html/project-laravel-8/worker.log
7. Using Nginx (Alternative to Apache)
For Nginx, configure separate server blocks for each Laravel project, specifying the PHP-FPM version.
Example configuration for Laravel 8:
server {
listen 80;
server_name laravel8.local;
root /var/www/html/project-laravel-8/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
8. Test Setup
Restart the server and ensure each project is accessible:
sudo systemctl restart apache2
or
sudo systemctl restart nginx
Visit the project URLs in your browser.
Let me know if you face any issues!
No comments:
Post a Comment