Valet: WordPress Multisite

Created on 31 Aug 2016  路  18Comments  路  Source: laravel/valet

Upon migrating some client dev sites to a new Mac running Valet (old Mac is running local installs of PHP, Apache & Mysql and using vhosts for everything), a handful of which are WordPress Multisite installations, I came across an interesting issue where whenever a multisite install of WP is accessed, the request hangs and eventually php crashes and needs to be restarted. I'm assuming this is due to a redirect loop somewhere but nothing shows up in the Caddy logs to verify this, eventually just 'broken pipe' entries.

I'm not sure the best way to handle this as with Apache and Nginx, rewrites are required in order for any site on the multisite 'network' (aside from the root site) to work properly. By default, the 'uploads' directory is no longer used and instead a separate directory called 'blogs.dir' is created with sub directories mapping to each site is used. This could be used in a driver to determine if it's a multisite install, but the requests to that directory would still need to be rewritten due to how wp populates the uri's. As an example: WP uri: http://site.dev/sitename/files/2016/1/image.jpg Actual location: http://site.dev/blogs.dir/siteid/files/2016/1/image.jpg . However another issue here is that multisite can either be enabled as Domain based (subdomains) or Path based (subfolders). This is determined when it's created and a flag is toggled in wp-config determining the type.

The easiest way to handle this I presume would be to add rewrites to the Caddyfile for this, but it looks like there's just a single Caddyfile that applies to all sites (unless I overlooked something) which would prevent this method so I'm not really sure where to go.

Here's the WP doc pages on multisite as well as pages listing rewrite rules needed, etc:
https://codex.wordpress.org/Create_A_Network
https://codex.wordpress.org/Multisite_Network_Administration

Bitnami also publishes both installer packages and pre-built vm images for multisite: https://bitnami.com/stack/wordpress/ - (Local install section on the right)

new-driver

Most helpful comment

@rawcreative @lauhakari I created a custom driver for handling subdirectory multisite installs:
https://github.com/Objectivco/WordPressMultisiteSubdirectoryValetDriver

It won't work with subdomains (and will in fact break them). A workaround to use this driver in tandem with subdomains is to modify the serves() function to something like this:

        $multisite_paths = array(
            "/Users/you/Sites/foo",
        );

        return in_array($sitePath, $multisite_paths); 

Then you can just add the site to the list for each additional subdirectory install.

All 18 comments

Ran into similar problems yesterday when I was to test some multisite stuff locally.
First setup the MU with /blog/, but as you say didn't work at all. But then I switched it to subdomain instead and that works just fine. The caveat is that you have to manually valet link all the subdomains you want to use, in the same folder. But seems to work fine =)

@rawcreative @lauhakari I created a custom driver for handling subdirectory multisite installs:
https://github.com/Objectivco/WordPressMultisiteSubdirectoryValetDriver

It won't work with subdomains (and will in fact break them). A workaround to use this driver in tandem with subdomains is to modify the serves() function to something like this:

        $multisite_paths = array(
            "/Users/you/Sites/foo",
        );

        return in_array($sitePath, $multisite_paths); 

Then you can just add the site to the list for each additional subdirectory install.

@clifgriffin I'm a little confused by your comment. So this does, or does not work with subdirectories? You said it's a driver for handling subdirectory installs, but then said it won't work with subdirectories and breaks them.

@rawcreative Typo. Fixed. :) It works with subdirectories but not subdomains.

I'm running into a 404 with this driver in the .valet/Drivers directory :(

So is there a solution for multisites on valet yet? Having same problem as @fabianhenzler when trying @clifgriffin driver.

The only 404 I see is when going to /wp-admin/ with no PHP file specified.

Just noticed it recently honestly...is that what you guys are seeing?

@clifgriffin Great work! It worked for sub-sites without me doing any extra work!

Yes if for network we go to /wp-admin we see 404 however this works fine /wp-admin/index.php as it is dev env so no issues as Sub sites in sub folders working great. Thank you!!!

@mohsinr Glad to hear it worked for you.

Open to ideas on the wp-admin slash problem!

hey @clifgriffin , I managed to get my mu wp app just by changing the nginx config file. If that's something you're interested in, I can share it with you.

That said, I'm not sure what's going to happen when I'll update valet. Does anyone know if my changes to the ~/.valet/Nginx/{site}.dev file will disappear?

@nicgutierrez can you please share your nginx config file and how you implemented it. As my mu install has stopped working and it is showing 404 with or without the driver from @clifgriffin (It worked great before but just a week ago i upgraded some stuff on composer and now everything else works but multisite.)

@nicgutierrez I wouldn't think those changes would be upgrade safe.

@mohsinr If you're seeing a 404 when going to wp-admin, try adding a slash to the end. I haven't had a chance to track down that bug. PR's are welcome.

@clifgriffin bummer! thanks for confirming.

I used to have a custom valet driver for this but for some reasons the loading time was incredibly slow, that's why I decided to change the config file. I'll just leave it here just in case.

````
server {
listen 80;
server_name blog.dev www.blog.dev *.blog.dev;
return 301 https://$host$request_uri;
}

map $uri $blogname{
~^(?P/[^/]+/)files/(.*) $blogpath ;
}

map $blogname $blogid{
default 1;
}

server {
listen 443 ssl http2;
server_name blog.dev www.blog.dev *.blog.dev;
root /Users/nic/blog;
charset utf-8;
index index.php;

location /41c270e4-5535-4daa-b23e-c269744c2f45/ {
    internal;
    alias /;
    try_files $uri $uri/;
}

ssl_certificate /Users/nic/.valet/Certificates/blog.dev.crt;
ssl_certificate_key /Users/nic/.valet/Certificates/blog.dev.key;

location ~ ^/files/(.+) {
    try_files /wp-content/blogs.dir/1/files/$1 /wp-includes/ms-files.php?file=$1 ;
    access_log off; log_not_found off; expires max;
}

#avoid php readfile()
location ^~ /blogs.dir {
    internal;
    alias /Users/nic/blog/wp-content/blogs.dir ;
    access_log off;     log_not_found off; expires max;
}

if (!-e $request_filename) {
    rewrite /wp-admin$ $scheme://$host$uri/ permanent;
    rewrite ^(/[^/]+)?(/wp-.*) $2 last;
    rewrite ^(/[^/]+)?(/.*\.php) $2 last;
}

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

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 /Users/nic/.valet/Log/nginx-error.log;

location ~ \.php$ {
    try_files $uri =404;
    include fastcgi_params;
    fastcgi_pass unix:/Users/nic/.valet/valet.sock;
}

}

````

I was involved in creating a custom driver for wordpress multisite at my former workplace, maybe it can help somebody: https://github.com/fewagency/best-practices/blob/master/Wordpress/multisite-url-rewrites.md

@bjuppa This driver fixes my issue. Thank you!

Glad to hear @phpanos ! And thanks to my former colleague @carolineschouenborg for the collaboration!

@phpanos Thanks a lot for sharing, it solved my issue also with multisite, on Valet.

Closing as it seems there are some solid options for custom drivers here.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

EHLOVader picture EHLOVader  路  4Comments

dustinleblanc picture dustinleblanc  路  4Comments

TimOgilvy picture TimOgilvy  路  3Comments

Alshie picture Alshie  路  4Comments

hsleewis picture hsleewis  路  5Comments