我在运行于 Raspberry Pi3 的 Apache2 实例上自行托管了两个网站(有关版本和配置的更多信息见下文):Website1 是我从头开始编码的静态网站,而 website2 在 Wordpress 上运行。
我以前住在公寓里,ISP 提供静态公共 IP,但最近我搬到了新家,ISP 不提供该选项。我一直试图重新配置一切,但无法让一切恢复正常。
配置
我有一台运行最新版本 Raspbian 的 Raspberry Pi 3
$ cat /etc/os-release
PRETTY_NAME="Raspbian GNU/Linux 9 (stretch)"
NAME="Raspbian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"
VERSION_CODENAME=stretch
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"
在我的路由器(型号Alcatel-Lucent G240W-B
:)上,我打开了http 端口 XX和ssh 的端口 YY(我没有使用标准端口来提高安全性)
Application Name | WAN Connection | WAN Port | LAN Port | Internal Client | Protocol | Status
Customer settings | {my_wan_connection} | XX~XX | XX~XX | 192.168.1.100 | TCPorUDP | ACTIVE
Customer settings | {my_wan_connection} | YY~YY | YY~YY | 192.168.1.100 | TCPorUDP | ACTIVE
我正在运行 apache2
$ apache2 -v
Server version: Apache/2.4.25 (Raspbian)
Server built: 2019-08-19T19:25:31
启用两个网站
$ sudo apache2ctl -S
VirtualHost configuration:
*:XX is a NameVirtualHost
default server mydomain.ddns.net (/etc/apache2/sites-enabled/website1.com.conf:1)
port XX namevhost mydomain.ddns.net (/etc/apache2/sites-enabled/website1.com.conf:1)
alias website1.com
alias www.website1.com
port XX namevhost mydomain.ddns.net (/etc/apache2/sites-enabled/website2.com.conf:1)
alias www.wesbiste2.com
alias wesbiste2.com
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex watchdog-callback: using_defaults
Mutex rewrite-map: using_defaults
Mutex default: dir="/var/run/apache2/" mechanism=default
Mutex mpm-accept: using_defaults
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
Define: MODSEC_2.5
Define: MODSEC_2.9
User: name="www-data" id=33
Group: name="www-data" id=33
这是我的ports.conf
$ sudo cat ports.conf
# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default.conf
Listen 0.0.0.0:XX
<IfModule ssl_module>
Listen 443
</IfModule>
<IfModule mod_gnutls.c>
Listen 443
</IfModule>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
这/etc/apache2/sites-enables/website1.conf
<VirtualHost *:XX>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin [email protected]
ServerName mydomain.ddns.net
ServerAlias website1.com
ServerAlias www.website1.com
DocumentRoot /var/www/website1/public_html
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
还有/etc/apache2/sites-enables/website2.conf
<VirtualHost *:XX>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin [email protected]
ServerName mydomain.ddns.net
ServerAlias www.website2.com
ServerAlias website2.com
DocumentRoot /var/www/website2/public_html
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
我已经使用 NoIP 设置了动态 DNS(mydomain.ddns.net),它通过我的路由器进行更新
什么有效,什么无效
SSH 运行正常,我既可以在家庭网络内,也可以从外部连接到我的树莓派 - 当我使用端口检查工具检查门 YY 是否打开时,我得到了肯定的回应
HTTP 更成问题:尽管门被关闭了(至少根据端口检查器),我可以从
- 内部 IP 地址:192.168.1.100:XX
- 公网 IP 地址:{my-public-ip}:XX
- 动态 DNS 主机名 mydomain.ddns.net
但是,由于这样我只能访问网站 1,而不能访问网站 2,因此我尝试禁用网站 1 ( sudo a2dissite website1.com.conf
) 并重新启动 apache ( sudo systemctl restart apache2
)
$ sudo apache2ctl -S
VirtualHost configuration:
*:XX mydomain.ddns.net (/etc/apache2/sites-enabled/website2.com.conf:1)
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex watchdog-callback: using_defaults
Mutex rewrite-map: using_defaults
Mutex default: dir="/var/run/apache2/" mechanism=default
Mutex mpm-accept: using_defaults
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
Define: MODSEC_2.5
Define: MODSEC_2.9
User: name="www-data" id=33
Group: name="www-data" id=33
但是,我仍然无法从内部或公共 IP 或 DDNS 主机名访问 website2。
更改 Wordpress URL
我认为这可能与 Wordpress 有关,事实上我发现本文并按照说明[直接在数据库中更改 URL][2]。事实上,“home”和“url”值等于我以前的公共静态 IP,所以我将其更改为我的 DDNS(myhostname.ddns.net)。然而,该网站仍然无法访问
仅供参考,我的首要任务是能够访问网站2并查看其内容,无论是通过内部还是外部 IP,还是 DDNS 主机名。
答案1
所以我想我明白了……也许……问题可能在于你测试的方式。在你用来测试的电脑上(不是 RPi),编辑你的主机文件,将 www.website1.com 和 www.website2.com 都指向你的 RPi 的内部 IP 地址(当你在同一个网络上时)。
hosts文件说明: https://www.howtogeek.com/howto/27350/beginner-geek-how-to-edit-your-hosts-file/
然后将浏览器指向 www.website1.com,然后指向 www.website2.com。
如果有效,请更改您的主机文件以将这些网站指向您的外部 IP 地址。
解释:Web 浏览器和 Web 服务器之间的握手部分包括您请求的站点。这允许在同一 Web 服务器上托管多个站点。由于您的 Apache 配置将别名声明为 www.website1.com 和 www.website2.com,但您请求的是您的 IP 地址对应的网站。Apache 响应 www.website1.com,因为该站点被设置为您的默认站点。
答案2
最后是路由器配置:传入端口(WAN 端口)应设置为 80,内部端口(LAN 端口)应设置为路由器正在监听的任何端口。