我在 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 (mydomain.ddns.net) 設定了動態 DNS,它透過我的路由器進行更新
什麼有效,什麼無效
SSH 工作正常,我可以在家庭網路內和外部連接到我的樹莓派 - 當我透過連接埠檢查工具檢查 YY 門是否打開時,我得到了積極的回應
HTTP 的問題更大:儘管門被關閉(至少根據連接埠檢查器),我可以看到 website1 (這是 apache 的預設伺服器)
- 內部IP位址:192.168.1.100:XX
- 公共IP位址:{my-public-ip}:XX
- 動態 dns 主機名稱 mydomain.ddns.net
但是,由於這樣我只能存取 website1,而不能存取 website2,因此我嘗試停用 website1 ( 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 網址
我認為這可能與 WordPress 有關,事實上我發現本文並按照說明[直接在資料庫中更改 URL][2]。事實上,「home」和「url」值等於我的舊公共靜態 IP,因此我將其變更為我的 DDNS (myhostname.ddns.net)。但該網站仍然無法訪問
僅供參考,我的首要任務是能夠存取網站2並查看其內容,無論是透過內部還是外部 IP,還是 DDNS hostnmae。
答案1
所以我想我明白了......也許......問題可能在於你測試這個的方式。在您用於測試的電腦(不是 RPi)上,編輯您的主機文件,將 www.website1.com 和 www.website2.com 指向您的 RPi 的內部 IP 位址(當您位於同一網路時)。
主機檔案說明: https://www.howtogeek.com/howto/27350/beginner-geek-how-to-edit-your-hosts-file/
然後將瀏覽器指向 www.website1.com,然後指向 www.website2.com。
如果有效,請變更您的主機檔案以將這些網站指向您的外部 IP 位址。
說明:Web 瀏覽器和 Web 伺服器之間的部分握手包括您要造訪的網站。這允許在同一網站伺服器上託管多個網站。由於您的 Apache 設定將別名指定為 www.website1.com 和 www.website2.com,但您要求提供您的 IP 位址的網站。 Apache 回應 www.website1.com,因為它被設定為您的預設網站。
答案2
最後是路由器設定:傳入連接埠(WAN 連接埠)應設定為 80,內部連接埠(LAN 連接埠)應設定為路由器正在偵聽的任何連接埠。