Recently I was asked if is it possible to run more than one Laravel instance on the same server. I’ve answered the first thing that came into my mind:
– sure, you can do it with different virtualhosts
– ¿Virtual hosts?
– you know, when your webserver serves one site or another based on the name you asked for on your browser.
And the guy that asked me was like:
Long story short, here I am writing a tutorial on how to run more than one Laravel on the same server.
Step 0: Laravel installation
Here on unixcop we have a couple of tutorials covering single Laravel installation, check the tags. In fact, I’m going to continue the one I’ve published before: https://unixcop.com/how-to-install-laravel-on-debian-11/. So, I assuming that you have Debian and Laravel prerequisites/dependencies already installed.
Step 1: make room for the 2nd instance
I’ve have laravel already installed on /var/www/html/laravel
. Imaginatively I renamed that folder to laravel1
and create another folder named laravel2
.
Step 2: create the project for the second instance
run the following commands to create the second instance:
cd /var/www/html composer create-project --prefer-dist laravel/laravel laravel2 chown -R www-data:www-data laravel2/
Last command (chown) is only needed if you run composer as root.
Step 3: hostnames
In this step you should set the appropriate DNS records (CNAME, A), or at least add a couple of records to the hosts
file. I’m going with this second option as this is just a tutorial:
Step 4: configure a new VirtualHost
I’ve created a /etc/apache2/sites-enabled/laravel2.conf file by copying the one for my first laravel. Then edited to change the important parts. It should be like this:
<VirtualHost *:80>
ServerName site2
ServerAdmin gonz@localhost
DocumentRoot /var/www/html/laravel2/public
<Directory /var/www/html/laravel2>
Options Indexes MultiViews
AllowOverride None
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Set ServerName
, DocumentRoot
and Directory
to the correct ones. Restart apache (or the webserver you are using) and this is it:
Pay attention to the ping on the screenshot, both hosts site1 and site2 goes to the same IP address. And when I request http://site1 and http://site2 apache serves different laravel “applications” from the same server.
Conclusion
After the last read of this article I realize that this more about VirtualHosts than Laravel. You can use the same ‘trick’ to deploy more than one instance of the same app on a single server, or even different web applications on the same server.