<p>I'd like to set up a cheap Linux box as a web server to host a variety of web technologies (PHP &amp; Java EE come to mind, but I'd like to experiment with Ruby or Python in the future as well). </p>

<p>I'm fairly versed in setting up Tomcat to run on Linux for serving up Java EE applications, but I'd like to be able to open this server up, even just so I can create some tools I can use while I am working in the office. All the experience I've had with configuring Java EE sites has all been for intranet applications where we were told not to focus on securing the pages for external users. </p>

<p>What is your advice on setting up a personal Linux web server in a secure enough way to open it up for external traffic?</p>

[{'answer_id': 5080, 'author': 'Adam Lerman', 'author_id': 673, 'author_profile': 'https://Stackoverflow.com/users/673', 'pm_score': 0, 'selected': False, 'text': "<p>There are plenty of ways to do this that will work just fine. I would usually jsut use a .htaccess file. Quick to set up and secure <em>enough</em> . Probably not the best option but it works for me. I wouldn't put my credit card numbers behind it but other than that I dont really care.</p>\n"}, {'answer_id': 5090, 'author': 'mauriciopastrana', 'author_id': 547, 'author_profile': 'https://Stackoverflow.com/users/547', 'pm_score': 1, 'selected': False, 'text': '<p>Its safe and secure if you keep your voice down about it (i.e., rarely will someone come after your home server if you\'re just hosting a glorified webroot on a home connection) and your wits up about your configuration (i.e., avoid using root for everything, make sure you keep your software up to date).</p>\n\n<p>On that note, albeit this thread will potentially dwindle down to just flaming, my suggestion for your personal server is to stick to anything Ubuntu (<a href="http://www.ubuntu.com/getubuntu" rel="nofollow noreferrer">get Ubuntu Server here</a>); in my experience, the quickest to get answers from whence asking questions on forums (not sure what to say about uptake though).</p>\n\n<p>My home server security BTW kinda benefits (I think, or I like to think) from not having a static IP (runs on DynDNS).</p>\n\n<p>Good luck!</p>\n\n<p>/mp</p>\n'}, {'answer_id': 5092, 'author': 'Marcel Levy', 'author_id': 676, 'author_profile': 'https://Stackoverflow.com/users/676', 'pm_score': 0, 'selected': False, 'text': "<p>Wow, you're opening up a can of worms as soon as you start opening anything up to external traffic. Keep in mind that what you consider an experimental server, almost like a sacrificial lamb, is also easy pickings for people looking to do bad things with your network and resources. </p>\n\n<p>Your whole approach to an externally-available server should be very conservative and thorough. It starts with simple things like firewall policies, includes the underlying OS (keeping it patched, configuring it for security, etc.) and involves every layer of every stack you'll be using. There isn't a simple answer or recipe, I'm afraid.</p>\n\n<p>If you want to experiment, you'll do much better to keep the server private and use a VPN if you need to work on it remotely.</p>\n"}, {'answer_id': 5108, 'author': 'helloandre', 'author_id': 50, 'author_profile': 'https://Stackoverflow.com/users/50', 'pm_score': 4, 'selected': True, 'text': '<p>This article has some of the best ways to lock things down:</p>\n\n<p><a href="http://www.petefreitag.com/item/505.cfm" rel="noreferrer">http://www.petefreitag.com/item/505.cfm</a></p>\n\n<p>Some highlights:</p>\n\n<ul>\n<li>Make sure no one can browse the directories</li>\n<li>Make sure only root has write privileges to everything, and only root has read privileges to certain config files</li>\n<li>Run mod_security</li>\n</ul>\n\n<p>The article also takes some pointers from this book:</p>\n\n<p><a href="http://spendfish.com/product/0596007248" rel="noreferrer">Apache Securiy</a> (O\'Reilly Press)</p>\n\n<p>As far as distros, I\'ve run Debain and Ubuntu, but it just depends on how much you want to do. I ran Debian with no X and just ssh\'d into it whenever i needed anything. That is a simple way to keep overhead down. Or Ubuntu has some nice GUI things that make it easy to control Apache/MySQL/PHP.</p>\n'}, {'answer_id': 5110, 'author': 'saint_groceon', 'author_id': 59, 'author_profile': 'https://Stackoverflow.com/users/59', 'pm_score': 1, 'selected': False, 'text': "<p>Be careful about opening the SSH port to the wild.  If you do, make sure to disable root logins (you can always <code>su</code> or <code>sudo</code> once you get in) and consider more aggressive authentication methods within reason.  I saw a huge dictionary attack in my server logs one weekend going after my SSH server from a DynDNS home IP server.</p>\n\n<p>That being said, it's really awesome to be able to get to your home shell from work or away... and adding on the fact that you can use SFTP over the same port, I couldn't imagine life without it.  =)</p>\n"}, {'answer_id': 5149, 'author': 'svrist', 'author_id': 86, 'author_profile': 'https://Stackoverflow.com/users/86', 'pm_score': 1, 'selected': False, 'text': '<p>You could consider an <a href="http://www.amazon.com/gp/browse.html?node=201590011" rel="nofollow noreferrer">EC2 instance from Amazon</a>. That way you can easily test out "stuff" without messing with production. And only pay for the space,time and bandwidth you use.</p>\n'}, {'answer_id': 5316, 'author': 'dragonmantank', 'author_id': 204, 'author_profile': 'https://Stackoverflow.com/users/204', 'pm_score': 1, 'selected': False, 'text': '<p>If you do run a Linux server from home, install <a href="http://www.ossec.net/" rel="nofollow noreferrer">ossec</a> on it for a nice lightweight IDS that works really well.</p>\n\n<p>[EDIT]</p>\n\n<p>As a side note, make sure that you do not run afoul of your ISP\'s Acceptable Use Policy <em>and</em> that they allow incoming connections on standard ports. The ISP I used to work for had it written in their terms that you could be disconnected for running servers over port 80/25 unless you were on a business-class account. While we didn\'t actively block those ports (we didn\'t care unless it was causing a problem) some ISPs don\'t allow any traffic over port 80 or 25 so you will have to use alternate ports.</p>\n'}, {'answer_id': 9195, 'author': 'icco', 'author_id': 1063, 'author_profile': 'https://Stackoverflow.com/users/1063', 'pm_score': 2, 'selected': False, 'text': "<p>One thing you should be sure to consider is what ports are open to the world. I personally just open port 22 for SSH and port 123 for ntpd. But if you open port 80 (http) or ftp make sure you learn to know at least what you are serving to the world and who can do what with that. I don't know a lot about ftp, but there are millions of great Apache tutorials just a Google search away.</p>\n"}, {'answer_id': 9218, 'author': 'Dogmang', 'author_id': 1135, 'author_profile': 'https://Stackoverflow.com/users/1135', 'pm_score': 3, 'selected': False, 'text': "<p>It's important to follow security best practices wherever possible, but you don't want to make things unduly difficult for yourself or lose sleep worrying about keeping up with the latest exploits. In my experience, there are two key things that can help keep your personal server secure enough to throw up on the internet while retaining your sanity:</p>\n\n<p><strong>1) Security through obscurity</strong></p>\n\n<p>Needless to say, relying on this in the 'real world' is a bad idea and not to be entertained. But that's because in the real world,  baddies know what's there and that there's loot to be had.</p>\n\n<p>On a personal server, the majority of 'attacks' you'll suffer will simply be automated sweeps from machines that have already been compromised, looking for default installations of products known to be vulnerable. If your server doesn't offer up anything enticing on the default ports or in the default locations, the automated attacker will move on. Therefore, if you're going to run a ssh server, put it on a non-standard port (>1024) and it's likely it will never be found. If you can get away with this technique for your web server then great, shift that to an obscure port too.</p>\n\n<p><strong>2) Package management</strong></p>\n\n<p>Don't compile and install Apache or sshd from source yourself unless you absolutely have to. If you do, you're taking on the responsibility of keeping up-to-date with the latest security patches. Let the nice package maintainers from Linux distros such as Debian or Ubuntu do the work for you. Install from the distro's precompiled packages, and staying current becomes a matter of issuing the occasional <em>apt-get update &amp;&amp; apt-get -u dist-upgrade</em> command, or using whatever fancy GUI tool Ubuntu provides.</p>\n"}, {'answer_id': 12845, 'author': 'Carl Russmann', 'author_id': 1347, 'author_profile': 'https://Stackoverflow.com/users/1347', 'pm_score': 1, 'selected': False, 'text': "<p>If you're going to do this, spend a bit of money and at the least buy a dedicated router/firewall with a separate DMZ port.  You'll want to firewall off your internal network from your server so that when (not if!) your web server is compromised, your internal network isn't immediately vulnerable as well.</p>\n"}, {'answer_id': 12856, 'author': 'Pondidum', 'author_id': 1500, 'author_profile': 'https://Stackoverflow.com/users/1500', 'pm_score': 2, 'selected': False, 'text': '<p>Bit-Tech.Net ran a couple of articles on how to setup a home server using linux. Here are the links:</p>\n\n<p><a href="http://www.bit-tech.net/bits/2007/06/05/build_your_own_server/1" rel="nofollow noreferrer">Article 1</a><br />\n<a href="http://www.bit-tech.net/bits/2007/07/24/build_your_own_better_server/1" rel="nofollow noreferrer">Article 2</a></p>\n\n<p>Hope those are of some help.</p>\n'}, {'answer_id': 24676, 'author': 'Gary Richardson', 'author_id': 2506, 'author_profile': 'https://Stackoverflow.com/users/2506', 'pm_score': 2, 'selected': False, 'text': "<p>@svrist mentioned EC2. EC2 provides an API for opening and closing ports remotely. This way, you can keep your box running. If you need to give a demo from a coffee shop or a client's office, you can grab your IP and add it to the ACL.</p>\n"}]
['https://Stackoverflow.com/questions/5078', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/318/']