So on the weekend I got the brilliant idea (thanks to Lifehacker) to set up a publicly accessible webserver on Tavehd-Kyn. While I’ve installed Apache on pretty much every computer I’ve had since something like circa 2000, I’ve never had anything accessible publicly, mostly because I didn’t know how to get past issues like the router and the fact that — like pretty much everyone nowadays — I don’t have a static IP address. Little did I know that these things really aren’t the huge hurdles that they seem! And that setting up and serving a website from home is really, really piss easy!i
Here’s how you do it.
(Before we start, a caveat: Opening up your computer to the internet in this fashion is highly likely to be dangerous at least on some level. This isn’t a tutorial on how to set up a hardened webserver; if some noobs hack your box because you’ve done this, don’t say I didn’t warn you. I’d also totally not recommend you do this on Windows. Just so you know.)
The first thing we’re going to do, even before we open ourselves up to the public, is get our own house in order. And we’re going to do this by learning how to set up virtual hosts in Apache. The reason we’re going to do this is because it allows us to have some separation between our ‘overall’ internal Apache install and the production site we’re ultimately going to be serving to the public.
otw.dev, void-star.dev and so forth. http://localhost on my system just goes to the default Apache install page; all my actual sites use vhosts.~/Sites/!prod/public. The !prod folder lets me know this is a ‘production’ site as opposed to my local, development sites (which are all located in ~/Sites/!dev), but this is just an internal convention. Ultimately, you can pretty much put the root of your site anywhere you like (just make sure the permissions are correct).vhosts.conf; there’ll probably be a commented-out link to it in httpd.conf which will tell you where to find it (do a search for ‘vhost’ or ‘virtual hosts’). Uncomment the link while you’re here, then open the file it points to. If there doesn’t seem to be a separate file (probably because you’re running version 1-point-something Apache), you can enter the information in the next step straight into httpd.conf.NameVirtualHost *:80 <VirtualHost *:80> ServerAdmin you@your.com DocumentRoot "/path/to/your/site/www" ServerName public.prod #ServerAlias us.somewhere.com </VirtualHost>
DocumentRoot directive pointing to the right location. Leave ServerAlias commented out for now; we’ll be using this when we’re finally ready to launch our site publicly. Finally, you can theoretically change the *:80 to lock down access to your vhost to certain IP addresses; the Apache manual has more info.http://public.prod in your browser. Don’t worry if it doesn’t work; actually I’d be shocked if it did. At the moment, Apache ‘knows’ where to find your site but your computer doesn’t. Like it always does when it encounters a domain name it doesn’t understand, it asks The Internet for the IP. But The Internet doesn’t know either, because public.prod is a local site. You’re going to have to tell your computer where your site is yourself.C:\Windows\system32\drivers\etchosts while on *NIX-style machines it’s /etc/hosts (remember to sudo).127.0.0.1 public.prod
public.prod resolves to what’s known as the loopback address, which in network-speak means ‘this computer’.http://public.prod again. If all is well, it should work! If it’s not resolving and your browser seems to be looking externally for resolution (usually it will try and dump you at http://www.public.prod.com), then there’s something wrong with your hosts file. If the name looks okay, but the site just isn’t displaying, it’s probably an Apache thing; check the syntax of your conf files using the command apachectl configtest.And that’s it for the first part of this tutorial. While you wait for the second part — where we get into the actual meat of serving stuff to to the public — you can practice setting yourself up some more vhosts. Because they’re fun.
Until next time…
sudo mv /var /usr/var. Just as, yanno, an example. ↩This post has been mirrored from void-star.net β. You may comment there using your LiveJournal/Dreamwidth/OpenID URL instead of an email address. Randoms and lurkers are more than welcome.