← Other topics

Multiple Laravel apps on a single Apache server

Video Notes

Following up on my video Deploy Laravel on Ubuntu Apache server, this guide will cover running multiple Laravel applications on the same Apache server using unique domains.

To demonstrate, I have two Laravel applications set up on an Apache server in these directories:

  • /var/www/demo1 - We’ll call this application Demo1
  • /var/www/demo2 - We’ll call this application Demo2

Set up apps

Following the instructions from the Deploy Laravel on Ubuntu Apache server guide, I have already completed the following steps:

  • Readied the server to run Laravel applications
    • Made sure the server had all the required PHP modules
    • Enabled URL rewriting
    • Installed Composer
  • Set up the Laravel applications
    • Cloned them from Github
    • Ran composer install --optimize-autoloader --no-dev to download all the outside dependencies
    • Created a .env file
    • Set appropriate permissions on the storage and bootstrap/cache directories

All that is left to do is set up the Apache site configs and configure the domains.

Apache site configs

For each of the apps, I’ll create an Apache site config file in the directory /etc/apache2/sites-available/ using the template provided below.

E.g.:

  • /etc/apache2/sites-available/demo1.conf
  • /etc/apache2/sites-available/demo2.conf

Within this template, you need to update the values ServerName, DocumentRoot, and ErrorLog as appropriate for your project.

Site config template:

<VirtualHost *:80>

    # Set the domain name that will load this site
    ServerName demoX.com

    # Set the document root for this Laravel app
    DocumentRoot /var/www/demoX/public

    # Configure settings for the above DocumentRoot
    <Directory /var/www/demoX/public>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    # Configure error logs
    ErrorLog ${APACHE_LOG_DIR}/demoX-error.log
    CustomLog ${APACHE_LOG_DIR}/demoX-access.log combined
</VirtualHost>

To enable the above configs, I’ll run these commands

> a2ensite demo1.conf
> a2ensite demo2.conf

Run this command to check for any errors in the configs:

> apache2ctl -t

Restart Apache to make the changes take effect:

> systemctl restart apache2

Configuring the domain

With the above setup in place, the next step is to configure the domains I want to use for these sites to point to the IP address of the server where they’re running.

For the purposes of the demonstration, I will configure a domain I own of hesweb.xyz to point to the Demo1 application, and I will use a subdomain of demo.codewithsusan.com to point to the Demo2 application.

Configuring domains for separate Laravel applications on the same Apache server

Test it

After saving the above settings and waiting a minute or two for the changes to propagate, I was able to load the individual sites on their respective domains:

Two separate Laravel applications with unique domains running on the same Apache server

If this info helped you out you can say thanks and support future content by clicking my Amazon affiliate link: https://amzn.to/3UtgnYk. If you make a purchase on Amazon within 24 hours of clicking the link I may receive a micro-commission and it costs you nothing extra. Any income from these sales goes directly to supporting me in making new videos and guides. Thank you for your support!

← Other topics