How to set up a virtual host in Apache (WAMP, MAMP, XAMPP)
HTML-код
- Опубликовано: 14 июн 2024
- PHP for Beginners course: ➤ davehollingworth.net/phpy
PHP MVC course: ► davehollingworth.net/phpmvcy
CodeIgniter 4 course: ► davehollingworth.net/codeigni...
When developing websites and web applications, it's typical to use a web server installed on your computer. One way to separate different projects is to use a separate subfolder for each one. This can cause problems however when you move your site to a different server, if that server doesn't use the same subfolder. In this video we'll learn how to fix this by creating a virtual host for each project.
The most common web server is Apache, which you'll have if you install a package like WAMP, MAMP, XAMPP and so on.
Code shown in the video:
gist.github.com/daveh/8af6a5c...
00:00 Introduction
01:04 Sample projects
01:55 Example relative URL
03:15 Enable virtual hosts
03:58 Virtual host config
07:36 Re-enable localhost
08:13 Summary
#Apache #WAMP #MAMP #XAMPP - Наука
Thank you Dave. I followed several blogposts and videos without success before finally succeeding with yours. A bit slow for my taste, but concise and clear. Now at least I understand what I'm doing!
Basicailly nothing changed since the 90s. Always amazes me how durable tech-knowledge is.
:-) Indeed - although some things never stop changing (I'm looking at you, Laravel)
I havent created an apache VH since the 90s. So when your video popped up i was curious "how would that work these days".
And Laravel: yes.. as with other more 'modern' open source projects. I think it's because they dont understand the importance of stability and things not breaking. Performance and new features is where the focus is at..
@Chris Alas you're right. I've been stung more than once writing a tutorial for things like that that have been completely changed, renderering the tutorial obsolete too. :-/
(Nice to hear from someone else who was doing this back in the 90s too!)
Thank you so much this worked for me
The equivalent in nginx would be very helpful!
These are called Server Blocks in nginx - there's a tutorial here: www.keycdn.com/support/nginx-virtual-host
Hi thanks for the tuto, on mobile devices can we acces to the virtual host we created ?
It depends on your network setup, if you're connected to the same network, you should be able to access it, however it might require some network configuration. Alternatively, you could open it up to the internet, with something like portforward.com/, or just use the mobile developer tools in your browser if you want to view it in a mobile device.
@@dave-hollingworth Ok its because im trying to check the render of my website on other device (tab mobile) and i tried several methods but i can't access to the website even if i m on the same network. Thanks for your answer.
@@andypro8565 If you want to check how your website renders on a mobile device, then I would use the browser's developer tools on your PC (where the web server is) - for example in Chrome: developer.chrome.com/docs/devtools/device-mode/
@@dave-hollingworth Yeah i m using it too on firefox but my tab has a uncommon screen res i like to be sure by checking on the tab directly thanks for your answers👍.(sorry for my bad english)
@@andypro8565 Then I would suggest either uploading your code to a server on the internet, or using one of the techniques here to access localhost over the internet: www.sitepoint.com/accessing-localhost-from-anywhere/
i followed your example but after i edited my httpd-vhosts and saved and try to run my apche server, server won't run. it gives error. any help? i am using xanpp on windows machine.
The error message will probably give you more detail - does it say where the error is?
hi dave, i've 2 virtual hosts configured:
DocumentRoot "/Applications/MAMP/htdocs"
ServerName localhost
DocumentRoot "/Users/peterbrozek/Documents/mvc/mvc-neu/public"
ServerName mvc.local
but when i prompt "mvc.local" i only got the localhost data. any help for me? thank you
If you've defined these in a separate file, make sure the line that includes that file isn't commented out; also make sure you restart the web server after making any changes to the config
many thanks for your quick reply. i did it like you said. unfortunately it did'nt work. i also wrote them in the hosts file. it only didn't work on mamp. the same in xamp works fine@@dave-hollingworth
Not working in my localhost server. My apache version 2.4.35. "site1.localhost" redirect me in root directory of the server by default.
Post your configuration if you like and I'll see if I can see any issues with it; also don't forget to restart the server when you make any changes
thanks for the great, simple and fast video. It works with "site.localhost" but doesn't with "localhost/site". Is that right? i would prefer the second version. Also, is this the right way to set private / public folders in a project? Thanks!
Yes, that's right. You don't need to set up a virtual host if you're just going to use a subfolder of the current web root folder. I'm not sure what you mean by private and public folders, can you elaborate?
@@dave-hollingworth am i crazy or do my replies get deleted?
@@joelalain Hi Joel - this is the only reply I got a notification about, and I can't see any other replies... it could be that the RUclips system is removing replies if it thinks they're spam maybe (if they contain code or links for example - could that be the case?)
@@dave-hollingworth i think it's a bug. When i click on "notification bell" on top right corner, it links this video + the comment. My guess is that the "&lc=xxxxx" arguments bug and doesnt send replies to the server. If i remove manually the "lc=xxxx" argument, then it works. Weird.
@Dave Hollingworth original reply to your question: first of all thanks for taking the time to reply :) So basically i'm trying to reproduce the experience that i get from of any web hosting companies, but locally on my server. Ex: MediaTemple. So when i log in to upload my file, the FTP allows me to upload files to the "private" part of the server and then there is a folder called "html/" or "public/" or "public_html" or whatever. Like you said, if i put the "database passwords" outside of the public folder, then no one can see it, but if it's in "public_html/" then ppl could find it. So my question was "how do i do that on localhost / XAMPP for EVERY project i have?" In you udemy course (/php-mvc-from-scratch/), you set permissions for the whole /www/ folder which is not what i want since i have many projects and i dont want each project to see the files from the other ones. So i was wondering if "virtual host" was the right way to do this locally & have every project have its own "private / public" section and dont see each other's proejcts files? thanks again!
Can this work on a system accessed over the network?
In principle yes, you should be able to just add an entry to the hosts file on the client computer that matches the virtual host address you've used to the server's IP address. More details here: stackoverflow.com/questions/11245242/access-virtual-host-from-another-machine-over-lan
Can u help me in my final graduation project please, I built the mvc using ur cours on udemy now I'm stuck at manipulating database . Please if u can
Of course, please ask your question in the Q&A section of the course on Udemy, I'll look out for your question there.
I tried this method, but I found that all my virtual host links did not work. For example, I made 'link1.localhost' as a virtual server, and 'link2.localhost' as another. When I navigate to that link, I just get the same HTML page that navigating to 'localhost' does (in this case, the Wampserver page).
Did you restart the web server so that the changes are recognised? Also, check the main configuration file to make sure the file that contains the virtual host configuration is included (it could be commented out by default)