DEV Community

loading...

Sharing Local Server with Local Network (XAMPP)

MMJ
Backend developer. Frontend enthusiast. Photographer & videomaker having time.
Updated on ・4 min read

In this simple step-by-step tutorial I’m gonna explain how to share a local server (using XAMPP) with your local network in order to share DB, folders in httpdocs (local websites) and even start a XAMPP server on other machines running a local website that connects to a DB in the shared server. That last feature was the most important for me to achieve because I needed to work with a teammate on the same project, each one on a local copy but both connected to the same DB, managing changes with a git repository on Github.

I found many similar tutorials online, but none was merging all I needed, so I decided to share my solution. Hope this can help even with different environment too.

STEP 1 – Getting static IPs for your machines

That’s not mandatory, but it’s the easiest and safest way to go.
There are many ways to do so and internet is full of good tutorials.
Here’s a video: https://bit.ly/3zrB8HA
And here’s an article: https://bit.ly/2So11HC

STEP 2 – Change some XAMPP settings

After setting static IP addresses let’s make an example assuming we have a machine sharing its XAMPP (which I’ll name “server” from now on) with IP 192.168.1.10 and another machine that will connect to the server (which I’ll name “client” from now on) with IP 192.168.1.11

You need to change some XAMPP server configuration in the server machine. I suggest to make a backup copy of the files we are going to change.
To reach the files you need to change, click on the Apache Config button in the XAMPP Control Panel as the image shows.

Alt Text

FIRST CHANGE: Now click on the httpd.conf and look for the part where it says

# Change this to Listen on specific IP addresses as shown below to 
# prevent Apache from glomming onto all bound IP addresses.
Enter fullscreen mode Exit fullscreen mode

Here you have to add the local IP address of your “server”, in this example 192.168.1.10. So, we’ll have

# Change this to Listen on specific IP addresses as shown below to 
# prevent Apache from glomming onto all bound IP addresses.
#
Listen 127.0.0.1:80
Listen 192.168.1.10:80 #(IP PC SERVER)
Enter fullscreen mode Exit fullscreen mode

SECOND CHANGE: Open again the Apache Config button in the XAMPP Control Panel and click on httpd-xampp.conf

Here we have to find where it says

    <Directory "C:/xampp/phpMyAdmin">
Enter fullscreen mode Exit fullscreen mode

We have to grant access, so we need to change it like this:

   <Directory "C:/xampp/phpMyAdmin">
        AllowOverride AuthConfig
        Require all granted
        ErrorDocument 403 /error/XAMPP_FORBIDDEN.html.var
    </Directory>
Enter fullscreen mode Exit fullscreen mode

Step 3 – Firewall Settings

This can be different basing on your machine and firewall settings.
Anyway, port 80 must be reachable from client but not open for anybody from anywhere.

To achieve that I had to turn off a blocking rule and adding one to open port 80.

To do so open your firewall configuration, in this example I use windows firewall: click on windows logo, type “firewall” then press enter.
In next window click on advanced settings.

Then click on Inbound rules and look for a blocking rule (red prohibition sign) that says “Apache HTTP Server”. If you find it, right click on it and choose disable.

After that, staying on Incoming rules section, click on New rule… in the upright corner.

In each step of the wizard to create rule choose:

  • Custom
  • All programs
  • Protocol: TCP, Local port: Specific port – 80
  • Remote IP address: 192.168.1.11 (client machine)
  • Allow the connection
  • Uncheck Public
  • Give name and description + finish button

Alt Text
Alt Text

At this point if server machine starts XAMPP, the client can reach the server using its browser using the url http://192.168.1.10 as it was on the server machine, so it can reach phpadmin and DBs (http://192.168.1.10/phpmyadmin) or any website in the htdocs folder (http://192.168.1.10/AnyLocalWebsiteFolder).

Anyway, it could be still impossible for any machine to start its own XAMMP, open a local copy of the same project and connect it to the DB on the server machine.

To make it work, we need another step.

Step 4 – Adding PhpMyAdmin Users

We finally have to add a user with all privileges for each machine (server included) in PhpMyAdmin, in our example we have to add root@192.168.1.10 and root@192.168.1.11

Now each machine can work freely on their local copy of the project, using a git versioning on a remote common repository, connecting on a single local DB on the server machine.

The snippet defining constants to build the connection will be the same for every machine

    } else if ($_SERVER['HTTP_HOST']=="localhost" || $_SERVER['HTTP_HOST']=="192.168.1.10"){
      // Local Development Server 
      DEFINE ('MASTER_BASE_URL', 'http://localhost');

      DEFINE ('DB_USER', 'root');
      DEFINE ('DB_PASSWORD', '');
      DEFINE ('DB_HOST', '192.168.1.10');
      DEFINE ('DB_NAME', 'db_name');
    } else {
Enter fullscreen mode Exit fullscreen mode

Client machine(s) will run its XAMPP working on its local copy and checking the results on its local http://localhost/LocalWebsiteFolder, but connection will be correctly set on the server DB.

I'm aware that there could be many other ways to achive what I've described, if you know any and you think it's even easyer, please let me know. This is just the solution I came up with after smashing my head to the wall countless times.
Anyway, I hope it helps.

Discussion (0)