How to work with multiple sites on localhost

Hi,

I am working on a local server on my Windows 10 computer for building and testing websites. I have Apache + PHP + MySQL setup. The site folders are in htdocs folder of Apache.

My issue is that I work on multiple websites during the day and currently, whenever I want to work on a website, I move its files to htdocs folder. When I want to work on another website, I remove the previous website’s files, and move in the other website’s files, etc.

I use http://localhost/ as the root of the website and have .htaccess rewrites, which kind of complicates having multiple site files at one place.

Is there a way to make this process more efficient? Is there a way to keep all websites’ files in htdocs but they will all work properly? Just like hosting accounts handle it, you have all your websites’ folders within the public_html and their .htaccess etc. do not conflict.

Thanks for any ideas.

1 Like

The way I set mine up is pretty efficient. I separate each project based on their domain. For instance, if my actual domain is something like hottubs.com, on my localhost, I’d create subdomains under .localhost.com because I rather run a project under a TLD and not just http://localhost/ because there are a couple of down falls for this. I don’t recall exactly if this is the case, but setting cookies can be an issue or was it the use of cookies on cross sub-domain? *scratches head*. Then there’s also the fact that Twitter API dislikes the use of http://localhost/ so it rejects URIs containing that URL. So I’d always go with using a TLD to make sure everything works correctly as expected on a live server if I were to transfer files.

But getting back to the subject. I would basically create different subdomains based on the project name of the actual domain name. So using the above, we were using hottubs.com, on my localhost, it would be https://hottubs.localhost.com/. I would also create the same exact database user and information so that when I transfer the data to a live server, it would be fairly easily to do.

2 Likes

Thanks. I use the same DB details too, for easier transfer as you say. But I don’t know how to create subdomains as you explained. I mean when I put my site folders into htdocs folder, it is accessible via

http://localhost/

What should I do to make, say, site1 accessible at

http://site1.localhost/

and site2 at

http://site2.localhost/

etc.?

I think one of the issues with not using TLD is using root URIs for pages and resources such as

/style.css
/page-1/

this is how I handle URLs mostly, VS. using relative URIs such as

style.css
page-1/

I built a menu system that allows switching to another project or file.

Basically:

  1. I have a www folder with a mirror image of the sites online.
  2. My browser home button is set to call my http://localhost/_menu/index.php which dynamically displays the following:

    AND deletes the current .htaccess and index.php files.
  3. selecting the a menu item:
    a. calls a ./_menu/PROJECT_SWITCHER.php which may copy .htaccess and index.php files to the www/ folder
    b. calls the relevant menu project/file that was selected
4 Likes

@ennaido
You have to modify the hosts file within C:/Windows/System32/drivers/etc/ to setup subdomains locally. Then edit httpd-vhosts.conf file within your Apache directory for those subdomains to point to a working directory. I’ve much prefer setting my own PHP environment and not rely on 3rd party softwares like XAMPP or WAMP because when something breaks, you have to rely on that 3rd party to fix your problem or wait for a solution to come out. But when you install it yourself, whatever you break is whatever you break. It’s also a lot easier I would say to install it yourself because when the PHP team come out with a new version of PHP, you can easily replace the current version you have with the newest one in about 3 mins.

@John_Betong
That is actually a pretty neat and smart idea. Never really thought of that before. I usually prefer browsing through the directories instead of having a self made website for this purpose. For me, I just find it easier to browse through directories based on the domains. That’s just me though. I still actually like your idea and the layout in which you setup. Very interesting.

3 Likes

@spaceshiptrooper @John_Betong

Thank you both for the ideas and suggestions. I surely learned new things from this thread and I will be applying this knowledge to improve my work efficiency. And yes, I always use custom installation of Apache-PHP-MySQL, much better to manage.

1 Like

There are 2 sensible ways to do this to enable a form of virtual hosting on ‘localhost’ Both ways will be called virtual hosting, this is the term for hosting multiple things on one box.

Method 1 - port based vhosting, you are always using http://localhost but the bit that then changes is the port number, so :80, :81 etc

Method 2 - (this is the method I opt for) - faked dns based virtual hosting. So I edit my hosts file, pointing several entries to the same IP (127.0.0.1) and the can work with http://site1.localhost and http://site2.localhost etc.

2 Likes

I’m also running Windows 10 with an XAMPP setup and my localhost resides within the htdocs folder.

Each site of mine has it’s own folder within htdocs - usually taking the domain name of the project. I find it’s a pretty simple way to keep everything together and identifiable.

I use git via bitbucket to version control every project so I simply set up each project folder as a repositry and commit frequently to the cloud. I use Beanstalk for projects that are live as this allows me to quickly deploy to a server by typing a command as part of my commit comments via FTP credentials I have already setup within my beanstalk account.

1 Like

Hi,
Just want to point you to Apache’s tutorial:
https://httpd.apache.org/docs/2.4/vhosts/name-based.html

and a Sitepoint thread that explain multiple virtual hosts in detail:

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.