Laravel inside WordPress on the same domain

Ivan Radunovic
Laravel inside WordPress on the same domain

Hosting Laravel in the directory of a WordPress site is possible by proxying the directory to the actual location of the Laravel application.

Modifying the Laravel framework and relocating folders and files inside it is not a good idea. The approach described in this article is the best since Laravel will be hosted separately, but on the front end, it’ll appear as in WordPress.

Case of hosting Laravel inside the WordPress site

WordPress is a well-known CMS with plugins for everything, a large user base, and many experts available to hire.

Clients love it since there is almost no learning curve in using the Admin panel. They can update the content and the landing pages without any help from the developers.

If the landing page is designed in a custom application of any framework, any change would require asking the developer to do it.

Using WordPress, you can remove bottlenecks in your editing process since anyone can update WordPress posts and pages.

Hiring for WordPress is easier.

In some situations, you’ll need to hire a WordPress expert, be it for updating the theme, implementing some plugins, or coding custom functionality. Since you separated your application, which you’re selling, from the marketing site of your business, there is no danger in giving access to new team members.

You can iterate faster landing pages and content in WordPress than any other custom CMS.

Separating the marketing site from the application site is always a good idea since no one is blocking the other side.

There is no shortage of WordPress experts, and rates are affordable compared to custom app development.

Configuring web server to serve Laravel from the subdirectory of WordPress

In this example, the Nginx web server will be used. It’s a standard at WPJack. You can apply the same principles for Apache or any other web server if it supports reverse proxy.

Basic WordPress Nginx setup

Below is the simplified Nginx setup:

server {
    listen 80;
    listen [::]:80;
    server_name startup.com;
    server_tokens off;
    root /home/starter/startup.com;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log off;
    error_log  /var/log/nginx/startup.com-error.log error;

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

Inside the folder /home/starter/startup.com/ we have WordPress installed.

When someone visits startup.com, WordPress will be served.

With this setup, you can host terms, privacy, contact pages, and posts inside WordPress and serve application from a subdirectory.

Laravel inside WordPress – Serving Laravel from the subdirectory

When someone visits startup.com/app Laravel application should be served.

There are a few options where Laravel applications can be uploaded:

  • Same VPS instance as the WordPress site
  • Second VPS instance on the same internal network
  • Second VPS instance on the remote network

The first option is the cheapest one, but it’s not clean to mix production applications with marketing sites on the same server. If this was a dedicated server, that could be OK, but VPS instances are very cheap today. It does not make sense not to use multiple VPSs.

Below, all 3 options will be elaborated.

Installing Laravel on the same server as the WordPress site

In this case, Laravel will be installed inside /home/app/startup-app/ folder.

Nginx will serve it on a different port internally. No domain will be tied to it, nor will we publicly disclose this installation from its Nginx configuration.

We’ll use WordPress Nginx configuration to proxy public traffic to the Laravel local site.

server {
    listen 62700;
    server_name 127.0.0.1;
    server_tokens off;
    root /home/app/startup-app/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log off;
    error_log  /var/log/nginx/laravel-error.log error;

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }
}

As you can see, we chose port 62700 to serve the application. For the server name, Nginx is using the address of the localhost 127.0.0.1.

If you try to visit this port from the public internet on the IP address of your server, you’ll see nothing.

Now, we will expose Laravel on the app subdirectory. To do that, we need to add a new section inside the WordPress nginx configuration.

  location /app/ {
        proxy_pass http://127.0.0.1:62700;
    }

This location section goes above the existing location section of the WordPress Nginx configuration.

After enabling both sites and reloading the Nginx web server, the Laravel application will be available at startup.com/app.

Install WordPress on any Cloud. In under 5 minutes.
Supports Linode, Digital Ocean, Hetzner and Vultr.
Free Tier includes 1 server and 2 sites.
Sign up today