Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What rules are needed for an Ubuntu LAMP server only having minimum necessary ports open.

share|improve this question
Exactly how minimal are we talking though? Since MySQL and PHP can be communicated with directly internally (hence the first item which allows loopback/localhost communication), there's no need to have their ports open to the universe). – Thomas W. Jul 7 '12 at 14:05
up vote 2 down vote accepted

Minimal? Depending on how minimal we're talking, this'd work (I'm including SSH as being needed in this though):

iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p udp --dport 80 -j ACCEPT
iptables -A INPUT -j REJECT --reject-with icmp-host-unreachable

If you want to add other ports or services to be internet facing, those ports can be opened with iptables -I INPUT [numberOfRules] -p [protocol] --dport [port or port:range] -j ACCEPT, where [numberOfRules] is the number of the rule which has the REJECT target, [protocol] is the protocol (TCP, UDP, etc.), and [port or port:range] is either a single port or a range of ports defined with start:end. Using comma separation also seems to work there.

A more in-depth explanation of what each of these rules does, and what I intentionally left out of the rules selections I did exists at my blog, if you wish to understand this a little bit better.

share|improve this answer
LIMITATIONS: This does not include rules to allow for pings under certain circumstances, nor does it allow for any other connections on other ports from the outside. – Thomas W. Jul 7 '12 at 14:06
Thanks, this will do until I move the database to another server. – amiawizard Jul 7 '12 at 14:12
This will allow for outbound communication though, so if the DB moves to another server you wont need to open another port. The conntrack item will allow for the communication with the external server though, because your server initiated the communication (RELATED to an initiated connection started by your server, and ESTABLISHED because its, well, established.) – Thomas W. Jul 7 '12 at 14:14
^ The above comment is not entirely correct, the ports on the external server that the DBs are going to reside on will need to be opened. – Thomas W. Jul 9 '12 at 16:24
FOR EVERYONE: I have further expanded and explained this answer at my blog, and have thrown a link up in the answer. – Thomas W. Aug 3 '12 at 16:12

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.