Linux apache 不接受来自本地主机外部的传入连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10729247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 06:28:22  来源:igfitidea点击:

apache not accepting incoming connections from outside of localhost

linuxapachewebservercentos

提问by Phildo

I've booted up a CentOS server on rackspace and executed yum install httpd'd. Then services httpd start. So, just the barebones.

我已经在机架空间上启动了一个 CentOS 服务器并执行了yum install httpd'd. 然后services httpd start。所以,只是准系统。

I can access its IP address remotely over ssh (22) no problem, so there's no problem with the DNS or anything (I think...), but when I try to connect on port 80 (via a browser or something) I get connection refused.

我可以通过 ssh (22) 远程访问它的 IP 地址没问题,所以 DNS 或任何东西都没有问题(我认为......),但是当我尝试连接端口 80(通过浏览器或其他东西)时,我得到拒绝连接。

From localhost, however, I can use telnet (80), or even lynx on itself and get served with no problem. From outside (my house, my school, a local coffee shop, etc...), telnet connects on 22, but not 80.

然而,从本地主机,我可以使用 telnet (80),甚至 lynx 本身,并且可以毫无问题地得到服务。从外面(我的房子、我的学校、当地的咖啡店等),telnet 连接到 22,但不是 80。

I use netstat -tulpn(<- I'm not going to lie, I don't understand the -tulpnpart, but that's what the internet told me to do...) and see

我使用netstat -tulpn(<- 我不会撒谎,我不明白这-tulpn部分,但这就是互联网告诉我要做的......) 然后看看

tcp    0    0 :::80     :::*    LISTEN    -                   

as I believe I should. The httpd.confsays Listen 80.

我相信我应该这样做。该httpd.confListen 80

I have services httpd restart'd many a time.

我有services httpd restart很多次。

Honestly I have no idea what to do. There is NO way that rackspace has a firewall on incoming port 80 requests. I feel like I'm missing something stupid, but I've booted up a barebones server twice now and have done the absolute minimum to get this functioning thinking I had mucked things up with my tinkering, but neither worked.

老实说,我不知道该怎么做。机架空间在传入端口 80 请求上没有防火墙。我觉得我错过了一些愚蠢的东西,但我现在已经启动了两次准系统服务器,并且已经做了绝对最少的事情来获得这个功能,认为我已经把事情搞砸了,但都没有奏效。

Any help is greatly appreciated! (And sorry for the long winded post...)

任何帮助是极大的赞赏!(对于冗长的帖子感到抱歉......)

EditI was asked to post the output of iptables -L. So here it is:

编辑我被要求发布iptables -L. 所以这里是:

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED 
ACCEPT     icmp --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:ssh 
REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited 

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited 

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination   

采纳答案by antonio.fornie

In case not solved yet. Your iptables say:

万一还没有解决。你的 iptables 说:

state RELATED,ESTABLISHED

状态相关,已建立

Which means that it lets pass only connections already established... that's established by you, not by remote machines. Then you can see exceptions to this in the next rules:

这意味着它只允许通过已经建立的连接……这是由您建立的,而不是由远程机器建立的。然后你可以在接下来的规则中看到例外情况:

state NEW tcp dpt:ssh

Which counts only for ssh, so you should add a similar rule/line for http, which you can do like this:

这仅对 ssh 有效,因此您应该为 http 添加类似的规则/行,您可以这样做:

state NEW tcp dpt:80

Which you can do like this:

你可以这样做:

sudo iptables -I INPUT 4 -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT

(In this case I am choosing to add the new rule in the fourth line)

(在这种情况下,我选择在第四行添加新规则)

Remember that after editing the file you should save it like this:

请记住,在编辑文件后,您应该像这样保存它:

sudo /etc/init.d/iptables save

回答by Pethical

Search for LISTEN directive in the apache config files (httpd.conf, apache2.conf, listen.conf,...) and if you see localhost, or 127.0.0.1, then you need to overwrite with your public ip.

在 apache 配置文件(httpd.conf、apache2.conf、listen.conf...)中搜索 LISTEN 指令,如果您看到 localhost 或 127.0.0.1,那么您需要用您的公共 IP 覆盖。

回答by nsfyn55

Set apache to list to a specific interface and port something like below:

将 apache 设置为列出特定接口和端口,如下所示:

Listen 192.170.2.1:80

Also check for Iptables and TCP Wrappers entries that might be interfering on the host with outside hosts accessing that port

还要检查 Iptables 和 TCP Wrappers 条目,这些条目可能会干扰主机与访问该端口的外部主机

Binding Docs For Apache

Apache 的绑定文档

回答by dAm2K

Try disabling iptables: service iptables stop

尝试禁用 iptables:service iptables stop

If this works, enable TCP port 80 to your firewall rules: run system-config-selinux from root, and enable TCP port 80 (HTTP) on your firewall.

如果可行,请为防火墙规则启用 TCP 端口 80:从 root 运行 system-config-selinux,并在防火墙上启用 TCP 端口 80 (HTTP)。

回答by Raja

Try with below setting in iptables.config table

尝试在 iptables.config 表中使用以下设置

iptables -A INPUT -p tcp --dport 80 -j ACCEPT

Run the below command to restart the iptable service

运行以下命令重启iptable服务

service iptables restart

change the httpd.config file to

将 httpd.config 文件更改为

Listen 192.170.2.1:80

re-start the apache.

重新启动apache。

Try now.

现在试试。

回答by Satish

Disable SELinux

禁用 SELinux

$ sudo setenforce 0

回答by Nadeer Madampat Tanalur

SELinux prevents Apache (and therefore all Apache modules) from making remote connections by default.

默认情况下,SELinux 会阻止 Apache(以及所有 Apache 模块)进行远程连接。

# setsebool -P httpd_can_network_connect=1

回答by Mr.T

this would work: -- for REDHAT use : cat "/etc/sysconfig/iptables"

这会起作用:--对于REDHAT 使用:cat "/etc/sysconfig/iptables"

iptables -I  RH-Firewall-1-INPUT -s 192.168.1.3  -p tcp -m tcp --dport 80 -j ACCEPT

followed by

其次是

sudo /etc/init.d/iptables save

回答by user4806038

this is what worked for us to get the apache accessible from outside:

这就是我们从外部访问 apache 的方法:

sudo iptables -I INPUT 4 -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
sudo service iptables restart

回答by Matt Pennington

If you are using RHEL/CentOS 7 (the OP was not, but I thought I'd share the solution for my case), then you will need to use firewalld instead of the iptables service mentioned in other answers.

如果您使用的是 RHEL/CentOS 7(OP 不是,但我想我会为我的案例分享解决方案),那么您将需要使用 firewalld 而不是其他答案中提到的 iptables 服务。

firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload

And then check that it is running with:

然后检查它是否正在运行:

firewall-cmd --permanent --zone=public --list-all

It should list 80/tcpunder ports

它应该列80/tcpports